import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
You must enable geo-replication on a per-tenant basis in Pulsar. For example, you can enable geo-replication between two specific clusters only when a tenant has access to both clusters.
Geo-replication is managed at the namespace level, which means you only need to create and configure a namespace to replicate messages between two or more provisioned clusters that a tenant can access.
Complete the following tasks to enable geo-replication for a namespace:
Any message published on any topic in that namespace is replicated to all clusters in the specified set.
When messages are produced on a Pulsar topic, messages are first persisted in the local cluster, and then forwarded asynchronously to the remote clusters.
In normal cases, when connectivity issues are none, messages are replicated immediately, at the same time as they are dispatched to local consumers. Typically, the network round-trip time (RTT) between the remote regions defines end-to-end delivery latency.
Applications can create producers and consumers in any of the clusters, even when the remote clusters are not reachable (like during a network partition).
Producers and consumers can publish messages to and consume messages from any cluster in a Pulsar instance. However, subscriptions cannot only be local to the cluster where the subscriptions are created but also can be transferred between clusters after the replicated subscription is enabled. Once the replicated subscription is enabled, you can keep the subscription state in synchronization. Therefore, a topic can be asynchronously replicated across multiple geographical regions. In case of failover, a consumer can restart consuming messages from the failure point in a different cluster.
In the aforementioned example, the T1 topic is replicated among three clusters, Cluster-A, Cluster-B, and Cluster-C.
All messages produced in any of the three clusters are delivered to all subscriptions in other clusters. In this case, C1 and C2 consumers receive all messages that P1, P2, and P3 producers publish. Ordering is still guaranteed on a per-producer basis.
This section guides you through the steps to configure geo-replicated clusters.
To replicate data among clusters, you need to configure each cluster to connect to the other. You can use the pulsar-admin
tool to create a connection.
Example
Suppose that you have 3 replication clusters: us-west
, us-cent
, and us-east
.
Configure the connection from us-west
to us-east
.
Run the following command on us-west
.
bin/pulsar-admin clusters create \ --broker-url pulsar://<DNS-OF-US-EAST>:<PORT> \ --url http://<DNS-OF-US-EAST>:<PORT> \ us-east
:::tip
--broker-url-secure
and --url-secure
. For more information, see pulsar-admin clusters create.--auth-plugin
and --auth-parameters
together to set cluster authentication, which overrides brokerClientAuthenticationPlugin
and brokerClientAuthenticationParameters
if authenticationEnabled
sets to true
in broker.conf
and standalone.conf
. For more information, see authentication and authorization.:::
Configure the connection from us-west
to us-cent
.
Run the following command on us-west
.
bin/pulsar-admin clusters create \ --broker-url pulsar://<DNS-OF-US-CENT>:<PORT> \ --url http://<DNS-OF-US-CENT>:<PORT> \ us-cent
Run similar commands on us-east
and us-cent
to create connections among clusters.
To replicate to a cluster, the tenant needs permission to use that cluster. You can grant permission to the tenant when you create the tenant or grant it later.
Specify all the intended clusters when you create a tenant:
bin/pulsar-admin tenants create my-tenant \ --admin-roles my-admin-role \ --allowed-clusters us-west,us-east,us-cent
To update permissions of an existing tenant, use update
instead of create
.
You can enable geo-replication at namespace or topic level.
You can create a namespace with the following command sample.
bin/pulsar-admin namespaces create my-tenant/my-namespace
Initially, the namespace is not assigned to any cluster. You can assign the namespace to clusters using the set-clusters
subcommand:
bin/pulsar-admin namespaces set-clusters my-tenant/my-namespace \ --clusters us-west,us-east,us-cent
You can set geo-replication at topic level using the command pulsar-admin topics set-replication-clusters
. For the latest and complete information about Pulsar admin
, including commands, flags, descriptions, and more information, see Pulsar admin doc.
bin/pulsar-admin topics set-replication-clusters --clusters us-west,us-east,us-cent my-tenant/my-namespace/my-topic
:::tip
serviceUrl
for the local cluster.2.10.x
, to enable geo-replication at topic level, you need to change the following configurations in the conf/broker.conf
or conf/standalone.conf
file to enable topic policies service.systemTopicEnabled=true topicLevelPoliciesEnabled=true
:::
By default, messages are replicated to all clusters configured for the namespace. You can restrict replication selectively by specifying a replication list for a message, and then that message is replicated only to the subset in the replication list.
The following is an example of the Java API. Note the use of the replicationClusters
method when you construct the Message object:
List<String> restrictReplicationTo = Arrays.asList( "us-west", "us-east" ); Producer producer = client.newProducer() .topic("some-topic") .create(); producer.newMessage() .value("my-payload".getBytes()) .replicationClusters(restrictReplicationTo) .send();
You can check topic-specific statistics for geo-replication topics using one of the following methods.
<Tabs groupId="api-choice" defaultValue="pulsar-admin" values={[{"label":"pulsar-admin","value":"pulsar-admin"},{"label":"REST API","value":"REST API"}]}> <TabItem value="pulsar-admin"> Use the [`pulsar-admin topics stats`](pathname:///reference/#/@pulsar:version_reference@/pulsar-admin/topics?id=stats) command. ```shell bin/pulsar-admin topics stats persistent://my-tenant/my-namespace/my-topic ``` </TabItem> <TabItem value="REST API"> {@inject: endpoint|GET|/admin/v2/:schema/:tenant/:namespace/:topic/stats|operation/getStats?version=@pulsar:version_number@} </TabItem> </Tabs>
Each cluster reports its own local stats, including the incoming and outgoing replication rates and backlogs.
Given that geo-replication topics exist in multiple regions, directly deleting a geo-replication topic is not possible. Instead, you should rely on automatic topic garbage collection.
In Pulsar, a topic is automatically deleted when the topic meets the following three conditions:
You can explicitly disable topic garbage collection by setting brokerDeleteInactiveTopicsEnabled
to false
in your broker configuration.
To delete a geo-replication topic, close all producers and consumers on the topic, and delete all of its local subscriptions in every replication cluster. When Pulsar determines that no valid subscription for the topic remains across the system, it will garbage collect the topic.
Pulsar supports replicated subscriptions, so you can keep the subscription state in sync, within a sub-second timeframe, in the context of a topic that is being asynchronously replicated across multiple geographical regions.
In case of failover, a consumer can restart consuming from the failure point in a different cluster.
If you want to use replicated subscriptions in Pulsar:
For broker side: set enableReplicatedSubscriptions
to true
in broker.conf
.
For consumer side: replicated subscription is disabled by default. You can enable replicated subscriptions when creating a consumer.
Consumer<String> consumer = client.newConsumer(Schema.STRING) .topic("my-topic") .subscriptionName("my-subscription") .replicateSubscriptionState(true) .subscribe();
1 second
. It means that a consumer failing over to a different cluster can potentially receive 1 second of duplicates. You can also configure the frequency of the snapshot in the broker.conf
file.Using geo-replication to migrate data between clusters is a special use case of the active-active replication pattern when you don't have a large amount of data.
Create your new cluster.
Add the new cluster to your old cluster.
bin/pulsar-admin clusters create new-cluster
Add the new cluster to your tenant.
bin/pulsar-admin tenants update my-tenant --cluster old-cluster,new-cluster
Set the clusters on your namespace.
bin/pulsar-admin namespaces set-clusters my-tenant/my-ns --cluster old-cluster,new-cluster
Update your applications using replicated subscriptions.
Validate subscription replication is active.
bin/pulsar-admin topics stats-internal public/default/t1
Move your consumers and producers to the new cluster by modifying the values of serviceURL
.
:::note
pulsar-admin topics create-subscription -s pulsar.repl.new-cluster -m earliest <topic>
.:::