« Previous
Next »
Summary
A one-page Redis summary: data types, when to use which, persistence, monitoring, and the production discipline that prevents the common failure modes.
Redis in one page
EXAMPLE
# ===== Data types — pick by access pattern ===== # string GET, SET, INCR counters, cache, locks, JSON blobs # list LPUSH, RPOP, LRANGE queues, activity feed # hash HSET, HGETALL per-entity bag (session) # set SADD, SISMEMBER, SUNION unique members, tags # zset ZADD, ZRANGEBYSCORE leaderboards, schedules, time series # stream XADD, XREADGROUP durable queues with consumer groups # hll PFADD, PFCOUNT approx unique counts # geo GEOADD, GEOSEARCH proximity queries # bitmap SETBIT boolean flags per id # ===== TTL discipline ===== # EVERY user-facing key should have an explicit TTL SET key value EX 60 EXPIRE key 60 PERSIST key # remove TTL TTL key # ===== Patterns by intent ===== # CACHE (read-through) # GET key -> on miss, fill from DB, SET key val EX 300 # RATE LIMIT (fixed window) # INCR rl:ip:bucket + EXPIRE 60 -> reject when > N # IDEMPOTENCY KEY # SET idem:$key 1 NX EX 86400 -> reject duplicate # DISTRIBUTED LOCK # SET lock:res token NX PX 5000 -> release via Lua CAS # JOB QUEUE # Streams + Consumer Groups (XADD + XREADGROUP + XACK) # SESSION STORE # HSET sess:$id field val + EXPIRE $id 1209600 # LEADERBOARD # ZADD lb:weekly score user; ZREVRANGE lb:weekly 0 9 # ===== Persistence ===== # RDB snapshot at intervals (save 900 1) fast restore # AOF append every write durable but slower # Both recommended in production # ===== Replication + HA ===== # Primary + N replicas, async by default # Redis Sentinel for HA failover # Redis Cluster for shards across nodes (hash slot routing) # ===== Security ===== # - TLS for all connections # - ACLs per app (+@read +@write -flushall) # - rename-command CONFIG ''; rename-command FLUSHALL '' # - Bind to private IPs only # - require_pass strong-secret # ===== Memory hygiene ===== maxmemory 4gb maxmemory-policy allkeys-lru MEMORY USAGE key # inspect single key memory redis-cli --bigkeys # find the largest keys redis-cli --hotkeys # find the most-accessed # ===== Observability ===== INFO memory / clients / stats / replication SLOWLOG GET 10 LATENCY HISTORY event CLIENT LIST # NEVER MONITOR for long in prod (serialises traffic) # ===== Prometheus exporter ===== # docker run -d -p 9121:9121 oliver006/redis_exporter --redis.addr=redis://prod.redis # Alerts that pay rent: # - hit rate < 90% # - evicted_keys > 0 # - replication_lag_seconds > 5 # - blocked_clients > 0 # - memory used > 90% of maxmemory # ===== Decision matrix ===== # - In-memory cache Redis OR Memcached # - Durable jobs Streams (NOT pubsub) # - Pub/sub for real-time Streams or PUBSUB (fire and forget) # - Distributed lock SET NX PX + Lua release # - Leaderboard / ranking zset # - Approximate cardinality HyperLogLog # - Proximity search geo (or PostGIS / specialised) # ===== Pitfalls ===== # - 'KEYS *' to enumerate keys (O(N) blocking) -> use SCAN # - PUBSUB for durable delivery (no replay) # - Storing 1MB blobs in strings (bytes bite) # - Forgetting TTL -> memory grows unbounded # - One client for many tenants without keyspace prefixes # - Production Redis on the public internet # - No maxmemory + eviction policy -> OOM crashes # ===== Reach for these libraries ===== # Node: ioredis, BullMQ (queues), Upstash (serverless) # Python: redis-py, arq (queues), celery (heavier) # Go: github.com/redis/go-redis # Ruby: redis-rb + Sidekiq (queues)
Why it matters
Default to "every key has a TTL, every lock has a release, every queue has a consumer group + retry". Get those three habits into the team and Redis becomes the most reliable piece of your stack instead of the silent failure waiting for a Friday.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
« Previous
Next »
Discussion
Loading…