Skip to content
Reliable Data Engineering
Practice problem hard partitioningdppcachingliquid-clusteringlayout
Practise with timer, notes and rubric

Partitioning, Dynamic Partition Pruning and Caching Decisions

Difficulty: Hard · Topics: table layout, DPP, caching, clustering · Asked at: Databricks, Netflix, Airbnb, Walmart

Scenario

You own events (180 TB, 5 years, ~400 GB/day, columns include event_ts, event_date, user_id, country, event_type, payload). Common queries:

A daily job also builds five different aggregate tables from the same filtered slice of yesterday’s events.

Your task

  1. Propose the physical layout for events (partitioning and clustering), with numbers.
  2. Q3 scans all 5 years. Explain why, and fix it.
  3. For the daily five-output job, choose between cache(), checkpoint() and an intermediate table, with reasons.

Hints

Hint 1

400 GB/day is a healthy partition size; what would partitioning by user_id or country do to file counts?

Hint 2

Dynamic partition pruning needs the fact table to be partitioned on the join key. Is date_key the partition column?

Solution

1. Layout.

2. Q3 scans everything. The query filters on dim_date.fiscal_quarter and joins on date_key, but events is partitioned by event_date. Dynamic partition pruning only applies when the fact table is partitioned on the join key, so Spark can’t turn the dimension filter into a partition filter and reads all 1,800 partitions.

Fixes (any one):

3. The five-output job.

OptionProsCons
cache() / persist(MEMORY_AND_DISK)Simple; fast reuse within one Spark applicationLost if executors die (recomputed); memory pressure; invisible outside the job
checkpoint()Truncates lineage; reliable storageWrites to a checkpoint dir that nobody can query; you still manage cleanup
Intermediate Delta table (e.g. stg_events_daily, partitioned by date)Computed once, reusable by the five jobs and others; restartable (if output 4 fails, rerun only 4 and 5); queryable for debugging; data-quality checks can gate itExtra storage and a write (cheap relative to recomputing a 400 GB filter five times)

Choose the intermediate table for a production daily job: it gives restartability, observability and reuse, and lets the five aggregates run as separate tasks (even in parallel) in the orchestrator. Use cache() for ad-hoc analysis or when all five outputs are computed in one short-lived session and the slice comfortably fits in memory. Either way, select only the needed columns before persisting.

What interviewers look for