Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

NATS $G to Custom Account Migration: Stream Paths, Consumer Models & Checkpoint Resumption

This article details how to migrate NATS JetStream streams from the default $G account to custom accounts, covering cross-account import/export rules, three migration strategies (backup/restore, Leaf Node mirroring, dual-write), consumer models for competing vs independent consumption, and how durable consumer checkpoints enable seamless resumption after migration.

1. Three Core Rules Before Migrating Out of $G

Before changing configuration, three mechanisms must be aligned:

Bidirectional export/import pairing : To access subjects in another account, the provider must declare exports (type stream for pub/sub, service for request/reply) and the receiver must declare matching imports. Optional prefix or to can remap local prefixes to avoid naming conflicts.

Custom accounts cannot import $G streams : $G is an implicit global domain created when multi-account is disabled. The server forbids declaring $G inside an accounts block, so no exports rules can be written for it. Consequently, a custom account cannot directly import ordinary subjects or JetStream streams from $G.

Client SDKs do not specify accounts in code : Clients only supply user-level credentials. The server maps the connection to an account automatically — via accounts.<NAME>.users in static nats.conf mode, or via the Account claim embedded in the User JWT ( .creds) in Operator JWT mode.

Because custom accounts cannot import $G streams, the usual “new account + export/import + mirror” pattern cannot pull data directly from $G, necessitating dedicated migration paths.

2. Three Migration Paths for JetStream Streams from $G

JetStream stores data in per-account directories on disk; a direct filesystem copy is not possible. Choose a path based on downtime tolerance and code-change cost:

Comparison table of three migration schemes: backup/restore, Leaf Node bridge + Mirror, dual-write.
Comparison table of three migration schemes: backup/restore, Leaf Node bridge + Mirror, dual-write.

Scheme 1: nats stream backup + restore (simplest, allows brief write pause)

Pause producers on $G, let consumers drain in-flight messages.

Snapshot backup using the $G connection context: nats stream backup ORDERS ./orders_backup Update server config to enable explicit custom account (e.g., ORDERS_ACC) with jetstream: enabled, then restart or reload.

Switch CLI to the target account’s user context and restore: nats stream restore ./orders_backup Rotate producer/consumer credentials to the new account and resume traffic.

Scheme 2: Leaf Node Bridge + JetStream Mirror (zero downtime, no producer code changes)

Leaf Node’s remotes[].account binds to the new cluster’s local custom account, while its connection to the old cluster (port 7422) uses a regular user credential from the old cluster’s $G. This bridges the old $G directly into the new custom account.

Stand up a new cluster (or transit Leaf node) with the custom account and independent JetStream domain. In leafnodes.remotes point to the old cluster and bind to the local target account (e.g., ORDERS_ACC).

In the new cluster’s ORDERS_ACC account, create a same-named Stream with mirror enabled, sourcing from the original stream in old $G.

Mirror automatically syncs all historical messages and live increments, preserving original sequence numbers and timestamps.

Verify mirror lag reaches 0 via nats stream info. Then cut over consumers to the new account. Promote the mirror to a standalone writable stream (NATS 2.12+ supports nats stream edit --remove-mirror with business subjects; older versions create a regular stream sourced from the mirror then remove the source). Finally, switch producers to the new account and decommission the old cluster and Leaf bridge.

Scheme 3: Producer Dual-Write + Consumer Drain-Then-Cutover

Producers write to both old $G and new account simultaneously (new account initially write-only).

Consumers keep reading from $G until the pre-dual-write backlog is fully drained.

Once the old stream catches up to the dual-write start point, move consumers to the new account, then stop writing to $G.

Caveat : Pure dual-write does not automatically carry historical messages, and both streams maintain independent Sequence numbers. Consumers must implement idempotent deduplication at cutover.

3. How Subscribers Achieve Seamless Resumption During Migration and Runtime

Resumption Relies on Server-Side Durable Consumer State Machine

