How Rails Apps Actually Run Under the Hood
π How Rails Apps Actually Run Under the Hood β A Deep Dive for Developers
Ever wondered what truly happens when you hit http://localhost:3000 in your browser? π€ Rails feels magical β but in reality, itβs a finely engineered machine powered by dozens of moving parts.
Today, letβs open that machine, see whatβs inside, and understand how a Rails request travels, what hidden classes do the heavy lifting, and how the whole framework works under the hood.
Letβs begin your journey from browser β server β your Rails code β back to browser. βοΈπ₯
π₯ π The Journey of a Rails Request (Step-by-Step Story)
To make it easy, weβll use an example app:
π URL requested:
GET /articles/5
π Expected output: A page showing article with ID = 5.
Letβs start!
π§± 1. The Server Layer β Puma Starts the Engine
When you run:
rails server
Rails starts Puma, the default multi-threaded web server.
π― What Puma does:
- Listens for HTTP requests
- Manages threads/pools
- Passes requests to the Rack interface
π§© Hidden class in action:
Puma::Server
Handles incoming TCP connections and dispatches them to worker threads.
π 2. Rack β The Gateway Between Web Server & Rails
Rails is a Rack app, meaning every request passes through the Rack pipeline.
π Rack responsibilities:
- Normalizes HTTP request
- Converts request into a Ruby hash
-
Calls the Rails app via:
call(env)
π§© Hidden classes:
Rack::Handler::PumaRack::Lint(ensures request validity)Rack::Runtime(adds request time headers)
π§ 3. Rails Boot Process β The Framework Wakes Up
Before your app does anything, Rails boots its full environment.
π― Key steps:
- Loads
config/application.rb - Loads Railties (ActiveRecord, ActionPack, ActiveJobβ¦)
- Runs initializers inside
config/initializers/**/* - Loads routes
- Prepares the middleware stack
π§© Hidden classes:
Rails::Application::FinisherHandles final boot steps like setting up I18n and eager loading.Rails::InitializableRuns initializers across all Railties.
π§± 4. Rails Middleware β The Security Shield π‘οΈ
Before your controller sees anything, the request passes through many middleware layers.
Example middleware:
Rack::SendfileActionDispatch::StaticActionDispatch::HostAuthorizationActionDispatch::CookiesRack::Session::CookieActionDispatch::RequestIdActionDispatch::RemoteIp
π§© Fun fact: Over 25 middleware execute before controller logic starts.
π§© Unknown but powerful classes:
ActionDispatch::ExecutorActionDispatch::ContentSecurityPolicy::MiddlewareActionDispatch::ServerTiming
Each plays a hidden but crucial role.
πΊοΈ 5. Router β Finding the Right Controller
The router uses the request path (/articles/5) and HTTP method (GET) to find a matching route.
Internally:
Rails uses a Radix tree-like matcher for super fast routing.
Example route:
GET /articles/:id β ArticlesController#show
π§© Hidden class that powers this:
ActionDispatch::Journey::RouterActionDispatch::Journey::Path::Pattern
These convert your route definitions into highly optimized matching structures.
π¬ 6. Controller Processing β Where Your Code Finally Runs
Rails now creates a controller object:
ArticlesController
Then runs:
- Filters
- Callbacks
- Params parsing
- Strong Params
- Rendering or Redirecting
Controller lifecycle:
before_actionβ Run checks- Action method (e.g.,
show) rendertemplateafter_action
π§© Unknown classes here:
ActionController::MetalThe super bare-bones controller Rails inherits everything from.ActionController::InstrumentationLogs all events for performance debugging.ActionController::ParametersManages strong params.
π§© 7. ActiveRecord β The Heart of Database Access π§‘
Inside ArticlesController#show:
@article = Article.find(5)
Hereβs what actually happens:
Steps behind the scenes:
- Query Builder builds SQL
- Connection pool assigns a DB connection
- SQL executes via adapter (e.g., PostgreSQL)
- Result is converted to Ruby objects
π§© Important internal classes:
ActiveRecord::RelationActiveRecord::ConnectionAdapters::ConnectionPoolActiveRecord::AttributeSetActiveModel::Type::Value(type casting)
Rails does a LOT before returning a simple model object.
π¨ 8. View Rendering β ERB/Haml/Builders Convert to HTML
When Rails runs:
render "articles/show"
Hidden flow:
- View lookup path resolver
- Template reading
- ERB compiling into Ruby methods
- Layout handling
- Response body formatting
π§© Key internal classes:
ActionView::Template::Handlers::ERBActionView::LookupContextActionView::Renderer
π¦ 9. Response Sent Back β Rack Again
Rails returns a response triplet:
[status_code, headers, response_body]
Rack sends it back to Puma β Browser.
Browser displays your HTML. Request lifecycle ends. π
βοΈ π§© Behind-the-Scenes Classes You Rarely Hear About
Here are some powerful but less-known Rails classes:
πΉ ActiveSupport::Notifications
Used for internal event tracking & performance monitoring.
πΉ ActiveSupport::Dependencies::ZeitwerkIntegration
Connected to app auto-loading & constant management.
πΉ ActionDispatch::Request
Parses cookies, JSON, headers β way more powerful than you think.
πΉ ActionView::PathSet
Handles template searching logic.
πΉ ActiveModel::Errors
Manages validation error collection and formatting.
β‘ Putting It All Together (Diagrammatic Summary)
Request Flow
Browser β Puma β Rack Middleware β Router β Controller β Model β View β Rack β Browser
Example for /articles/5
- Puma accepts request
- Rack formats env
- Middleware security checks
- Router finds route
- Controller loads article
- ActiveRecord fetches DB
- View renders
- Response returns
π Final Thoughts
Rails is not magic β itβs beautiful engineering. From middleware to autoloading, from routing engines to view renderers β hundreds of classes work silently to deliver that single page you see.
Understanding these internals makes you a 10Γ better Ruby on Rails developer π₯.
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.