Atomic Writes
Concepts covered: Distributed Primitives
Writing a 1,000-partition DataFrame runs 1,000 tasks, and each task writes its own Parquet file. Those tasks finish minutes apart and some of them fail and run again. The job itself can die at any moment. If every task wrote straight into the destination, a reader polling it would see 400 files at 02:01 and 700 at 02:03. A crash would leave a table that is 70% complete and looks finished, and a retried task could leave 2 copies of the same part file. Spark prevents all 3 problems with a commit protocol: tasks write where no reader looks, and the driver publishes the result at the end.
Write first, publish later
For file sources, Spark writes through a committer, FileOutputCommitter, using its algorithm version 1 by default. Task output lives under a hidden _temporary directory inside the destination until the job finishes. When Spark lists a table it skips every path whose name starts with _ or ., so nothing under _temporary is ever read as data.
| Step | Runs on | What happens | Readers see it |
|---|---|---|---|
| Task write | Executor | The attempt writes `part-00042-*.parquet` under `_temporary/0/_temporary/attempt_*/` | No |
| Task commit | Executor, once the driver agrees | The attempt directory is renamed to `_temporary/0/task_*/` | No |
| Job commit | Driver, after the last task | Each committed file is renamed into the destination, then `_temporary` is deleted | File by file |
| Marker | Driver | An empty `_SUCCESS` file is written in the destination | Yes: the output is complete |
- Task attempts (transform, executors); parallelism: 1000 partitions
- Attempt dirs (storage, _temporary/0/_temporary)
- Commit check (quality gate, driver: OutputCommitCoordinator)
- Committed task dirs (storage, _temporary/0/task_*)
- Job commit (transform, driver: renames, then _SUCCESS)
- Destination (storage, part files + _SUCCESS)
- Downstream job (consumer, waits for _SUCCESS)
- Task attempts -> Attempt dirs
- Attempt dirs -> Commit check
- Commit check -> Committed task dirs
- Committed task dirs -> Job commit
- Job commit -> Destination
- Destination -> Downstream job
One winner per task
A task can run more than once. An attempt that throws partway is retried on another executor, and an executor that stops answering heartbeats is presumed dead even though it may still be writing. So 2 attempts of task 42 can each finish a complete file. Before renaming its directory, an attempt asks the driver for permission. The driver's OutputCommitCoordinator says yes to the first attempt that asks for each partition and no to every later one. A refused attempt aborts, and anything it left behind goes when job commit deletes _temporary. Exactly 1 part-00042 reaches the destination.
What a crash leaves behind
| The job dies during | The destination holds | A reader waiting for _SUCCESS |
|---|---|---|
| Task writes | `_temporary` only | Sees nothing |
| Task commits | `_temporary`, with some task directories committed | Sees nothing |
| Job commit | Some part files and no `_SUCCESS` | Keeps waiting and never reads the partial files |
| Nothing: it finished | Every part file and `_SUCCESS` | Reads the complete output |
Job commit is a loop on the driver, 1 rename per file. Each rename is atomic and the loop is not, which is the third row. The all-or-nothing view a downstream job gets comes from the marker: Spark writes _SUCCESS after the last rename, so a consumer that waits for it reads every file or none. A consumer that only checks whether the directory exists can read part of a job.
Algorithm version 2
- Version 1, the default
- Task commit renames into _temporary/0/task_*
- Job commit renames every file on the driver
- Nothing appears in the destination before job commit
- Job commit slows as the file count grows
- Version 2
- Task commit renames straight into the destination
- Job commit does little beyond writing _SUCCESS
- Finished tasks' files are visible mid-job and after a crash
- Spark's configuration docs warn it may cause a correctness issue
The setting is spark.hadoop.mapreduce.fileoutputcommitter.algorithm.version. Version 2 removes the driver's rename loop, which shortens commits for jobs with many files, and gives up the property this section is about: a reader can see half a job's output with no marker to warn it. Keep version 1 for any table someone reads while you write it.
A job writing 1,000 part files with the default committer dies after the driver has renamed 600 of them into the destination. A downstream job checks the destination every minute. What does it find, and what should it do?
- It finds all 1,000 files, because job commit publishes them in 1 atomic step
- It finds no files, because Spark deletes the destination when a job fails
- It finds 600 part files and no _SUCCESS, so it should keep waiting
- It finds 600 part files and a _SUCCESS, so it can read what is there
About this lesson
This section is part of the Fault Tolerance: Intermediate 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.