Rails, Sidekiq and Redis in 3 gifs
As part of this intro to Ruby and Ruby on Rails I wrote a small introduction to Sidekiq, Redis and some concurrence concepts. Then I decided to split it into a second article, which follows:
To get you started with the some scalability concepts I’ll take the liberty to tell a short story illustrated by one of my favorite movies, Ratatouille.
Suppose you go to a restaurant and the waiter is also the cook. You won’t feel the difference if you are there alone, because he’s a hell of a waiter and can cook well enough. But the restaurant is popular and clients are demanding.
Even though the waiter is so good, he can’t take, produce and serve all requests at the same time all alone. He needs his sidekicks.
There’s all sorts of instances of this problem in software development, with all sorts of implementations and solutions. Running locally, you are alone in the restaurant; you can make a “right-now”, synchronous request and you’ll get some yummy response. In production, you’d hold everybody else’s orders if you get so demanding. Your app basically crashes.
Get Chris’s stories in your inbox
Join Medium for free to get updates from this writer.
Enter async requests. You don’t order your food for “right-now”, you order your food “to-wait”. If everyone else does the same — or rather, if the restaurant only takes “to-wait” requests — and the waiter gets the help of other processing threads, he can queue the orders while others work on the response. You just need a job queue and some workers to execute the jobs.
In the Ruby world (and beyond), a popular approach to solve this problem is to use Redis and Sidekiq. After a request passses the application middleware and routing frontiers (e.g. authentication), Redis queues the jobs that will result in a response for that request. The user waits while a Sidekiq worker thread prepares a response.
The service doesn’t get blocked if a costly request is made, and you can make more working nodes available with minimal changes in configuration and communication between services.
For a more hands-on approach, check this awesome video by Jordan Leigh.





