Procesamiento de Flujos
Stream processing · Consultas continuas
Escrito por Darjus Kazakevic · revisado el
A job that never ends, updating its results as each event arrives.
Stream processing runs a job that never finishes. It reads an unbounded sequence of events, such as clicks, payments or sensor readings, usually from a durable log like Kafka or Kinesis. It updates its results as each event arrives. Apache Flink, Kafka Streams and Spark Structured Streaming are common engines. The output is a running count, an alert, an enriched event, or a table kept current.
The hard parts are time and failure. Events arrive late and out of order, so the job must decide when a window of time is complete. And the job runs for months, so it must save its state and resume after a crash without losing or double-counting events.
En esencia — A stream job keeps state per key and updates it with every event; event time, watermarks and checkpoints decide when a result is final and how it survives a crash.
Cuándo recurrir a él
Recurre a él cuando
- Results must be seconds or minutes old: fraud checks, alerts, live dashboards
- Events already flow through a durable log that you can read and replay
- You need state per key over time: sessions, running totals, limits per user
Reconsidéralo cuando
- A daily answer is fine: a batch job is simpler to build, test and rerun
- Each event needs one action and no state: a plain queue consumer is enough
- No one can run a stateful job around the clock, with checkpoints and lag alerts
How it works
- Event time vs processing time
- Event time is when the thing happened, stamped by the device or service that produced the event. Processing time is when your job sees it. Network delays, retries and offline devices put a gap between the two.
- Windows
- A window cuts the endless stream into finite groups you can count or sum. The window is chosen by event time and, usually, per key.
- Watermarks and late data
- A watermark is the job's claim that no more events older than time T are expected. When the watermark passes the end of a window, the window fires its result. An event that arrives after that is late.
- Keyed state and checkpoints
- Events are partitioned by a key, such as user id, so every event for one key reaches the same worker and its local state. The engine takes regular snapshots of all state together with the input offsets. After a crash it restores the last snapshot and rereads the log from those offsets.
- Delivery guarantees (At-least-once · Exactly-once)
- At-most-once can lose events. At-least-once never loses one but replays some after a crash. Exactly-once in an engine means its state ends up as if each event were applied once. Writes to outside systems are only exactly-once if the sink is idempotent or commits with the checkpoint.
- Backpressure
- When a sink or a later step is slower than the input, the engine slows the readers instead of buffering without limit. Because the input is a durable log, the backlog waits there safely and shows up as consumer lag.
Reglas de diseño
- Stamp events at the source and window by event time
- Put a timestamp in every event when it is created. Windows built on arrival time give a different answer on every replay, and an outage moves its traffic into the minute the job came back.
- Decide what happens to late events
- Set the allowed lateness from the skew you measure, not a guess. Send anything later to a side output and count it. Late events that are dropped silently look like a dip in the chart.
- Make every sink idempotent
- Recovery replays events, so outputs are written more than once. Upsert by a key derived from the data, such as (page, window start), or use a sink that commits with the checkpoint. Keep calls with side effects, like payments, behind an idempotency key.
- Alert on lag and checkpoint time, not only on uptime
- A job can be running and an hour behind. Watch consumer lag on the input and how long checkpoints take. Both rise before the results go stale.
Errores habituales
- Windowing by processing time, so a one-hour outage moves an hour of sales into one minute.
- Trusting "exactly-once" and calling a payment API from inside the job, which charges twice after a restart.
- One input partition with no traffic that holds the watermark back, so no window ever fires.
- Session or join state that never expires, so checkpoints grow until they take minutes and the job falls behind.
- One hot key, such as a bot, that sends most events to one worker while the others sit idle.