Catalog My learning Blog
Full-Stack Developer Course: React, .NET and SQL Server · Phase 12 · Lesson 121 · 7 min read

Event Sourcing in .NET: Rebuilding State and Privacy

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: An Append-Only Store, an Aggregate, a Projection and Crypto-Shredding
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Explain event sourcing and when it is justified, implement an append-only event store on SQL Server with optimistic concurrency, build projections, evolve event schemas, and reconcile immutability with the right to erasure using crypto-shredding.

Concept Explanation

Most applications store the current state of things: a row says the order is shipped. Event sourcing stores the facts that happened instead: OrderPlaced, PaymentReceived, OrderShipped. The current state is derived by replaying the events for that entity, called a stream. The idea is old; accounting ledgers have always recorded transactions and computed balances from them, rather than overwriting a balance, and the technique was popularized in software by Greg Young alongside CQRS from Day 63. Its benefits are a complete audit trail that cannot be silently edited, the ability to answer questions about the past, to rebuild new read models from history, and to debug by replaying what happened.

The costs are significant, which is why it should be used selectively. Queries cannot be answered from the event store directly, so you build projections: read models updated by handlers that consume events, stored in ordinary tables shaped for the screens that need them, and eventually consistent with the events. Replaying long streams is slow, so aggregates with many events use snapshots. Events are permanent, so their schema must evolve carefully: new versions of an event type need upcasting, a transformation of old events into the current shape when they are read, and you cannot fix a mistake by editing history, only by appending a compensating event. And the whole team needs to think differently. For most CRUD applications a normal database with an audit table from Day 64 is simpler and enough. Event sourcing earns its place in domains where the history is the product: finance, inventory, compliance, or collaborative workflows.

A relational database can serve as the event store. A table keyed by stream id and version, with a unique constraint on that pair, gives optimistic concurrency: two writers who both read version 7 both try to append version 8, and one fails with a unique violation, which is the same protection as the version columns of Day 37. Appending should specify the expected version, and events in one append are written in one transaction. Publish the new events to consumers through the outbox pattern so projections and other services see them at least once, and make handlers idempotent.

Immutability collides with privacy. If events contain personal data and a person exercises the right to erasure, you cannot delete or edit the events without breaking the log. Crypto-shredding solves it: encrypt personal fields in events with a key specific to the person, keep the keys in a separate store, and to erase the person delete their key, leaving events that can no longer be read. Better still, keep personal data out of events entirely, storing a reference to it in a separate erasable table, and put only business facts in the stream. Events also need authorization on read, since a stream can contain information that different users may not see, and projections must apply the same rules as any other read path.

ConceptPurposeWatch for
EventAn immutable fact in past tenseNaming, schema versioning
StreamThe ordered events of one entityVersion numbers for concurrency
ProjectionA read model built from eventsEventual consistency, rebuild support
SnapshotCached state at a versionOnly needed for long streams
UpcastingConverting old event versions when readNever edit stored events
Crypto-shreddingErasure by deleting a person's keyKeep personal data out of events where possible

Worked Example: An Append-Only Store, an Aggregate, a Projection and Crypto-Shredding

The SQL defines the store, the C# appends with an expected version and rebuilds state, and the last block handles erasure.

SQL
CREATE TABLE dbo.Events (
    GlobalPosition bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,   -- total order, used by projections and the outbox reader
    StreamId       uniqueidentifier NOT NULL,
    Version        int NOT NULL,
    EventType      nvarchar(200) NOT NULL,
    SchemaVersion  smallint NOT NULL DEFAULT 1,
    Payload        nvarchar(max) NOT NULL,                      -- JSON
    OccurredAtUtc  datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),
    CONSTRAINT UX_Events_Stream_Version UNIQUE (StreamId, Version)   -- two writers cannot both append version N
);
C#
public interface IEvent;
public sealed record OrderPlaced(Guid OrderId, string CustomerRef, decimal Total, string Currency) : IEvent;   // Currency added in schema version 2
public sealed record OrderShipped(Guid OrderId, string TrackingNumber) : IEvent;

// The aggregate is rebuilt by applying its events in order. Deciding never mutates storage directly.
public sealed class Order
{
    public Guid Id { get; private set; }
    public string Status { get; private set; } = "new";
    public int Version { get; private set; }

    public static Order Rehydrate(IEnumerable<IEvent> history)
    {
        var order = new Order();
        foreach (var e in history) { order.Apply(e); order.Version++; }
        return order;
    }
    private void Apply(IEvent e) { switch (e) { case OrderPlaced p: Id = p.OrderId; Status = "placed"; break; case OrderShipped: Status = "shipped"; break; } }

