Part 3 of 6 · 2 min read
Actual serial execution
Remove concurrency entirely: one thread, one transaction at a time.
Think of a single cook in a tiny kitchen. No two orders can collide, because only one is cooked at a time. This works if each dish is fast and the cook never waits for ingredients to arrive.
The book explains that designers around 2007 decided a single-threaded loop was practical. Two things changed: memory became cheap enough to hold the whole active data set in RAM, so a transaction does not wait for the disk; and designers saw that typical OLTP transactions are short, with few reads and writes. Long analytic queries are mostly read-only and can run on a snapshot outside the loop. VoltDB, Redis and Datomic work this way.
Stored procedures. If a transaction talks to the application one statement at a time, the database waits on the network most of the time, and one thread is far too slow. So these systems do not allow interactive multi-statement transactions. The application submits the whole transaction as code ahead of time, a stored procedure, which runs from start to end without waiting. Modern systems use general languages: Java or Groovy in VoltDB, Java or Clojure in Datomic, Lua in Redis. VoltDB also replicates by running the same procedure on every replica, so the procedure must be deterministic [give the same result every time, so no raw clock reads].
Old complaints about stored procedures, per the book: vendor-specific languages, hard to debug, version and test, and a badly written procedure hurts a database shared by many application servers more than bad code on one app server.
Partitioning. One thread limits throughput to one CPU core. If data can be split so that each transaction touches one partition only, each partition gets its own thread and throughput grows with cores. A transaction across partitions must be coordinated in lock-step and is far slower. The book reports that VoltDB manages about 1,000 cross-partition writes per second, far below its single-partition rate, and adding machines does not raise it.
When serial execution fits (the book's summary): transactions are small and fast, since one slow transaction stalls all; the active data fits in memory; write throughput fits one core, or transactions stay within one partition.
A worked example, made up to show the idea. Each transaction takes 50 microseconds in memory. One thread runs 1,000,000 / 50 = 20,000 transactions per second. If a transaction waits 1 millisecond for one network round trip in interactive style, the same thread would run only 1,000 per second. Stored procedures remove the wait and give the 20x gain. With 8 partitions and no cross-partition work, 8 × 20,000 = 160,000 per second.
Check yourself:
- Why can the system not let a user think in the middle of a transaction?
- Why are cross-partition transactions so costly?