CORS configuration for cross-origin API requests

When building APIs consumed by frontend applications hosted on different domains, CORS (Cross-Origin Resource Sharing) headers are mandatory. The rack-cors gem simplifies configuration by letting me whitelist specific origins, HTTP methods, and header

Health check endpoint for deployment monitoring

Load balancers and orchestration platforms like Kubernetes rely on health check endpoints to determine if an application instance is ready to serve traffic. A robust health check doesn't just return 200 OK—it verifies critical dependencies like databa

Fragment caching for expensive JSON serialization

Serializing complex ActiveRecord objects to JSON can consume significant CPU time, especially when rendering collections with nested associations. Fragment caching stores rendered JSON fragments in Redis keyed by a cache key that includes the record's

Service objects for complex business logic

As business logic grows, controllers become bloated with transaction management, error handling, and cross-model orchestration. Service objects extract this complexity into dedicated classes with a single public method (usually call), keeping controll

Database indexes for query optimization

Proper indexing is the difference between millisecond and multi-second query response times. I add indexes to foreign keys automatically since Rails doesn't do this by default, and I create composite indexes for common query patterns that filter on mu

ActiveRecord scopes for reusable query logic

Scopes encapsulate reusable query logic directly in the model, improving code readability and reducing duplication across controllers and services. I use scopes for common filters like active, published, or recent rather than writing raw where clauses

Background jobs with Sidekiq and reliable queues

Moving slow operations to background jobs keeps API response times fast and improves perceived performance. Sidekiq with Redis provides a robust, production-proven job queue that handles millions of jobs per day. I organize workers into separate queue

N+1 query detection with Bullet gem

N+1 queries are the silent performance killer in Rails apps—they're easy to introduce during rapid development and expensive to diagnose in production. The Bullet gem monitors queries during development and test runs, raising alerts when it detects mi

Pagination with cursor-based approach

Traditional offset-based pagination becomes unreliable and slow for large datasets when records are frequently inserted or deleted—users can miss items or see duplicates across pages. Cursor-based pagination solves this by using an opaque token that e

Structured JSON error responses

Consistent error handling transforms debugging from guesswork into systematic troubleshooting. I use a rescue handler that catches exceptions globally and transforms them into a standard JSON structure containing an error code, human-readable message,

Rate limiting with Redis and Rack::Attack

Rate limiting is essential protection against abuse and ensures fair resource distribution across API consumers. Rack::Attack with Redis backing provides a robust, shared state solution that works across multiple application servers. I define differen

JWT authentication with refresh tokens

Stateless authentication with JWT tokens simplifies horizontal scaling but introduces security concerns around token lifetime and revocation. I use short-lived access tokens (15 minutes) combined with longer-lived refresh tokens stored in an encrypted