Leanpub Header

Skip to main content

Build It From Scratch in Python: Distributed Systems and Databases

Bought separately

$59.98

Minimum price

$29.99

$44.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

The following 2 books are included in this bundle...

These books have a total suggested price of $59.98. Get them now for only $29.99!
About

About

About the Bundle

Two hands-on books that build the core of modern backend systems in plain Python, then break them on purpose so you see exactly how they fail.

Kill the Leader: build Raft, gossip, CRDTs and fencing tokens in pure Python, then break them on your laptop with a deterministic fault simulator. Leader election, log replication, split brain and stale leaders become failing seeds you can replay.

Build Your Own Database in Python From Scratch: build a crash-proof storage engine on a simulated disk that loses unsynced writes and tears the last one when the power is cut. Append-only log, hash index, B+tree, write-ahead log, copy-on-write pages, LSM tree, Bloom filters, transactions, MVCC with snapshot isolation, and a small SQL engine. 85 tests, plus 34 companion Python files as Extras.

Together they cover the two questions behind most system design interviews and most production incidents: how do several machines agree, and where does the data actually live when the power goes out. Standard library only, every listing printed in full and tested.

Who it is for: working Python developers who want to understand distributed systems and storage engines by building them, not by reading diagrams.

How these books were made: both were drafted with AI assistance from outlines and source lists chosen by the author, then edited. All code was run and tested, measured numbers come from real runs, and named papers were checked against primary sources.

Books

About the Books

Kill the Leader

Kill the Leader

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

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.

Also by A. C. Weishaupt: Promoted to Babysitter (https://leanpub.com/promoted-to-babysitter), about the part of the developer job that stays human when a coding agent writes the first draft.

New: Build Your Own Database in Python From Scratch (https://leanpub.com/build-your-own-database-in-python), the companion pure-Python build: an append-only log, B+Tree, write-ahead log, LSM tree, Bloom filters, MVCC and a small SQL layer, all tested.

Build Your Own Database in Python From Scratch

Build Your Own Database in Python From Scratch

Build a Crash-Proof Storage Engine in Pure Python: Append-Only Log, B+Tree, WAL, LSM Tree, Bloom Filters, MVCC, and SQL

You use PostgreSQL, SQLite or Redis every day. You have heard "write-ahead log", "B-tree" and "MVCC" in interviews and incident reviews. This book builds each one, in plain Python, on a simulated disk that loses unsynced writes and tears the last one when the power is cut.

Because the power cuts are seeded, a lost row that once took a real outage to find becomes a failing seed you can paste into a test. You build an append-only log, a hash index, fixed-size pages, a B+tree, a write-ahead log, copy-on-write pages, an LSM tree, Bloom filters, transactions, MVCC with snapshot isolation, and a small SQL engine. Every chapter ends with a Pull the Plug sweep over seeds and crash points, and a deliberate bug the sweep catches.

Every listing is printed in full and was run and tested on Python 3.14 and 3.15 (85 tests, also passing on 3.12 and 3.13). The SQL chapters are checked against the sqlite3 module in Python's standard library. It is a teaching engine, not a fast one, and the book says so.

What you'll build
  • A one-file simulated disk (disk.py) with durable and pending bytes, torn writes, durable names and replayable power cuts
  • A checked append-only log that recovers from a torn tail
  • A hash-indexed store with compaction and hint files, in the Bitcask style
  • Slotted pages with checksums, and a B+tree with insert, delete, merge and range scans
  • A write-ahead log with redo recovery and checkpoints, and a copy-on-write store with a free list
  • An LSM tree with a memtable, SSTables and compaction, plus Bloom filters per SSTable
  • Atomic transactions, then MVCC with snapshots and a first-committer-wins conflict check
  • A SQL tokenizer and parser, tables with secondary indexes, and an iterator-style query executor with EXPLAIN
  • 16 "Break It" experiments, 17 incidents (invented, labeled), 51 interview questions and a 20-item storage checklist
Who it is for

Working Python developers with 3 to 15 years of experience who want to understand where data really lives, for a system design interview or the next "where did my data go" incident. No hype, no frameworks, no diagrams without code.

Table of contents

Part I. Pull the Plug

  • 1. Your Data Is Not on Disk Yet
  • 2. One Seed, One Power Cut: The Crash Disk
  • 3. The Simplest Database: An Append-Only Log
  • 4. Find It Fast: A Hash Index and Compaction

Part II. Trees on Disk

  • 5. Pages, Bytes and struct
  • 6. The B+Tree, Part 1: Search and Insert
  • 7. The B+Tree, Part 2: Delete, Range Scans and Random Tests Against a dict
  • 8. Write-Ahead Logging: Redo After the Crash
  • 9. Free Lists and Copy-on-Write Pages

Part III. Fast Writes, Clever Reads

  • 10. The LSM Tree: Memtable, SSTables and Compaction
  • 11. Bloom Filters: Saying "Not Here" With a Few Hashes

Part IV. Transactions

  • 12. All or Nothing: Atomic Transactions and Rollback
  • 13. Readers Never Wait: MVCC and Snapshot Isolation

Part V. Speaking SQL

  • 14. A SQL Parser You Can Read
  • 15. Tables, Rows and Secondary Indexes on a Key-Value Store
  • 16. The Query Executor: Scans, Filters, Joins and EXPLAIN
  • 17. From Toy to Production
  • Appendix A. Running and Extending the Code
  • Appendix B. Sources and Further Reading
About the code

All 34 Python files are printed in full in the book and included as downloadable Leanpub Extras. Standard library only, plus pytest.

How this book was made

This book was drafted with AI assistance from an outline, a fixed simulated disk and a source list chosen by the author, then edited. Every listing was extracted from the manuscript and run and tested on CPython 3.14.10 and 3.15.2, and the suite also passes on 3.12.15 and 3.13.5. Every measured number comes from those runs. Every paper and document named was checked against its primary source in December 2026. The incidents are invented and labeled as such.

Also by A. C. Weishaupt

If you enjoy building core systems from scratch in Python, check out Kill the Leader (distributed consensus, Raft, gossip, and CRDTs in pure Python) and Promoted to Babysitter.

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