How It Works


~22 min read

🪲 The Tiger Style

TigerBeetle is a database for financial transactions. TigerStyle is the coding guide its team follows. It ranks three goals in this order: safety, performance, and developer experience. The guide gives rules for each goal: assert what every function expects, limit every loop and queue, allocate all memory at startup, batch the work, and name things well.

This post reposts the guide under its Apache 2.0 license and adds two figures. Figure 1 shows what compile-time assertions catch in TigerBeetle’s 128-byte Transfer struct. Figure 2 shows why batching changes which cost to cut first. The latest version of the guide is in the TigerBeetle repository.

The Essence Of Style #

“There are three things extremely hard: steel, a diamond, and to know one’s self.” — Benjamin Franklin

TigerBeetle’s coding style is evolving. A collective give-and-take at the intersection of engineering and art. Numbers and human intuition. Reason and experience. First principles and knowledge. Precision and poetry. Just like music. A tight beat. A rare groove. Words that rhyme and rhymes that break. Biodigital jazz. This is what we’ve learned along the way. The best is yet to come.

Why Have Style? #

Another word for style is design.

“The design is not just what it looks like and feels like. The design is how it works.” — Steve Jobs

Our design goals are safety, performance, and developer experience. In that order. All three are important. Good style advances these goals. Does the code make for more or less safety, performance or developer experience? That is why we need style.

Put this way, style is more than readability, and readability is table stakes, a means to an end rather than an end in itself.

“…in programming, style is not something to pursue directly. Style is necessary only where understanding is missing.” ─ Let Over Lambda

This document explores how we apply these design goals to coding style. First, a word on simplicity, elegance and technical debt.

On Simplicity And Elegance #

Simplicity is not a free pass. It’s not in conflict with our design goals. It need not be a concession or a compromise.

Rather, simplicity is how we bring our design goals together, how we identify the “super idea” that solves the axes simultaneously, to achieve something elegant.

“Simplicity and elegance are unpopular because they require hard work and discipline to achieve” — Edsger Dijkstra

Contrary to popular belief, simplicity is also not the first attempt but the hardest revision. It’s easy to say “let’s do something simple”, but to do that in practice takes thought, multiple passes, many sketches, and still we may have to “throw one away”.

The hardest part, then, is how much thought goes into everything.

We spend this mental energy upfront, proactively rather than reactively, because we know that when the thinking is done, what is spent on the design will be dwarfed by the implementation and testing, and then again by the costs of operation and maintenance.

An hour or day of design is worth weeks or months in production:

“the simple and elegant systems tend to be easier and faster to design and get right, more efficient in execution, and much more reliable” — Edsger Dijkstra

Technical Debt #

What could go wrong? What’s wrong? Which question would we rather ask? The former, because code, like steel, is less expensive to change while it’s hot. A problem solved in production is many times more expensive than a problem solved in implementation, or a problem solved in design.

Since it’s hard enough to discover showstoppers, when we do find them, we solve them. We don’t allow potential memcpy latency spikes, or exponential complexity algorithms to slip through.

“You shall not pass!” — Gandalf

In other words, TigerBeetle has a “zero technical debt” policy. We do it right the first time. This is important because the second time may not transpire, and because doing good work, that we can be proud of, builds momentum.

We know that what we ship is solid. We may lack crucial features, but what we have meets our design goals. This is the only way to make steady incremental progress, knowing that the progress we have made is indeed progress.

Safety #

“The rules act like the seat-belt in your car: initially they are perhaps a little uncomfortable, but after a while their use becomes second-nature and not using them becomes unimaginable.” — Gerard J. Holzmann

NASA’s Power of Ten — Rules for Developing Safety Critical Code will change the way you code forever. To expand:

Control Flow And Limits #

Assertions #

Assertions detect programmer errors. Unlike operating errors, which are expected and which must be handled, assertion failures are unexpected. The only correct way to handle corrupt code is to crash. Assertions downgrade catastrophic correctness bugs into liveness bugs. Assertions are a force multiplier for discovering bugs by fuzzing.

Fig. 1Edit the 128-byte Transfer
Byte map of TigerBeetle's 128-byte Transfer, 16 bytes per row, and the three compile-time assertions that check it; with amount as u64, 8 bytes of padding follow it and only no_padding fails
Fields and assertions from src/tigerbeetle.zig, abridged. Predict which assertion fails, then switch amount between u128 and u64 to compare the bytes. Offsets and the zig build output are from Zig 0.16 compiling a copy of the struct, with paths shortened.

Memory, Scope, And Size #

Beyond The Power Of Ten #

“Specifically, we found that almost all (92%) of the catastrophic system failures are the result of incorrect handling of non-fatal errors explicitly signaled in software.”

Performance #

“The lack of back-of-the-envelope performance sketches is the root of all evil.” — Rivacindela Hudsoni

Fig. 2Napkin sketch of the largest cost as the batch grows
Per-transfer cost of network, disk, memory, and CPU as the batch grows from 1 to 8,189 transfers
Estimated costs for one batch at a time, with no overlap. Sample costs: a 500 µs round trip and 100 ns per cache miss (latency numbers), 10 Gbit/s network, 2 GB/s disk and 20 GB/s memory bandwidth, and 100 ns of CPU per transfer. 8,189 is the default create_transfers batch limit (requests).

Developer Experience #

“There are only two hard things in Computer Science: cache invalidation, naming things, and off-by-one errors.” — Phil Karlton

Naming Things #

Cache Invalidation #

Off-By-One Errors #

Style By The Numbers #

Dependencies #

TigerBeetle has a “zero dependencies” policy, apart from the Zig toolchain. Dependencies, in general, inevitably lead to supply chain attacks, safety and performance risk, and slow install times. For foundational infrastructure in particular, the cost of any dependency is further amplified throughout the rest of the stack.

Tooling #

Similarly, tools have costs. A small standardized toolbox is simpler to operate than an array of specialized instruments each with a dedicated manual. Our primary tool is Zig. It may not be the best for everything, but it’s good enough for most things. We invest into our Zig tooling to ensure that we can tackle new problems quickly, with a minimum of accidental complexity in our local development environment.

“The right tool for the job is often the tool you are already using—adding new tools has a higher cost than many people appreciate” — John Carmack

For example, the next time you write a script, instead of scripts/*.sh, write scripts/*.zig.

This not only makes your script cross-platform and portable, but introduces type safety and increases the probability that running your script will succeed for everyone on the team, instead of hitting a Bash/Shell/OS-specific issue.

Standardizing on Zig for tooling is important to ensure that we reduce dimensionality, as the team, and therefore the range of personal tastes, grows. This may be slower for you in the short term, but makes for more velocity for the team in the long term.

The Last Stage #

At the end of the day, keep trying things out, have fun, and remember—it’s called TigerBeetle, not only because it’s fast, but because it’s small!

You don’t really suppose, do you, that all your adventures and escapes were managed by mere luck, just for your sole benefit? You are a very fine person, Mr. Baggins, and I am very fond of you; but you are only quite a little fellow in a wide world after all!”

“Thank goodness!” said Bilbo laughing, and handed him the tobacco-jar.

Authors1

This document is licensed under the Apache License 2.0. See the LICENSE-APACHE file for details.


  1. List of authors taken from git history. Original author is Joran Dirk Greef (first commit). Credits to all contributors of TIGER_STYLE.md ↩︎

Tags: