You must shuffle a training set of hundreds of terabytes (for example, cached model activations) that cannot fit in memory on any one machine. Design an approach and analyse its cost.
Answer
Model answer, written in a candidate's voice
In short
- Why shuffle: SGD needs near-i.i.d. batches; sorted or clustered order creates correlated batches and biased gradients.
- External multi-pass shuffle: scatter by seeded random/hash key into memory-sized buckets, shuffle locally, repeat; passes grow like ceil(log_F(D/M)).
- Parallel streaming workers over object storage: partition by key, stream buckets, durable staging, idempotent retries, large objects.
- Cost: I/O dominated; each pass reads and writes about D bytes, so total movement is roughly 2PD plus final read, plus hashing CPU and object-store request/egress costs.
- Exact uniformity vs good-enough: a true global permutation is impractical; seeded hash/random keys give approximately uniform order and should be validated.
- Shuffle buffers only approximate a global shuffle; acceptable for SGD if large and seeded, not for exact sampling or correctness-critical ordering.
In full
I would treat this as an external shuffle/sort, not an in-memory operation: the goal is to remove order effects so SGD sees near-i.i.d. batches rather than correlated runs of similar activations. The design is to assign each record a seeded random or hash key, scatter records into F buckets sized to fit in memory, shuffle each bucket locally, write the buckets to object storage, and repeat the scatter/shuffle until the remaining buckets are small enough to process or read directly; with data size D, memory/bucket capacity M, and fan-out F, the number of passes grows roughly like ceil(log_F(D/M)), so larger memory and larger fan-out reduce passes but increase bucket count, metadata, and small-object overhead. Parallel streaming workers process buckets independently over object storage, with large objects, idempotent retries, and durable staging so failures do not require recomputing the whole shuffle. The cost is dominated by I/O rather than CPU: each pass streams essentially all data once for read and once for write, so total bytes moved are on the order of 2PD plus the final read, with additional hashing CPU, object-store request costs, and egress; parallelism reduces wall-clock time but not total bytes.
A truly uniform global permutation is not practical at this scale, so I would use a fixed seed and hash/random keys as a good-enough approximation, then validate bucket balance and sample order; fixed-size shuffle buffers are only an approximation and are fine for SGD when large and seeded, but they should not be mistaken for an exact global shuffle.
What a strong answer covers
- Why shuffle: avoid order effects in SGD.
- Multi-pass external shuffle: scatter randomly into buckets that fit in memory, shuffle each, repeat. The number of passes grows with log(data size) / log(fan-out).
- Parallel streaming workers over object storage.
- Exact uniformity vs good-enough approximations.
- Shuffle buffers only approximate a global shuffle.
Common mistakes
- Treating a local shuffle buffer as a global shuffle.
- Ignoring IO cost.