Redis — cache, lock, rate limiter, queue done correctly
Redis is four primitives wearing one server: a cache, a distributed lock, a **rate
limiter, and a job queue**. Each has a correctness contract that has nothing to do with
whether SET returns OK. You almost certainly already "have Redis working" — a value goes
in, a value comes out. What you do not yet have is a cache that won't stampede your database, a
lock that won't release someone else's hold, a rate limiter that survives concurrent requests,
and a queue whose jobs aren't silently lost when a worker dies. This skill is engine- and
pattern-level, client-SDK-agnostic: examples are redis-cli and Lua, so they hold across
node-redis, ioredis, redis-py, go-redis, and Lettuce. Every pattern applies unchanged to Valkey
and any Redis-protocol store, since Valkey is a BSD fork of Redis 7.2 and stays protocol-compatible.
When to use / When NOT
Use when you are:
- Adding or reviewing a cache: TTL strategy, cache-aside vs write-through, key naming,
stampede/thundering-herd prevention, negative caching, invalidation.
- Building a distributed lock: "only one worker does X", cron de-duplication, leader-ish
mutual exclusion — the SET NX PX + Lua-release + fencing-token contract, and the Redlock call.
- Building a rate limiter: per-user/IP/API-key throttling (fixed window, sliding-window log,
sliding-window counter, token bucket) and why naive INCR+EXPIRE is a race.
- Running a queue / background jobs on Redis: Lists vs Streams vs a library (BullMQ, Sidekiq,
RQ, Celery), at-least-once delivery, acks, stalled-job recovery, idempotency.
- Deciding eviction & persistence:
maxmemory-policy, RDB vs AOF, cache mode vs database mode. - Reviewing Redis code for foot-guns:
KEYS *, unbounded keys, non-atomic read-modify-write,
missing TTL, a lock released by the wrong owner.
NOT for (route instead):
- Durable SQL queue (
SELECT … FOR UPDATE SKIP LOCKED), engine-level SQL caching →