Checkpoint Store
A checkpoint records where one event consumer resumes after a restart. The CheckpointStore type is the contract an event observer uses to load and persist those checkpoints. The core package defines the types and ships an in-memory implementation. Persistent implementations live in the storage packages, for example the MongoCheckpointStore.
The contract knows nothing about a specific event store. A position is an opaque string owned by whoever runs the consumers. For EventSourcingDB it is the id of the last event the consumer finished.
An in Depth Example
This guide also has an in depth example of a working application built with Nimbus. Combining DDD, CQRS and Event Sourcing.
Check out the In Depth Example page to learn how everything is connected and works out in a real-world application.
Checkpoint
type Checkpoint = {
consumer: string;
observer: string;
position?: string;
updatedAt: string;
failure?: {
position: string;
error: string;
at: string;
};
};| Property | Description |
|---|---|
consumer | The stable name of the consumer, unique across the whole application. Renaming it replays the stream |
observer | The name of the observer the consumer belongs to |
position | The last position the consumer finished. Absent until the first one, in which case it starts at the beginning |
updatedAt | When the checkpoint was last written, as ISO 8601 |
failure | Set while the last attempt at failure.position failed. Cleared by the next successful save |
A matching Zod schema is exported as checkpointSchema for store implementations that want to validate what they read back.
CheckpointStore
type CheckpointStore = {
load(consumers: readonly string[]): Promise<Map<string, Checkpoint>>;
save(updates: readonly CheckpointUpdate[]): Promise<void>;
reset(consumer: string, position?: string): Promise<void>;
};load
Returns the checkpoints of the given consumers keyed by name. Consumers without a checkpoint are absent from the result and start from the beginning of the stream.
save
Persists every given CheckpointUpdate. Implementations write them in one round trip where the storage allows it, since an observer saves after every event.
type CheckpointUpdate = {
consumer: string;
observer: string;
position?: string;
failure?: {
position: string;
error: string;
};
};positionleft out keeps the stored position, so a failure can be recorded for a consumer that has not finished any event yet.failureleft out clears an existing failure marker. The marker therefore always reflects the most recent attempt.
reset
Moves a consumer to position, or back to the beginning of the stream when position is omitted, and clears any failure marker. Use it to rebuild a projection or to skip an event a consumer cannot handle. A consumer without a checkpoint is left alone. To give a new consumer a starting position use save.
This is an offline operation. The observer running the consumer must not be active. It keeps its positions in memory and would overwrite the reset with its next save.
Resetting a consumer declared with startFrom: "head" has no effect, since it ignores its stored position on every start (see Live-only Consumers).
InMemoryCheckpointStore
import { InMemoryCheckpointStore } from "@nimbus-cqrs/core";
const checkpointStore = new InMemoryCheckpointStore();Keeps checkpoints in process memory. Every restart starts from an empty store, so consumers replay the whole stream. Use it in tests, or for consumers whose read models live in memory as well and have to be rebuilt on every start anyway.
Implementing a Store
A store for another database only has to implement the three methods above. Keep these properties:
- Key records by
consumeralone. Consumer names are unique across the application, andresetonly receives the consumer name. loadmust not fail for unknown consumers. Leave them out of the result.saveis called with several consumers at once. Write them together.- A save without
failureremoves a stored failure marker. - Treat
positionas an opaque string. Do not parse or compare it.
See the MongoCheckpointStore for a reference implementation.