Sub topic 15
Operating Auto Loader in production
This chapter assumes you can already build a cloudFiles stream, you know what a checkpoint and a schema location are, and you have chosen a discovery mode and a schema evolution mode. It covers what to watch once the stream runs on a schedule, and how to restart it safely.
What to monitor
An Auto Loader stream can fail loudly or stall quietly. Watch for both.
- Batch progress. Each micro-batch reports
numInputRowsand the input rate. A stream that runs but reads zero rows for days may point at a wrong path or a broken notification setup. - Backlog. Auto Loader reports how many files and bytes are waiting. A growing backlog means ingestion is slower than arrival, so you need a larger cluster or a higher rate limit.
- Rescued data. Count rows where
_rescued_datais not null. A rising count means the source changed in a way your schema did not absorb. - Job failures. Alert on failed runs of the job that hosts the stream, not only on the table's freshness.
You can read batch metrics from the query handle in a notebook:
query = (df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))
query.awaitTermination()
print(query.lastProgress)For availableNow jobs, lastProgress describes the final micro-batch, so it is a good place to log row counts at the end of each run.
Schema changes cause the most restarts
With addNewColumns, the stream stops when it meets a new column. It records the new schema in the schema location. Restarting the stream picks the change up. So the job needs a retry, or it will stay failed after the first run that sees new data.
The diagram below shows the cycle.
Configure the job to retry at least once. If you would rather never stop, use rescue mode and review _rescued_data on a schedule.
Restarting safely
The checkpoint is the stream's memory, so treat it carefully.
- Keep one checkpoint per stream. Never share a checkpoint path between two streams.
- Do not delete a checkpoint to fix an error. The stream will treat every existing file as new and load it again, which creates duplicates in the target table.
- Keep the schema location with the checkpoint. Back them up or manage them together, because they describe the same history.
- Change the code, not the state. You can change the cluster, the trigger, and rate limits and restart on the same checkpoint. Changing the source path or format usually calls for a new checkpoint and a deliberate backfill plan.
Tuning for cost and speed
| Symptom | Likely cause | Action |
|---|---|---|
| Long listing time before each batch | Directory listing over a large folder | Move to file notification mode |
| Memory pressure or very long batches | Batch too large | Lower `maxFilesPerTrigger` or set `maxBytesPerTrigger` |
| Backlog grows | Too little compute | Add workers or raise the rate limit |
| Cluster idle between arrivals | Continuous trigger on sparse data | Use `availableNow` on a schedule |
Housekeeping
- Use Unity Catalog volumes or external locations so access to landing, schema, and checkpoint paths is governed in one place.
- Give landing folders a retention policy. Auto Loader does not delete source files, so storage grows until you remove them.
- Keep schema and checkpoint folders outside the landing path so they are never read as data.
- Test changes on a copy of the stream with its own checkpoint before you change the production one.
Exercise
An availableNow job fails the first time it sees a new column under addNewColumns, then succeeds when run again. Is this a bug, and what should you change in the job?
Show answer
It is expected. The stream stops after saving the new schema to the schema location, and the next start uses it. Add an automatic retry to the job, or switch to rescue mode if the job must never stop on schema changes.
Exercise
A teammate wants to delete the checkpoint folder to clear a stuck stream. What will happen to files that were already loaded?
Show answer
The stream will treat them as new and ingest them again, producing duplicate rows unless the target table is cleared or deduplicated.