Skip to content

Cratis Chronicle reducer event filtering

Cratis Chronicle reducer filters select which events can update a read model by matching stored event metadata. Use them when the same event type serves several views, but one view should include only a tagged subset or a particular source or stream type.

Place filter attributes on a .NET reducer class, or on the model for a self-reducing read model. Events must match both the reducer’s observed event types and its metadata filters before they reach its transition methods. A nonmatching event is skipped for that reducer, not removed from the event store.

Attribute Filters by Matches when
[FilterEventsByTag("...")] Appended or static event tag Any filter tag matches any tag on the appended event
[EventSourceType("...")] Event source type set at append time The event source type matches exactly
[EventStreamType("...")] Event stream type set at append time The event stream type matches exactly

An event source type categorizes the source, such as customer; a stream type categorizes the stream, such as fulfillment. These are types, not individual source or stream IDs. Set the metadata when appending the event:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
[EventType]
public record ReducersFilteringOrderPlaced(decimal TotalAmount);
public class ReducersFilteringMetadataExampleService(IEventLog eventLog)
{
public Task PlaceOrder(decimal totalAmount) =>
eventLog.Append(
EventSourceId.New(),
new ReducersFilteringOrderPlaced(totalAmount),
tags: ["priority"],
eventSourceType: "order",
eventStreamType: "fulfillment");
}

View C# snippet source on GitHub

Append an event with a tag and place [FilterEventsByTag] on the reducer to accumulate state only from tagged events.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringByTagOrderPlaced(decimal TotalAmount);
public record ReducersFilteringPriorityOrderTotals(int Count, decimal Total);
public class ReducersFilteringByTagOrderService(IEventLog eventLog)
{
public Task PlacePriorityOrder(decimal totalAmount) =>
eventLog.Append(
EventSourceId.New(),
new ReducersFilteringByTagOrderPlaced(totalAmount),
tags: ["priority"]);
}
[FilterEventsByTag("priority")]
public class ReducersFilteringPriorityOrderTotalsReducer : IReducerFor<ReducersFilteringPriorityOrderTotals>
{
public ReducersFilteringPriorityOrderTotals Placed(ReducersFilteringByTagOrderPlaced @event, ReducersFilteringPriorityOrderTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);
}

View C# snippet source on GitHub

In this example, only orders whose appended events carry the priority tag update the totals. An order event without that tag leaves this read model unchanged.

Multiple [FilterEventsByTag] attributes widen the match. The reducer receives the event if the appended event has any of the configured tags:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringMultiTagOrderPlaced(decimal TotalAmount);
public record ReducersFilteringFastTrackOrderTotals(int Count);
[FilterEventsByTag("priority")]
[FilterEventsByTag("express")]
public class ReducersFilteringFastTrackOrderTotalsReducer : IReducerFor<ReducersFilteringFastTrackOrderTotals>
{
public ReducersFilteringFastTrackOrderTotals Placed(ReducersFilteringMultiTagOrderPlaced @event, ReducersFilteringFastTrackOrderTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1);
}

View C# snippet source on GitHub

Place [EventSourceType] on the reducer to accumulate state only from events appended with a matching eventSourceType:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringInvoiceIssued(decimal Amount);
public record ReducersFilteringCustomerInvoiceTotal(decimal Amount);
public class ReducersFilteringInvoicingService(IEventLog eventLog)
{
public Task IssueCustomerInvoice(decimal amount) =>
eventLog.Append(
EventSourceId.New(),
new ReducersFilteringInvoiceIssued(amount),
eventSourceType: "customer");
}
[EventSourceType("customer")]
public class ReducersFilteringCustomerInvoiceTotalReducer : IReducerFor<ReducersFilteringCustomerInvoiceTotal>
{
public ReducersFilteringCustomerInvoiceTotal Issued(ReducersFilteringInvoiceIssued @event, ReducersFilteringCustomerInvoiceTotal? current, EventContext context) =>
new((current?.Amount ?? 0m) + @event.Amount);
}

View C# snippet source on GitHub

If the same event is appended with eventSourceType: "supplier", this reducer does not receive it.