Core NATS subscriptions have no progress persistence; messages during disconnect are lost. JetStream’s continuous consumption depends on a server-maintained Durable Consumer .

When a consumer is created with a durable_name, the server persistently tracks two watermarks: delivered position ( delivered ) and acknowledged floor ( ack_floor ) .

After the client processes a message and calls msg.Ack(), ack_floor advances. If the client crashes or exceeds AckWait without acknowledging, the server automatically redelivers.

Therefore, a client restart requires no local offset storage. Simply rebind to the same durable_name and the server resumes delivery from after ack_floor (unacknowledged in-flight messages + new messages accumulated during downtime).

Can One Consumer Serve Multiple Clients?

Yes, but the server maintains only a single progress cursor, not per-client cursors. The choice between sharing or splitting depends entirely on your business semantics. The diagram below illustrates the two models:

Two consumer connection models: competing consumption (shared pull consumer) vs independent broadcast (separate consumers).
Two consumer connection models: competing consumption (shared pull consumer) vs independent broadcast (separate consumers).

Model A (Competing Consumption / Horizontal Scaling) → Share 1 Consumer : Multiple replicas of the same service (e.g., 3 order workers) should share a single Durable Pull Consumer and fetch concurrently. JetStream automatically shards messages across instances; if one instance crashes without acking, messages are redelivered to surviving instances. With legacy Push Consumers, use the same deliver_group (Queue Group) to achieve competing consumption.

Model B (Independent Broadcast / Multiple Downstreams) → 1 Consumer per Service : If an order persistence service and a risk audit service both need every order message, they must each create their own Durable Consumer (e.g., billing and audit). Otherwise, when the audit service pulls and acks, the cursor advances and the persistence service permanently misses that batch.

4. After Cross-Account Restore, Can a New User Resume from the Same Checkpoint?

When executing “Scheme 1 (backup/restore)”, a common concern: after restoring the $G stream and its consumers to a new custom account, can a brand-new user under that account pick up exactly where the old consumer left off?

Answer: Yes, seamless resumption works. The reason follows a four-step chain:

Cross-account backup/restore checkpoint resumption flow: progress bound to stream and consumer name, not user identity.
Cross-account backup/restore checkpoint resumption flow: progress bound to stream and consumer name, not user identity.

Consumer cursor follows the stream, not the user : JetStream stores ack_floor and delivered inside the stream’s consumer object, keyed by Stream name + durable_name. The original connecting username is irrelevant.

nats stream backup includes consumer state by default : The generated backup.json contains not only stream config and message archives, but also all durable consumer definitions and their runtime watermarks ( ack_floor , delivered ) at backup time . On restore, the new account’s stream is recreated with original sequence numbers and each consumer’s exact checkpoint — no replay from message 1.

Three practical guardrails to avoid pitfalls during restore:

Do not add --no-consumers during backup : Only an explicit --no-consumers flag drops consumer definitions and checkpoints. Default (or explicit --consumers) preserves them fully.

Target account must not have a same-named stream pre-created : nats stream restore errors out if the stream already exists.

Verify new user permissions and push delivery subjects : Ensure the new account’s consumer user has publish/subscribe permissions on the business subjects and required JetStream API scopes. For push consumers, confirm the new client’s deliver_subject matches the restored consumer’s configuration.

Viewing cross-account boundaries, migration paths, and consumer checkpoint mechanics together shows that moving from $G to custom accounts is not fragile: allow a brief pause → use backup/restore for one-step migration preserving all messages and checkpoints; require zero downtime → use Leaf Node bridge + mirror for online catch-up; wire consumers following “share pull consumer for same-group scaling, create independent consumers for distinct downstreams” with durable_name, and the entire migration lands smoothly.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

mirrorbackup restoremulti-accountNATSJetStreamcheckpoint resumptiondurable consumerLeaf Nodestream migration

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion