When a stream is configured with a source or mirror, it will automatically and asynchronously replicate messages from the origin stream.
source or mirror are designed to be robust and will recover from a loss of connection. They are suitable for geographic distribution over high latency and unreliable connections. E.g. even a leaf node starting and connecting intermittently every few days will still receive or send messages over the source/mirror link.
Another use case is when connecting streams cross-account.
There are several options available when declaring the configuration.
Name - Name of the origin stream to source messages from.StartSeq - An optional start sequence of the origin stream to start mirroring from.StartTime - An optional message start time to start mirroring from. Any messages that are equal to or greater than the start time will be included.FilterSubject - An optional filter subject which will include only messages that match the subject, typically including a wildcard. Note, this cannot be used with SubjectTransforms.SubjectTransforms - An optional set of subject transforms to apply when sourcing messages from the origin stream. Note, in this context, the Source will act as a filter on the origin stream and the Destination can optionally be provided to apply a transform. Since multiple subject transforms can be used, disjoint subjects can be sourced from the origin stream while maintaining the order of the messages. Note, this cannot be used with FilterSubject.Domain - An optional JetStream domain of where the origin stream exists. This is commonly used in a hub cluster and leafnode topology.The stream using a source or mirror configuration can have its own retention policy, replication, and storage type.
{% hint style="info" %}
source or mirror agreement.
{% endhint %}{% hint style="info" %}
Sources is a generalization of the Mirror and allows for sourcing data from one or more streams concurrently.
If you require the target stream to act as a read-only replica:
API prefixA stream defining Sources is a generalized replication mechanism and allows for sourcing data from one or more streams concurrently. A stream with sources can still act as a regular stream allowing direct write/publish by local clients to the stream. Essentially the source streams and local client writes are aggregated into a single interleaved stream.
Combined with subject transformation and filtering sourcing allows to design sophisticated data distribution architectures.
{% hint style="info" %} Sourcing messages does not retain sequence numbers. But it retain the in stream sequence of messages . Between streams sourced to the same target, the sequence of messages is undefined. {% endhint %}
A mirror can source its messages from exactly one stream and a clients can not directly write to the mirror. Although messages cannot be published to a mirror directly by clients, messages can be deleted on-demand (beyond the retention policy), and consumers have all capabilities available on regular streams.
{% hint style="info" %}
MirrorDirect stream attribute. See: https://docs.nats.io/nats-concepts/jetstream/streams#configuration
{% endhint %}Source and mirror works with origin stream with workqueue retention in a limited context. The source/mirror will act as a consumer removing messages from the origin stream.
The implementation is not resilient when connecting over intermittent leaf node connections though. Within a cluster where the target stream (with the source/mirror agreement) it will generally work well.
{% hint style="warning" %} Source and mirror for workqueue based streams is only partially supported. It is not resilient against connection loss over leaf nodes.
The consumer pulling message from a remote stream is not durable and other clients may be able to consume and remove messages from the workqueue while leaf connection is down. {% endhint %}
{% hint style="warning" %} If you try to create additional (conflicting) consumers on the origin workqueue stream the behavior becomes undefined. A workqueue allows only one consumer per subject. If the source/mirror connection is active local clients trying to create additional consumers will fail. In reverse a source/mirror cannot be created when there is already a local consumer for the same subjects. {% endhint %}
{% hint style="warning" %} Source and mirror for interest based streams is not supported. Jetstream does not forbid this configuration but the behavior is undefined and may change in the future. {% endhint %}