Structured Streaming job becomes progressively slower
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
48m ago
What could cause a Structured Streaming job to become slower after running continuously for several days?
The pipeline performs deduplication and aggregations, and the incoming data volume remains relatively consistent.
Could this be related to the growing state store?
How can we monitor state store growth and determine whether the configured watermark duration is appropriate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
36m ago
Hi Angelina,
Yes, with deduplication in the pipeline, a growing state store is the first thing I'd check. The usual cause is calling dropDuplicates without the watermarked event-time column in the keys. The Databricks docs say that in that case the operator doesn't evict state at all, so it grows with every new key. Over time, that slows the query down and can end in out-of-memory errors. Steady input volume doesn't help because every new key still adds to the state.
Depending on how your duplicates show up, the docs give two ways to bound it:
duplicates are exact copies (e.g. retried writes): include event time in the keys
df.withWatermark("event_time", "30 minutes").dropDuplicates(["id", "event_time"])
duplicates arrive within some delay of each other (DBR 13.3 LTS and above)
df.withWatermark("event_time", "30 minutes").dropDuplicatesWithinWatermark(["id"])
If you really need every duplicate removed and the number of keys has no upper limit, the docs suggest using the target table as the source of truth instead, with MERGE in foreachBatch or AUTO CDC in a pipeline, rather than keeping all keys in state. The aggregations need a watermark too. Without one, their state also grows without limit.
To see what's going on, look at the stateOperators section of the streaming query progress (Spark UI streaming tab, lastProgress, or a StreamingQueryListener):
- numRowsTotal and memoryUsedBytes should level off after a while. If they keep climbing day after day, state isn't being evicted.
- numRowsRemoved should go above zero once the watermark starts moving. If it stays at zero, the watermark isn't cleaning anything up.
- numRowsDroppedByWatermark counts rows that arrived too late. If it keeps going up, the watermark is shorter than how late your data actually arrives.
- customMetrics.numDroppedDuplicateRows shows how many duplicates were actually removed.
You can also read the state itself from the checkpoint with the state reader (state-metadata and statestore formats) to see which operator holds the most rows. The requirements depend on the runtime and compute access mode.
For picking the watermark, it's a trade-off between latency and how much late data you tolerate. For dedup specifically, the docs say it has to be longer than the maximum time difference between duplicate events if you want to guarantee that all duplicates within that expected delay are removed. A practical way to land on a number is to start from how late your data really arrives and adjust using numRowsDroppedByWatermark and the state size.
One last thing, on DBR 17.3 LTS and above, RocksDB is already the default state store, with changelog checkpointing on. On older runtimes it's worth turning on if your state is large.
Principal Data Architect & AI Strategy — CI&T
thomazn@ciandt.com
linkedin.com/in/thomaz-antonio-rossito-neto | Databricks Solutions Architect Champion