KubeMQ
Client SDKsC#How-to guidesEvents Store

Replay from Sequence

Replay events starting from a specific sequence number

Overview

Replaying from a sequence number lets a consumer resume an events-store subscription from an exact point in a channel's history, instead of re-reading everything or only catching new traffic. It's the checkpoint-recovery pattern: a worker persists the last sequence it processed, and after a crash or redeploy it reopens the subscription right there — no gap, no reprocessing everything that came before.

Sequence numbers are broker-assigned per channel, starting at 1 and increasing monotonically with every stored event; they never reset unless the channel is purged. Setting StartPosition = EventStoreStartPosition.StartAtSequence with StartSequence = 5 tells the broker to begin delivery at that sequence inclusive, replaying stored events from that point, then transitioning the subscription to live delivery for anything published afterward.

Gotchas: the sequence value is inclusive, so StartSequence = 5 still delivers event 5 — off by one and you'll reprocess or silently drop a message; you must track and persist the "last processed" sequence yourself, KubeMQ doesn't checkpoint it for you; and requesting a sequence past the current head isn't an error — you'll just get nothing until new events catch up to it.

Prerequisites

  • KubeMQ server running on localhost:50000
  • C# SDK installed (dotnet add package KubeMQ.SDK.CSharp)

Code

Program.cs
// KubeMQ .NET SDK — Events Store: Replay From Sequence
//
// This example demonstrates replaying events starting from a specific sequence number.
// Useful for resuming consumption from a known checkpoint.
//
// Prerequisites:
//   - KubeMQ server running on localhost:50000
//   - dotnet run

using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.EventsStore;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions
{
    ClientId = "csharp-eventsstore-replay-from-sequence-client",
});
await client.ConnectAsync();

Console.WriteLine("Connected to KubeMQ server");

// Publish some events
for (var i = 1; i <= 10; i++)
{
    await client.SendEventStoreAsync(new EventStoreMessage
    {
        Channel = "csharp-eventsstore.replay-from-sequence",
        Body = Encoding.UTF8.GetBytes($"Event #{i}")
    });
}

Console.WriteLine("Published 10 events. Replaying from sequence 5...");

// Subscribe starting from sequence 5 — skips events 1-4
var cts = new CancellationTokenSource();
var subscribeTask = Task.Run(async () =>
{
    await foreach (var msg in client.SubscribeToEventsStoreAsync(
        new EventStoreSubscription
        {
            Channel = "csharp-eventsstore.replay-from-sequence",
            StartPosition = EventStoreStartPosition.StartAtSequence,
            StartSequence = 5
        }, cts.Token))
    {
        Console.WriteLine($"[Seq={msg.Sequence}] {Encoding.UTF8.GetString(msg.Body.Span)}");
    }
});

await Task.Delay(3000);
cts.Cancel();

Console.WriteLine("Done.");

How It Works

  • StartPosition = StartAtSequence combined with StartSequence = 5 instructs the broker to begin replay at the event whose sequence number is 5. Events with lower sequence numbers are skipped.
  • Sequence numbers are assigned by the broker in monotonically increasing order and are stable across restarts — use them as persistent checkpoints.
  • After replaying from the checkpoint, the subscription continues receiving new events as they arrive, making this pattern suitable for resuming after a consumer restart.
  • The 3-second Task.Delay before cancellation gives the replay time to complete; adjust if your channel has more stored events.

Was this page helpful?

On this page