Skip to content

Bootstrap

When you subscribe with bootstrap: true, the server sends a complete snapshot of all existing entities before transitioning to live notifications. This ensures the subscriber starts from a consistent and complete view.

The server uses a subscribe-before-scan approach:

  1. The subscription is registered for live notifications.
  2. The server sends BOOTSTRAP_BEGIN (yielded as a BootstrapBegin notification), with an approximate entity count.
  3. The server scans every partition a page at a time and sends the entities as Bootstrap notifications.
  4. When every partition has finished, the server sends BOOTSTRAP_END (yielded as BootstrapComplete).

Because the subscription is registered before the scan begins:

  • No gaps. Every entity that exists at subscribe time, or is created/updated after, is delivered at least once. You will never miss a change.
  • No entity count limit. The server reads the snapshot a page at a time, only as fast as the subscriber consumes it, so kinds with millions of entities bootstrap correctly regardless of buffer sizes. If a bootstrap consumes nothing for bootstrap_progress_timeout_secs (default 30), the connection is closed.
  • No Bootstrap notification follows BootstrapComplete. Every snapshot entity arrives before it, however long the bootstrap takes.
  • Duplicates are possible. An entity that changes during the scan window may arrive both as a Bootstrap notification and as a live Created or Updated notification. This is by design. Gaps would mean stale data, while duplicates are easily resolved.

A server runs a limited number of bootstraps of one kind at once (max_concurrent_bootstraps_per_kind, default 2). A bootstrap that waits for a slot already receives live notifications.

Notification interleaving during bootstrap

Section titled “Notification interleaving during bootstrap”

Between BootstrapBegin and BootstrapComplete, you may receive Bootstrap and live Created, Updated and Deleted notifications interleaved. Do not assume that all Bootstrap notifications arrive before any live notifications.

For any one entity, the server never sends a Bootstrap row after a later change to that entity. The client rules below don’t rely on this; they hold on their own.

Every notification carries a Version that is monotonically increasing per entity. A Deleted carries the version of the deletion. During a bootstrap, the merge rule is highest version wins, regardless of notification type or arrival order, and deletes count:

  • A Bootstrap for entity X at version 3, then a live Updated for X at version 4: apply version 4.
  • A live Updated for X at version 4, then a Bootstrap for X at version 3: ignore the bootstrap row (you already have newer data).
  • A live Deleted for X at version 5, then a Bootstrap for X at version 5 or lower: ignore the bootstrap row. X stays deleted. Removing X is not enough; remember the delete’s version (a tombstone) until the bootstrap completes.

Outside a bootstrap, apply changes in arrival order. Versions restart when an in-memory server restarts, so comparing a live change with a version from before a reconnect can wrongly discard it.

EntityMirror<TKey, T> implements these rules. Feed it every change from one loop and read Entries from anywhere:

var mirror = new EntityMirror<string, Player>(id => Convert.ToHexString(id.Span));
await foreach (var change in players.SubscribeAsync(
bootstrap: true, reconnectMode: ReconnectBootstrapMode.Full, ct: ct))
{
mirror.Apply(change);
if (change.Type == NotificationType.BootstrapComplete)
SignalReady(); // mirror.Entries now holds the whole kind
}

The mirror collects each bootstrap, re-bootstraps included, into a fresh set: snapshot rows and live changes keep the higher version, and deletes leave tombstones. At BootstrapComplete it swaps that set in whole, so entities deleted while you were disconnected disappear. Live changes are also applied to Entries as they arrive. See Building correct state for reacting to changes and for writing to a database.

The simplest way to detect bootstrap completion is the BootstrapComplete sentinel notification. It is yielded in the same stream, immediately after the last bootstrap entity. Its counterpart BootstrapBegin is yielded before the first. Both have Entity = null and zero-valued fields, and both are also emitted for re-bootstrap cycles (e.g. after reconnection with ReconnectBootstrapMode.Full).

If you are using SubscriptionHandle directly, the BootstrapStatus property also tracks progress:

NotRequested -> InProgress -> Complete, then RebootstrapInProgress -> RebootstrapComplete for each re-bootstrap.

sub.BootstrapStatusChanged += status =>
{
if (status is BootstrapState.Complete or BootstrapState.RebootstrapComplete)
Console.WriteLine("Bootstrap complete.");
};

Important: Do not use BootstrapStatus to filter notifications. Never drop live notifications while a bootstrap is in progress. The status is informational: it tells you when you have a complete snapshot.

If you need a point-in-time snapshot without maintaining a live subscription, use BootstrapSnapshotAsync:

IReadOnlyList<Player> snapshot = await players.BootstrapSnapshotAsync();

This subscribes with bootstrap: true, feeds every notification into an EntityMirror as the snapshot streams in, and unsubscribes once bootstrap completes. Live updates and deletes that arrive during the bootstrap are applied, so the result is current as of BootstrapComplete. Because it drains continuously, it works for kinds of any size and is not limited by SubscriberBufferSize. It is useful for periodic reconciliation tasks.

  1. Apply version-wins during a bootstrap. Bootstrap, Created, and Updated all carry entity data and a version. Highest version wins, regardless of notification type or arrival order.
  2. Never ignore live notifications during bootstrap. They may carry newer state than the scan.
  3. Treat Bootstrap and Created identically for merge purposes. Both mean “here is the current state of this entity.”
  4. Deleted removes the entity, and during a bootstrap leaves a tombstone at its version. Ignore any later Bootstrap row for that entity at or below that version. Discard the tombstones when the bootstrap completes.
  5. Replace, don’t merge, at BootstrapComplete. The bootstrap’s set is the whole kind: an entity you held that it lacks was deleted while you were disconnected.

EntityMirror follows all five.