Skip to content

Multi-cloud failover: WAL timeline divergence after promotion leaves replica unrecoverable #375

Closed

Description

@markjbrown

Summary

Rapid back-to-back failovers between members of a multi-cloud DocumentDB resource leave the demoted primary in a state where its WAL receiver cannot reconnect to the new primary, because the two clusters end up on incompatible PostgreSQL timelines. The demoted primary is unrecoverable via log replay and requires a full pg_basebackup rebuild to re-attach as a replica.

The DocumentDB operator does not appear to:

  1. pg_rewind the demoted primary before re-attaching it as a replica, or
  2. Refuse / serialize a second promotion until cross-cloud replication is confirmed streaming again, or
  3. Auto-bootstrap a replica via pg_basebackup when its WAL receiver fails with a timeline-mismatch FATAL.

The result is a manual recovery path (delete the CNPG Cluster resource so the operator re-creates it) that is easy to get wrong, especially in a demo / live-failover scenario.

Repro

Multi-cloud setup from https://github.com/markjbrown/documentdb-local-to-multicloud:

  • Hub cluster + 2 member clusters (Azure AKS + AWS EKS), instancesPerNode: 3 on both
  • DocumentDB resource with clusterReplication.primary: azure-documentdb and crossCloudNetworkingStrategy: serviceLoadBalancer
  • DocumentDB operator preview build

Steps:

  1. Promote AWS: kubectl --context hub -n documentdb-preview-ns patch documentdb documentdb-preview --type=merge -p '{"spec":{"clusterReplication":{"primary":"aws-documentdb"}}}'
  2. Wait until both clusters report Healthy in their CNPG status (~30-60s).
  3. Promote back to Azure with the same patch (target=azure-documentdb).
  4. Within 30s of the second promotion completing, observe the replica.

Observed

On the new replica (AWS in this repro):

$ kubectl --context aws-documentdb -n documentdb-preview-ns logs <pod>-1 -c postgres | tail
FATAL: requested starting point 3/88000000 on timeline 8 is not in this server's history
DETAIL: This server's history forked from timeline 8 at 3/8704C8C0
$ kubectl --context aws-documentdb -n documentdb-preview-ns exec <pod>-1 -c postgres -- \
    psql -U postgres -tA -c "select status, sender_host from pg_stat_wal_receiver"
(0 rows)

The WAL receiver process keeps exiting with the FATAL above, so pg_stat_wal_receiver is empty. The cluster appears Healthy from CNPG's perspective (the pods are Running) but is silently not replicating.

pg_stat_replication on the new primary correspondingly shows zero connected standbys from the cross-cloud peer.

Expected

One of the following:

  • The operator detects the timeline mismatch and runs pg_rewind on the demoted primary against the new primary before starting the WAL receiver, OR
  • The operator refuses (or serializes) the second promotion until cross-cloud replication health is confirmed (pg_stat_wal_receiver.status = 'streaming' on the current replica), OR
  • The operator detects the persistent WAL-receiver failure and re-bootstraps the replica via pg_basebackup from the new primary (the same path used for initial replica setup via externalCluster).

Recovery (manual workaround)

Deleting the CNPG Cluster resource on the broken replica triggers the DocumentDB operator to re-create it from scratch, which uses pg_basebackup from the current primary via the existing externalCluster definition:

kubectl --context <broken-replica-context> -n documentdb-preview-ns \
  delete cluster.postgresql.cnpg.io <cnpg-cluster-name> --wait=false

After ~2-3 min the cluster is back at 3/3 Healthy and pg_stat_wal_receiver shows status: streaming.

This is destructive (wipes all PVCs) but functions as a workable rebuild-replica primitive. It would be useful if the operator exposed this as a first-class action — e.g. an annotation like documentdb.io/rebuild-replica: "true" or a sub-resource — instead of requiring users to delete the underlying CNPG cluster directly.

Two related issues observed during the same repro

These showed up in the same investigation; happy to split them out into separate issues if preferred:

  1. Stale promotionToken after every successful failover. After a successful primary swap, the new primary's CNPG cluster keeps spec.replica.promotionToken populated. CNPG then reports Cluster is unrecoverable / Promotion token content is not correct until you manually kubectl patch ... --type=json -p '[{"op":"remove","path":"/spec/replica/promotionToken"}]'. The operator should remove the token once promotion has succeeded.

  2. instancesPerNode honored only on the primary side. Setting spec.instancesPerNode: 3 on the hub DocumentDB resource creates 3 instances on the primary's CNPG cluster but only 1 on the replica's. Direct-patching spec.instances: 3 on the replica's CNPG cluster works, but the operator does not reconcile this back, and the patch is silently lost on the next failover.

