Backend6 min read

Redis

In-memory data store for caching, sessions, rate limiting, queues, and pub/sub - millisecond response times with persistence options.

RedisCachingPub/SubRate LimitingSession Store

skill: Redis category: Backend tags: [Redis, Caching, Pub/Sub, Rate Limiting, Session Store] readingTime: 6

Redis is the standard in-memory layer for any production web application - caching DB queries, storing sessions, and rate limiting. Add it early; retrofitting is more expensive. Use Redis 7+ for ACLs and multi-value commands. For queues at scale, use BullMQ on top of Redis.

Workflow

  1. Connect to Redis with ioredis (Node.js)ioredis supports cluster mode, sentinel, pipelining, and Lua scripts - more production-ready than the basic redis npm package.
  2. Cache DB queries with TTLCaching expensive DB queries reduces latency from 100ms to < 1ms for cache hits and reduces database load.
  3. Implement rate limiting with RedisRedis atomic INCR + EXPIRE implements sliding window rate limiting that works across multiple server instances.
  4. Store sessions with RedisServer-side sessions in Redis scale across multiple servers - unlike in-memory sessions which disappear on server restart.
  5. Build job queues with BullMQBullMQ provides reliable job queues on Redis - retries, priorities, delayed jobs, and concurrency control for background work.

Output Format

Redis integration: caching with TTL, rate limiting, session store, and BullMQ job queue.

redis:
  caching:    key pattern user:{id}, TTL 300s
  rate limit: ratelimit:{ip}:{window}, INCR+EXPIRE
  sessions:   connect-redis store
  queues:     BullMQ with retry + backoff
  monitoring: redis-cli monitor, INFO stats

Examples

Input

Output

Edge Cases & Gotchas

  • Cache stampede - When a cached item expires, many requests hit the DB simultaneously. Use probabilistic early expiration or a mutex (Redis SETNX) to let only one request refresh the cache.
  • Memory limits - Redis stores everything in memory. Set maxmemory and a maxmemory-policy (allkeys-lru) in redis.conf so it evicts old keys instead of crashing when memory is full.
  • Persistence vs. speed - RDB snapshots and AOF logging add overhead. For pure cache use, disable persistence. For queues and sessions, enable AOF with appendfsync everysec.
  • Key naming collisions - Without namespacing, keys from different services collide. Use consistent prefixes: service:entity:id (e.g., api:user:123).

When to Break These Rules

  • Use Memcached instead of Redis when you only need simple caching with no persistence, pub/sub, or data structures - Memcached is slightly faster for that specific use case.
  • Use a dedicated queue service (SQS, RabbitMQ) when queue durability, dead-letter handling, and cross-service delivery guarantees matter more than simplicity.

Reference Files

  • https://redis.io/docs/latest/
  • https://docs.bullmq.io/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.