API key authentication for service-to-service calls

While JWT works well for user authentication, service-to-service communication often uses simpler API key authentication. I generate cryptographically random API keys using SecureRandom.hex(32) and store them hashed in the database, similar to passwor

Database query explain analysis for optimization

Understanding query execution plans is essential for optimizing slow queries. Rails provides explain method on ActiveRecord relations to show PostgreSQL's query planner output. I look for sequential scans on large tables (indicating missing indexes),

ActiveStorage for file uploads

ActiveStorage provides a unified interface for uploading files to cloud storage services like S3, GCS, or Azure Storage. I configure storage services in config/storage.yml and attach files to models using has_one_attached or has_many_attached macros.

ETags for conditional requests and caching

ETags enable efficient caching by allowing clients to make conditional requests that return 304 Not Modified when content hasn't changed. Rails automatically generates ETags based on response content, and fresh_when or stale? methods handle the condit

Background job scheduling with sidekiq-scheduler

Recurring tasks like cleanup jobs, report generation, or cache warming need reliable scheduling. The sidekiq-scheduler gem extends Sidekiq with cron-like scheduling without requiring separate infrastructure like cron or Kubernetes CronJobs. I define s

Secure password reset flow with signed tokens

Password reset workflows require careful security design to prevent account takeover. I generate time-limited, single-use tokens using Rails' signed_id feature which creates tamper-proof tokens without database storage. The token expires after a short

Multi-tenancy with apartment gem

SaaS applications serving multiple customers (tenants) need data isolation to prevent cross-tenant data leaks. The apartment gem provides schema-based multi-tenancy for PostgreSQL where each tenant gets a separate schema, ensuring complete database-le

API response compression with Rack::Deflater

Large JSON payloads consume bandwidth and increase latency, especially for mobile clients on slow connections. Rack::Deflater middleware automatically compresses responses using gzip when clients send Accept-Encoding: gzip headers. This typically redu

Request deduplication with idempotency keys

Network failures and client retries can cause duplicate request processing, leading to duplicate charges, double-created resources, or inconsistent state. Idempotency keys solve this by tracking processed requests and returning cached responses for du

JSON column for flexible schema extensions

PostgreSQL's jsonb columns provide schema flexibility for semi-structured data without sacrificing query performance. I use JSON columns for user preferences, feature flags, or metadata that varies by record type. Unlike traditional EAV patterns, json

Bulk operations with ActiveRecord import

Inserting thousands of records one-by-one is prohibitively slow due to the overhead of individual INSERT statements. The activerecord-import gem provides bulk insert capabilities that compile multiple records into a single multi-row INSERT, dramatical

Audit logging for sensitive operations

Audit logs provide accountability and forensic capabilities for sensitive operations like permission changes, data deletion, or financial transactions. I store audit events in a dedicated table with who performed the action, what changed, when it occu