Small File Problem
After the partition fix, the nightly job finishes in 40 minutes again. A week later the team that reads its output complains that their queries got slower, and a listing of the output folder for a single day shows 50,000 files averaging 42 KB. The interview version asks why that happens and how to fix it, and each fix has a different price.
Where the files come from
Every task writes its own files, 1 for each output partition value among the rows it holds. The job writes with partitionBy("country"), and the shuffle hashed rows on the full grouping key, profile and title as well as country, so each of the 1,000 reduce tasks holds rows from all 50 countries. The file count is the product: 1,000 tasks times 50 countries is 50,000 files for 2 GB of output. Before the fix, 200 tasks wrote 10,000 files of about 200 KB, already too small for readers; the memory fix multiplied the count by 5, and that is how the interviewer's 50,000 files arrive.
What small files cost readers
- Every file costs a listing entry, an open request and a footer read before its first row comes back.
- Readers plan work per file or per packed group of files, so 50,000 files mean many short read tasks.
- Columnar files keep min and max statistics in each footer, so tiny files hand the reader thousands of footers to check.
- Compression and encoding find fewer repeated values in 42 KB of rows than in 128 MB.
- Any catalog or metadata service that tracks individual files slows down as the count reaches millions.
Spark's file reader limits the damage by packing small files into shared read tasks. It fills each task up to spark.sql.files.maxPartitionBytes, 128 MB by default, but charges every file an extra spark.sql.files.openCostInBytes, 4 MB, to account for the cost of opening it. Each 42 KB file therefore counts as about 4 MB, and about 32 of them fill a task.
- 1,563
- 200 GB
Fix 1: control the file count at write time
Take an output with no partition column first. The 1,000 aggregate tasks would write 1,000 files of about 2 MB, and the instinct is to reduce the task count right before the write. The 2 methods for that look alike in code and behave very differently on the cluster.
- coalesce(16)
- Narrow: merges whole partitions in place, with no shuffle
- Adds no stage boundary, so the aggregate stage itself shrinks to 16 tasks
- Each of those 16 tasks then reads 40 GB of shuffle data and spills heavily
- Fits when the step before the write is a scan or filter of modest size
- repartition(16)
- Wide: adds a shuffle of the 2 GB result
- The aggregate keeps its 1,000 tasks; only the final write runs as 16
- Rows spread evenly, giving 16 files of roughly 128 MB
- Fits after a large shuffle, which is this job's case
With partitionBy("country") the file count is tasks times countries per task, so the fix is to make each task hold fewer countries. Repartitioning on country sends every row for a country to the same task, so each country folder receives 1 file: 50 files for the day, averaging 41 MB. The biggest country then lands on a single task, and the maxRecordsPerFile option caps rows per file so that task writes several files instead of a giant one.
(
daily
.repartition("country")
.write
.partitionBy("country")
.option("maxRecordsPerFile", 5000000)
.mode("overwrite")
.save("/lake/daily_watch_time/dt=2026-10-22/")
)Fix 2: compact after the fact
Write-time control fails when files arrive through many small writes. The viewing events table that feeds this job is filled by a stream that commits every minute, 1,440 commits a day, and each commit adds files. Late events add more: a play watched offline and uploaded 3 days later lands in a date partition that was finished long ago. The tidy 128 MB input files from the first section exist because a compaction job rereads each partition's small files and rewrites them as a few large ones.
- Play event stream (source, Event stream)
- Raw partitions (storage, Small files per commit); freshness: 1 minute
- Compaction (transform, Spark job, hourly); retries: 3; backfill: Recompact partitions late data touched; idempotency: Rewrite from current files, swap in 1 commit
- Compacted partitions (storage, 128 MB files)
- Nightly rollup (consumer, Spark job)
- Play event stream -> Raw partitions
- Raw partitions -> Compaction
- Compaction -> Compacted partitions
- Compacted partitions -> Nightly rollup
About this lesson
This section is part of the Distributed Compute: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.