Skip to content

Dynamic Consistency Boundaries

A Dynamic Consistency Boundary (DCB) lets a write stay consistent with exactly the events its decision depended on, across as many entities as it needs, without locks, distributed transactions or sagas. The idea is formalized at dcb.events. EvidentSource implements it with append conditions.

Classic event sourcing works well when every command touches one entity. It stops working neatly when a decision depends on several: “approve this transfer only if neither account changed since I checked them.”

The traditional answers are distributed transactions (slow and lock-prone), sagas (eventual consistency and compensation logic), or aggregates grown large enough to hold everything they might need.

A transaction carries append conditions. Each one names an event selector, and checks the latest event matching it at the moment of commit. If any condition fails, nothing commits.

So a decision says what it read, and the append says: only if that is still true.

Each condition names a selector s, over stream, subject and event type, and tests L(s): the revision of the latest event matching s, or 0 if nothing matches.

ConditionHolds when
Min(s, r)L(s) ≥ r
Max(s, r)L(s) ≤ r
Range(s, a, b)a ≤ L(s) ≤ b

The bounds are revisions, not counts of events. A condition can’t count matches, and no selector can refer to time.

GoalConditionWhy
Nothing matches yetMax(s, 0)L(s) is 0 only when nothing matches. This is the DCB spec’s failIfEventsMatch.
Something matchesMin(s, 1)Any match puts L(s) at 1 or higher.
Nothing changed since you readMax(s, r), with r the revision you read atA later matching event pushes L(s) past r.

Use Max for concurrency, never Min. L(s) only moves forward, so once L(s) ≥ r holds it keeps holding, whatever else commits.

A transfer that must not commit if either account changed after the revision the decision read, 42:

{
"events": [{"specversion": "1.0", "id": "t-1", "source": "transfers", "type": "TransferApproved", "subject": "transfer-9"}],
"conditions": [
{"max": {"selector": {"equals": {"subject": {"hasValue": true, "value": "account-A"}}}, "revision": 42}},
{"max": {"selector": {"equals": {"subject": {"hasValue": true, "value": "account-B"}}}, "revision": 42}}
]
}

A State Change’s decide returns the same conditions alongside its events. The SDKs name the common cases; in Rust and Python they are must_not_exist, must_exist, unchanged_since and at_revision.

The whole transaction fails: HTTP 409, or gRPC FAILED_PRECONDITION. Nothing commits. Read again at the latest revision, decide again, and send the transaction with conditions against the new revision.

Retrying is safe. A transaction carries an id, and resending the same id returns the result already recorded for it, instead of committing twice.

  • The server adds no conditions of its own. A decision is protected only by the conditions it sends.
  • One check the server does add is not a condition. If the database a decision read was deleted and the name created again before the transaction reached the writer, it is refused with HTTP 409, or gRPC FAILED_PRECONDITION. It never looks at revisions, so no condition could express it.
  • Every condition is checked on its own, and all of them must hold. Several conditions can share a selector: must_not_exist(s) sent with unchanged_since(s, r) commits only while nothing matches s. The order you send them in makes no difference. A 409, or gRPC FAILED_PRECONDITION, names in its message each condition that failed, as you sent it, and normally none that held. Rarely, when the server cannot tell from its index which of several conditions on one selector failed, it names all of them that it could not decide. A long list is cut short and ends with the number left out. Conditions on the same selector that no state could satisfy together, such as Min(s, 5) with Max(s, 3), are refused with 400, or gRPC INVALID_ARGUMENT, because retrying them can never succeed. So is a condition whose selector is too large to evaluate safely: nested deeper than 64 levels, expanding to more than 4096 terms once its ands are distributed over its ors, or building more than 131,072 leaves on the way; and so is a transaction whose conditions together build more than twice that. Over gRPC and HTTP, a request nested past about 50 levels is refused before it is decoded. “The same selector” means written identically: a AND b and b AND a are checked as two selectors, so a contradiction between them fails at commit with 409 instead.

Whenever a command’s correctness depends on state you read, and that state could change between your read and your append. Conditions are how a decision says: “This was decided assuming X. If X isn’t still true, don’t apply it.”