Every data pipeline answers one question first: how fresh does the result have to be? The answer decides almost everything else about its design and cost.
Batch processing
A batch job collects data over a period, such as an hour or a day, and processes it all at once. It starts, finishes and leaves a result you can inspect. If it fails, you fix the problem and run it again.
Batch is simple to reason about, easy to test with a fixed input and cheap, because resources are used only while the job runs. Most reports, billing runs and daily dashboards are batch work and are perfectly happy that way.
Stream processing
A streaming system handles each event, or a tiny group of events, shortly after it happens. It never really finishes: it runs continuously and keeps state, for example a running count per user over the last five minutes.
Streaming is the right tool for fraud checks, live monitoring, alerts and features that must react in seconds. It brings real complexity: events arrive late or out of order, the same event can be delivered twice, and the system must recover its state after a crash.
# The idea of a tumbling window: count events per minute.
from collections import Counter
def per_minute(events):
counts = Counter()
for timestamp, _payload in events:
counts[timestamp // 60] += 1 # timestamp in seconds
return counts
print(per_minute([(0, "a"), (30, "b"), (61, "c")]))Micro-batch: the middle road
Many systems run a batch job every minute or every few seconds. This gives near real time results with the simplicity of batch semantics. For many "real time" requests, a five-minute micro-batch is enough and far cheaper to operate.
How to decide
- Ask who acts on the data and how quickly. If nobody can act within an hour, hourly batch is enough.
- Estimate the cost of being late. A late dashboard is an inconvenience, a late fraud decision is a loss.
- Count the people who can operate it. A streaming platform needs on-call attention that a nightly job does not.
Takeaway
Start with batch, measure whether the delay hurts, and move only the parts that need it to streaming. Real time is a feature with a running cost, not a default.
Comments