Apache Spark interview questionsQuestion 3 of 6
Apache Spark interview question · Question 3 of 6
How does partition count affect Spark performance?
Short answer
Each partition becomes one task, so the number of partitions sets how much work runs in parallel. Too few partitions leave cores idle and make each task so big that it spills to disk or runs out of memory. Too many tiny partitions waste time on scheduling and create small output files. Aim for evenly sized partitions of roughly 100–200 MB, and at least a few times as many partitions as cores. Let Adaptive Query Execution merge small shuffle partitions for you.
Detailed explanation
- Input partitions when reading files come mostly from file sizes and
spark.sql.files.maxPartitionBytes(128 MB by default). - Shuffle partitions come from
spark.sql.shuffle.partitions(200 by default), which AQE can coalesce. - Output files: each task writing to a location usually produces at least one file, so partition count also controls the small-files problem.
Rules of thumb
| Situation | Action |
|---|---|
| Tasks spill or run out of memory | More, smaller partitions |
| Thousands of tasks of a few MB | Fewer partitions; rely on AQE coalescing; compact inputs |
| Many tiny output files | coalesce before writing, or repartition by the output partition column |
| A few tasks much slower | Skew, not count: fix the key distribution |
repartition(n) does a full shuffle and can increase or decrease the count evenly; coalesce(n) only decreases it, merging partitions without a full shuffle, but may leave them uneven.
Common mistakes
- Treating skew as a partition-count problem.
repartition(1)before writing large data (one task does all the work).- Hard-coding 200 shuffle partitions for every job size.
Progress is saved in this browser only. No account needed.

