Scaling Redis for Millions of Requests
An in-depth look at how we optimized our caching layer to handle massive traffic spikes without breaking a sweat. Includes Lua scripting strategies.
Redis is more than a simple key-value cache — it is a data structure store that, when used correctly, can absorb millions of read and write operations per second. After hitting a wall with our PostgreSQL read replicas, we made the decision to move our session layer entirely onto Redis. This is a post-mortem on what worked, what burned us, and what we wish we had known.
The Caching Problem at Scale
At roughly 800k daily active users, our API latency P99 jumped from 40ms to 340ms during peak hours. The bottleneck was never the application logic — it was the database. Our session lookups alone were generating ~12,000 queries/sec against a single Postgres primary.
The Silent Killer: Connection Pool Exhaustion
Before adding Redis, we discovered our connection pool was saturating at ~500 concurrent connections. Increasing pool size only made things worse — Postgres degrades non-linearly past its optimal connection count. Redis fixed the root cause, not the symptom.
We introduced Redis as a read-through cache layer for all session tokens and user preference objects. The cache hit rate reached 94% within 24 hours, dropping database load by 91%.
Lua Scripting Strategies
The single biggest win was using Lua scripts for atomic conditional updates. Rather than doing a GET → validate → SET across multiple round trips (prone to race conditions), we embedded the logic in a server-side script that runs atomically in a single call.
-- Atomically extend TTL only if session is valid
local session = redis.call('GET', KEYS[1])
if session == false then
return nil
end
local data = cjson.decode(session)
if data.version ~= tonumber(ARGV[1]) then
return redis.error_reply('VERSION_MISMATCH')
end
data.last_seen = ARGV[2]
redis.call('SET', KEYS[1], cjson.encode(data), 'EX', 3600)
return 'OK'This script eliminated an entire class of race conditions that had been causing silent session invalidations. Lua scripts in Redis are guaranteed to run atomically — no other command can interleave during execution.
Cluster vs Sentinel
For our scale, we evaluated both Redis Sentinel (HA for a single master) and Redis Cluster (sharded, horizontally scalable). Our data access patterns were heavily skewed — about 80% of requests hit the "hot" session namespace — which made naive sharding risky.
- Sentinel — simpler ops, automatic failover, single master bottleneck. Good for <500k req/s.
- Cluster — shards data automatically across nodes, removes hotspot risk, more complex client code.
- We chose Cluster with a custom hash tag strategy to co-locate related keys on the same shard slot.
Benchmarks & Results
After full migration to Redis Cluster with Lua-scripted session management, our numbers transformed significantly. P50 API latency dropped from 85ms to 6ms. P99 dropped from 340ms to 28ms. Database CPU during peak hours dropped from 94% to 12%. Our infrastructure cost actually decreased by 30% — fewer read replicas needed, smaller instance sizes.
Key Takeaway
Redis is not a drop-in cache layer you bolt on after the fact. Design your data model around Redis from the start — decide what lives in Redis, what requires consistency guarantees, and how you will handle eviction before you write a single line of application code.