Skip to content
Flink · Worked example

Use case: sensor anomaly detection for a water utility

A worked example with a made-up utility: pressure and flow readings that arrive late and out of order, event-time windows per sensor, baselines kept as state, silence detected with timers, and late data reconciled.

A water network is full of sensors: pressure at junctions, flow at district meters, reservoir levels. A burst main shows up as a pressure drop and a flow spike minutes before customers call. The data to catch it exists; the hard part is that field telemetry is messy. Sensors report on different schedules, cellular links drop and devices buffer readings and send them in a burst an hour later, and some sensors simply go quiet.

This worked example shows how we design a Flink job for that kind of data. The utility, its network and its numbers are made up.

Example · Bramblewood Water, a made-up regional utility

The starting point

Bramblewood has around three thousand pressure and flow sensors. Readings arrive in Kafka through a telemetry gateway. Today, a batch job runs every hour and flags readings outside fixed thresholds. Operators see bursts an hour late, and fixed thresholds raise alarms every night when demand naturally drops.

1. Event time, because the network is unreliable

Each reading carries the time the sensor took it. The job runs in event time, so a burst of buffered readings from a sensor that was offline is placed in the right windows rather than all counted "now"; see event time and watermarks. The settings follow from measured lateness:

  • An out-of-orderness bound of five minutes, which covers the normal jitter in Bramblewood's gateway.
  • Idleness on the source, so a Kafka partition whose sensors are all quiet at night doesn't hold back the watermark for the whole job.
  • A side output for late readings: anything arriving after its window closed (a sensor that was offline for an hour) is written to the lake and folded into a daily reconciliation, instead of being dropped or reopening old alerts.

2. Windows per sensor

Readings are keyed by sensor and aggregated into five-minute tumbling windows: minimum, mean and maximum, and the number of readings. The count matters: a window with too few readings is marked low-confidence rather than compared.

3. Baselines instead of fixed thresholds

What is normal for a pressure sensor depends on where it is and when: night-time pressure is higher because demand is lower. The job keeps, as keyed state, a baseline per sensor for each hour of the week (a running mean and spread), updated from the sensor's own history. A window is anomalous when it is far outside that baseline, and an alert needs two consecutive anomalous windows, which filters single noisy readings.

Baselines are state, so treat them like data. They are part of the checkpoint and the savepoint, they survive upgrades only with stable operator ids, and they need a plan for a new sensor (start from its neighbours' baseline) and a replaced one (reset it). Decide these rules with the operations team before go-live.

4. Silence is a signal too

A sensor that stops reporting may be a flat battery, or a flooded chamber. For each sensor the job registers an event-time timer fifteen minutes after its latest reading; every new reading pushes the timer forward. If the timer fires, the sensor has been silent for fifteen minutes of event time, and a "sensor silent" alert is raised. Using event time means a replay of yesterday's data produces exactly the same silence alerts, which makes the logic testable. One caveat: event-time timers fire only as the watermark advances, so if every sensor goes quiet at once (a gateway or network outage) no silence alert fires; a separate processing-time check on the gateway or on Kafka input rate covers that case.

5. Alerts operators can act on

  • Alerts are published to a Kafka topic and picked up by the control-room system, with the sensor, its district, the window and the baseline it was compared with.
  • Correlated alerts (several sensors in one district in the same window) are grouped, so a burst produces one incident, not twenty pages.
  • Every alert carries the job version that produced it, so changes to the logic can be compared over time.

Choosing the window size

Five minutes is a trade-off, not a rule. Shorter windows detect a burst sooner but hold fewer readings each, so more windows are marked low-confidence and noise matters more. Longer windows are steadier but slower. Bramblewood chose its window from its sensors' reporting intervals and the operations team's view of how quickly they can act; a network with sensors reporting every few seconds could use shorter windows.

Testing with history

Because everything runs in event time, the job can be run over months of stored readings, and its alerts compared with incidents the operations team already knows about: bursts that were reported, sensors that were replaced. That replay is how baselines, thresholds and the two-window rule were tuned before go-live, and it is repeated whenever the logic changes.

6. Operating it

The job checkpoints every minute to object storage with RocksDB state and incremental checkpoints, so recovery after a failure resumes from recent state and replays only a minute or so of readings; see checkpoints and savepoints. It runs on Kubernetes under the Flink Kubernetes Operator, which takes a savepoint on every upgrade; see the Kubernetes guide.

Example · what changed at Bramblewood

After the move

Operators see anomalies within minutes of the readings arriving, rather than at the next hourly batch, and the nightly false alarms from fixed thresholds are gone because each sensor is compared with its own normal night. The daily reconciliation of late readings showed which districts had the worst connectivity, which became a separate maintenance project.

All names and details are fictional.

How to decide if this design fits

  1. Do your sensors' normal values depend on time of day or season? Then baselines beat fixed thresholds.
  2. Do devices buffer and resend after an outage? Then event time and a plan for late data are required, not optional.
  3. Is a silent sensor a problem in itself? Then timers belong in the design from the start.

Questions we are often asked

Why not a machine learning model?

A per-sensor statistical baseline is simple to explain to operators and to test against history. A model can be added later, for example scoring the windows the job already produces, once the simple version shows where it falls short.

What happens when the job is upgraded?

It is upgraded from a savepoint, so baselines, open windows and silence timers carry over. Without stable operator ids, every baseline would restart from nothing.

Building or rescuing a Flink job?

The Flink POD designs, builds and operates Apache Flink pipelines: event time, state, checkpoints, Kubernetes and the tuning that keeps them fast, inside your environment.

Flink POD

Keep reading

Back to blog