Sub topic 10
Directory listing versus file notification discovery
This chapter assumes you can already write a basic cloudFiles stream and know what a checkpoint is. It also assumes you are comfortable with cloud object storage paths such as s3:// and /Volumes/.... Here you will learn how Auto Loader finds new files and when to change the default way it does so.
Two ways to find new files
Before Auto Loader can read a file, it has to learn that the file exists. This step is called file discovery. Auto Loader has two discovery modes:
- Directory listing is the default. On each micro-batch, Auto Loader lists the input path through the storage API and compares the result with the files it has already recorded in the checkpoint.
- File notification asks the cloud provider to publish an event whenever a file lands in the input path. Auto Loader reads those events from a queue and skips most of the listing.
Both modes feed the same stream. They differ in how Auto Loader learns about files, not in how it parses them, applies the schema, or writes to the target. Both record processed files in the checkpoint, which is why the exactly-once behavior from the checkpoint chapter holds in either mode.
How directory listing works
Directory listing needs no setup beyond read access to the path. That is why it is the default. It works with Unity Catalog volumes, external locations, and plain storage paths, and you can start a stream the moment the path exists.
Each time a micro-batch starts, Auto Loader lists the input directory, including subdirectories. Listing is a series of paged API calls, and each call returns a limited number of objects. Auto Loader then checks the returned files against the file state stored in the checkpoint. Files that are not in that state become the next batch.
This design has three consequences:
- Cost grows with the size of the input path. A directory with millions of objects takes many calls per listing, and it takes more calls every day as files accumulate.
- Listing frequency is tied to your trigger. A continuous stream lists over and over. An
availableNowjob lists once per run, so a nightly job pays for one listing per night. - Listing latency sets a floor on freshness. A file that lands just after a listing is picked up on the next one.
For many pipelines this is fine. A job that runs once an hour over a few thousand new files a day rarely needs anything more.
How file notification works
In file notification mode, Auto Loader sets up cloud services that watch the input path. When a file arrives, the storage service emits an event. The event goes to a queue, and Auto Loader reads the queue to learn which files are new.
The services depend on the cloud:
| Cloud | Event source | Queue |
|---|---|---|
| AWS | S3 event notifications through SNS | SQS |
| Azure | Event Grid subscription | Azure Queue Storage |
| Google Cloud | Cloud Storage notifications | Pub/Sub subscription |
You turn the mode on with one option:
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.useNotifications", "true")
.option("cloudFiles.backfillInterval", "1 day")
.load("s3://my-bucket/landing/events"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/events")
.trigger(availableNow=True)
.toTable("main.bronze.events"))When the stream starts with cloudFiles.useNotifications set to true, Auto Loader creates the notification and queue resources for you. The compute needs permission to create and read them. If it lacks those permissions, the stream fails at startup instead of falling back to listing, so check this early.
The cloudFiles.backfillInterval option in the example is worth understanding. Event delivery is reliable in practice but is not a hard guarantee. A dropped or delayed event could leave a file unseen. Setting a backfill interval tells Auto Loader to run a full listing at that interval, which catches any file the events missed. Databricks recommends setting it when you need completeness guarantees. The cost is one full listing per interval, which is far less than a listing per batch.
Comparing the two modes
| Aspect | Directory listing | File notification |
|---|---|---|
| Setup | None | Cloud event resources and permissions |
| Cost driver | Listing calls, which grow with directory size | Queue and event service, which grow with file count |
| Latency | Depends on trigger and listing time | Usually low, since files are announced |
| Completeness | Every listing sees every file | Needs a periodic backfill to be safe |
| Large directories | Slows as the path grows | Not affected by directory size |
| Failure mode | Slow or expensive listings | Missing permissions, queue limits, lost events |
Directory listing is the safer default because there is little to configure and little to break. File notification is the better fit when listing has become the bottleneck, which usually means very large or deeply nested directories, or a continuous stream that needs low latency.
Choosing a mode
Work through these questions in order:
- Is the input path small or moderate, and does the job run on a schedule? Stay with directory listing.
- Do batch start-up times grow as the directory fills, or do listing costs show up on your bill? Consider file notification.
- Does the stream run continuously and need new files quickly? File notification fits better.
- Can your compute create and manage queue and event resources in the cloud account? If not, notification mode is not an option until that is resolved.
- Does your platform offer a managed file events option for your external location? Check the current Databricks documentation. It can give notification-style discovery without Auto Loader managing the queue resources itself.
Common errors and edge cases
- Permission errors at startup. In notification mode, the identity running the stream must be allowed to create topics, queues, and subscriptions, or the equivalents in your cloud. Ask your cloud admin to set this up ahead of time if you cannot create them yourself.
- Leftover resources. The queue and subscription Auto Loader creates exist outside Databricks. If you delete a stream and its checkpoint without cleaning up, they remain and may keep costing money or count against provider limits. Databricks provides a resource manager utility for listing and removing them. Check the documentation for its current name and usage before relying on it.
- Provider limits. Cloud providers cap how many notification configurations one bucket or container can have. Many streams reading the same bucket under different prefixes can hit that cap.
- Missed files with no backfill. If you rely only on events and lose some, those files will not be picked up. A periodic backfill is the remedy.
- Moving or recreating the path. Notification resources are tied to the input location. If the location changes, the old notification setup does not follow it.
- Cost surprises in listing mode. A continuous stream over a huge directory can spend more on listing than on compute. The same job as an
availableNowrun on a schedule can bring that down sharply.
Choosing by measurement
Do not choose a mode from instinct alone. Look at how long each micro-batch spends before it starts reading data, and at the storage API cost for the input bucket. If the stream metrics show long discovery phases, switching modes is a reasonable experiment. Because you can reuse the checkpoint, the experiment is cheap to run and cheap to reverse.
Exercise
A nightly availableNow job ingests about 2,000 new files from a landing folder that holds 40 million older files. Each run spends most of its time before reading any data. Which discovery mode would you try, and what option keeps the result complete if an event is lost?
Show answer
Try file notification mode by setting cloudFiles.useNotifications to true, because the full listing of 40 million objects is the likely cause of the slow start. Set cloudFiles.backfillInterval, for example to 1 day, so a periodic full listing catches any file whose event was dropped. Restart with the same checkpoint so files already processed are not read again.
Exercise
Your stream fails at startup right after you turn on cloudFiles.useNotifications, with an authorization error. What is the most likely cause, and what are two ways forward?
Show answer
The identity running the stream is not allowed to create the event and queue resources in the cloud account. Either grant it the required permissions, or have an administrator create the resources. If neither is possible, set cloudFiles.useNotifications back to false and keep using directory listing with the same checkpoint.