Menu

Apache Spark interview question · Question 3 of 6

How does partition count affect Spark performance?

  • Medium
  • optimization / conceptual
  • ~7 min
  • High relevance
  • 2 min read
  • Updated Oct 2026

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.

On this page
  1. Detailed explanation
  2. Rules of thumb
  3. Common mistakes

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

  1. Treating skew as a partition-count problem.
  2. repartition(1) before writing large data (one task does all the work).
  3. Hard-coding 200 shuffle partitions for every job size.

By , Data Engineer and Founder of DataDank · Last reviewed Oct 2026 · Examples run on PySpark 4.2 in local mode; behaviour notes say where Spark 3.x differs

Progress is saved in this browser only. No account needed.

Search
Filter by type