Custom Partitioners

Concepts covered: Shuffle Optimization

Every hash shuffle on a DataFrame places a row by pmod(murmur3(key), n): the key's hash, modulo the partition count. With thousands of mid-sized keys the hash spreads load evenly enough. With a few giant keys it runs a lottery you never see, because the hash knows nothing about key sizes and 2 giant keys can draw the same partition.

Take a table of 100M events from 10,000 tenants. Tenant A has 30M rows, B has 28M, C has 25M, and the other 9,997 share the last 17M. Shuffled into 8 partitions, the average partition holds 12.5M rows. The 3 big tenants draw partitions independently, so the chance that all 3 land apart is 8 x 7 x 6 out of 8 x 8 x 8. When A and B do share a partition, that task starts with 58M rows, more than 4.6 times the average.

A partitioner decides where whole keys go and cannot cut 1 key across tasks. The best placement here gives A, B and C a partition each and spreads the tail over the rest. At 16 partitions, that is 3 dedicated partitions plus 13 sharing 17M rows, about 1.3M each. The slowest task is then A's 30M rows, and no partitioner goes below that floor; splitting A itself takes salting. Reach for a custom partitioner when the biggest single key is acceptable and the collisions or the packing are what hurts. Placement also matters when the output layout is the goal, such as 1 partition per tenant before a write.

Which problem can a custom partitioner fix on its own, with no salting?

Writing one

The DataFrame API has no partitioner hook: repartition(n, col) always hashes. Exact placement goes through the RDD API. On an RDD of key-value pairs, partitionBy(numPartitions, partitionFunc) calls your function on each key and sends the record to partition partitionFunc(key) % numPartitions.

import zlib

N = 16
DEDICATED = {"tenant_a": 0, "tenant_b": 1, "tenant_c": 2}
TAIL_SLOTS = N - len(DEDICATED)


def place(tenant_id):
    if tenant_id in DEDICATED:
        return DEDICATED[tenant_id]
    if tenant_id is None:
        return len(DEDICATED)
    return len(DEDICATED) + zlib.crc32(tenant_id.encode()) % TAIL_SLOTS


placed = (
    events.rdd
    .map(lambda row: (row["tenant_id"], row))
    .partitionBy(N, place)
)

The price is the RDD escape hatch you already know. Once rows leave the DataFrame, Catalyst cannot see the placement, so a DataFrame rebuilt from placed reports no partitioning and a later groupBy("tenant_id") shuffles again. Use the placement where it pays directly: per-partition processing with mapPartitions, or a write that should produce 1 file set per partition.

A DataFrame-only approximation computes a slot column with the same rules and calls repartitionByRange(N, "slot"). Range boundaries come from a sample, so placement lands close to the plan without a guarantee of 1 slot per partition.

The cell below places store_sales_raw rows the way a partitioner keyed on store_id % 4 would, and counts rows and stores per slot. Run it and find the heaviest slot. To find the store that makes it heavy, change groupBy("slot") to groupBy("slot", "store_id") and run again: in that slot, the row with the largest rows names the store. Then give that store a slot of its own: change the expression to F.when(F.col("store_id") == 1, 0).otherwise(1 + F.col("store_id") % 3), replace 1 with that store's id, group by "slot" alone again, and compare the counts.

About this lesson

This section is part of the Data Skew: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.