Things to Remember When Working with Sidekiq
In Netguru, it’s mostly used for time-consuming tasks such as sending emails to customers or long database queries. There are few things you must remember about when working with Sidekiq.
TIP: Keep Jobs Parameters Simple
Sidekiq jobs can receive parameters of any format, but the more complex they are, the more likely they are to become corrupted. The parameters are serialised and stored in Redis until the queued job is run. It’s a good practice to pass only the most basic data types: integers (database IDs) or strings.
TIP: Don’t Expect Jobs to Be Completed in Fixed Time
It’s essential to design the application independent of the jobs' completion time. Even when jobs are mostly run in two seconds, they may always get enqueued for a long time, especially if the application scales up.
TIP: Binding.pry Works with Sidekiq in Development Environment
Even though the jobs are run in different threads, the binding.pry works fine. Developers tend to think that it doesn’t because of lack of code autoreload. You need to restart Sidekiq after every change in the code.
TIP: Remember about Redis Namespaces
When Redis is shared with many containers or apps, Sidekiq needs to be configured to use namespaces to avoid running jobs from other applications. You can use a gem called "redis-namespace" and setup a proper namespace in config/initializers/sidekiq.rb:
Sidekiq.configure_server do |config|
config.redis = { url: AppConfig.redis.url, namespace: AppConfig.redis_namespace }
end
Sidekiq.configure_client do |client|
client.redis = { url: AppConfig.redis.url, namespace: AppConfig.redis_namespace }
end
TIP: Remember to Clear the Retries Queue
Don’t forget about jobs that fail during the development. They are put in the retires queue and rerun every few minutes, even after you fix your code. By spamming the Sidekiq output with continuous failures, they may make you think that the job doesn’t work properly. Remember to clear them in /sidekiq/retries.
I hope these few tips make your dev’s life easier. Take care!