-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
117: Practice Exercise: Building a Simple Job Queue with Sidekiq
I want to talk about one of the most common mistakes I see developers make when their app starts to grow: doing too much work during the request-response cycle. Imagine you're building a user registration flow. You've got the user's details, you've saved them to the database, and now you want to send a "Welcome!" email. It seems simple enough to just call your mailer right there in the controller and move on.
The "Just Do It Now" Trap
In a naive implementation, your code probably looks something like this:
def create
@user = User.new(user_params)
if @user.save
UserMailer.welcome_email(@user).deliver_now
redirect_to dashboard_path, notice: "Welcome aboard!"
end
end
On your local machine with one user, this feels instantaneous. But here is where it breaks in production. Sending an email requires a network request to an external provider like SendGrid or Postmark. If that provider takes two seconds to respond, your user is staring at a white screen for two seconds. If the provider is down or timing out, your entire registration process crashes with a 500 error, even though the user was actually saved to the database. You've tied the availability of your core business logic to a third-party API. That's a gamble I'm not willing to take.
Offloading the Heavy Lifting
The better way—the professional way—is to treat the email not as a part of the request, but as a "job" to be done later. This is where Sidekiq comes in. Instead of forcing the user to wait, we tell Sidekiq, "Hey, here is a user ID; send them a welcome email whenever you have a spare millisecond," and we immediately redirect the user to their dashboard.
First, we move the logic into a Worker class:
class WelcomeEmailWorker
include Sidekiq::Worker
def perform(user_id)
user = User.find(user_id)
UserMailer.welcome_email(user).deliver_now
end
end
Then, in the controller, we swap the direct call for perform_async:
def create
@user = User.new(user_params)
if @user.save
WelcomeEmailWorker.perform_async(@user.id)
redirect_to dashboard_path, notice: "Welcome aboard!"
end
end
Notice that I passed the user_id and not the whole user object. This is a crucial detail. Sidekiq stores job arguments in Redis as JSON. If you pass a complex Ruby object, you're serializing a snapshot of that object. If the user changes their email address in the split second between the job being queued and the job being executed, the worker would be using stale data. Always pass simple identifiers.
The Price of Asynchronousity
Now, I'll be honest with you: this isn't a "free" upgrade. By introducing Sidekiq, you've just added two new pieces of infrastructure to your stack: Redis (to hold the queue) and a separate Sidekiq process (to execute the jobs). You now have to monitor that process. If the Sidekiq process crashes, your users will sign up just fine, but they'll never get their emails, and you won't know it until someone complains.
However, the trade-off is almost always worth it. You get automatic retries—if the email API is down for five minutes, Sidekiq will exponentially back off and try again until it succeeds. More importantly, your application remains snappy. Your users get an immediate response, and your web workers are freed up to handle the next request instead of idling while waiting for a remote server to say "OK."
📋 Practical Task
Exercise: Implementing a Background Image Processor
Imagine you are building a profile page where users upload a high-resolution avatar. Processing this image (resizing, cropping, and uploading to S3) is too slow to do in the controller and is causing request timeouts.
Your Goal: Refactor the synchronous image processing logic into a Sidekiq worker.
- Create a
ProfileImageWorkerclass that includesSidekiq::Worker. - Implement a
performmethod that accepts auser_id. - Inside the worker, simulate the processing work by calling a hypothetical
ImageService.process_avatar(user)method. - Modify a hypothetical
UsersController#update_avataraction so that it no longer calls theImageServicedirectly, but instead queues theProfileImageWorkerusingperform_async. - Ensure you are passing the
idof the user to the worker, not the user object itself.
There are no comments for now.