Article

Erasure coding for decentralized storage, explained

In a decentralized storage network, nodes go offline all the time. Erasure coding lets a file survive those failures without storing full copies everywhere. Here's how it works, with a Go example you can run.

By Digvijay Khatri4 min read
01

Why not just make copies?

The simplest way to protect a file is replication: store three full copies on three nodes. Lose two, and one copy remains. The cost is 200% storage overhead: every 1 GB costs 3 GB.

Erasure coding gets the same protection for far less. With 4 data shards and 2 parity shards, a file is cut into 4 pieces and 2 extra parity pieces are computed from them. Any 4 of the 6 rebuild the original, so the file still survives two node failures, but the overhead is only 50%.

In a peer-to-peer network where providers are run by strangers and can vanish without warning, that difference decides whether the network is affordable.

02

How Reed–Solomon works, in short

Reed–Solomon codes treat each column of bytes across the data shards as points on a polynomial. Parity shards are extra points on the same polynomial. Because a polynomial of degree k−1 is fully defined by any k points, any k surviving shards are enough to recover the rest.

You choose two numbers:

  • k, data shards: how many pieces the file is split into.
  • m, parity shards: how many failures you can tolerate.

Storage overhead is m / k, and the file survives the loss of any m shards.

03

A working example in Go

Using the widely used klauspost/reedsolomon package.

Install the package with go get github.com/klauspost/reedsolomon, then run:

package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/klauspost/reedsolomon"
)

func main() {
	data := []byte("files survive when storage providers go offline")

	// 4 data shards + 2 parity shards: any 4 of the 6 rebuild the file.
	enc, err := reedsolomon.New(4, 2)
	if err != nil {
		log.Fatal(err)
	}

	shards, err := enc.Split(data) // 6 equal-size shards, parity still empty
	if err != nil {
		log.Fatal(err)
	}
	if err := enc.Encode(shards); err != nil { // fill in the parity shards
		log.Fatal(err)
	}

	// Simulate two providers going offline.
	shards[1], shards[4] = nil, nil

	if err := enc.Reconstruct(shards); err != nil {
		log.Fatal(err)
	}
	if ok, err := enc.Verify(shards); err != nil || !ok {
		log.Fatal("parity check failed")
	}

	var out bytes.Buffer
	if err := enc.Join(&out, shards, len(data)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(out.String())
}

Split pads the data and cuts it into equal shards, Encode computes parity, and Reconstruct rebuilds any missing shards. In a real network each shard is sent to a different provider, and the client keeps the original length and the shard hashes so it can verify what comes back.

04

Design choices in a real network

Pick k and m from failure rates

If providers are unreliable, raise m. If bandwidth is expensive, keep k moderate, since every read touches k nodes.

Place shards on independent nodes

Two shards on the same machine, or the same data centre, fail together. Spread them across operators and regions.

Detect failures early

Erasure coding only helps if you repair before too many shards disappear. A heartbeat or proof-of-storage check spots dead providers, and the network rebuilds their shards onto healthy nodes.

Know the trade-offs

Repairs cost bandwidth, since rebuilding one shard means fetching k others. Encoding costs CPU, and very small files gain little. Many systems replicate tiny files and erasure-code large ones.

05

How I used it

My decentralized storage protocol combines these ideas: a P2P layer written in Go, erasure-coded shards spread across providers, heartbeat liveness checks, and on-chain payments and penalties in Solidity so providers are paid for uptime and penalised for downtime without a trusted operator.

06

FAQ

What is erasure coding in simple terms?

It splits a file into pieces and adds a few extra parity pieces, so the original can be rebuilt from any large enough subset. You can lose some pieces and still recover the whole file.

Is erasure coding better than replication?

For large files, usually yes: it gives the same fault tolerance with much less storage. Replication is simpler and faster to repair, so it is still common for small or very hot files.

Which decentralized storage systems use erasure coding?

Several decentralized storage networks use erasure coding, as do many cloud object stores and distributed file systems.