11 / 35 · Distributed Computation
Distributed aggregation and shuffle
Compute local states, route matching groups to the same worker and merge the states into totals.
The sales files are partitioned by day. Stores 1 and 2 both appear on several workers. A total from any one worker is now incomplete. Moving all input lines would make a correct total possible, but additions performed before the transfer can shrink what has to move.
Combine values before sending#
Each worker computes one sum for each store it sees. The toy input below gives cents. The two lists on a row are separate groups from the same worker.
| Worker | Store 1 amounts | Store 2 amounts | Partial sums |
|---|---|---|---|
| A | 100, 200 | 50 | 300, 50 |
| B | 150 | 70, 30 | 150, 100 |
| C | 100, 50 | 200 | 150, 200 |
| D | 200 | 120, 80 | 200, 200 |
The receiver for store 1 adds cents. The receiver for store 2 adds cents. The workers read disjoint inputs and each line belongs to one partial. That makes the sum of the partials equal to the sum over the original rows. PostgreSQL uses partial and final aggregate nodes for some single-machine parallel plans. In a cluster, the exchange between such stages can cross the network. (PostgreSQL 18, §15.3.3)
Merge partial sums by store
A shuffle redistributes records between partitions. Hashing the store key can send all partials for one store to one destination. The receiver can then finish the group without consulting another receiver. If each raw pair and each partial pair is 16 bytes, moving the 12 raw pairs would carry 192 bytes, while the eight partials carry 128 bytes. These are payload sizes if all pairs cross the boundary. Some destinations may be local. The model leaves out framing, compression and temporary reads and writes. Spark’s reduceByKey uses local combining for a compatible reduction. (Spark 3.5.8, Shuffle operations; Dean and Ghemawat, 2004, §4.3)
Send a state that can be merged#
An average needs both a sum and a count. Suppose one worker sees 100 and 200, and another sees 300. Their averages are 150 and 300. The average of those two values is 225, but the correct mean is . Send the states (300, 2) and (300, 1). Merging them adds both fields, then divides 600 by 3.
Exact distinct counts are another case. Adding the number of distinct values on each worker would count a repeated value from two workers twice. We need to merge sets or partition the distinct values so that each is counted once. Partial aggregation helps when the state is small relative to its input. If each local group contains one input row, there may be little volume reduction. An engine also has to manage a large number of groups without assuming their states fit in memory. (PostgreSQL 18, §15.3.3)
Next#
Aggregation routes partial states for a group to the same receiver. A join also needs related records together, but must keep enough detail to produce every matching pair. Distributed joins compare bringing both inputs together with moving only a small one.
References#
- PostgreSQL Global Development Group. Parallel Plans, §15.3.3. PostgreSQL 18 documentation on partial and final aggregation.
- Apache Software Foundation. RDD Programming Guide. Spark 3.5.8 documentation, sections on shuffle and local aggregation for key-based reductions.
- Jeffrey Dean and Sanjay Ghemawat. MapReduce: Simplified Data Processing on Large Clusters. OSDI, 2004, §4.3 on the combiner.