Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

Kill the Leader

Build Raft, Gossip, CRDTs and Fencing Tokens in Pure Python, Then Break Them on Your Laptop With a Deterministic Fault Simulator

Kill the Leader

Kill a Raft leader at 3 a.m. on purpose, on your laptop, with one seed you can replay forever.

Minimum price

$19.99

$29.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Distributed systems fail in ways you cannot reproduce. A packet arrives late, a leader pauses for a garbage collection, a retry lands after the write it was retrying, and the bug is gone before you open the logs.

This book fixes the "cannot reproduce" part. In Chapter 2 you write a small deterministic fault simulator in pure Python: one seed controls every delay, drop, duplicate, partition and crash. Then you use it for the rest of the book to build the classic algorithms yourself and break them on purpose.

You build Lamport and vector clocks, a phi accrual failure detector, SWIM-style gossip, a consistent hash ring, Raft elections and log replication, a key-value store that survives the death of its leader, a tiny linearizability checker, CRDTs, Merkle-tree anti-entropy, fencing tokens, two-phase commit and sagas, and retry policies with backoff and jitter.

Every chapter ends with a Break It section. You plant one realistic bug, run a seed sweep, and watch the test name the exact seed that fails. Then you replay that seed and see the bug happen again, every time.

No cloud account, no containers, no third-party libraries. Just Python and pytest.

What you will build:

- A deterministic network and clock simulator (105 lines) that replays any failure from a single seed

- Lamport clocks and vector clocks that catch a stale write

- A phi accrual failure detector that tells a slow peer from a dead one

- SWIM-style gossip membership with suspicion

- A consistent hash ring with virtual nodes that does not depend on PYTHONHASHSEED

- Raft leader election and log replication, plus a replicated key-value store on top

- A linearizability checker and a chaos sweep that finds history bugs on its own

- G-Counter, PN-Counter and OR-Set CRDTs that merge without coordination

- A Merkle tree that finds one different slot out of 1,024 with 21 hash comparisons

- Leases and fencing tokens that stop a paused lock holder from corrupting storage

- Two-phase commit and a saga with compensation, including a coordinator crash

- Retry policies measured against each other: in the book's run, full jitter finishes 24 of 24 callers where fixed retries finish 16

- A publisher whose retries outlive a network partition, plus a production assumptions list and a system design interview checklist

Who this book is for:

- Python developers who use etcd, Consul, Kafka, Cassandra or a cloud database and want to know what is happening underneath

- Backend engineers preparing for system design interviews (every chapter has an Interview corner)

- Engineers who have read the Raft paper and want to see it work, and fail, in code they can step through

- Anyone who has been told "it is a race condition, we cannot reproduce it"

You should be comfortable with Python classes, dataclasses and pytest. No prior distributed systems knowledge is assumed.

Tested code: All 32 chapter listings plus one appendix test are printed in full in the book. They were run and tested on CPython 3.12, 3.13, 3.14 and 3.15 with pytest: 16 chapter projects, 55 chapter tests, all passing. Every Break It bug was planted and confirmed to be caught by the chapter's seed sweep.

Drafted with AI assistance. Every code listing was run and tested on Python 3.14 and 3.15 (also 3.12 and 3.13), every measured number in the book comes from a real run, and every cited paper and date was checked against the original source.

Author

About the Author

A. C. Weishaupt

A. C. Weishaupt writes from Europe with a humble devotion to the long craft of publishing many books - some reached international Amazon Top 100 placements.

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub