Breaking the Redis Read Bottleneck with a Redis→SQLite Pipeline
How we restored sub-second inbox latency by shifting the read path off Redis and onto a local SQLite replica, while keeping Redis as a write buffer.
TL;DR
We moved inbox/thread reads from Redis to a local SQLite replica (LiteFS), reducing API latency from 10+ seconds to under 1 second during peak load. Redis now serves as a write buffer and coordination layer only.
The New Architecture
Write Path
API endpoints emit sync events to a Redis list. The API never blocks on SQLite writes.
Sync Path
A Celery worker drains Redis events every second and writes to SQLite. LiteFS replicates to all nodes.
Read Path
Inbox and thread reads come from the local SQLite replica. No network hops, predictable latency.
The Pain: Why Redis Became the Bottleneck
Redis wasn't just a cache anymore—it had grown into our primary read database and derived index engine. Every inbox load required:
- ✗ZSET paging across conversation indexes
- ✗Multiple HGETALL operations per conversation
- ✗Python-side filtering and joins
- ✗Network round-trips proportional to page size
Under load, this created a vicious cycle: slower Redis → higher request concurrency → even slower Redis. Tail latency spikes became contagious across the entire system.
Production Blockers We Solved
Blocker #1: Redis Cluster Blocked Pipelined RPOPLPUSH
Our Celery sync tasks crashed with RedisClusterException becauseredis-py blocks pipelined RPOPLPUSH in cluster mode.
Solution:
- • Hash-tagged keys to ensure same-slot operations
- • Lua script for server-side batch drain (one round-trip)
- • Reliable processing queue with crash recovery
Blocker #2: SQLite Foreign Key Constraint Failures
Message sync events arrived before their parent users/conversations existed in SQLite, causing FOREIGN KEY constraint failed errors.
Solution:
- • Collect referenced IDs from each batch
- • Backfill missing parents from Redis before insert
- • Apply writes in parent → child order within transaction
Additional Optimizations Shipped
Frontend Event Buffering
Status updates are now buffered and flushed every 5 seconds, reducing render frequency from ~1000/min to ~12/min during batch sends. Inbound messages bypass the buffer for instant delivery.
Server-Side Lua Filtering
Conversation filtering moved from Python loops to a Redis Lua script, eliminating thousands of network round-trips per request.
Batch & Billing Reads Migrated
Extended the pattern to batch job listings, batch items, and billing ledgers. All now read from SQLite with Redis fallback.
Cron Storm Resolution
Reconciliation tasks moved from every-minute to hourly/daily schedules, with buffered queue processing replacing synchronous scans.
Results
Why "Local Reads" Wins
This approach aligns with a broader industry trend—popularized by teams like 37signals—toward simpler, more resilient architectures:
Local beats distributed
Reading from a local SQLite file eliminates network hops, cluster routing, and noisy neighbor problems.
Redis as a buffer, not a database
Redis excels at queues and coordination. It becomes fragile when it silently grows into your primary read store.
"Less" architecture wins
LiteFS + SQLite turns distributed complexity into single-node writes + replicated reads. Conceptually smaller, operationally calmer.
Key Takeaway
We didn't "optimize Redis" into submission. We changed the shape of the system:
Redis
Buffer, queue, coordination layer
SQLite (LiteFS)
Stable, local read model
That's why the gains feel outsized: we removed an entire class of latency and contention rather than shaving milliseconds.