---
title: You must shuffle a training set of hundreds of terabytes (for example, cached model activations) that cannot fit in…
url: https://gurkanozkan.ai/learn/questions/research-shuffling-hundreds-of-terabytes-043/
markdown: https://gurkanozkan.ai/learn/questions/research-shuffling-hundreds-of-terabytes-043.md
kind: question
role: Research
stage: System design
difficulty: Hard
json: https://gurkanozkan.ai/api/learn/questions/research-shuffling-hundreds-of-terabytes-043.json
robots: noindex
---

# 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.

Research · System design · Hard

## 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 2*P*D 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 2*P*D plus the final read, with additional hashing CPU, object-store request costs, and egress; parallelism reduces wall-clock time but not total bytes.

```calc
D = 300 TB, in-memory bucket size M = 100 GB  →  D/M = 3,000 buckets needed
F = 1,000: passes = ceil(log_1000(3,000)) = ceil(1.16) = 2
  bytes moved ≈ 2 × P × D = 2 × 2 × 300 TB = 1.2 PB, plus a final 300 TB read
F = 3,000: one pass, ≈ 600 TB moved plus the final read
At 100 GB/s aggregate throughput: 1.2e15 B / 1e11 B/s = 12,000 s ≈ 3.3 hours
```

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.

*Corrected by an independent AI reviewer.*

## 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.

## Topic guides

- [Data pipelines for ML and LLM systems](https://gurkanozkan.ai/learn/topics/data-pipeline.md)
- [Distributed training and parallelism](https://gurkanozkan.ai/learn/topics/distributed-training.md)
- [Interpretability and explainability](https://gurkanozkan.ai/learn/topics/interpretability.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
