redis

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

Lock-Free Read Pattern for Hot Counters (Approximate)

Sometimes exact counters aren’t worth the write cost. For high-traffic views, track increments in Redis and periodically aggregate into the DB. It’s an operational tradeoff that can dramatically reduce DB pressure.

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

Redis Pub/Sub subscriber with reconnect-friendly loop

Pub/Sub consumers should assume connections will drop: Redis restarts, network blips, or idle timeouts happen. I keep the subscription loop simple: subscribe, range over the channel, and exit cleanly when ctx.Done() fires. If the subscription ends une

Rails Kredis for higher-level Redis operations

Kredis provides typed Redis structures as Active Model attributes, simplifying common patterns like counters, flags, and lists. Instead of raw Redis commands, I define kredis accessors on models that handle serialization automatically. Counters track

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

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

Redis cache-aside for expensive reads

Most ‘caching’ bugs are really invalidation bugs, so I stick to a simple cache-aside pattern with conservative TTLs and treat cache misses as normal. The big failure mode to avoid is a stampede: if many requests miss at once, you can crush your DB. Fo

BullMQ worker with retries + dead-letter

Background jobs will fail in production, so I like having a predictable story for retries and poison messages. BullMQ is a solid middle ground: Redis-backed, straightforward, and good enough for most apps. I set explicit attempts and backoff, and when

API throttling with custom Redis-based limiter

While Rack::Attack handles basic rate limiting, custom throttling logic gives fine-grained control over quotas, burst allowances, and per-feature limits. I implement a token bucket algorithm in Redis using sorted sets to track request timestamps per u

BullMQ job idempotency via dedupe id

Retries are great, but duplicates are inevitable—workers crash, Redis reconnects, deploys happen. I prefer building idempotency into the job key itself. BullMQ supports a jobId that acts like a de-dupe key: if a job with the same id already exists, en

Rate limiting and API throttling

Rate limiting prevents API abuse and ensures fair resource usage. I implement rate limiting using Bucket4j for token bucket algorithm or Redis for distributed scenarios. Limits apply per user, IP, or API key. HTTP 429 (Too Many Requests) indicates lim