Dynamic Consistency Boundary (DCB)
The Dynamic Consistency Boundary (DCB) pattern allows you to query and enforce consistency across events from multiple streams using tags -- strong-typed identifiers attached to events at append time. This is useful when your consistency boundary doesn't align with a single event stream.
Concept
In traditional event sourcing, consistency is enforced per-stream using optimistic concurrency on the stream version. DCB extends this by letting you:
- Tag events with one or more strong-typed identifiers
- Query events across streams by those tags
- Aggregate tagged events into a view (like a live aggregation, but cross-stream)
- Enforce consistency at save time -- detecting if new matching events were appended since you last read
Polecat uses a single append strategy (direct INSERT with OUTPUT inserted.seq_id), so DCB tags are always persisted immediately after each event insert. There are no separate append modes to configure -- tags just work.
Registering Tag Types
Tag types are strong-typed identifiers (typically record types wrapping a primitive). Register them during store configuration:
public override async ValueTask InitializeAsync()
{
await StoreOptions(opts =>
{
// Register tag types -- each gets its own table (pc_event_tag_student, pc_event_tag_course)
opts.Events.RegisterTagType<StudentId>("student")
.ForAggregate<StudentCourseEnrollment>();
opts.Events.RegisterTagType<CourseId>("course")
.ForAggregate<StudentCourseEnrollment>();
});
}Each tag type gets its own table (pc_event_tag_student, pc_event_tag_course, etc.) with a composite primary key of (value, seq_id).
Tag Type Requirements
Tag types should be simple wrapper records around a primitive value:
// Strong-typed tag identifiers
public record StudentId(Guid Value);
public record CourseId(Guid Value);Supported inner value types: Guid, string, int, long, short.
Registering Tag Types under Trimming or Native AOT
RegisterTagType<TTag>() finds the tag's inner value by reflecting over the tag type, so it is marked [RequiresUnreferencedCode] and [RequiresDynamicCode], and an application analyzed for trimming or Native AOT gets IL2026/IL3050 warnings where it calls it. Pass the unwrapper explicitly instead, which builds the registration from TagTypeRegistration.FromUnwrapper<TTag, TInner> with no reflection:
opts.Events.RegisterTagType<StudentId, Guid>(x => x.Value, "student")
.ForAggregate<StudentCourseEnrollment>();The table suffix is optional and defaults to the tag type's name.
Tagging Events
Use BuildEvent and WithTag to attach tags before appending:
var enrolled = theSession.Events.BuildEvent(new StudentEnrolled("Alice", "Math"));
enrolled.WithTag(studentId, courseId);
theSession.Events.Append(streamId, enrolled);
await theSession.SaveChangesAsync(TestContext.Current.CancellationToken);Events can have multiple tags of different types. Tags are persisted to their respective tag tables in the same transaction as the event.
Querying Events by Tags
Use EventTagQuery to build a query, then execute it with QueryByTagsAsync:
var query = new EventTagQuery().Or<StudentId>(studentId);
var events = await session2.Events.QueryByTagsAsync(query, TestContext.Current.CancellationToken);Multiple Tags (OR)
var query = new EventTagQuery()
.Or<StudentId>(student1)
.Or<StudentId>(student2);
var events = await session2.Events.QueryByTagsAsync(query, TestContext.Current.CancellationToken);Filtering by Event Type
var query = new EventTagQuery()
.Or<AssignmentSubmitted, StudentId>(studentId);
var events = await session2.Events.QueryByTagsAsync(query, TestContext.Current.CancellationToken);Events are always returned ordered by sequence number (global append order).
Tag Predicates in LINQ (HasTag)
Tag predicates can also compose directly into a LINQ Where() over an event query, alongside ordinary event predicates (timestamp, event type, stream). Use the IEvent.HasTag<TTag>(value) marker method:
var events = await session.Events.QueryAllRawEvents()
.Where(e => e.HasTag<StudentId>(alice))
.ToListAsync(TestContext.Current.CancellationToken);HasTag compiles to the same tag-table subquery that QueryByTagsAsync emits (a correlated seq_id IN (SELECT seq_id FROM pc_event_tag_{suffix} WHERE value = @p) predicate), and under conjoined tenancy it is automatically tenant-scoped. AND-ing several HasTag calls with normal predicates is supported:
var events = await session.Events.QueryAllRawEvents()
.Where(e => e.HasTag<StudentId>(alice)
&& e.HasTag<CourseId>(math)
&& e.Timestamp > cutoff)
.ToListAsync();For OR-across-tags or the richer tag/event-type interplay, keep using the EventTagQuery builder with QueryByTagsAsync — that remains the rich escape hatch. HasTag is a marker method recognized by the LINQ provider; calling it outside a Polecat event query throws NotSupportedException, and using an unregistered tag type throws InvalidOperationException.
Aggregating by Tags
Build an aggregate from tagged events, similar to AggregateStreamAsync but across streams. First define an aggregate that applies the tagged events:
// Aggregate for DCB
public partial class StudentCourseEnrollment
{
public Guid Id { get; set; }
public string StudentName { get; set; } = "";
public string CourseName { get; set; } = "";
public List<string> Assignments { get; set; } = new();
public bool IsDropped { get; set; }
public void Apply(StudentEnrolled e)
{
StudentName = e.StudentName;
CourseName = e.CourseName;
}
public void Apply(AssignmentSubmitted e)
{
Assignments.Add(e.AssignmentName);
}
public void Apply(StudentDropped e)
{
IsDropped = true;
}
}Then aggregate across streams by tag query:
var query = new EventTagQuery()
.Or<StudentId>(studentId)
.Or<CourseId>(courseId);
var aggregate = await session2.Events.AggregateByTagsAsync<StudentCourseEnrollment>(query, TestContext.Current.CancellationToken);Returns null if no matching events are found.
Identity-less Boundary Aggregates
The StudentCourseEnrollment aggregate above carries an Id property, so Polecat's source generator emits its evolver automatically and no extra annotation is needed.
Some DCB aggregates, though, are pure boundary aggregates: they span streams only by tag and have no single-stream identity -- no Id property and no [AggregateIdentity]. For these, mark the aggregate type with [BoundaryAggregate] (from JasperFx.Events.Aggregation) so the source generator emits an evolver for it:
using JasperFx.Events.Aggregation;
[BoundaryAggregate]
public partial class CourseEnrollmentSummary
{
public int EnrolledCount { get; private set; }
public List<string> Students { get; } = new();
public void Apply(StudentEnrolled e)
{
Students.Add(e.StudentName);
EnrolledCount++;
}
public void Apply(StudentDropped e)
{
EnrolledCount--;
}
}Register it exactly like any other DCB aggregate -- the registration API is unchanged:
opts.Events.RegisterTagType<CourseId>("course")
.ForAggregate<CourseEnrollmentSummary>();Why the marker is required. Without a single-stream identity the source generator can't infer a TId, so it intentionally emits nothing. A bare no-Id aggregate is far more often a forgotten Id property than a deliberate boundary aggregate, and silently generating an evolver would mask that mistake -- so [BoundaryAggregate] is the explicit opt-in. Without it, the DCB fetch/aggregate path fails fast at runtime:
InvalidProjectionException: No source-generated dispatcher found
for SingleStreamProjection<CourseEnrollmentSummary, string>(The string type argument is vestigial -- it matches the SingleStreamProjection<T, string> the DCB aggregator builds and is never used by boundary-aggregate dispatch.)
Placement. Put [BoundaryAggregate] on the aggregate type itself, and keep the type partial so the generator can attach the emitted dispatcher. The attribute must sit on the type in its own defining assembly -- that is the compilation the generator emits the [assembly: GeneratedEvolver] into, and it is the assembly the runtime scans (typeof(T).Assembly) when resolving the evolver.
Aggregates with an Id need no marker and keep working unchanged -- [BoundaryAggregate] is only for the identity-less case.
TIP
[BoundaryAggregate] requires JasperFx.Events 2.0.0-alpha.21 / JasperFx.Events.SourceGenerator 2.0.0-alpha.13 or later. The marker is a JasperFx.Events source-generator concern, so its behavior is backend-agnostic -- it works identically across the Critter Stack event stores; only the surrounding RegisterTagType<...>().ForAggregate<T>() registration is Polecat's SQL Server-backed API.
Fetch for Writing (Consistency Boundary)
FetchForWritingByTags loads the aggregate and establishes a consistency boundary. At SaveChangesAsync time, Polecat checks whether any new events matching the query have been appended since the read, throwing DcbConcurrencyException if so:
await using var session2 = theStore.LightweightSession();
var query = new EventTagQuery().Or<StudentId>(studentId);
var boundary = await session2.Events.FetchForWritingByTags<StudentCourseEnrollment>(query, TestContext.Current.CancellationToken);
// Read current state
var aggregate = boundary.Aggregate; // may be null if no events yet
var lastSequence = boundary.LastSeenSequence;
// Append via boundary
var assignment = session2.Events.BuildEvent(new AssignmentSubmitted("HW1", 95));
assignment.WithTag(studentId, courseId);
boundary.AppendOne(assignment);
// Save -- will throw DcbConcurrencyException if another session
// appended matching events after our read
await session2.SaveChangesAsync(TestContext.Current.CancellationToken);Handling Concurrency Violations
public static async Task handling_a_concurrency_violation(IDocumentSession session)
{
try
{
await session.SaveChangesAsync();
}
catch (DcbConcurrencyException violation)
{
// Reload and retry -- the boundary's tag query had new matching events since the read.
// violation.Query is the original tag query, violation.LastSeenSequence the sequence
// the boundary was read at.
Console.WriteLine($"DCB violation on {violation.Query} at {violation.LastSeenSequence}");
}
}TIP
The consistency check only detects events that match the same tag query. Events appended to unrelated tags or streams will not cause a violation.
WARNING
A session can establish more than one consistency boundary. If several of them are violated in the same SaveChangesAsync(), Polecat throws an AggregateException carrying one DcbConcurrencyException per violated boundary rather than a single exception. A session with one boundary -- the common case, and what the sample above shows -- always throws the DcbConcurrencyException directly.
Checking Event Existence
If you only need to know whether any events matching a tag query exist -- without loading or deserializing them -- use EventsExistAsync. This is a lightweight existence check that avoids the overhead of fetching and materializing event data:
[Fact]
public async Task events_exist_returns_true_when_matching_events_found()
{
var studentId = new StudentId(Guid.NewGuid());
var courseId = new CourseId(Guid.NewGuid());
var streamId = Guid.NewGuid();
var enrolled = theSession.Events.BuildEvent(new StudentEnrolled("Alice", "Math"));
enrolled.WithTag(studentId, courseId);
theSession.Events.Append(streamId, enrolled);
await theSession.SaveChangesAsync(TestContext.Current.CancellationToken);
// Check existence -- lightweight, no event loading
await using var session2 = theStore.LightweightSession();
var query = new EventTagQuery().Or<StudentId>(studentId);
var exists = await session2.Events.EventsExistAsync(query, TestContext.Current.CancellationToken);
exists.ShouldBeTrue();
}This is useful for guard clauses and validation logic in DCB workflows where you need to check preconditions before appending new events.
EventsExistAsync is also available in batch queries via batch.EventsExist(query).
How It Works
Storage
Each registered tag type creates a table:
CREATE TABLE [dbo].[pc_event_tag_student] (
value uniqueidentifier NOT NULL,
seq_id bigint NOT NULL,
CONSTRAINT pk_pc_event_tag_student PRIMARY KEY (value, seq_id),
CONSTRAINT fk_pc_event_tag_student_seq_id
FOREIGN KEY (seq_id) REFERENCES [dbo].[pc_events](seq_id) ON DELETE CASCADE
);
CREATE INDEX ix_pc_event_tag_student_seq_id ON [dbo].[pc_event_tag_student] (seq_id);The primary key answers "which events carry this tag". The index answers the opposite question, which every delete from pc_events asks through the foreign key. A store whose tag tables predate it gets it from its next schema migration as a plain CREATE INDEX, which holds a lock on the tag table while it builds. To avoid that on a large table, build it yourself first with ONLINE = ON under the same name and columns, and the migration leaves it alone.
The table's shape follows the event store's:
| Configuration | Difference |
|---|---|
TenancyStyle.Conjoined | tenant_id joins the primary key |
UseTenantPartitionedEvents | Also carries tenant_ordinal, and the foreign key is on (seq_id, tenant_ordinal), because seq_id is numbered per tenant. The tag table itself is not partitioned |
UseArchivedStreamPartitioning | is_archived joins the primary key and there is no foreign key. Polecat deletes the tag rows itself wherever it deletes events |
Archived Events
QueryByTagsAsync, EventsExistAsync, AggregateByTagsAsync and FetchForWritingByTags leave out archived events, as every other event query does. Archiving or un-archiving a stream also moves the version of each tag on its events, so it conflicts with a boundary that read those tags: the boundary's decision was made from a set of events that has since changed. Tombstoning a stream conflicts for the same reason, as described below.
The event store explorer's tag lookup (IEventStore.QueryByTagsAsync) still shows archived events, because it is a diagnostic surface, like its stream reads.
WARNING
Through 5.47.0 these reads included archived events, so an application that relied on that will see fewer events and may see EventsExistAsync answer false.
Consistency Check
Polecat keeps a side table, pc_dcb_tag_version, holding one row per tag boundary with a version that changes on every commit that touches it. FetchForWritingByTags reads the version for every tag the query names; at SaveChangesAsync time, before any events are inserted, Polecat asserts the version has not moved and replaces it with a single UPDATE ... WHERE version = @captured. Concurrent savers queue on that row's exclusive lock, and the loser gets a DcbConcurrencyException.
Every save that appends a tagged event moves the version, boundary or not — so an ordinary session.Events.Append(streamId, taggedEvent) invalidates an in-flight boundary just as another boundary save would.
A save that appends nothing does not assert its boundaries. A boundary is a condition on an append, so a session that read one and then wrote nothing has nothing for it to guard, and moving the version would invalidate other sessions over a save that changed nothing.
Every version comes from one sequence per schema, pc_dcb_tag_version_sequence, so a value never repeats, even on a row that was deleted and created again. That is what lets a tombstone delete rows. The sequence is created by the ordinary schema migration, as long as a tag type is registered, and starts above anything a row could have counted up to. Rows written by earlier versions keep their values until the next commit moves them. Under AutoCreate.None, apply the migration before deploying: with a tag type registered, a tagged append, an archive or a tombstone can't run without the sequence. A login with db_datawriter can draw from it. One with grants on individual tables needs UPDATE on the sequence too.
What a Tombstone Erases
A tag can be personal data, and pc_dcb_tag_version holds the value as text. So TombstoneStream deletes the row of every tag on the stream that no other stream in the tenant carries, in the same transaction as the events, and its tag rows go with the events. Once a tombstone commits, nothing in pc_event_tag_* or pc_dcb_tag_version holds a value that only that stream carried.
- A tag another stream carries keeps its row. The row is shared by value, and the other stream still stores the tag, so deleting it would erase nothing. Its version moves instead. The last stream to go takes it. An archived stream counts as carrying its tags, because its events are still stored.
- A tombstone never creates a row. A tag with no row stays that way. A tag can have events and no row if they were written before this table existed, or tagged later with
AssignTagWhere. - A boundary still can't save through it. A boundary that captured a version finds the row moved or gone. A boundary that read events under a tag with no row has no row to lose against, so its save checks that the events it read are still there and throws
DcbConcurrencyExceptionif they aren't. That check costs one more statement, only on a save whose boundary read events and had a tag with no row. - A concurrent append can't be lost. A tombstone takes the row before it asks whether another stream carries the tag, and a save takes the row before it inserts an event. So the two queue, and the tombstone sees the appended event or the append creates a new row with a new version.
- Tenants are separate. The row is per tenant, and only the tombstoned stream's tenant is considered.
What this doesn't do:
- Archiving isn't erasure.
ArchiveStreamandUnArchiveStreamkeep the events, so they move rows and create missing ones. - Compaction doesn't erase either. It removes events without touching rows, so a tag that only compacted events carried keeps its row, and tombstoning the stream afterwards can't find it. Delete such a row yourself.
CleanAllEventDataAsyncandDeleteAllTenantDataAsynctake the rows too. - It doesn't look outside the tag tables. A value you copied into a projected document or a backup is yours to remove, and the transaction log keeps what it keeps until it is truncated. For a stream you have to keep, use
ApplyEventDataMasking, which doesn't touch tags. - It doesn't cover a rolling upgrade from 5.47.0 while it runs. An older node still counts up from the current value, doesn't delete rows, and can create a row at 1. Finish the rollout before relying on this. A row an older node's tombstone leaves behind stays until you delete it.
Marten differs here
Marten keeps mt_dcb_tag_version rows through a stream delete, and its counters restart if a row is removed. Code moving between the two should not assume a tombstone erases tag values on Marten.
WARNING
Through 5.20.0 this check was an EXISTS query over the tag tables. That is a non-locking predicate read: concurrent savers each ran it before any of them had committed, so none could see the others, and their event inserts did not collide either because a boundary routinely spans distinct streams. Every racer committed. If you rely on DCB boundaries under concurrent load, upgrade — and note that the new table is created by the ordinary schema migration, with no data migration needed.
Tag Routing
Events appended via IEventBoundary.AppendOne() are automatically routed to streams based on their tags. Each tag value becomes the stream identity, so events with the same tag value end up in the same stream.

JasperFx provides formal support for Polecat and other Critter Stack libraries. Please check our