Every storage system that has to survive a disk or node dying decides how much extra data to write up front so it can reconstruct what it loses later. The simplest answer is replication: keep two or three full copies on different machines. It’s easy to reason about and implement, and it’s the most expensive option, because the cost grows linearly with the number of failures you want to survive. Three copies means 3x the storage bill for every byte, forever.
Erasure coding splits the file into pieces, computes extra “parity” pieces from them with finite-field math, and spreads originals plus parity across machines. It still survives a set number of failures, for a fraction of replication’s storage overhead. The cost moves from disk to CPU: reconstructing a lost piece means matrix arithmetic instead of copying a spare.
The overhead numbers, concretely
A Reed-Solomon scheme is described by two numbers: k, the number of original data chunks, and n, the total number of chunks stored (data plus parity). Any k of the n chunks are enough to reconstruct the whole file, which means the scheme tolerates the loss of any n - k chunks.
SecStore, a small erasure-coded object store, implements this over GF(2^8) in coordinator/erasure_coding.py. Its default topology and the numbers that come out of it:
| k | n | Failures tolerated | Overhead |
|---|---|---|---|
| 2 | 3 | 1 | 1.5x |
| 4 | 7 | 3 | 1.75x |
| 1 | 3 (triple replication) | 2 | 3x |
At k=2, n=3 you tolerate one failure for 1.5x the original size, where two full copies would cost 2x. At k=4, n=7 you tolerate three failures, one more than triple replication, for 1.75x instead of 3x. That second row is why erasure coding is worth the complexity: a stronger guarantee for less storage.
There is a limit. n is capped at 15 and k at 10 in encode() (“For performance, max k=10, max n=15”), and the field itself caps n at 256, since GF(2^8) only has 256 distinct elements and every chunk index needs a unique one for the reconstruction matrix to stay invertible.1 Replication has no such ceiling; you can keep adding copies. Erasure coding’s storage efficiency comes with a scheme that is hard to scale past a modest number of chunks.
Where the CPU cost goes
The arithmetic underneath Reed-Solomon runs over GF(2^8), a finite field where addition is XOR and multiplication needs log/exp lookup tables built from a primitive polynomial:
# coordinator/erasure_coding.py
def _precompute_tables(self):
x = 1
for i in range(255):
self.gf_exp[i] = x
self.gf_log[x] = i
x <<= 1
if x & 0x100:
x ^= 0x11D # primitive polynomial
for i in range(255, 512):
self.gf_exp[i] = self.gf_exp[i - 255]
def gf_mul(self, a, b):
if a == 0 or b == 0: return 0
return self.gf_exp[self.gf_log[a] + self.gf_log[b]]
Encoding a file means building a Vandermonde matrix over this field, converting it to systematic form so the top k rows are an identity matrix, and multiplying it against the k data chunks to produce the n - k parity chunks. When everything is healthy, decoding is nearly free. If the surviving fragments are exactly the original k data chunks, the code concatenates them:
# coordinator/erasure_coding.py
def decode(self, fragments: dict, k: int, n: int, original_size: int, padding_len: int):
indices = sorted(fragments.keys())[:k]
if indices == list(range(k)):
combined = b"".join([fragments[i] for i in indices])
return combined[:len(combined) - padding_len]
# otherwise: invert the submatrix for whichever fragments survived
...
The expensive path, inverting a k x k matrix over GF(2^8) via Gaussian elimination, only runs when an actual data chunk is missing and has to be rebuilt from parity. So erasure coding’s CPU cost is paid on the reconstruction path, which only runs when something has already gone wrong, and not on every read. Replication has no cost on that path at all, since a surviving replica needs no math. That is the other half of choosing parity over copies.
The pieces you read back have to be the right ones
Erasure coding’s math has no way to detect that its own inputs are wrong. If one node was offline during a write and comes back later holding a fragment from an older version of the file, and a read combines that stale fragment with fresh ones from other nodes, the reconstruction math still runs and produces wrong output, and nothing in the decode step signals it.
The surrounding system has to guard against that; the coding scheme can’t. SecStore does it by giving every upload a fresh version ID and recording a SHA-256 hash per chunk against that version at write time, then re-verifying each chunk’s hash before it’s allowed into the reconstruction:
# coordinator/main.py, download_file()
for i, r in enumerate(results):
if isinstance(r, Exception) or r.status_code != 200 or get_hash(r.content) != meta_list[i]["hash"]:
missing.append(idx_list[i])
else:
retrieved[idx_list[i]] = r.content
if len(retrieved) < k:
raise HTTPException(status_code=500, detail="Not enough valid fragments to reconstruct file.")
A hash mismatch, whatever caused it, routes that chunk into missing instead of retrieved. The decoder only ever sees chunks that verified against the specific version being reconstructed. If fewer than k verified chunks are left, the request fails with an error instead of returning a plausible but wrong file.
This applies to any scheme that rebuilds data from partial pieces. Fragments can come from different points in time, which can’t happen with a whole-copy replica, so a system that takes erasure coding’s storage savings also has to build the version bookkeeping that replication didn’t need.
Footnotes
-
GF(2^8)has exactly 256 elements, 0 through 255, and every fragment index needs to map to a distinct one for the Vandermonde matrix to stay invertible. That’s a separate limit from thek <= 10, n <= 15capencode()enforces for performance; the two checks live in the same file but bound different things. ↩