Chained reads
Usually, a command knows up front which Events it has to read: the identifiers in its properties are the Tags of its Query. Sometimes, however, part of the decision depends on state that is only known after reading other state.
This article explains how such a chained read works with a DCB-capable Event Store, and why it needs a so-called high watermark to stay consistent.
An advanced pattern
Most projects will never need chained reads, and those that do should use them sparingly. Every link in a chain makes a decision harder to follow, and widens its consistency boundary. A simpler model is often possible:
- Pass the identifier along: If the client knows the borrower anyway, the command can receive it as a property and verify it against the state of the copy – a single read is sufficient then
- Accept eventual consistency: Make the decision and revoke it, or compensate, if a constraint turns out to be violated afterwards
- Question the constraint: Not every rule has to be enforced immediately. Maybe a reservation that comes in during a renewal is perfectly acceptable for the business
What is a chained read
In the library loans example, a loan is renewed by scanning a copy of a book. The RenewLoan command only knows the copy, but it has to check constraints of the borrower and of the book, too:
- A loan can be renewed at most twice
- A loan can't be renewed while the borrower is suspended
- A loan can't be renewed while a member has reserved the book
Who borrowed the copy, and which book it is a copy of, follows from the Events of the copy. So the command reads in two steps:
- Read the Events of the copy, to learn its borrower, its book and the number of renewals
- Read the Events of that borrower and of that book
The second read takes its Tags from the result of the first one, hence the name chained read.
One consistency boundary
The chain does not split the decision into two boundaries. The Append Condition contains the Query Items of all reads, so the append fails if any Event matching one of them was appended in the meantime.
This also covers changes that affect the chain itself: if the copy is returned and lent to another member while the loan is renewed, the borrower that was read is no longer the right one. But the CopyReturned and CopyLent Events match the Query Items of the first read, so the append fails and the command can be retried with the new borrower.
The high watermark
An Append Condition contains one Query and one after position, but a chained read consists of multiple reads, each of which ends at a different position. None of these positions is a suitable after:
- Position of the first read: The last Event of the copy might be older than the last Event of the borrower. If the member was suspended and reinstated after the copy was lent, the
MemberReinstatedEvent lies after that position and makes every renewal fail, even without any concurrent change - Position of the last read: Events of the copy that were appended between the two reads are ignored by the condition. If another renewal of the same loan is appended in that moment, the loan can be renewed a third time
Instead, after has to be the high watermark, i.e. the position of the last Event in the Event Store, regardless of its type and Tags. It has to be determined no later than the first read, so that every Event appended after it, during or after the reads, is checked by the Append Condition.
Determining the high watermark
The specification doesn't require a way to determine the high watermark, so it depends on the features of the Event Store:
1. A dedicated request: Some Event Stores provide a function for this. Others allow to read a single Event backwards (pseudo-code):
highWatermark = eventStore.head()
// or
highWatermark = eventStore.read(Query.all(), { backwards: true, limit: 1 }).first()?.position
Neither head() nor reading backwards with a limit is required by the specification. The request has to be made before the first read.
2. A checkpoint of the first read: If read() returns a stream that includes the position of the last Event in the Event Store at the time of reading (a checkpoint), that position of the first read can be used as high watermark, and no additional request is needed. Note that this has to be the position of the last Event in the Event Store, not of the last Event matching the Query. A checkpoint of a later read is not suitable, for the same reason as the position of the last read.
3. Re-reading all Query Items: With only the features required by the specification, the client can read the Query Items of all reads once more, in a single read, after the chain has determined them. The Decision Model is then rebuilt from that read, and its last position is a suitable after, like for any other single read. If the result changes one of the Tags that were derived from the chain (e.g. because the copy was lent to another member in the meantime), the procedure is repeated. This approach needs no additional features but loads the relevant Events twice.
If the Event Store doesn't contain any Events yet, the high watermark is 0, or absent, depending on how the implementation represents Sequence Positions. In the latter case, after is omitted and the condition checks all Events.
Note
The reads are not limited to the high watermark. If a read already returns an Event that was appended after it, the Event is part of the Decision Model and matches the Append Condition. The append fails even though the Decision Model was up to date. This is safe, and rare, because it requires an Event to be appended between determining the high watermark and reading – a retry resolves it.
As an optimization, the client can fail (or retry) as soon as a read returns an Event with a position greater than the high watermark: that Event matches the Append Condition, so the append is bound to fail anyway, and the remaining reads can be skipped.
The high watermark is only required when the Decision Model is built from more than one read, as a chained read always is. If all Query Items are known up front, a single read suffices, and its last position can be used as after directly.
Walkthrough
The following steps show the requests of the RenewLoan command for copy c1, with Tags in the form <type>:<id>. Initially, the Event Store contains the following Events:
| Position | Type | Tags |
|---|---|---|
| 1 | CopyAddedToCatalog |
copy:c1, book:b1 |
| 2 | MemberSuspended |
member:m1 |
| 3 | CopyLent |
copy:c1, member:m1 |
| 4 | MemberReinstated |
member:m1 |
| 5 | BookReserved |
book:b2, member:m2 |
1. Determine the high watermark: 5
2. Read the copy:
read(Query {
items: [
{ types: ["CopyAddedToCatalog", "CopyLent", "CopyReturned", "LoanRenewed"], tags: ["copy:c1"] }
]
})
This returns the Events at positions 1 and 3: the copy c1 is a copy of book b1, lent to member m1, and was not renewed yet.
3. Read the borrower and the book:
read(Query {
items: [
{ types: ["MemberSuspended", "MemberReinstated"], tags: ["member:m1"] },
{ types: ["BookReserved", "ReservationCancelled"], tags: ["book:b1"] }
]
})
This returns the Events at positions 2 and 4: member m1 is not suspended, and book b1 is not reserved.
4. Append with the Query Items of both reads:
append(
Event { type: "LoanRenewed", tags: ["copy:c1"] },
AppendCondition {
failIfEventsMatch: Query {
items: [
{ types: ["CopyAddedToCatalog", "CopyLent", "CopyReturned", "LoanRenewed"], tags: ["copy:c1"] },
{ types: ["MemberSuspended", "MemberReinstated"], tags: ["member:m1"] },
{ types: ["BookReserved", "ReservationCancelled"], tags: ["book:b1"] }
]
},
after: 5
}
)
Note that the position of the first read (3) would have made the append fail because of the MemberReinstated Event at position 4.
Concurrent changes
If one of the following Events is appended after the high watermark was determined, it has this effect on the append:
| Concurrent Event | Matching Query Item | Append |
|---|---|---|
MemberSuspended for m1 |
borrower | fails |
CopyReturned and CopyLent for c1 |
copy | fails |
LoanRenewed for c1 |
copy | fails |
BookReserved for b1 |
book | fails |
MemberSuspended for m2 |
– | succeeds |
BookReserved for b2 |
– | succeeds |
Fanned-out chains
A read in a chain can also return a list of identifiers, e.g. all students subscribed to a course. The next read then contains one Query Item per identifier, so the size of the Query depends on the state that was read. The high watermark works just the same. See chained reads in the DCB notation guide for an example.
Conclusion
Chained reads allow a command to enforce constraints of entities it only learns about while deciding, without a denormalized Read Model and without an Aggregate that holds them together. The Query is as deep as the chain of reads, but only as wide as the constraints it checks. The only requirement for the Event Store client is to determine the high watermark no later than the first read and to use it as the after position of the Append Condition.
Still, chained reads are an advanced pattern: if a simpler model can enforce the same constraints, it is usually the better choice.