Batch at night or stream right now?
FotoFlow's stats page recomputes views per photo every minute with a big GROUP BY on the production database. By mid-morning each run scans hundreds of millions of rows, the viewers wait while the database counts, and the incident channel lights up. Production databases are tuned for many small, fast queries; huge scans steal their memory, CPU and disk.
Batch: collect now, compute later
Instead, the app appends every view as an event to cheap object storage, files, not database rows. Once a night a batch job reads the whole day, computes the counts on a cluster of workers, and writes results to an analytics warehouse. The dashboard reads the warehouse; the production database serves the app again. Batch is simple, cheap, and easy to rerun: if the job had a bug, fix it and run it over the same files.
The job is split so it scales: each worker reads its own chunk and counts views per photo (map), counts for the same photo are sent to the same worker (shuffle), and that worker adds them up (reduce). Add workers and it finishes proportionally faster. Spark and BigQuery do the same thing behind a SQL interface today.
Then a celebrity photo goes viral and takes 40% of yesterday's views. Every one of its events lands on the same reducer, which grinds for 3 hours while the other 99 workers finish in 5 minutes, and the job waits for the slowest. The fixes are standard: a combiner pre-aggregates on each worker so one partial count ships instead of millions of rows, and salting splits the hot key, 4f2#0 to 4f2#9, across ten reducers whose totals are added at the end. Three hours back to about 20 minutes.
Stream: compute as events arrive
But trending needs to surface a viral photo now, and a bot farm faking views should be caught in minutes, before it earns anything. Batch answers what happened. Some products need what is happening. So the app also publishes each view to an event stream, and a stream processor, Flink or Kafka Streams, reads events as they arrive and keeps running counts in local state, checkpointed so a crash resumes instead of restarting.
A stream never ends, so counting needs a window: tumbling windows cut fixed slices, views per minute, and sliding windows overlap, views in the last 10 minutes. A sudden jump between windows is exactly what trending, or fraud, looks like. Real streams are messy: a phone in the subway uploads its views two hours late, and flaky networks send events twice. Processors use event time, a watermark that decides when a window is complete enough to publish, and event ids to drop duplicates.
What this does not solve
The celebrity live-stream triples traffic for an hour, the processor cannot keep up, and unprocessed events pile up in the stream: consumer lag reaches 4 minutes and 18 million events. Nothing is lost, but the live dashboard is now 4 minutes old. Consumer lag is the key health metric of any streaming system. Alert on it, in seconds or events. The cure is partitions: the stream is split by photo id so each photo's events stay in order, each processor owns some partitions, and adding processors adds throughput. The partition count caps parallelism, so create more than you need today, since adding them later reshuffles keys.
Counting unique viewers is harder: exact counting means remembering every viewer id, 50 million ids, about 800 MB of state per photo per window. A HyperLogLog sketch estimates the distinct count in 12 KB with about 1% error, and sketches merge across machines and windows. Approximate algorithms trade a tiny error for enormous savings. Dashboards rarely need the last 1%. Many systems run both: a stream for fast approximate answers and a nightly batch recomputing the exact numbers, or stream-only with replay to rebuild results.
Your trending list updates once a night. What would you catch in minutes instead, and what would it cost?
One photo takes 40% of today's views. Which worker is still running at hour three?
The numbers move in seconds now. Next in the series: Someone knocks at the API door: how do you know who?
Sources
Apache Kafka
Netflix Technology Blog
System Design Primer
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.