# Plan Topology and Manage Cluster Lifecycle

Source: https://docs.formance.com/v4.0/modules/ledger/operations/cluster-lifecycle

Stack: v4.0

Every Ledger member stores a copy of the cluster state and participates in one Raft group. Plan changes around voters and quorum rather than around Kubernetes pod count.

## Choose the topology

| Voting replicas | Majority required | Voter failures tolerated |
| --------------- | ----------------- | ------------------------ |
| 1               | 1                 | 0                        |
| 3               | 2                 | 1                        |
| 5               | 3                 | 2                        |

Use one voter only for local development or a non-critical environment. Three voters are the normal production starting point. Five voters improve failure tolerance, but every committed write crosses a larger consensus group and every voter needs the full storage and memory budget.

Even replica counts do not increase failure tolerance. For example, both three and four voters tolerate one voter failure, while four voters require three members for a majority.

Spread voters across independent nodes and, where possible, zones. Keep a disruption budget that preserves a majority during node drains and upgrades. Each member must own its node identity, WAL, and data volume for its whole lifetime.

```mermaid
flowchart LR
    Desired["Operator adds a member"] --> Learner["Member joins as a learner"]
    Learner --> CatchUp["State catches up"]
    CatchUp --> Voter["Member becomes a voter"]
    Voter --> Tolerance["Member counts toward failure tolerance"]
```

Do not count a pod, a ready container, or a learner toward failure tolerance. Count the member only after the Ledger cluster view reports it as a voter and its applied position has caught up.

## Before any membership change

Run the same precheck for scaling, replacement, and rolling changes:

```bash
ledgerctl cluster status
```

Record:

- the current leader, voters, and learners;
- each member's committed and applied positions;
- the current alert state and recent request error rate;
- WAL and data headroom on every member;
- the persistent-volume retention policy;
- the intended voter set after the change.

Do not begin if the cluster has no leader, already lacks its expected failure tolerance, has an out-of-sync member, or is blocking writes because of disk or clock health.

## Add capacity or failure tolerance

Use the Stack Operator's supported replica field and reconciliation workflow. Do not combine an Operator-managed change with manual membership commands.

1. Confirm that the target failure domain can schedule the member and provision both persistent stores.
2. Apply one desired-state change.
3. Wait for the new member to appear as a learner.
4. Keep watching until its state and log position catch up.
5. Wait for the member to become a voter.
6. Send a controlled write and current read.
7. Confirm the new voter appears in telemetry and the alert baseline returns to normal.

```bash
ledgerctl cluster watch --interval 2s
```

The operation succeeds when the expected odd voter count is present, every voter has caught up, and the cluster can still commit after one voter becomes unavailable within the designed tolerance.

### Roll back a failed addition

If the learner cannot catch up, stop the reconciliation and investigate network, storage, identity, TLS, and resource pressure. Remove the failed learner through the supported Operator workflow only after confirming that it never became a required voter. Keep the original voter set and its volumes unchanged.

## Replace a member

Prefer add-then-remove when capacity and the Operator support it:

1. Run the membership precheck.
2. Add the replacement with a new node identity and new or explicitly restored storage.
3. Wait for the replacement to become a caught-up voter.
4. Transfer leadership away from the member being removed when necessary.
5. Remove the old member through the Operator workflow.
6. Confirm that the old identity no longer appears in the Ledger membership view.
7. Apply the approved retention or deletion policy to its volume.
8. Verify a controlled write, a current read, and the expected failure tolerance.

When leadership must be transferred as part of an approved workflow, the Ledger command is:

```bash
set -euo pipefail

ledgerctl cluster status --json > cluster-before.json

PREVIOUS_LEADER="$(jq -er '.leader | select(. != 0)' cluster-before.json)"
TARGET_NODE_ID="$(
  jq -er '
    . as $cluster
    | [
        .nodes[]
        | select(
            .suffrage == "Voter"
            and .id != $cluster.leader
            and .progress.isLearner == false
            and .progress.recentActive == true
            and .progress.state == "Replicate"
            and .progress.msgAppFlowPaused == false
            and .progress.isPaused == false
            and .progress.match == $cluster.raftStatus.commit
            and .syncProgress.status == "normal"
          )
      ]
    | sort_by(.id)
    | .[0].id
  ' cluster-before.json
)"

ledgerctl cluster transfer-leader "$TARGET_NODE_ID"
ledgerctl cluster status --json > cluster-after.json

jq -e \
  --argjson previous "$PREVIOUS_LEADER" \
  --argjson target "$TARGET_NODE_ID" \
  '.leader == $target and .leader != $previous' \
  cluster-after.json
```

The selection accepts only an active, unpaused follower that is a voter, reports normal sync, and has replicated through the leader's current commit index. It fails instead of transferring when no member meets those conditions. Review `cluster-before.json` before running the transfer when a change requires a specific failure domain or excludes another member. The final `jq` command verifies that the cluster view reports the selected member as the new leader.

Never attach an old member's volume to a different node identity, reuse a removed identity for a replacement, or bootstrap a new cluster over retained member data. These shortcuts can create divergent membership and state.

### Roll back a replacement

Before removing the old voter, roll back by removing the new learner or voter through the same Operator workflow and returning to the original desired count. After removing the old voter, do not reintroduce it from stale state. Recover the replacement or add another fresh member instead.

## Remove a member or scale down

Scaling down changes quorum and failure tolerance. Prefer an odd target count and remove one member at a time.

1. Confirm that the remaining voters form a majority and meet the target service objective.
2. Transfer leadership if the target is the leader.
3. Apply the supported Operator change.
4. Wait for the target to disappear from cluster membership, not only from Kubernetes.
5. Stop and retain or delete its storage according to the approved policy.
6. Verify the final voter set and send a controlled write and read.

A three-to-two change leaves both remaining voters required for writes. Treat it as a temporary transition, not a production steady state.

`ledgerctl cluster remove-node --force` bypasses Raft consensus. Use it only as an approved break-glass action for a permanently unreachable member, on the current leader, with a recovery plan that guarantees the removed node and its stale state can never rejoin. Do not use it to speed up a normal scale-down.

## Run a rolling change

For an image or supported node-configuration change, follow [Upgrade Ledger](https://docs.formance.com/v4.0/modules/ledger/operations/upgrade). That page owns the authoritative sequence, including which upgrade class the release permits. The lifecycle invariant it enforces is always the same:

1. change one follower;
2. wait for it to rejoin and catch up;
3. verify the cluster and a controlled request;
4. change the next follower;
5. transfer leadership to an updated voter;
6. change the former leader;
7. run the final integrity and service checks.

Stop when a member cannot catch up, when a second member would be unavailable, or when the current voter set no longer meets the service objective.

Changes to internal TLS need a compatibility transition across mixed members. Follow [Secure Cluster Traffic with TLS](https://docs.formance.com/v4.0/modules/ledger/operations/tls-and-network-security) instead of treating certificate changes as an ordinary restart.

## Completion record

Attach the following evidence to the change:

- cluster status before and after;
- the identity and storage disposition of every added or removed member;
- learner-to-voter and catch-up confirmation;
- any leadership transfer;
- controlled write and current-read results;
- alert and integrity results;
- the rollback decision and any manual intervention.

A Kubernetes rollout completing is not evidence that Ledger membership converged. The Ledger cluster view is the source for voter state.
