April 2nd, 2015
(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: email@example.com
I love that Rails 4.2 offers a unified interface for working with queues. This is obviously important for Ruby, since Ruby sucks at concurrency, and needs to rely on a lot of queue technology to facilitate most forms of async (some people then jump in: “blah, blah, blah, Fiber, etc, etc.” I get that, but still, the reality is, using Fiber does not magically give you Clojure, or Elixir).
Why do I need Background Jobs?
One common situation for needing background jobs is consuming external APIs. It is unrealistic to think that all of your external APIs will have 100% uptime. By allowing API interaction to happen in a background job we can manage failures more effectively. Instead of having the user wait for a task to end, we send that task to the background queue. The User can then continue interacting with the app. Background jobs also give us the opportunity to prioritize jobs which are waiting in out background job queue. In many cases using background jobs can significantly reduce the required resources of the app.
Jumping into background jobs can be a little daunting for someone new to rails. Right off the bat you are urged to learn about about several new gems, key-value stores, queues, asynchronous programming, thread safety, and error handling. The good news is that learning background jobs will be very worthwhile. Many Rails apps today are, or should be, using background jobs. With Active Job now in the rails core its likely we will start to see more people using background jobs. Below contains a brief example of implementing a background job with Active Job.
Background Job Candidates
We want our background jobs to have no additional effects if we call them multiple times with the same parameters. This is the concept of idempotency. If we are choosing to use Sidekiq we must also consider if the gems we are using are threadsafe.
Common Background Job Use Cases
3rd Party APIs – Working with external services.
Computational Intensive Work – Such as Solving equations or image processing.
Sending Emails – We don’t want to keep the user waiting for an email to send.