FAILOVER(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ‘‘ FAILOVER πŸ’…

okay bestie, welcome to the Valkey Command Manual β€” this one's FAILOVER, and it's giving main character energy. it's a (3valkey) page, so buckle up πŸŽ€


✨ NAME ✨

failover β€” starts a coordinated failover from a server to one of its replicas. yes, we're handing the crown to somebody else. iconic behavior, honestly πŸ‘‘


🌸 SYNOPSIS 🌸

FAILOVER [TO host port [FORCE]] [ABORT] [TIMEOUT milliseconds]

πŸ’– DESCRIPTION πŸ’–

okay so here's the tea. this command kicks off a coordinated failover between the primary you're currently connected to and one of its replicas. it is not synchronous β€” instead a background task does the actual coordinating. the whole point is to limit data loss and limit how unavailable the cluster gets during the switch. it's basically the same vibe as the valkey-cluster-failover(7) CLUSTER FAILOVER command, but for non-clustered Valkey, and it's similar to the failover support sentinel gives you. slay 🌈

so the default failover flow goes a little something like this:

  1. the primary internally starts a valkey-client-pause(7) CLIENT PAUSE WRITE, which pauses incoming writes and stops new data from piling up in the replication stream. no new drama allowed πŸ’…
  2. the primary monitors its replicas, waiting for one of them to say it has fully consumed the replication stream. if the primary has multiple replicas, it only waits for the first replica to catch up.
  3. the primary then demotes itself to a replica. this is done to prevent any dual primary scenarios. NOTE: the primary does not discard its data, so it can still rollback if the replica rejects the failover request in the next step. safety net, bestie 🫢
  4. the previous primary sends a special PSYNC request to the target replica β€” valkey-psync(7) PSYNC FAILOVER β€” telling the target replica to become a primary. glow up time πŸ‘‘
  5. once the previous primary gets acknowledgement that the PSYNC FAILOVER was accepted, it unpauses its clients. if the PSYNC request is rejected, the primary aborts the failover and goes back to normal. no harm no foul 😌

the field master_failover_state in valkey-info(7) INFO replication can be used to track the current state of the failover. it has these values:

  • no-failover: there is no ongoing coordinated failover.
  • waiting-for-sync: the primary is waiting for the replica to catch up to its replication offset.
  • failover-in-progress: the primary has demoted itself, and is attempting to hand off ownership to a target replica.

NOTE: during the failover-in-progress phase, the primary first demotes itself to a replica and then notifies the replica to promote itself to primary. these two steps are an asynchronous process, which may result in the simultaneous existence of two nodes as replicas 😭. in this scenario, for clients that support REDIRECT (that means explicitly executing valkey-client-capa(7) CLIENT CAPA REDIRECT), the redirection result may bounce back and forth between the two replicas until the target replica completes the process of promoting itself to primary. to avoid this situation, during the failover-in-progress phase, we temporarily suspend the clients that need to be redirected until the replica truly becomes the primary, and then resume the execution. problem solved, periodt ✨

if the previous primary had additional replicas attached to it, they will continue replicating from it as chained replicas. you will need to manually execute a valkey-replicaof(7) REPLICAOF on these replicas to start replicating directly from the new primary. manual labor, unfortunately πŸ₯²

πŸŽ€ Optional arguments πŸŽ€

the following optional arguments exist to modify the behavior of the failover flow:

  • TIMEOUT milliseconds β€” this option lets you specify a maximum time a primary will wait in the waiting-for-sync state before aborting the failover attempt and rolling back. it's meant to set an upper bound on the write outage the Valkey cluster can experience. failovers typically happen in less than a second, but could take longer if there's a large amount of write traffic or the replica is already behind in consuming the replication stream. if this value is not specified, the timeout can be considered to be "infinite". no cap, infinite ♾️
  • TO HOST PORT β€” this option lets you designate a specific replica, by its host and port, to failover to. the primary will wait specifically for this replica to catch up to its replication offset, and then failover to it. picking your fave, we love that for you πŸ’œ
  • FORCE β€” if both the TIMEOUT and TO options are set, the force flag can also be used to designate that once the timeout has elapsed, the primary should failover to the target replica instead of rolling back. this can be used for a best-effort attempt at a failover without data loss, but limiting write outage.

NOTE: the primary will always rollback if the PSYNC FAILOVER request is rejected by the target replica. that part is not negotiable bestie.

πŸ¦‹ Failover abort πŸ¦‹

the failover command is intended to be safe from data loss and corruption, but it can encounter some scenarios it can not automatically remediate from and may get stuck πŸ’€. for this purpose, the FAILOVER ABORT command exists, which will abort an ongoing failover and return the primary to its normal state. the command has no side effects if issued in the waiting-for-sync state, but it can introduce multi-primary scenarios in the failover-in-progress state. if a multi-primary scenario is encountered, you will need to manually identify which primary has the latest data and designate it as the primary and have the other replicas. (yes, that's how the original says it β€” go figure which one's the real one πŸ‘€)

NOTE: valkey-replicaof(7) REPLICAOF is disabled while a failover is in progress. this is to prevent unintended interactions with the failover that might cause data loss. hands off the wheel while the car's switching drivers πŸš—


πŸ’Œ REPLY πŸ’Œ

valkey-protocol(7) Simple string reply: OK if the command was accepted and a coordinated failover is in progress. an error if the operation cannot be executed.


⚑ COMPLEXITY ⚑

O(1) β€” constant time, she's efficient πŸ’…


πŸ” ACL CATEGORIES πŸ”

@admin @dangerous @slow

she's tagged @dangerous for a reason bestie. this is not a casual lil command. respect the crown πŸ‘‘

πŸ“œ HISTORY πŸ“œ

  • available since: 6.2.0

πŸ‘― SEE ALSO πŸ‘―

acl(3valkey), acl-cat(3valkey), acl-deluser(3valkey), acl-dryrun(3valkey), acl-genpass(3valkey), acl-getuser(3valkey), acl-help(3valkey), acl-list(3valkey), acl-load(3valkey), acl-log(3valkey), acl-save(3valkey), acl-setuser(3valkey), acl-users(3valkey), acl-whoami(3valkey), bgrewriteaof(3valkey), bgsave(3valkey), command(3valkey), command-count(3valkey), command-docs(3valkey), command-getkeys(3valkey), command-getkeysandflags(3valkey), command-help(3valkey), command-info(3valkey), command-list(3valkey), commandlog(3valkey), commandlog-get(3valkey), commandlog-help(3valkey), commandlog-len(3valkey), commandlog-reset(3valkey), config(3valkey), config-get(3valkey), config-help(3valkey), config-resetstat(3valkey), config-rewrite(3valkey), config-set(3valkey), dbsize(3valkey), debug(3valkey), flushall(3valkey), flushdb(3valkey), info(3valkey), lastsave(3valkey), latency(3valkey), latency-doctor(3valkey), latency-graph(3valkey), latency-help(3valkey), latency-histogram(3valkey), latency-history(3valkey), latency-latest(3valkey), latency-reset(3valkey), lolwut(3valkey), memory(3valkey), memory-doctor(3valkey), memory-help(3valkey), memory-malloc-stats(3valkey), memory-purge(3valkey), memory-stats(3valkey), memory-usage(3valkey), module(3valkey), module-help(3valkey), module-list(3valkey), module-load(3valkey), module-loadex(3valkey), module-unload(3valkey), monitor(3valkey), psync(3valkey), replconf(3valkey), replicaof(3valkey), restore-asking(3valkey), role(3valkey), save(3valkey), shutdown(3valkey), slowlog(3valkey), slowlog-get(3valkey), slowlog-help(3valkey), slowlog-len(3valkey), slowlog-reset(3valkey), swapdb(3valkey), sync(3valkey), time(3valkey)


aaaand that's the whole page bestie πŸ’– this one's from Valkey 9.0.0, dated 2025-10-21. go forth and failover responsibly, mwah 🫢✨

πŸ“¦ where this page comes from