    public IEvent[] Ship(string tracking) =>
        Status == "placed" ? [new OrderShipped(Id, tracking)] : throw new DomainException("Only placed orders can be shipped.");
}

public sealed class EventStore(AppDb db)
{
    public async Task AppendAsync(Guid streamId, int expectedVersion, IEnumerable<IEvent> events, CancellationToken ct)
    {
        var version = expectedVersion;
        foreach (var e in events)
            db.Events.Add(new EventRow { StreamId = streamId, Version = ++version, EventType = e.GetType().Name, Payload = JsonSerializer.Serialize(e, e.GetType()) });
        db.Outbox.Add(OutboxMessage.ForEvents(streamId, events));         // same transaction: publish at least once
        try { await db.SaveChangesAsync(ct); }
        catch (DbUpdateException ex) when (ex.IsUniqueViolation()) { throw new ConcurrencyException(streamId, expectedVersion); }
    }
}
TypeScript
// A projection handler updates an ordinary table for screens. It is idempotent, so replays and redelivery are safe.
public async Task HandleAsync(EventRow row, CancellationToken ct)
{
    if (row.EventType == nameof(OrderShipped))
    {
        var e = JsonSerializer.Deserialize<OrderShipped>(row.Payload)!;
        await db.OrderSummaries.Where(o => o.OrderId == e.OrderId && o.LastPosition < row.GlobalPosition)   // skip already-applied events
            .ExecuteUpdateAsync(s => s.SetProperty(o => o.Status, "shipped").SetProperty(o => o.LastPosition, row.GlobalPosition), ct);
    }
}

// Upcasting: an old OrderPlaced (SchemaVersion 1) had no currency. Convert when reading, never rewrite history.
static OrderPlaced Upcast(string json, short schemaVersion) => schemaVersion == 1
    ? JsonSerializer.Deserialize<OrderPlacedV1>(json)!.ToCurrent(currency: "USD")
    : JsonSerializer.Deserialize<OrderPlaced>(json)!;
C#
// Crypto-shredding: personal data is encrypted per person, the key lives elsewhere, and erasure deletes the key.
public sealed record CustomerRegistered(Guid CustomerId, string EncryptedName);   // ciphertext only

public async Task EraseAsync(Guid customerId, CancellationToken ct)
{
    await db.PersonKeys.Where(k => k.CustomerId == customerId).ExecuteDeleteAsync(ct);   // events remain, the name is unreadable forever
    await keyVault.PurgeWrappingKeyForAsync(customerId, ct);
}
// Preferred where possible: events hold only CustomerId. Name and email live in a normal table that supports deletion.

The projection's position check makes replays harmless, and the stream version check makes lost updates impossible. Both are cheap to build early and painful to add later.

A command loads the stream, replays events to rebuild state, decides new events, and appends them with the expected version so a unique constraint rejects concurrent writers. The outbox feeds projections, old event versions are upcast when read, and erasure is done by deleting a per-person key.
A command loads the stream, replays events to rebuild state, decides new events, and appends them with the expected version so a unique constraint rejects concurrent writers. The outbox feeds projections, old event versions are upcast when read, and erasure is done by deleting a per-person key.

Real-World Examples

  • Double-entry bookkeeping, described in Luca Pacioli's 1494 mathematics text and used by merchants before that, is the oldest widely used event-sourced system: ledgers append entries and never erase them, and a mistake is corrected with a new entry. The idea maps directly to financial software, where audit trails and reproducible balances are legal requirements.
  • Greg Young developed and taught event sourcing together with CQRS during the late 2000s, and built an event store database around the idea. His guidance is that it fits domains where history matters and is over-engineering for simple CRUD, which is a useful test before adopting it.
Key TakeawaysEvent sourcing stores immutable facts and derives current state by replaying them, giving you audit, history and rebuildable read models. The cost is projections, versioning and eventual consistency. Use it only where history is central, protect streams with a unique stream and version constraint, publish through the outbox, make projections idempotent, and keep personal data out of events or crypto-shred it.

Review Questions & Answers

Two writers that load the same version will both try to append the next version number, and the constraint lets only one succeed. The other receives a conflict and must reload and retry, so no update is lost.

The log is an immutable record and projections and other systems have already consumed it, so rewriting it would break consistency and destroy the audit trail. A compensating event states the correction and the history remains truthful.

Keep personal data out of events, or encrypt it with a key per person and delete the key on erasure, so the remaining events cannot be read. Events keep their business facts, but the personal data is gone.

In most CRUD applications where the current state is what matters and a change history is only occasionally needed. It is far simpler to build and query, and it avoids the complexity of projections, upcasting, and replay.