Marc's Blog

About Me

My name is Marc Brooker. I like to build things that work, and do cool stuff. I like building big things. I also dabble in machining, welding, cooking, and skiing.

I am an engineer at Amazon Web Services (AWS) in Seattle, where I work on agentic AI, especially safety and policy for agentic AI. Before that, I worked on EC2, EBS, databases, serverless, and serverless databases.
All opinions are my own.

Links

My Publications and Videos
@marcbrooker on Mastodon @MarcJBrooker on Twitter


Is this blog written by AI?

A Correctness Approach for Modern Systems Software

In 2026. Next year, who knows?

When I speak to AWS customers, especially developers and technology leaders, the most common question I get is: how do I ship high quality code now? Or it’s variants: how do I avoid shipping slop? What does testing look like now? Do I still need code review for quality? Etc.

This is an important question for software of all kinds, but most critical for what I’d call systems software. The low-level components that we build other components on top of. Databases, schedulers, object stores. That kind of thing.

In this post, I dive into lease client, a distributed lock client backed by Aurora DSQL or DynamoDB. lease client is a useful piece of software in its own right (the Java predecessor has over 500 stars, and many more downloads), but I primarily built it as an example of what I consider today’s best practices for building correct systems software.

This code is likely to have the lowest bug density of any code I’ve written in 30 years of writing software for money. And that’s despite me not having read or reviewed a single line of the implementation.

This post dives into the approach, the tools I used, and the remaining gaps.

But first: is this overengineering? I think not, for two reasons. First, components like this have an awful lot of leverage. If they have bugs, everything on top of them behaves in an unexpected way. Second, this approach is not particularly expensive. Probably three hours of my time, and a day of Kiro’s time was all it took. That’s less time than it would have taken me to dash out a solid first release two years ago, and the quality bar here is much higher. I wouldn’t set this bar for most business logic code, but for lower-level implementations it seems to be the right one.

Let’s dive into the tools.

The Tools

API Design. The API is designed to resist misuse, in two key ways. First, we recognize that leases only correctly implement mutual exclusion under some timing assumptions (i.e. you have to check you hold the lease, and then do work within a certain amount of time). The is_valid API tries to make it as obvious as possible what the requirements are.

// Timing-based exclusion: check before acting, act within max_pause.
if lease.is_valid() { /* ... */ }

Second, we’ve added a clock free fenced write API, which makes it easy to write to the underlying data store while enforcing we hold the lock, in a way that doesn’t make assumptions about time or clock skew.

    // Clock-free exclusion for data in the same store: a fenced write.
    match store.fenced(&lease.guard().unwrap(), writes).await {
        FencedOutcome::Applied => { /* done */ }
        FencedOutcome::Rejected => { /* lost the lock: stop */ }
        _ => { /* see "Fenced writes" below */ }
    }

This is important, because our goal isn’t to write correct software, it’s to enable people to build correct systems. The Config object is also defensive, and tries to reject all invalid or illegal configurations.

The Spec. The spec lays out what the client does, its assumptions, and the properties it needs to hold. Everything else grows from the spec. What I mean when I say that this code is correct is that it matches the spec. For that to be a useful property, the spec must match the expectations of programmers using this library.

At a high level, the spec is broken into four parts: the properties (what we want to hold true), some assumptions (things we assume to be true), a structured text description of the protocol, and a semantics for the services we build on (i.e. how we expect DSQL and DynamoDB to behave).

Rust. All of the runtime code is written in safe Rust, which provides a set of basic memory safety properties that help avoid basic issues in the code. We enforce the use of no unsafe in our runtime code at build time.

Design. The code is split into two crates: a no-IO core containing the implementation of the lease algorithm, and a client containing the DSQL and DynamoDB interactions. Both need to be correct, but separating the core allows us to focus correctness work on the fundamental protocol without the messiness of the real world.

Direct proof. We use the Aeneas tool chain to translate Rust into Lean (via a few intermediate representations). This approach allows us to prove critical properties of the actual core code, not some abstract model of the code. We prove that the core code does not panic, that it implements core timing logic correctly, and some other key properties. This proof depends on the soundness of the Aeneas tool chain.

Model Checking. We use Veil on a model of the protocol to check that the protocol we have implemented behaves correctly. This work is done with an exhaustive breadth-first search of the state space that checks at each step that no safety properties have been violated. Our model check works on a small instance of the problem (two clients, three, steps, three writes), bounding the number of states it needs to check (this approach should be very familiar if you’ve used TLC to check TLA+ code). Despite the bounding, our model check explores 10,198,126 unique states. Model checking allows us to check concurrency and failure properties that are difficult to prove in Aeneas’s model.

ghost relation valid (c : client) := holds c ∧ now ≤ stamp c + holdMax

-- P1: at most one client holds a lease it judges valid.
safety [mutual_exclusion] checkMutex → (valid C ∧ valid D → C = D)

Refinement. We build a refinement proof in Lean which links the abstract Veil model and the model Aeneas extracted from the code. This allows us to use Veil’s proof capabilities to prove (via inductive invariants) some of the temporal properties of the code. Unlike the bounded model checking results, these are real proofs. The most critical properties we prove on this path are P2 and P3, our monotonicity and total ordering properties.

P2: If session S2 acquires the lock after session S1 did, then S2.fence > S1.fence. The store’s fence never decreases. P3: Every fenced operation that succeeds was issued by the session the store named as owner when the operation took effect.

