The 30 second answer
I put a stream between the web tier and Postgres. Every web process appends the view to a Redis stream, which is a fast in-memory write. A separate consumer, the janitor, reads in batches through a consumer group, writes each batch in one transaction, and only then acknowledges it. Unacknowledged entries stay pending and can be retried. That roughly tripled throughput, until a single Redis instance hit 100% CPU.
The design in the video.
- Producers: all web processes append each view to a Redis stream instead of inserting into Postgres. The speaker mentions Kafka and RabbitMQ as alternatives.
- Consumer: a separate "janitor" process (it could be any language, on any machine) reads through a consumer group in batches, writes each batch to Postgres in one transaction, and acknowledges the entries.
- Crash safety: an entry a consumer has read is marked pending. Other consumers in the group don't take it. Once the batch is committed, the consumer acknowledges it, and acknowledged entries are trimmed later to free memory. If the consumer dies before committing, the pending entries can be claimed again after a timeout.
- Batch size: 5,000 views per batch, as stated in the video.
In Redis terms that is XADD on the producer side, XREADGROUP and XACK on the consumer side, the Pending Entries List for in-flight entries, and XAUTOCLAIM (or XCLAIM) to take over entries that sat idle too long.
The result. Throughput went from about 50,000 to about 180,000 requests per second, roughly three times. The janitor kept writing after the test stopped, draining what had queued up.
The next bottleneck. One Redis instance sat at 100% CPU. Redis serves commands on a mostly single-threaded design, so one instance can't use more cores. The fix was Redis cluster mode with several instances on the machine, which meant sharding the stream manually across instances in the app. The database at this point did about 500 writes/s and 100 MB/s, far below its limits, even though its CPU was busy.
The bug he admits to. When he killed the janitor mid-batch, its pending entries were discarded on restart instead of re-read. The correct behaviour is to reclaim and write them, while handling the case where another consumer already took them. Writes must therefore be safe to repeat.
Alternate interview scenarios
"Why not Kafka?"
Kafka is a fine choice and gives you a replicated, disk-backed log. Here Redis was already in the stack and the stream lives in RAM on the same server, which is fast but only as durable as Redis persistence and replication make it. Say that tradeoff explicitly.
"A consumer crashed after reading a batch but before writing it. What happens?"
The entries stay in that consumer's pending list. Another consumer claims entries idle longer than a threshold and writes them. Because a batch can then be written twice, the insert should be idempotent or deduplicated by an event ID.