title: What is the formula for setting WEB_CONCURRENCY on heroku?
body: <p>I am frequently getting this error <code>Memory quota exceeded</code> for my <code>ruby on rails</code> application hosted on heroku:</p>
  
  <pre><code>2014-12-29 11:09:37.876 355 &lt;45&gt;1 2014-12-29T11:09:37.210533+00:00 heroku worker.1 - - source=worker.1 dyno=heroku.22144543.00c11b4d-8ec6-46d9-addf-f03163e10f0c sample#memory_total=2899.25MB sample#memory_rss=1023.73MB sample#memory_cache=0.00MB sample#memory_swap=1875.52MB sample#memory_pgpgin=2603236pages sample#memory_pgpgout=2341160pages
  2014-12-29 11:09:37.876 132 &lt;4
  2014-12-29 11:09:37.876 132 &lt;455&gt;1 2014-12-29T11:09:37.210533+00:00 heroku worker.1 - - Process running mem=2899M(283.1%)&gt;1 2014-12-29T11:09:37.210533+00:00 heroku worker.1 - - Error R14 (Memory quota exceeded)
  </code></pre>
  
  <p>I read the blog of which suggests the <a href="http://heridev.com.mx/ruby/how-i-solvedfixed-the-r14-error-in-heroku-com-in-a-spreerails-application/" rel="nofollow noreferrer">solution</a>  i.e. set <code>WEB_CONCURRENCY</code> config variable but it is not clear to me.</p>
  
  <p>My current configuration on heroku are:</p>
  
  <p><img src="https://i.stack.imgur.com/dkpI7.png" alt="enter image description here"></p>
  
  <h1>config/unicorn.rb</h1>
  
  <pre><code>worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
  timeout 29
  preload_app true
  </code></pre>
  
  <p><strong>Edited:</strong></p>
  
  <p>Worker Code</p>
  
  <pre><code>class PhotoWorker
    include Sidekiq::Worker
    sidekiq_options queue: "high"
    # sidekiq_options retry: false
  
    def perform(params)    
      begin
        puts "Start Woker for #{params.inspect}"
        site = Site.find params['site_id']      
        if params["id"].present?
          photo = Photo.find(params["id"])
          if params['key'].present?
            photo.key = params['key']
            photo.remote_image_url = photo.image.direct_fog_url(:with_path =&gt; true)
            photo.image_name = params['key'].split('/').last
            photo.save(validate: false)
            puts photo.inspect
          end     
        else     
          #photo = site.photos.build 
          #photo.hotel_detail_id = site.hotel_detail.id
          #photo.album_id = site.hotel_detail.album.id
          #photo.save(validate: false)
          photo.key = params['key']
          photo.remote_image_url = photo.image.direct_fog_url(:with_path =&gt; true)
          #photo.image = params['key']
          #photo.remote_image_url = params['key']
          photo.image_name = params['key'].split('/').last
          puts photo.inspect
          photo.save(validate: false)
        end
  
        if params["id"].present? &amp;&amp; params["crop_x"].present? 
          photo.crop_x = params["crop_x"]
          photo.crop_y = params["crop_y"]
          photo.crop_w = params["crop_w"]
          photo.crop_h = params["crop_h"]
          photo.save(validate: false)
        end
  
  
        if params["id"].present? 
          if params['key'].present?
            s3 = AWS::S3.new.buckets[ENV["FOG_DIRECTORY"]]
            s3.objects[params['key']].delete
          end
        else
          AmazonFile.new(params['key']).delete
        end
  
        puts "Deleted temp file: #{params['key']}" if params['key'].present?
        puts "Photo(#{photo.id}) saved successfully. URL: #{params['key']}"
      rescue Exception =&gt; exc
        #puts "Photo not saved. URL: #{params['key']}" 
        puts "Error:  #{exc.message}"
      end      
    end
  
  end
  </code></pre>
  
  <p>Apart from sidekiq i am also using <code>carrierwave_backgrounder</code> gem.</p>
  
  <p>I am using Kraken for image compression </p>
  
  <p>Photo Uploader</p>
  
  <pre><code>class PhotoUploader &lt; CarrierWave::Uploader::Base 
    #include ::CarrierWave::Backgrounder::Delay
    include CarrierWaveDirect::Uploader
    include CarrierWave::MiniMagick
    include CarrierWave::MimeTypes
    process :set_content_type
    process :crop  
    storage :fog
    after :store, :kraken
  
    version :admin do
      process :resize_to_fit =&gt; [200,200]
    end
  
    #Leisure theme
    version :leisure_358x243, :if =&gt; :leisure_theme? do
      process :resize_to_fill =&gt; [358,243]
    end
    version :leisure_900x500, :if =&gt; :leisure_theme? do
      process :resize_to_fill =&gt; [900,500]
    end
   version :leisure_350x147, :if =&gt; :leisure_theme? do
      process :resize_to_fill =&gt; [350,147]
    end
    version :leisure_1100x344, :if =&gt; :leisure_theme? do
      process :resize_to_fill =&gt; [1100,344]
    end
  
    #Business theme
    version :business_360x160, :if =&gt; :business_theme? do
      process :resize_to_fill =&gt; [360,160]
    end
    version :business_1100x315, :if =&gt; :business_theme? do
      process :resize_to_fill =&gt; [1100,315]
    end
    version :business_1140x530, :if =&gt; :business_theme? do
      process :resize_to_fill =&gt; [1140,530]
    end
    version :business_1100x355, :if =&gt; :business_theme? do
      process :resize_to_fill =&gt; [1100,355]
    end
  
    #Commthree theme
    version :commthree_550x300, :if =&gt; :commthree_theme? do
      process :resize_to_fill =&gt; [550,300]
    end
    version :commthree_319x183, :if =&gt; :commthree_theme? do
      process :resize_to_fill =&gt; [319,183]
    end
    version :commthree_1920x700, :if =&gt; :commthree_theme? do
      process :resize_to_fill =&gt; [1920,700]
    end
  
    #All theme
    version :all_360x188 do
      process :resize_to_fill =&gt; [360,188]
    end
    version :all_1100x401 do
      process :resize_to_fill =&gt; [1100,401]
    end
    version :all_140x88 do    
      process :resize_to_fill =&gt; [140,88]
    end
  
    def kraken(file)
      if version_name.to_s == ""      
        storepath = store_dir + "/" + filename
      else
        fname = filename.split('.')
        #originalfile = "http://s3.amazonaws.com/" + ENV["FOG_DIRECTORY"] + "/" + store_dir  + "/" + fname[0] + "_" + version_name.to_s + "." + fname.last
        storepath = store_dir + "/" + fname[0] + "_" + version_name.to_s + "." + fname.last
      end
  
      originalfile = "http://s3.amazonaws.com/" + ENV["FOG_DIRECTORY"] + "/" + store_dir + "/" + filename
  
      kraken = Kraken::API.new(
          :api_key =&gt; ENV['KRAKEN_API'],
          :api_secret =&gt; ENV['KRAKEN_SECRET']
      )
  
      params = {}
  
  
      Rails.logger.info('Kraken About to Process: ' + originalfile);
      p "version #{version_name}"
      case version_name.to_s
        when "admin"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                       'width' =&gt; 200,
                       'height' =&gt; 200,
                      'strategy' =&gt; "auto",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
  
        when "leisure_358x243"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 358,
                      'height' =&gt; 243,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "leisure_900x500"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 900,
                      'height' =&gt; 500,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }           
        when "leisure_350x147"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 350,
                      'height' =&gt; 147,
                      'strategy' =&gt; "exact",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "leisure_1100x344"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1100,
                      'height' =&gt; 344,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
  
        when "business_360x160"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 360,
                      'height' =&gt; 160,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }                
        when "business_1100x315"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1100,
                      'height' =&gt; 315,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }                
        when "business_1140x530"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1140,
                      'height' =&gt; 530,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "business_1100x355"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1100,
                      'height' =&gt; 355,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
  
        when "commthree_550x300"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 550,
                      'height' =&gt; 300,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "commthree_319x183"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 319,
                      'height' =&gt; 183,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "commthree_1920x700"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1920,
                      'height' =&gt; 700,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
  
        when "all_360x188"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 360,
                      'height' =&gt; 188,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "all_1100x401"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 1100,
                      'height' =&gt; 401,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
        when "all_140x88"
          params = {
                  'lossy' =&gt; true,
                  'resize' =&gt; {
                      'width' =&gt; 140,
                      'height' =&gt; 88,
                      'strategy' =&gt; "fit",
                  },
                  's3_store' =&gt; {
                                'key' =&gt; ENV['AWS_ACCESS_KEY_ID'],
                                'secret' =&gt; ENV["AWS_SECRET_ACCESS_KEY"],
                                'bucket' =&gt; ENV["FOG_DIRECTORY"],
                                'acl' =&gt; 'public_read',
                                'path' =&gt; storepath
                                },
                    }
      end
  
  
      #Store the file online
  
      if !version_name.blank?
  
        Rails.logger.info('UPLOADING TO: ' + store_path);
  
        data = kraken.url(originalfile,params)
  
        if data.success
            Rails.logger.info('KRAKEN: Success! Optimized image URL: ' + data.kraked_url)
        else
            puts 
            Rails.logger.info('KRAKEN: Fail. Error message: ' + data.message)
        end
  
      end    
    end
  
    # Add a white list of extensions which are allowed to be uploaded.
    # For images you might use something like this:
    def extension_white_list
      %w(jpg jpeg gif png)
    end
  
    # def cache_dir    
    #   "#{Rails.root}/tmp/uploads"
    # end
  
    def store_dir
      if model.id.present?
        "photos/#{model.id}"
      else
        "photos"
      end
    end
  
    #file name is missing extension!!!
    def filename
      original_filename if original_filename.present?
    end
  
    def crop
       puts "i am uploader"
      if model.crop_x.present?
        manipulate! do |img|
          puts "i am cropping"
          cx = model.crop_x.to_i
          cy = model.crop_y.to_i
          cw = model.crop_w.to_i
          ch = model.crop_h.to_i        
          img.crop"#{cw}x#{ch}+#{cx}+#{cy}"
          #img
        end
      end
    end
  
    def business_theme? (image)
      p "model:#{model.id}"
      (model.hotel_detail.site.theme.layout == "x1")if model.id.present?
    end
  
    def leisure_theme? (image)
      p "model:#{model.id}"
      (model.hotel_detail.site.theme.layout == "x2")if model.id.present?
    end
  
    def commthree_theme? (image)
      p "model:#{model.id}"
      (model.hotel_detail.site.theme.layout == "x3") if model.id.present?
    end
  
    protected
    def secure_token(length=16)
      var = :"@#{mounted_as}_secure_token"
      model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
    end
  
  end
  </code></pre>
  
  <p>Please suggest me how to calculate the correct value of <code>WEB_CONCURRENCY</code>.</p>