Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

TigerBeetle 6 min read Opinion mediumAdept

It Takes Two to Contract

Design by contract mostly reduces to types and assertions, but a real contract needs two parties: the same condition asserted at both the definition site and the call site. In TigerBeetle, paired assertions (e.g., checking a function's return value both before returning and after the call) improve readability, act as an "airlock" for local reasoning, catch drift during refactoring, and even exposed a postcondition that was missing at the definition. The bigger payoff comes when the two halves are reached by different paths, such as sender and receiver replicas checking consensus invariants with different local knowledge, hash-chained checksums, or verifying that state rebuilt from a snapshot matches state built from replaying events. The recommendation is to look for the distant code that relies on each assertion and assert it there too.

Assumes: assertionsbasic consensus protocols

Related posts

  • Snapshot Testing For the Masses

    TigerBeetle mediumAdept

    A minimal snapshot testing library can be built without macros, external tools or editor integration: the core primitive is a self-updating string literal that records its source file and line (via Zig's @src()) and rewrites itself in place on mismatch. Because the snapshot is a first-class value rather than an assertion macro, it composes into helpers like diff_fmt, diff_json and domain-specific check functions, as in TigerBeetle's CLI argument parser tests. Practical details covered include opt-in updates via an env var, splicing in correctly indented string literals, tracking line shifts across multiple updates, touching the filesystem only when a test would fail anyway, CRLF pitfalls, and an <snap:ignore> marker for volatile fields like timestamps. The whole thing fits in under 500 lines.

  • Protocol-Aware Deterministic Simulation Testing

    TigerBeetle hardMaster

    TigerBeetle's deterministic simulator (the VOPR) has full visibility into each replica's consensus and storage state, so it can check invariants that black-box tools like Jepsen or Antithesis cannot see through user-facing APIs. For safety, it asserts that every committed WAL entry has matching checksums across replicas, that LSM manifest structure is consistent everywhere, and that replicas are byte-for-byte identical, which goes deeper than the cheaper production checks that crash a diverging backup. For liveness, it enforces per-replica invariants: a replica with an uncorrupted WAL must never need cluster coordination to recover, and missing blocks must be repaired from intact copies on other replicas whenever any copy still exists.

  • The Tests Are Passing, Why Would I Read The Diff Again?

    ClickHouse mediumAdept

    Code review reduces defects largely because explaining code to another person exposes gaps in understanding, much as writing comments or rubber duck debugging does. Reviewers should focus less on style and gatekeeping and more on whether the code is understandable. They should ask for clarification when something takes effort to follow and make sure the answers end up in the code and comments rather than staying in the conversation. Authors should do design review first, write introductory and why-focused comments, document data structures and invariants, and back comments with assertions and tests. The post also rebuts the "self-documenting code" and "comments go stale" objections to heavy commenting.

  • Random Fuzzy Thoughts

    TigerBeetle mediumAdept

    Treating a fuzzer's byte slice as a finite PRNG unifies coverage-guided fuzzing with structured property-based testing: the entropy length measures input complexity, gives test-case minimization almost for free, and tells interactive simulations when to stop injecting faults. A seed reproduces a failure only at a specific commit, so robust regression tests should persist the generated structured data (e.g. a .wasm module) rather than the seed, using a separate generate/build/process pipeline that also supports hand-written inputs. For interactive distributed-system tests, a livelock in TigerBeetle was reproduced by running the VOPR simulator with a perfect network and steering it with predicates over system state, suggesting regression suites made of predicate 'key frames' that random traces must hit.

  • Swarm Testing Data Structures

    TigerBeetle mediumAdept

    Zig's comptime reflection (std.meta.DeclEnum) can turn a data structure's public API into an enum that drives a model-based property test, so adding a new public method triggers a compile error from the non-exhaustive switch until the test covers it. TigerBeetle tests its intrusive queue against a ring-buffer model and adds swarm testing: rather than picking operations uniformly, each run randomly chooses a subset of operations and random weights for them, so runs reach states like long queues that uniform push/pop never produces. Code is included for weighted enum sampling, random weight generation and an allocation-free k-combination helper.

The quest for software engineering knowledge.

Look:
Layout: