DEV Community

Cover image for Understanding Rack #1 : Notes from Rebuilding Rails by Noah Gibbs
Shrouk Abozeid
Shrouk Abozeid

Posted on

Understanding Rack #1 : Notes from Rebuilding Rails by Noah Gibbs

What is Rack?

Rack is a Ruby gem that acts as a middleman between your Ruby framework (like Rails or Sinatra) and the web server/application server that runs your code.

Think of it like a translator:

Browser <--> Web Server/App Server <--> Rack <--> Rails App
Enter fullscreen mode Exit fullscreen mode

When a browser sends a request:

  1. The server receives it.
  2. Rack converts the request into a format Rails understands.
  3. Rails processes the request and generates a response.
  4. Rack converts the response back into a format the server can send to the browser.

What is an application server?

An application server is a program that actually runs your Ruby application.

Examples:

  • Puma
  • Thin
  • Passenger
  • WEBrick
  • Unicorn

These servers execute your Rails code and return the result to users.


Development vs Production

Development (your local machine)

Usually you run only one application server:

rails server
Enter fullscreen mode Exit fullscreen mode

Rails starts Puma by default.

Browser <--> Puma <--> Rails
Enter fullscreen mode Exit fullscreen mode

This is simple and works well for development.


Production (real website)

In production, there is often a dedicated web server in front of the application server:

Browser <--> Nginx/Apache <--> Puma <--> Rails
Enter fullscreen mode Exit fullscreen mode

Why?

  • Nginx/Apache can serve static files efficiently.
  • They can handle SSL/HTTPS.
  • They can distribute traffic to multiple Puma processes.
  • They provide better performance and security.

Real-world analogy

Imagine a restaurant:

  • Browser = customer
  • Nginx/Apache = receptionist
  • Rack = waiter translating orders
  • Rails app = chef
  • Puma = kitchen where the chef works

The customer doesn't talk directly to the chef. The request passes through several layers, and Rack helps them communicate using a common language.

So in one sentence we can say

Rack is the standard interface that allows Ruby web servers (Puma, Unicorn, etc.) and Ruby web frameworks (Rails, Sinatra, etc.) to communicate with each other.

Top comments (0)