When you're just writing Controllers and Models day to day, you don't really think about what's running underneath Rails.
But the moment you try to add an "always-connected" feature like real-time chat or notifications to a Rails app, suddenly people start saying things like "Unicorn doesn't play well with that" or "Action Cable handles it fine."
In this post, I'll lay out the machinery under Rails that you need to make sense of those conversations, using a restaurant as the analogy.
1. Rack: the standard order ticket format
Rack is the common agreement that connects Ruby web servers and web apps.
Any object that has
call(env)and returns[status, headers, body]is a Rack app
class HelloApp
def call(env)
[200, { 'content-type' => 'text/plain' }, ['Hello']]
end
end
In restaurant terms, it's the order ticket format. Because the format is fixed, you can freely mix and match the dining floor (Unicorn, Puma) and the kitchen (Rails, Sinatra).
Before an order ticket reaches the kitchen, it passes through middleware (the security guard and the front desk at the entrance).
Customer → Security guard (Rack::Attack) → Front desk (Rack::Cors) → Kitchen (Rails)
2. Layers: dining floor → front desk → kitchen
Web server (Unicorn / Puma) Dining floor: greets customers and assigns staff
Rack + middleware Order tickets, plus the front desk and security
Application layer (Rails) Kitchen: where Controllers and Models run
The "application layer" here is not the one from the OSI reference model. It just means the stage where your Rails code runs.
3. Processes and threads: restaurants and staff
Process = a restaurant (has its own kitchen and fridge)
Thread = a staff member (shares the fridge within the same restaurant)
| Process (restaurant) | Thread (staff member) | |
|---|---|---|
| Memory | Separate | Shared |
| Cost to create | Heavy | Light |
| The scary part | Eats memory | Fighting over shared data |
x = 0
Thread.new { x = 100 }.join
p x # => 100 (shared, so it gets overwritten)
y = 0
Process.wait(fork { y = 100 })
p y # => 0 (a different restaurant, so no effect)
When staff share a fridge, you get accidents like "two people count the eggs at the same time and the inventory count ends up wrong." That's the thread-safety problem. In Ruby, class variables and global variables are that "shared fridge."
4. Blocking: a staff member stuck standing by the table
When a thread is waiting on something and can't do anything else in the meantime, that's called blocking.
Staff: "What can I get you?"
Customer: (staring at the menu for 30 minutes...)
Staff: (just standing there by the table the whole time)
For normal requests the wait is a split second, so it's no big deal. The trouble starts with "always-connected" communication like long polling or WebSockets.
5. Unicorn: open lots of one-person restaurants
Unicorn master (owner)
├ Worker 1 (one staff member)
├ Worker 2 (one staff member)
└ Worker 3 (one staff member)
- Each worker can only handle one request at a time
- Each restaurant is independent, so it's safe. But it eats memory
When always-connected traffic shows up, this happens:
Workers 1–3: all standing by a customer's table
→ Nobody's free to serve the next customer 💀
6. Puma: put lots of staff in one restaurant
Puma process
├ Thread 1 (staff)
├ Thread 2 (staff)
└ Thread 3 (staff)
- Handles more customers with less memory than Unicorn
- Your code needs to be thread-safe
Ruby has the GVL, which means only one thread can run Ruby code at a time (picture a kitchen with just one knife). Still, a thread lets go of the knife while it waits on the DB and such, so for web apps it's plenty fast.
But with always-connected traffic, the threads all get tied up waiting, and you end up jammed just like with Unicorn.
7. Action Cable: a floor attendant making rounds with call bells
(Runs on top of Rails)
Here we flip the idea: stop waiting by the table.
Floor attendant (just one):
Puts a call bell on every table
→ Only goes over to the tables whose bell rings, and handles them quickly
This is an event loop. Since nobody is glued to idle connections, it can handle a huge number of them.
Action Cable uses Rack's rack.hijack to take over the connection itself and hand it to this event loop. That frees up the web server's worker right away, so it even works on top of Unicorn.
Weakness: if the floor attendant gets absorbed in one heavy task, every table has to wait.
→ On Action Cable, avoid heavy queries and sleep, and hand heavy work off to background jobs.
Summary
| What gets stuck on idle connections | As connections grow | Heavy work | |
|---|---|---|---|
| Unicorn | Processes | Runs out of memory | Strong |
| Puma | Threads | Runs out of threads | Decent |
| Action Cable | Nothing | Mostly fine | Weak |
Top comments (0)