Too few partitions spill, too many drown in overhead, and a careless write leaves 98,000 tiny files behind.
A daily job reads 600 GB of click events, groups them into sessions and writes the result partitioned by country. It takes 52 minutes on 40 executors × 5 cores, the Stages tab shows 310 GB of disk spill in the aggregation, and the output folder holds 98,000 files of about 1 MB each.
None of this is about the code. It's about how many partitions each stage has and how big they are: at the read, at the shuffle and at the write.
events = spark.read.parquet("s3://lake/events/dt=2025-06-01/")
sessions = (events
.groupBy("user_id", "session_id", "country")
.agg(F.count("*").alias("events"),
F.min("ts").alias("start"), F.max("ts").alias("end")))
(sessions.write.partitionBy("country")
.mode("overwrite").parquet("s3://lake/sessions/"))Reach the last step for the run summary: the key takeaways from this lesson.