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

Query objects for complex ActiveRecord queries

When queries become too complex for scopes—involving multiple joins, subqueries, or raw SQL fragments—I extract them into dedicated query objects. Each query object is a plain Ruby class that encapsulates one specific query pattern and returns an Acti

db/sql prepared statements with context and explicit Close

Prepared statements are useful when the same query runs in hot loops, but they come with lifecycle responsibilities. I prepare once (usually at startup), store the *sql.Stmt, and always close it on shutdown. The important detail is still using QueryRo

Prisma transaction with retries for serialization errors

Under real concurrency, even ‘simple’ write paths can hit serialization failures, especially with stricter isolation levels. Instead of pretending it won’t happen, I wrap the transaction in a small retry loop with jitter. I only retry on known transie

Transactional Outbox for Reliable Event Publishing

I used a transactional outbox when I needed my database write and my event publish to succeed or fail together. In OutboxEvent model, I treated the outbox like a queue: a durable row per event, a dedupe_key for idempotency, and a ready scope that pull

Copy-to-clipboard button with Stimulus

Copy buttons are everywhere (invite links, API keys, CLI commands). With Stimulus, I keep it tiny and resilient: use navigator.clipboard.writeText when available, and fall back to selecting a hidden input for older browsers. I also provide immediate f

Redis caching for expensive computations

Redis provides a fast, in-memory cache for expensive computations that don't change frequently. I use Rails.cache with the Redis store to cache things like trending posts calculations, aggregated statistics, or external API responses. The fetch method

A reusable respond_to block for turbo_stream + html

Once you’ve built a few Hotwire controllers, you notice the same respond_to shape repeated. I extract a small helper (a concern) that yields a respond_to block and centralizes the “default HTML redirect” pattern. This keeps controllers readable and re

Django logging configuration for production

Proper logging is critical for production debugging. I configure loggers for different apps and libraries. I use file handlers for persistence and console handlers for development. Log rotation prevents disk fill-up. I set appropriate levels (DEBUG, I

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

Background job retry strategies

Not all background job failures should be retried the same way. Transient failures like network timeouts benefit from exponential backoff, but bugs in code or invalid data should fail immediately after a few attempts. Sidekiq provides retry configurat

Notifications dropdown that live-updates with Turbo Streams

For a notifications dropdown, I keep the list server-rendered and let Turbo Streams keep it fresh. The dropdown content sits in a turbo_frame_tag (so you can also navigate to a full notifications page), and the unread badge is a separate small target