Streaming: fresh, always on, and stateful
When a day is too old, the next tier keeps values fresh as events arrive.
A 5-minute count is read at 20:40, but the stream is 40 minutes behind. What does the feature count?
A stream processor is a job that never stops. It reads events as they happen: plays, impressions, searches. For each key it keeps running counts over time windows, and it writes each update to the online store.
Uber's version publishes metrics to Kafka, an event log, and runs Samza streaming jobs over them. The results are written to Cassandra, a key-value database, for serving. They're also logged back to HDFS, a distributed file system, for future training jobs.
The tool names don't matter. The last step does, and it's the one designs forget: the stream's output has to land in a log for training.
The cost of a stream is state and uptime. Take impressions of this title to this member in the last 24 hours. It lets the ranker stop pushing a title the member keeps scrolling past.
Assume 100 million members see 200 titles each in a day. That's 20 billion counters. At 16 bytes per counter, the stream holds 320 GB of state, the counts it carries from one event to the next. It checkpoints that state all the time, and restores it after every restart.
The processor runs every second of every day. It still needs a separate batch path to make training data for the months before it existed. A backfill is that path: it computes a feature's past values from history. Uber built one. To avoid a cold start, its tool backfills the data by running a batch job over historical logs.
During a traffic spike, or after a restart, the stream falls behind. Its feature still claims the last 5 minutes. But a stream 40 minutes behind counts a window that ended 40 minutes ago, and no error says so.
In the editor, write state_gb(members, titles_each, bytes_each) for the impressions counter's state, and counted(read_at, window, lag) for the window a lagging stream really counts. Times are minutes from midnight, so 20:40 is 20 * 60 + 40.
- 1
Write
state_gb(members, titles_each, bytes_each): the state in GB of 10**9 bytes, one counter per member and title. - 2
Write
counted(read_at, window, lag): the (start, end) minutes a count of the lastwindowminutes really covers when it's read at minuteread_atand the stream runslagminutes behind.
Run your code to check it. Output shows here.