Leanpub Header

Skip to main content

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

Build Your Own Database in Python From Scratch
This book is 100% completeUpdated 3 Dec 2026

Build a crash-proof database from scratch in pure Python: append-only log, B+tree, WAL, LSM tree, Bloom filters, MVCC, and SQL, tested against a seeded crash disk with 85 passing tests.

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

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.

Bundle

Bundles that include this book

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