Prompt
I'm building a device telemetry pipeline and need out-of-order event support
Latest observation
To support out-of-order device telemetry, design the pipeline so it is event-time aware, idempotent, and can handle late arrivals gracefully.
Core principles
1) Use event time, not arrival time
Each telemetry event should include:
device_idevent_timefrom the deviceingest_timewhen your system receives itsequence_numberor monotonic counter if available- payload fields
Use event_time for analytics, state, and ordering logic. Arrival order should not be assumed.
2) Accept late and duplicate events
Devices may:
- buffer while offline
- retry after network failures
- send events out of order
- duplicate events on reconnect
So the pipeline should be idempotent:
- deduplicate by
(device_id, sequence_number)if possible - otherwise deduplicate by a stable
event_id - if neither exists, use a best-effort fingerprint of key fields + event_time
3) Window with allowed lateness
If you’re doing aggregations:
- define a watermark based on how late events can arrive
- keep windows open for a grace period
- update aggregates when late events arrive within that period
- route very-late events to a side output / dead-letter stream / correction path
Example:
- watermark = 5 minutes behind max seen event time
- allowed lateness = 15 minutes
- after that, events are considered too late and handled separately
4) Partition by device
For ordering-sensitive processing:
- partition events by
device_id - within each partition, maintain a small reorder buffer keyed by
event_timeorsequence_number - emit in order when possible
- fall back to watermark-based release to avoid unbounded buffering
5) Store raw and derived data separately
Keep:
- raw immutable event log for replay/auditing
- processed stream/table for queries and dashboards
This lets you reprocess if the lateness policy changes.
Recommended pipeline pattern
-
Ingest
- Kafka/Kinesis/PubSub topic accepts raw telemetry
- no strict ordering guarantees assumed
-
Validate and enrich
- check schema
- add ingest metadata
- assign partition key =
device_id
-
Deduplicate
- use event ID / sequence number
- drop exact duplicates
-
Reorder or window
- buffer events briefly per device
- process by event time
- apply watermark/allowed lateness
-
Aggregate and persist
- write corrected aggregates to a serving store
- preserve raw data in object storage/data lake
-
Late-event handling
- update prior aggregates if still within lateness window
- otherwise send to correction stream or audit queue
Data model suggestions
If you control the device protocol, include:
event_id: UUID generated by device or gatewaysequence_number: strictly increasing per device sessionevent_time: UTC timestampdevice_boot_idorsession_id: helps reset sequence handling after reboot
A strong combination is:
device_id + session_id + sequence_number
This handles reboot/reset cases better than timestamp alone.
Important edge cases
- Clock skew: device clocks may be wrong; don’t rely solely on event_time without bounds
- Clock drift: compare device time to ingest time to detect anomalies
- Retries: ensure writes are idempotent downstream
- Reboots: sequence numbers may reset; use session IDs
- Very late events: define a clear policy for corrections vs rejection
Technology fit
This is commonly implemented with:
- Kafka + Flink
- Kafka Streams
- Spark Structured Streaming
- Kinesis Data Analytics
- Beam/Dataflow
These frameworks support event-time processing, watermarks, and late data handling.
Practical rule of thumb
For telemetry, a robust default is:
- partition by
device_id - use
event_time - dedupe on
device_id + sequence_number - allow lateness of 5–30 minutes depending on connectivity
- send anything later to a correction path
If you want, I can sketch:
- a reference architecture,
- a Kafka/Flink implementation pattern, or
- a schema for telemetry events with out-of-order support.