Lesson plan

Distributed systems with three boards lesson plan

Three ESP32-C3 boards on ESP-NOW: clocks, loss, retries, elections, consensus and partitions, ending in a counter that survives all of them.

4 chapters, 7 lessons, about 6 hours, on the ESP32-C3-DevKitM-1. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Order events without a shared clock, and count what the network loses.
  • Make retries safe, pick a leader, and agree on a value with a majority.
  • Keep working through a partition and merge without losing a thing.
  • One counter that rides out loss, crashes and partitions, and a write-up that proves it.

Materials for the whole unit

  • ESP32-C3-DevKitM-1 x 3
  • LED x 3
  • Resistor x 3

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 40 min

Time and messages

Order events without a shared clock, and count what the network loses.

LessonTimeWhat students do
Clocks and orderThree boards, three clocks that disagree. Wall-clock stamps put a reply before its question; Lamport clocks put causes first, and a tie-break makes the order total.50 min
  1. Three boards, one air
  2. Lamport clocks
  3. Clocks that jump
  4. Break the tie
  5. What Lamport cannot see
Messages and lossHeartbeats with sequence numbers: count what arrives, lose a quarter on purpose, spot the gaps, and learn why a silent node may be dead or just unlucky.50 min
  1. Numbered heartbeats
  2. Lose a quarter
  3. Crash a node
  4. Dead, or just unlucky?
  5. Name the gaps

Materials

  • ESP32-C3-DevKitM-1 x 3
  • LED x 3
  • Resistor x 3

Check for understanding

Each student finishes "Messages and loss" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Name the gaps".

Chapter 2. About 2 h 30 min

Doing it once, and agreeing

Make retries safe, pick a leader, and agree on a value with a majority.

LessonTimeWhat students do
Retries and idempotenceTwo clients add to a counter on node A with acks, timeouts and backoff. Lost acks make retries count twice, until the server remembers what it has already applied.40 min
  1. Ask, wait, ack
  2. Lose 30 percent
  3. Spot the duplicates
  4. Make adds idempotent
Leader electionThe bully algorithm on three boards: heartbeats, timeouts, elections and terms, with the leader's lamp lit, and a stale leader that a term number teaches A to ignore.60 min
  1. Who leads?
  2. The leader's lamp
  3. Kill the leader
  4. The bully returns
  5. Ignore a stale leader
  6. Bully, ring and Raft
Consensus lightThree lamps agree on one value with ballots and votes. A value commits only with a quorum, so no lamp ever shows what was not agreed, and a majority keeps going when one board dies.50 min
  1. Propose, vote, commit
  2. Every lamp agrees
  3. One board dies
  4. A majority is enough
  5. What this leaves out

Materials

  • ESP32-C3-DevKitM-1 x 3
  • LED x 3
  • Resistor x 3

Check for understanding

Each student finishes "Consensus light" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What this leaves out".

Chapter 3. About 50 min

When the network splits

Keep working through a partition and merge without losing a thing.

LessonTimeWhat students do
PartitionsA visitor count gossiped between three boards. Split the network, count on both sides, heal it, and watch a naive merge forget people until a slot-by-slot merge (a CRDT) keeps them all.50 min
  1. Count at three doors
  2. Split the network
  3. Heal, and lose people
  4. Merge slot by slot
  5. Consistent or available

Materials

  • ESP32-C3-DevKitM-1 x 3
  • LED x 3
  • Resistor x 3

Check for understanding

Each student finishes "Partitions" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Consistent or available".

Chapter 4. About 1 h 10 min

Boss: a fault-tolerant counter

One counter that rides out loss, crashes and partitions, and a write-up that proves it.

LessonTimeWhat students do
Boss: a fault-tolerant counterThe visitor counter, finished: a G-Counter gossiped between three boards that rides out loss, crashes and partitions, with an agreement lamp on each node and a read that knows when it might be stale.70 min
  1. The finished counter
  2. Agreement lamps
  3. Lose 40 percent
  4. Crash and catch up
  5. Split and heal
  6. A read that knows
  7. Write it up

Materials

  • ESP32-C3-DevKitM-1 x 3
  • LED x 3
  • Resistor x 3

Check for understanding

Each student finishes "Boss: a fault-tolerant counter" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Write it up".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.