SETTING UP REDIS AND SIDEKIQ FOR HEROKU

By: Saurav

2018-02-12 10:16:00 UTC

It's a pain in the ar*e as well as a great avenue of learning something new, bringing the pieces together and finally getting the happiness of having your app work on the production server. Setting up Sidekiq with Redis is what I am talking about.

I invested two days in this, reading the documentation, reading stack overflow, and GitHub issues and so I am writing this blog for those who want to save some time and have a more channeled way of implementing background processes in your rails app.

I would go into the details of implementing background process in another blog. Let's say you already got your Redis server and sidekiq jobs in place in the development environment and have a working code which uses perform_async to add jobs to the message queues, and now you want to set it all up on Heroku.

There are few addons available on Heroku as shown below:

Addons

I have used Heroku Redis before and it was a mistake as I mixed setting with Redis To Go nano.

In this blog, I will choose Redis To Go.

Just select the addon as indicated with the above list and add the Nano-free option as below:

Addon2

Next step after adding this addon, we need to check if the worker has been started or not by using the command:

heroku ps:scale

This will give you the numbers of web and worker. Right now it would be showing the only web

Now, you need to start the worker process on Heroku. The command is shown below.

heroku ps:scale worker+1

#S.O: It turns out that there's a bug in the web UI in that some team members were not allowed to increase the number of workers from 0 to 1, even though the UI seemed to show that!

Now, if you use the last command heroku ps, it will show the web and worker as shown below:

Addon3

Now would be a great time to add gem 'redis' to your gemfile and run bundle to install redis config.

Also, we need to setup the Proc file and sidekiq.rb initializer as shown below:

#Create a new sidekiq.rb fle in the initializer directory and put the following in there:

Sidekiq.configure_client do |config|
	config.redis = {size: 1, db: 0, url: ENV["REDISTOGO_URL"] || 'redis://localhost:6379/0'}
end

Sidekiq.configure_server do |config|
	config.redis = {size: 7, db: 0, url: ENV["REDISTOGO_URL"] || 'redis://localhost:6379/0'}
end

#create a new Proc file in the main directory and put the following line in it. (It should be in the same directory where gemfile is).

worker: bundle exec sidekiq -c 5 -v

Finally, you need to do a heroku push, and do a Heroku restart. You can add sidekiq web monitoring if you want or you can test out by just running those processes which use async processes.

Finally, if everything works fine, you will have the hooray moment!


Let me know what you think!
twitter: sprakash24oct
linkedin

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)