Workarounds we baked into our demo monitor app

  • /api/promote is now gated on a replicationHealthy probe (pg_stat_wal_receiver.status = 'streaming' on the replica + no stale tokens on either side). Returns HTTP 409 with reason if not healthy.
  • /api/promote runs an auto-reconcile loop (4 retries over ~22s) after every successful promotion to clear the re-stamped promotionToken and re-apply a fast-write sync profile.
  • /api/rebuild-replica exposes the cluster-delete recovery path as a single button on the replica card.

Happy to share the code if useful — it's all in the markjbrown/documentdb-local-to-multicloud demo repo under app/monitor-app/.

Environment

  • DocumentDB operator: preview build (hub context: 1 master node, AKS 1.31)
  • Member clusters: Azure AKS 1.31 + AWS EKS 1.31, both with CNPG 1.24
  • Cross-cloud networking strategy: serviceLoadBalancer
  • instancesPerNode: 3 on both members
  • DocumentDB CR namespace/name: documentdb-preview-ns/documentdb-preview

Activity

  1. added
    bug
    go
    on May 9, 2026
  2. documentdb-triage-tool commented on May 9, 2026

    @documentdb-triage-tool

    🤖 Auto-triaged by documentdb-triage-tool.

    Applied: go, bug
    Project fields suggested: Component controllers · Priority P1 · Effort XL · Status Triage
    Confidence: 0.82 (llm)

    Reasoning

    LLM: Rapid multi-cloud failover leaves replicas silently unrecoverable due to WAL timeline divergence with no automated remediation (pg_rewind, serialized promotions, or pg_basebackup re-bootstrap), requiring manual cluster deletion to recover.

    If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

  3. markjbrown commented on May 9, 2026

    @markjbrown
    Author

    Reproduced in the opposite direction (AWS primary -> Azure replica -> AWS dead)

    Just hit this same bug again, mirrored. Posting the second occurrence in case it helps confirm it isn't a one-off.

    What happened

    1. Multi-cloud cluster was healthy: AWS PRIMARY, Azure REPLICA, streaming, no stale tokens.
    2. Triggered a normal promote of Azure -> primary via the operator handshake.
    3. Promotion succeeded on Azure (timeline 8 -> 14). AWS demoted itself, wrote demotionToken.
    4. Within minutes, Azure went unrecoverable (same phaseReason as the original report - "Promotion token content is not correct for current instance" type stuck state).
    5. AWS at this point had demotionToken set in status, was waiting for the new primary's WAL on a timeline it could not follow. Both sides therefore stuck. No writes accepted from anywhere.
    6. After clearing the bad state on AWS by hand, AWS itself ended up with the same "Promotion token content is not correct for current instance" once it tried to come back as primary - because the operator restamped a stale token from before the previous failover.

    So the failure mode is symmetric: whichever side the operator tries to land as the new primary after a divergence event can end up with a token that does not match its own data, and there is no auto-recovery.

    Manual recovery sequence that worked

    1. Patch the Fleet hub documentdb CR clusterReplication.primary to the side that still has the most recent data.
    2. On that same side, replace spec.replica.promotionToken with the value from the local promotion-token ConfigMap (data["index.html"]) - i.e. force CNPG to use the cluster's own current checkpoint.
    3. CNPG then promotes pod-1 in place. Cluster transitions through "Setting up primary" -> "Cluster in healthy state".
    4. Delete the CNPG Cluster on the other side and let the DocumentDB operator re-bootstrap via pg_basebackup.

    After step 4, replication is streaming again with sub-100 ms cross-cloud lag and the Fleet/operator stack is consistent.

    Suggested operator behavior

    The "Promotion token content is not correct for current instance" condition is currently terminal - it requires manual intervention (and quite specific knowledge of the underlying CNPG / Fleet model). It would be very useful if the operator either:

    • detected this condition and offered a first-class action (annotation, status field, or new CR field) to "promote this side using its own current local checkpoint", or
    • after a configurable number of seconds in the unrecoverable state, automatically rolled back to the previous primary (i.e. cancelled the promotion handshake and restored the prior clusterReplication.primary).

    I have implemented option (a) in the demo monitor app I am building on top of the operator (one button, posts a patch that sets replica.primary = self and replica.promotionToken = <local CM value>); happy to share the code if that helps shape the upstream design.

    Same repo as before: https://github.com/markjbrown/documentdb-local-to-multicloud (see app/monitor-app/server.js -> /api/force-promote-local-token).

  4. changed the title [-]Multi-cloud failover: WAL timeline divergence after rapid back-to-back promotions leaves replica unrecoverable[/-] [+]Multi-cloud failover: WAL timeline divergence after promotion leaves replica unrecoverable[/+] on May 12, 2026
  5. added 2 commits that reference this issue on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

buggo

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions