Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 72/ 160
04. Big Data Processing & Apache Spark

Partitioning (repartition vs coalesce) & Bucketing Strategies

Control parallelism and file layout with repartition/coalesce and table bucketing for joins.

Advancedrepartition · coalesce · Bucketing3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Bytes, count, and duration skew. Next, full shuffle or local merge. Finally, bound small files and preserve table metadata.

Think of it like…

Repartitioning empties every sorting bin and routes it again, coalescing stacks adjacent bins into fewer boxes, and bucketing permanently labels compatible storage shelves for repeated joins.

Where you’ve seen it

df.repartition('customer_id') creates a shuffle and may skew on one giant tenant; df.coalesce(20).write.parquet(...) reduces 2,000 balanced task files to about 20; bucketing CLUSTERED BY(customer_id) INTO 128 BUCKETS supports compatible joins.

Words you’ll hear

repartition
A shuffle operation that creates a requested number of partitions, optionally by hash of specified keys.
coalesce
A narrow partition operator that merges neighboring partitions and cannot increase count or redistribute skew.
Bucketing
A persistent table layout based on hashing specified keys into a fixed number of buckets.
Operational boundary
coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers. Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

Visual diagram

Loading interactive diagram

Theory & deep dive

repartition: the working mechanism

repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys.

Bytes, count, and duration skew. Full shuffle or local merge, then repartition, bucket, or sort within partition; this exposes the state governed by Bucketing.

Partitioning & Bucketing Strategies in one concrete design

df.repartition('customer_id') creates a shuffle and may skew on one giant tenant; df.coalesce(20).write.parquet(...) reduces 2,000 balanced task files to about 20; bucketing CLUSTERED BY(customer_id) INTO 128 BUCKETS supports compatible joins.

Key takeaway: repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys

Partitioning & Bucketing Strategies: failure mode and production response

coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.

Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata. Track Target bytes as "often 128–512 MB per output partition after compression", and make Bucket count visible to the dataset owner.

Key takeaway: Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

How it works, step by step

Step 1 of 4
repartition

1. Measure current partitions

Bytes, count, and duration skew. repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys.

bytes, count, and duration skew
Key numbers at this step
Target bytes
often 128–512 MB per output partition after compression
coalesce
A narrow partition operator that merges neighboring partitions and cannot increase count or redistribute skew.

When to use it & trade-offs

What it is

repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys. df.repartition('customer_id') creates a shuffle and may skew on one giant tenant; df.coalesce(20).write.parquet(...) reduces 2,000 balanced task files to about 20; bucketing CLUSTERED BY(customer_id) INTO 128 BUCKETS supports compatible joins.

Why it matters: coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers. Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

When to reach for it

  • Control parallelism and file layout with repartition/coalesce and table bucketing for joins.
  • Choose repartition because it provides this capability: A shuffle operation that creates a requested number of partitions, optionally by hash of specified keys.
  • Keep evidence for Target bytes at "often 128–512 MB per output partition after compression" so the last production stage-Compact - bound small files and preserve table metadata-remains testable.

When to avoid or be careful

  • coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.
  • Do not conflate coalesce with repartition. The second concept contributes different behavior: A narrow partition operator that merges neighboring partitions and cannot increase count or redistribute skew.
  • Release only after this guardrail is in place: Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

repartition / coalesce / Bucketing - roles, limits, and operating cost

repartition
Best for: A shuffle operation that creates a requested number of partitions, optionally by hash of specified keys.

Strengths: A shuffle operation that creates a requested number of partitions, optionally by hash of specified keys. repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys.

Watch out: coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.

coalesce
Best for: A narrow partition operator that merges neighboring partitions and cannot increase count or redistribute skew.

Strengths: A narrow partition operator that merges neighboring partitions and cannot increase count or redistribute skew. In this workflow it supports: Write with key layout - repartition, bucket, or sort within partition.

Watch out: The operating cost is real: fixed, balanced cardinality and future data volume.

Bucketing
Best for: A persistent table layout based on hashing specified keys into a fixed number of buckets.

Strengths: A persistent table layout based on hashing specified keys into a fixed number of buckets. Its output can be checked with Small file count.

Watch out: Operating guardrail: Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for repartition, and how would Target bytes reveal it?”
  • “Why is coalesce distinct from this behavior: a persistent table layout based on hashing specified keys into a fixed number of buckets?”
  • Production clue: coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.
  • Design probe: Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

Benchmarks & rules of thumb

Target bytes -
often 128–512 MB per output partition after compression
Bucket count -
fixed, balanced cardinality and future data volume
Small file count -
files per partition should be operationally bounded

Common mistakes & follow-ups

Common interview mistakes

  • coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.
  • Reporting success from a job exit code without checking Target bytes and Bucket count.
  • Treating this example as complete without replay, rollback, or access boundaries: df.repartition('customer_id') creates a shuffle and may skew on one giant tenant; df.coalesce(20).write.parquet(...) reduces 2,000 balanced task files to about 20; bucketing CLUSTERED BY(customer_id) INTO 128 BUCKETS supports compatible joins.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A shuffle operation that creates a requested number of partitions, optionally by hash of specified keys.
  • What does the recovery plan do when this occurs? coalesce cannot create more partitions or fix upstream skew, a repartition on a random high-cardinality key fragments similar rows, and unmaintained bucket metadata silently disappears across Spark table writers.
  • When is repartition insufficient for Bucket count, and what does adding coalesce cost in Target bytes?
Interview tip: Lead with the data path and failure boundary: repartition performs a full shuffle into an exact count or key partitioning, coalesce only merges existing partitions without an all-to-all exchange, and bucketing pre-shuffles/hash-orders table writes by join/group keys. Then make the answer operational: Size repartition from data bytes and output parallelism, use coalesce before small file writes only when the source is balanced, and bucket stable join keys only when repeated jobs and table writers honor the metadata.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Partitioning (repartition vs coalesce) & Bucketing Strategies with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
User-Defined Functions (UDFs) vs Pandas Vectorized UDFs
Next topic
Join Strategies in Spark (Broadcast Hash Join, Sort-Merge Join, Shuffle Hash Join)