Redis cache and queues: speeding up backends without corrupting data
Backend

Redis cache and queues: speeding up backends without corrupting data

Nam Nguyen
2 min read
4 views
#redis
#cache
#queue
#backend

Redis is fast, but it is not a cure-all#

Redis is commonly used for caching, rate limiting, sessions, lightweight locks, and queues. It can reduce database load and improve response time, but it can also create stale data, unsafe keys, duplicate jobs, and operational complexity.

Cache-aside#

With cache-aside, the application reads Redis first. On a miss, it reads the database, stores the result with a TTL, and returns the response.

TypeScript
async function getPost(slug: string) {
  const key = `post:${slug}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);

  const post = await db.posts.findBySlug(slug);
  if (!post) return null;

  await redis.set(key, JSON.stringify(post), { EX: 300 });
  return post;
}

Do not cache personalized responses with keys that are too broad. Tenant and user scope must be part of the key when data is private.

TTL, invalidation, and stampede#

Short TTLs reduce benefit; long TTLs increase stale data. Delete or refresh cache keys when important data changes. For hot keys, reduce cache stampede with locks, TTL jitter, request coalescing, or stale-while-revalidate behavior.

Queues need idempotency#

Redis-backed queues are useful for email, image resizing, webhooks, reports, and external sync. Jobs can run more than once because of retries or worker restarts, so important jobs must be idempotent.

  • Use idempotency keys for webhooks and payment-like operations.
  • Store important job state in the database.
  • Use retry limits and dead-letter queues.
  • Log job id, attempt count, duration, and final error.

Rate limits#

Redis works well for rate limits because counters are fast and can expire. Login, comments, contact forms, search, and uploads should each have appropriate limits, especially in multi-instance deployments.

Production checklist#

  • Use clear namespaces and tenant/user scopes in keys.
  • Add TTL jitter for hot keys.
  • Invalidate cache on important writes.
  • Do not cache sensitive data without correct scope.
  • Make queue jobs idempotent with retry limits.
  • Monitor memory, hit rate, evictions, latency, and clients.

Primary references#

Share this article

Help others discover this article

Comments

Loading comments...

Loading comments...

Leave a Comment

Your email will not be published

Minimum 10 characters

0 / 5000