DEV Community

Cover image for Part 2: "Bidirectional Elasticsearch Replication Doesn't Exist. Here's the Architecture That Does."
Pranay Ravi
Pranay Ravi

Posted on

Part 2: "Bidirectional Elasticsearch Replication Doesn't Exist. Here's the Architecture That Does."

Cross-cluster replication is active-passive per index. Two-way replication is two one-way streams and a naming rule, and Elastic's own 9.x disaster recovery tutorial builds it the same way.

When we evaluated cross-cluster replication (CCR) to replace snapshot-based DR (Part 1), the requirement was "bidirectional": both datacenters take writes, and both see all the data. The first finding of the proof of concept was that CCR doesn't do that. The second was that you can build it anyway, once you accept what it is.

CCR in two minutes

  • Replication is set up per index: a leader index on a remote cluster and a follower index on the local cluster.
  • It is pull-based. Each follower shard pulls operations from its leader shard, so a follower has the same shard count as its leader.
  • A new follower bootstraps by copying segment files from the leader (remote recovery), then replays individual operations that the leader keeps through soft deletes.
  • Followers are read-only. Mappings and most settings follow the leader.
  • Auto-follow patterns create followers automatically for new leader indices that match a pattern. Existing indices need explicit followers.

CCR supports several topologies: one-way (production to DR), bidirectional, chained (A to B to C), broadcast (one leader to many followers), and consolidation (many sites into one reporting cluster).

Five CCR topologies: one-way, bidirectional, chained, broadcast and consolidation.

The catch: no index is writable in two places

A follower index rejects writes. The same index can't be a leader in both clusters, and CCR has no conflict resolution. In CCR, "bidirectional" means each cluster holds some leader indices and some follower indices. It is two active-passive streams running in opposite directions.

The design: two one-way streams and a naming rule

Every index name carries the site that writes it:

  • orders_east_202311 is written at East, which leads it; West follows it.
  • orders_west_202311 is written at West, which leads it; East follows it.

Each cluster gets one auto-follow pattern that points at the other cluster and matches only the other site's suffix:

# On Cluster West: follow everything East writes
PUT /_ccr/auto_follow/from_east
{
  "remote_cluster": "east",
  "leader_index_patterns": ["orders_east_*"]
}

# On Cluster East: the mirror image
PUT /_ccr/auto_follow/from_west
{
  "remote_cluster": "west",
  "leader_index_patterns": ["orders_west_*"]
}
Enter fullscreen mode Exit fullscreen mode

Applications at each site write only to indices with their own suffix and read from orders_*. That pattern matches both the local leader indices and the replicated followers, so every site has a global read view.

Each cluster leads its own site's indices and follows the other site's; apps write locally and read across both.

Elastic's current tutorial does the same thing

Elastic's bi-directional disaster recovery tutorial for 9.x uses data streams instead of monthly indices, but the architecture is the same. Each cluster runs an auto-follow pattern against the other, uses leader_index_exclusion_patterns to avoid replicating followers back (recursion), and uses follow_index_pattern to rename replicated backing indices with a "replicated from" suffix. Logstash writes to both clusters' hosts, and queries search across both data streams. The tutorial also notes that update and delete by query can only run on the leader. In other words, the site suffix moved from the application into the follow pattern, and nothing else changed.

What the design buys you

  • No write failover. If East goes down, West keeps writing to its own indices. East's data, up to the last replicated operation, stays readable at West.
  • A DR site that is always open for reads. Reporting and ad hoc queries can run there instead of competing with production indexing.
  • Lag in seconds, not hours. Followers pull continuously instead of waiting for an hourly restore.
  • Regional indices. Keeping each site's data in its own indices gives you independent retention and sizing per site, contains a bad deploy to one site's indices, and lets you replicate only the index families that need a global view.

What it can't do

  • Resolve conflicts. A document belongs to the site that wrote it, so updates and deletes must go to that site. Elastic's docs describe bi-directional replication as particularly useful for index-only workloads, where documents are immutable.
  • Hide ownership from the application. If your app updates documents, store the owner site in the document (a field, or a site prefix in _id) and route updates there. An update sent to the wrong site fails against a follower, and a "fix" that writes a new copy locally creates two versions of the truth.
  • Save in-flight writes on site loss. Operations the other site hasn't pulled yet are stranded until the failed site comes back (Part 7).
  • Replicate the whole cluster. Users, dashboards, templates and policies don't travel (Part 6).

Plan the first sync like a migration. Our proof of concept covered about 2,000 indices, and following all of them took up to about 10 hours of remote recovery.

How applications should use it

  • Write only to your own site's indices, by exact name or through a site-local write alias. Never write across sites.
  • Read through an index pattern or a read alias that spans both sites.
  • Update and delete by routing to the owner site.
  • Authenticate as a per-site application user that can only create and write its own site's indices (Part 4).
  • Stay local. Clients talk to the cluster in their own datacenter.

Risks and precautions

  • The naming rule is the entire replication contract. One mis-named index never matches the other site's pattern and silently never replicates.
  • With no conflict resolution, keep two-way replication for append-mostly data, or enforce document ownership in the application.
  • Auto-follow ignores indices that already exist. Plan explicit followers and the initial sync.
  • Prevent loops: a pattern must never match follower indices on its own cluster. Exclusion patterns help on newer versions.
  • Check your subscription. CCR is a paid feature in Elastic's distribution.
  • You need to remove/ control write privileges on your DR site to accidentally write to the follower ,this can be done in a few ways at the Elastic level and also in the CI/CD config management

Two clusters joined by two one-way arrows with the words Every index has exactly one writer.


Part 2 of 8 in Elasticsearch Replication & DR Field Notes. Previous: Part 1, Elasticsearch DR the Hard Way. Next: Part 3, Before You Turn On CCR.

Sources

Top comments (0)