Write-heavy data: batching and the right store
Handle a flood of writes by batching them and choosing a store built for it.
Writes are expensive
Each database write updates indexes and flushes to disk. A relational database comfortably takes thousands of writes a second; clickstreams and sensor data can bring hundreds of thousands.
Batch them
Writing 500 events in one call costs far less than 500 separate calls. Put events on a stream (Kafka), and let workers write them in batches.
The trade: an event may take a second or two to appear. For analytics that's fine.
worker:
batch = stream.take(up to 500, wait up to 1 s)
db.insertMany(batch) // 1 round trip instead of 500
Pick a store for appends
Stores like Cassandra are built to take writes across many nodes by spreading rows by a key. They give up joins and flexible queries in return. Use them when the data is mostly appended and read by key or time.
Key takeaways
- Batch writes to cut round trips.
- A stream in front of the store absorbs bursts.
- Use a write-optimised store when data is appended and read by key.
Try it in the simulator: Put a flood of writes on the right store
Put this concept into practice on Global Clickstream Analytics:
- Build Client → Load balancer → API servers → Kafka → Workers → PostgreSQL and run it. Read the diagnostics.
- Replace PostgreSQL with Cassandra and run again.
What to watch: Behind the queue, PostgreSQL can't save the events as fast as they arrive, so queued writes are lost and the run fails. Cassandra spreads the writes across its nodes and keeps up, and the same design passes.
Check your understanding
Answer these questions interactively to test what you learned and track progress on your skill map:
- What does batching 500 events per write mainly trade away?
- Which data fits a write-optimised store like Cassandra best?