Introduction
Sinatra is a minimal Ruby web framework that maps HTTP verbs directly to Ruby blocks. Created in 2007, it provides a simple DSL for routing and handling requests without the overhead of a full MVC stack, making it ideal for APIs, microservices, and small web applications.
What Sinatra Does
- Maps HTTP methods (GET, POST, PUT, DELETE) to Ruby blocks with a one-liner DSL
- Renders views with ERB, Haml, Slim, and other template engines
- Handles URL parameters, query strings, and request bodies with simple accessors
- Supports before/after filters, helpers, and error handlers
- Serves as a Rack application, compatible with any Rack-based middleware or server
Architecture Overview
Sinatra is built on Rack, the standard Ruby web server interface. Each route is a pair of an HTTP method and a URL pattern stored in a lookup table. When a request arrives, Sinatra matches it against registered routes in order, executes the matching block, and wraps the return value in a Rack response. The framework supports two modes: classic (top-level DSL) and modular (subclassing Sinatra::Base) for mounting multiple apps or using middleware. There is no ORM, migration system, or asset pipeline built in — you add only what you need.
Self-Hosting & Configuration
- Install with
gem install sinatraor add to a Gemfile with Bundler - Run apps directly with
ruby app.rbor use Rackup with aconfig.rufile - Configure settings like port, environment, and logging via
set :option, value - Use Puma, Thin, or Falcon as the production Rack server
- Deploy anywhere Ruby runs: Heroku, Docker, systemd, or traditional hosting
Key Features
- Routes defined in a single file with expressive HTTP verb methods
- Modular design lets you mount Sinatra apps as Rack middleware inside Rails or other frameworks
- Streaming and Server-Sent Events support for real-time responses
- Built-in development reloading via the
sinatra-reloaderextension - Lightweight footprint under 2,000 lines of code
Comparison with Similar Tools
- Rails — Full-stack MVC framework; Sinatra is minimal and requires you to choose your own components
- Flask (Python) — Similar micro-framework philosophy; Sinatra inspired Flask's route decorator pattern
- Express (Node.js) — Comparable minimalism in JavaScript; Sinatra predates Express and influenced its design
- Hanami — Modern Ruby framework with more structure; Sinatra trades structure for simplicity
FAQ
Q: When should I use Sinatra instead of Rails? A: Sinatra is a good fit for APIs, microservices, webhooks, and small apps where Rails' conventions would be overkill.
Q: Can Sinatra scale to large applications?
A: Yes, using the modular style with Sinatra::Base. However, for complex apps with many models and views, a full framework like Rails may be more productive.
Q: Does Sinatra include an ORM? A: No. Pair it with ActiveRecord, Sequel, ROM, or any Ruby database library of your choice.
Q: How does Sinatra handle testing?
A: Sinatra provides Rack::Test integration for simulating HTTP requests in tests with RSpec, Minitest, or any Ruby test framework.