Place [EventStreamType] on the reducer to accumulate state only from events appended to a matching stream type:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringShipmentSent(decimal ShippingCost);
public record ReducersFilteringShippingTotals(int Count, decimal TotalCost);
public class ReducersFilteringShippingService(IEventLog eventLog)
{
public Task Send(decimal shippingCost) =>
eventLog.Append(
EventSourceId.New(),
new ReducersFilteringShipmentSent(shippingCost),
eventStreamType: "shipping");
}
[EventStreamType("shipping")]
public class ReducersFilteringShippingTotalsReducer : IReducerFor<ReducersFilteringShippingTotals>
{
public ReducersFilteringShippingTotals Sent(ReducersFilteringShipmentSent @event, ReducersFilteringShippingTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1, (current?.TotalCost ?? 0m) + @event.ShippingCost);
}

View C# snippet source on GitHub

You can combine [FilterEventsByTag], [EventSourceType], and [EventStreamType] on the same reducer. Chronicle requires all filter categories to match:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringCombineOrderPlaced(decimal TotalAmount);
public record ReducersFilteringPremiumFulfillmentTotals(int Count, decimal Total);
public class ReducersFilteringCombineOrderService(IEventLog eventLog)
{
public Task PlacePremiumOrder(decimal totalAmount) =>
eventLog.Append(
EventSourceId.New(),
new ReducersFilteringCombineOrderPlaced(totalAmount),
tags: ["premium"],
eventSourceType: "order",
eventStreamType: "fulfillment");
}
[FilterEventsByTag("premium")]
[EventSourceType("order")]
[EventStreamType("fulfillment")]
public class ReducersFilteringPremiumFulfillmentTotalsReducer : IReducerFor<ReducersFilteringPremiumFulfillmentTotals>
{
public ReducersFilteringPremiumFulfillmentTotals Placed(ReducersFilteringCombineOrderPlaced @event, ReducersFilteringPremiumFulfillmentTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);
}

View C# snippet source on GitHub

The reducer only receives events that match all three: the premium tag, the order event source type, and the fulfillment stream type. Multiple tags are alternatives within the tag category; they do not relax the source or stream requirements.

Without a source filter, Unspecified matches any source type. Without a stream filter, All matches any stream type. An explicit Default source filter matches only the default source type, not every source. Subscription filtering by an individual stream ID is not supported.

The .NET client applies the same matching rules to passive reads and reducer scenarios for separate and self-reducing models, using stored or seeded metadata. When a collection read has an event-count limit, it selects that many events of the observed types before applying metadata filters. It does not keep reading until that many matching events have been accepted.

Inbox forwarding preserves tags and route metadata. Older inbox events affected by the tag-loss issue (#4823) remain untagged and cannot match a tag filter. See external-store route filters before relying on tags in an existing inbox history.

[Tag] and [Tags] label the reducer itself, not the events it accepts. Use [FilterEventsByTag] when delivery should depend on event tags:

using Cratis.Chronicle;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
[EventType]
public record ReducersFilteringTagVsFilterOrderPlaced(decimal TotalAmount);
public record ReducersFilteringTagVsFilterTotals(int Count, decimal Total);
// These labels appear on the reducer definition — they do not affect dispatch
[Tag("reporting")]
[Tag("premium")]
public class ReducersFilteringLabeledFulfillmentTotalsReducer : IReducerFor<ReducersFilteringTagVsFilterTotals>
{
public ReducersFilteringTagVsFilterTotals Placed(ReducersFilteringTagVsFilterOrderPlaced @event, ReducersFilteringTagVsFilterTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);
}
// These filter which events are dispatched to the reducer
[FilterEventsByTag("premium")]
[EventSourceType("order")]
public class ReducersFilteringFilteredFulfillmentTotalsReducer : IReducerFor<ReducersFilteringTagVsFilterTotals>
{
public ReducersFilteringTagVsFilterTotals Placed(ReducersFilteringTagVsFilterOrderPlaced @event, ReducersFilteringTagVsFilterTotals? current, EventContext context) =>
new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);
}

View C# snippet source on GitHub

If you only need to describe or group reducers, continue with tagging reducers. To choose which sequence supplies their events, use event sequence selection.