Xavier Noria - The Rails Boot Process - Rails World 2024

Dive into Rails' boot sequence with Xavier Noria as he explores config files, initializers, and lazy loading improvements in Rails 8, enhancing app performance and startup.

Key takeaways
  • Rails boot process consists of three main config files: config/boot.rb, config/environment.rb, and config/application.rb

  • The boot process follows a specific order:

    1. Load config/boot.rb to set up Bundler
    2. Set up autoloaders (once and main)
    3. Load Rails and gem dependencies
    4. Load initializers in topological order
    5. Build middleware stack
    6. Load routes (lazy in Rails 8)
  • Rails applications are built on three key concepts:

    • Railties (base class for extending Rails)
    • Engines (like mini Rails apps with their own MVC)
    • Applications (subclass of Rails::Engine)
  • Initializers are fundamental to the boot process:

    • Over 300 initializers in a typical Rails app
    • Run in topological order
    • Can be configured with before/after hooks
    • Come from both the framework and application code
  • Lazy loading improvements in Rails 8:

    • Routes are lazy loaded by default
    • Constants managed by autoloaders load on demand
    • Better boot performance through delayed loading
  • Key boot process features:

    • Configuration hooks for customization
    • Inheritance-based setup through Rails::Application
    • Environment-specific configuration precedence
    • Separate autoloaders for application and framework code
  • Most Rails commands require full application boot, with few exceptions like rails stats

  • The boot process is distinct from server launch - booting means getting the application ready for use, while launching the server is a separate step