Inductive invariants. We prove inductive invariants for key properties like P1 (our mutual exclusion property) on the Veil model, extending our confidence in the protocol to instances of all sizes. This proof currently adds a cvc5 to our trusted compute base, but I expect that with more agent time we could prove these same properties with Lean.

Deterministic simulation. Using turmoil, we run deterministic simulation tests on the core library and async driver. These tests inject failures, different timing conditions, and different patterns of API use. The tests run against simulated store implementing the semantics we require DSQL and DynamoDB to implement for us. The simulations also force 15 rare protocol transitions to occur, making sure that they aren’t missed.

Runtime monitoring. As the deterministic simulations run, we use the same Lean code that checks each state in our model checking phase to check that the simulation state is legal. Conceptually similar to PObserve, but at test time. This approach lets us test that each simulation run only follows transitions formally allowed by our model.

Mutation testing. At the proof level, model checking level, and deterministic simulation testing level we check that the tests fail if things are changed. This helps us test whether our proof is vacuous (i.e. proves something uninteresting), and whether our model checking and simulation catches actual bugs. Beyond mutation testing, we also check for non-vacuity in a few other ways, including: making sure our Lean code has no sorry and no new axioms, cross-checking the monitor’s correctness definition against the proof, and injecting a false condition into the proof and checking that the proof fails.

-- Non-vacuity witness, false by design when `witness` is on: some run steals
-- the lock (a second session acquires). The witness run must violate it.
safety [witness_no_second_session] witness → ¬ (holds C ∧ myFence C ≥ 2)

Unit testing. No software correctness practice would be complete without some basic low-level unit tests. These are fast, cheap to run, and can easily find simple regressions in the code. Property-based testing is used for some properties (like the round-trip of the state codec, and strongly rejecting invalid stored states).

Integration testing. As a final step, we test the code against real DynamoDB and DSQL tables.

What’s Not Covered

Assumptions. By its very nature, this code makes some assumptions about some things it depends on. Every proof and test is in context of these assumptions.

A7: Lock records are never deleted while any session or fenced resource might still use their fence.

Others come from the definition of leases, and are assumptions behind the very idea of using time for mutual exclusion.

A1: Each client’s monotonic clock runs at a rate within [1-ρ, 1+ρ] of real time.

One subtlety around assumptions is that only some of the properties depend on some of the assumptions. For example, our mutual exclusion property P1 depends on bounded clock drift (A1), but our monotonicity and fence ordering properties do not.

Dependencies. We assume that the backend store, DSQL or DynamoDB, behave as documented in some key ways. Mostly this comes down to linearizable reads, and compare-and-swap offering strict serializability (which, for DSQL, requires us to structure the schema in a way that a CAS always results in write-write conflict). These aren’t edge cases for either database, so we’re not depending on unusual functionality here.

Not all properties are proven. Specifically, our ‘no spurious loss’ property P5 is only model checked. This is a liveness property and not a safety one, but there’s still a gap here which appears like it could lead to live lock if the model checker misses a case.

Frequently Asked Questions

Is this code bug free? No, that seems unlikely. But, as I said above, I’m pretty sure this is the lowest bug density code I’ve ever written.

How much expertise does it take to build this way? Some, in knowing what you need to ask for in the first few prompts, but not much. You don’t need to be able to read or write Lean, or even really know what terms like proof and bounded model checking mean exactly. In my mind, this is the biggest single technical advancement of the agentic coding era: advanced correctness tools are now usable by non-experts.

This was my first prompt:

I want to build a version of https://github.com/awslabs/amazon-dynamodb-lock-client in Rust, supporting both DynamoDB and DSQL. I really want a great formal verification story. But it seems Verus doesn’t work well with this kind of distributed thinking, and it’s not clear how to connect a Lean+Veil story to the Rust code reliably. Maybe https://hydro.run/ would help. Let’s start by brainstorming the approach to verification.

A bit of back-and-forth followed, but none of it requiring deep expertise beyond what most distributed systems developers could learn. But what you do need to be clear on is what problem you’re trying to solve. “Go build something” isn’t going to achieve what we need.

Do you understand this code? In a sense, yes. I understand the properties and assumptions, and understand the lease protocol well. In another sense, no. The Rust code is an implementation detail I haven’t read, and I don’t understand the details of how it works. In a deeper sense, I think I understand this project more deeply than most code I wrote, because I could focus on properties and assumptions (as I wrote about in What does it mean to understand what we built?.

Why Aeneas and not Verus? Aeneas seemed like a better fit for this kind of code, while Verus seems like a better fit for things that do more low-level bits-and-byte work (especially with unsafe). I’m not 100% sure I made the right decision here, but I do like Aeneas’s approach.

What about bugs in the prover? Don’t people find Lean bugs all the time? I’d recommend reading Leo’s posts on this topic and learning about the verified kernels. But there are risks here: Lean could be broken, Veil could be broken, Aeneas could be broken, our CPU could be broken, etc. If I was a mathematician, I’d be spending a bunch of time thinking about those things. But I’m an engineer, and so I get to do something much less absolute. I get to compare this to the past: is this a higher bar for correctness than what I could have achieved without these tools. The answer is unequivocally yes.

How do I learn to do this? Try it. That’s the first step. Pick something you want to build, and work with your favorite coding agent to build it this way. If you want to know more about what we do at AWS, check out the S3 team’s great pair of papers about applying formal methods to systems software. We did a survey in 2025 and 2015, but much of what we say there is a bit stale.