Replication vs erasure coding: same safety, a third the cost
Splitting an immutable object into k data plus m parity fragments lets any k of the k+m fragments rebuild it — tolerating m losses at ~1.2x storage instead of the 3x that plain triple-replication spends to tolerate only 2.
Now that an object is a sealed, never-changing blob, we can store it durably — and the cheapest safe way is to split it, not to copy it.
Scene 05
Replication vs erasure coding: same safety, a third the cost
- Watch
- Try it
- Predict
- Capture
The sealed object feeds the encoder and comes out as 17 data fragments plus 3 small extra fragments computed from them. The lever kills three fragments; the survivors still rebuild the original byte-for-byte. Watch the two readouts: storage overhead and durability.
Highlighted lines are the ones running in the diagram right now.
def encode(blob, k, m):data = split_into(blob, k) # k data fragments# parity = matrix-multiply over GF(2^8)parity = rs_encode(data, m) # m parity fragmentsfragments = data + parity # k + m total# immutable: computed once, never re-touchedfor f in fragments:node = pick_failure_domain(f)node.write(f)index.commit(key, locations(fragments))
def read(key):locs = index.lookup(key)alive = [f for f in locs if f.up]if len(alive) < k:raise Unrecoverable # fewer than k surviveif all_data_present(alive):return concat(data_fragments(alive))# degraded read: rebuild from ANY k survivorsk_subset = alive[:k]return rs_decode(k_subset) # inverse-matrix solve
def cost(scheme, k, m):if scheme == 'replicate':copies = m + 1overhead = copies # 1 byte stored per copytolerated = copies - 1 # lose all-but-oneelse: # erasureoverhead = (k + m) / k # thin parity, not copiestolerated = m # lose any m of k+mreturn overhead, tolerated
Where this sits in Build an S3-style distributed object store
Scene 05 of 12, in the Keep it alive act — Erasure coding, placement, the two planes, and repair.. Split into k+m fragments — any k rebuild it — tolerating m losses at ~1.4× instead of 3× replication.
Up next. Splitting into k+m fragments tolerates m losses only if those losses are independent — but real disks fail together, so where you put the fragments decides whether the math is true.
All 12 scenes in Build an S3-style distributed object store · Every curriculum