Does Spark Structured streaming query require write-ahead logs for at least once delivery with ForeachBatch output sink?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2023 03:01 PM
Is it sufficient to use the checkpoint directory with write-ahead logs?
BTW. I use Kafka connector to read data from EventHub
- Labels:
-
Checkpoint Directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Structured Streaming already manages offset tracking and commit logs through its checkpoint directory. You do not need to configure a separate write-ahead log when reading from Azure Event Hubs through the Kafka connector.
However, foreachBatch provides at-least-once write guarantees by default, meaning a micro-batch may be executed again after a failure.
For example, if a batch successfully writes to Delta but the application fails before recording the batch completion, Spark may replay that batch after restarting.
For Delta targets, you can prevent duplicate writes using txnAppId and txnVersion:
def write_batch(batch_df, batch_id):
(
batch_df.write
.format("delta")
.mode("append")
.option("txnAppId", "eventhub-stream-01")
.option("txnVersion", batch_id)
.saveAsTable("bronze.events")
)
query = (
source_df.writeStream
.foreachBatch(write_batch)
.option("checkpointLocation", checkpoint_path)
.start()
)Keep the application ID consistent across restarts using the same checkpoint. If you create a new checkpoint, use a new application ID because batch IDs restart from zero.
My recommendation is to combine durable checkpoint storage, sufficient Event Hubs retention and idempotent destination writes.
Checkpointing handles processing recovery, while idempotent writes prevent duplicate records during batch retries.
Official references:
https://docs.databricks.com/aws/en/structured-streaming/checkpoints
https://docs.databricks.com/aws/en/structured-streaming/foreach
https://docs.databricks.com/aws/en/structured-streaming/delta-lake
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
In micro batch engine, the checkpoint directory is the write ahead log - before each batch runs, Spark records the Kafka/Event Hubs offsets it is about to consume in offsets and only after the batch fully completes does it write the confirmation marker to commits. The reason you get at least once rather than exactly once with foreachBatch is that your function runs outside Spark's transactional control - if the job crashes mid batch, the commit marker usually never lands and on restart Spark replays the same offsets from the durable, retention backed Event Hubs topic, re-running your entire function against the same data. No separate WAL is required for this - the Kafka source is replayable by design and the checkpoint's offset log is the mechanism that tracks progress - driver side write ahead log terminology belongs to the legacy file sink and not to Kafka sources.
The checkpoint directory is sufficient for at least once but to avoid duplicate rows on replay you make the batch function idempotent. The right pattern is to use the batchId handed to your function together with Delta Lake's txnAppId/txnVersion options so a replayed batch with the same ID is silently skipped by Delta. Alternatively, use a MERGE on a stable business key or dropDuplicates on the Kafka (topic, partition, offset) columns. At least once assumes the data still exists in Event Hubs, so monitor consumer lag against the retention window and always place the checkpoint on persistent storage (Unity Catalog volume) with one unique path per stream.
# Unique per stream run — Delta uses this to recognize replayed batches
txn_app_id = f"eventhub-orders-stream-{uuid.uuid4()}"
def write_batch(batch_df, batch_id):
# txnVersion = batchId: if Spark replays batch N after a crash,
# Delta sees the same (txnAppId, txnVersion) pair and skips the write
(batch_df
.write
.format("delta")
.mode("append")
.option("txnVersion", batch_id) # dedupe key = the batch ID
.option("txnAppId", txn_app_id) # ties writes to this stream
.saveAsTable("main.streaming.orders"))
(spark.readStream
.format("kafka") # Event Hubs via Kafka protocol
.option("kafka.bootstrap.servers", "<namespace>.servicebus.windows.net:9093")
.option("subscribe", "orders")
.option("kafka.sasl.mechanism", "PLAIN")
.option("kafka.security.protocol", "SASL_SSL")
.load()
.writeStream
.foreachBatch(write_batch)
# The checkpoint IS the WAL: offsets logged before, commits after
.option("checkpointLocation", "/Volumes/main/checkpoints/orders_stream")
.trigger(processingTime="30 seconds")
.start()
.awaitTermination())