Sub topic 13
Limiting how much each micro-batch reads
This chapter assumes you can write a Structured Streaming query with readStream and writeStream. It also assumes you know what a checkpoint is and have run a basic cloudFiles stream. Here you will learn how to cap the amount of data each micro-batch reads, and why you would want to.
Why batch size matters
A micro-batch is one round of work that the stream runs. It reads some files, transforms them and writes the result. Without a limit, Auto Loader tries to take every file it knows about in a single batch.
That is fine for a small folder. It causes problems after an initial backfill, where a folder may hold millions of files. One huge batch can run out of memory, spill to disk, or take hours. If it fails, the whole batch is retried from the start.
Smaller batches keep memory use steady. They also commit progress to the checkpoint more often, so a failure loses less work.
The two options
Auto Loader has two rate-limit options. Both are set on the cloudFiles source.
| Option | Limits | Default |
|---|---|---|
| `cloudFiles.maxFilesPerTrigger` | Number of files per micro-batch | 1000 |
| `cloudFiles.maxBytesPerTrigger` | Approximate bytes per micro-batch | No limit |
The byte limit is a soft maximum. A batch can read slightly more than the value you set, because Auto Loader does not split a file across batches.
If you set both options, a batch stops growing when it reaches whichever limit comes first.
Setting the options
The option goes on the reader, next to the other cloudFiles options. This example uses the file notification mode from earlier in the tutorial and caps each batch at 500 files.
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.maxFilesPerTrigger", "500")
.load("s3://my-bucket/landing/events"))To limit by size instead, replace the last option with a byte value such as "10g".
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.maxBytesPerTrigger", "10g")
.load("/Volumes/main/raw/landing/orders"))How limits work with availableNow
A rate limit does not make the stream do less work overall. It only splits the work into more batches.
With trigger(availableNow=True), the stream processes all pending files and then stops. When a limit is set, it still processes everything, but as several batches in one run. The run takes the same amount of data in total, and each batch is smaller.
With a continuous trigger, the limit controls how much each batch takes when many files arrive at once. The stream then catches up over several batches.
Choosing a value
Start from the symptoms you see.
- Batches fail with out-of-memory errors: lower the limit, or use
maxBytesPerTriggerso size matters more than count. - File sizes vary a lot: prefer
maxBytesPerTrigger. A count of 1000 files might be 10 MB or 500 GB. - Many tiny files and slow batches: raise
maxFilesPerTrigger, since per-batch overhead dominates. - The stream falls behind a steady arrival rate: raise the limit or add compute. Check that the batch finishes faster than new files arrive.
Common errors
- Setting the option without the
cloudFiles.prefix. PlainmaxFilesPerTriggerbelongs to other Spark file sources, so use the prefixed name with Auto Loader. - Expecting the byte limit to be exact. It is approximate.
- Assuming a limit stops the run early under
availableNow. It does not. All pending files are still processed.
Exercise
A stream with trigger(availableNow=True) has 5,000 pending files and cloudFiles.maxFilesPerTrigger set to 1000. How many files does the run process, and in how many micro-batches?
Show answer
The run processes all 5,000 files, in about 5 micro-batches of up to 1,000 files each. The limit changes the batch size, not the total, and the stream stops only after the backlog is empty.
Exercise
Your landing folder holds files from 5 MB to 200 GB, and batches sometimes run out of memory. Which option should you use, and why?
Show answer
Use cloudFiles.maxBytesPerTrigger. A file count says nothing about how much data a batch holds when file sizes vary this much, while a byte limit bounds the batch size directly.