Skip to content

Deploying KeyDB multi-master on AlmaLinux 9

Stand up a two-node KeyDB Pro cluster in active-active replication on AlmaLinux 9 — installer, conflict-resolution model, when multi-master is and isn't the right call.

You have an application that reads and writes a shared session cache from two regions. Redis Cluster solves “scale out the keyspace across shards in one location.” It does not solve “two writers in two regions need to converge eventually.” You consider Redis Enterprise’s CRDT-based Active-Active. You consider running two separate Redis clusters and replicating with redis-replicator. Both are paths. The third path is KeyDB in multi-master mode — Redis-protocol-compatible, active-active out of the box, last-write-wins conflict resolution, single binary. It’s a real option, and it’s not the right option in every case. This article gets you to a running two-node multi-master deployment on AlmaLinux 9 and tells you the limits up front.

What’s happening

KeyDB is a multi-threaded fork of Redis with active-replication features that mainline Redis does not provide. In active-active (multi-master) mode, every node accepts writes and propagates them to all peers. The replication protocol carries an originating node ID on each write, so loops are avoided — a node won’t re-replicate writes that originated from itself.

Conflict resolution is last-write-wins, decided by the timestamp on the originating write. There is no CRDT-level merging like Redis Enterprise’s Active-Active offers. This means:

  • If both nodes write to the same key within the same millisecond, the deterministic tiebreak (node ID) decides which one survives. Both writes still propagate; the loser is overwritten.
  • For monotonic counters (INCR), multi-master is unsafe. Two INCR foo on two nodes can each return 1 and the merged state is 1, not 2. Don’t use multi-master for counters.
  • For sessions, caches, feature flags, rate-limit buckets that are user-keyed, lists where you don’t care about insertion order across nodes: multi-master is fine.
  • For idempotent writes (set this user’s profile JSON to this value): last-write-wins is what you want.

In practice the trade-off looks like this:

PatternMulti-master OK?
Per-user session cacheYes
Per-user rate-limit bucket (token bucket refilled by client)Yes
Global INCR counterNo
Sliding-window rate limiter using INCR on a windowed keyNo
Pub/SubYes, but messages are local-only by default
Sorted set leaderboardsMaybe — if writes are partitioned by ID
Lua scripts that depend on monotonic stateNo

KeyDB also adds a KEYDB.CRON data type and per-thread CPU pinning that mainline Redis doesn’t have. Those are nice to know about; they’re not why most operators reach for KeyDB. The reason is multi-master.

How to verify

Once both nodes are up:

# From either node
keydb-cli -h 10.30.0.11 -p 6379 -a "$ADMIN_PASS" info replication

You should see:

# Replication
role:active-replica
connected_clients_replicas:1
master_global_link_status:up

And a slave0: (KeyDB still uses the old field name) entry for each peer.

Write/read cross-check:

keydb-cli -h 10.30.0.11 -p 6379 -a "$ADMIN_PASS" set test:from-node1 "$(date)"
keydb-cli -h 10.30.0.12 -p 6379 -a "$ADMIN_PASS" get test:from-node1
# should return the same timestamp
keydb-cli -h 10.30.0.12 -p 6379 -a "$ADMIN_PASS" set test:from-node2 "$(date)"
keydb-cli -h 10.30.0.11 -p 6379 -a "$ADMIN_PASS" get test:from-node2
# should also return the same timestamp

If a write on node 1 isn’t visible on node 2 within a second, the active-replica link is broken. Check info replication for master_global_link_status:down and inspect the log.

The fix

Save the script as /root/setup-keydb-node.sh and run it on each of the two nodes after editing the variables at the top. It does the full install on AlmaLinux 9: base hardening (SELinux enforcing, fail2ban, dnf-automatic, SSH key-only), kernel/sysctl tuning, KeyDB from the EOS Linux Foundation repo, TLS material from a private CA shared between the two nodes, keydb.conf configured for active-replica mode against the peer, an ACL file with admin/app/replication users, and a one-shot reachability check.

#!/usr/bin/env bash
set -euo pipefail

### AlmaLinux 9 KeyDB multi-master (active-replica) installer
### Run on each of 2 nodes after editing the variables
### Multi-master conflict resolution is last-write-wins — do not use for global counters
### Cloud firewall must block 6379/16379 from anywhere outside your private CIDR

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# === EDIT PER NODE ===
NODE_NAME="keydb-1"           # keydb-1 or keydb-2
NODE_IP="10.30.0.11"          # this node's private IP
PEER_IP="10.30.0.12"          # the other node's private IP
IS_BOOTSTRAP="true"           # true on exactly one node — generates the shared CA
ADMIN_PASS="CHANGE_ME_ADMIN"
APP_PASS="CHANGE_ME_APP"
REPL_PASS="CHANGE_ME_REPL"
INTERNAL_CIDR="10.30.0.0/16"
# =====================

echo
echo "========================================"
echo "KeyDB node ${NODE_NAME} (${NODE_IP})"
echo "Peer: ${PEER_IP}"
echo "========================================"
echo

echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y install vim curl wget chrony fail2ban dnf-automatic \
  policycoreutils-python-utils lsof tcpdump bind-utils tar gzip \
  openssl jq net-tools firewalld

systemctl enable --now chronyd
systemctl enable --now firewalld

echo "==> SELinux enforcing"
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1 || true

echo "==> Kernel/sysctl tuning for KeyDB"
cat > /etc/sysctl.d/99-keydb.conf <<'SYSCTL'
vm.overcommit_memory = 1
vm.swappiness = 1
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
fs.file-max = 1048576
SYSCTL
sysctl --system

echo "==> Disable transparent hugepages"
cat > /etc/systemd/system/disable-thp.service <<'THP'
[Unit]
Description=Disable Transparent Huge Pages
After=sysinit.target local-fs.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled; echo never > /sys/kernel/mm/transparent_hugepage/defrag'
RemainAfterExit=yes

[Install]
WantedBy=basic.target
THP
systemctl daemon-reload
systemctl enable --now disable-thp

cat > /etc/security/limits.d/99-keydb.conf <<'LIMITS'
keydb soft nofile 200000
keydb hard nofile 200000
keydb soft nproc 65535
keydb hard nproc 65535
LIMITS

echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart sshd

echo "==> firewalld — internal only"
firewall-cmd --permanent --new-zone=keydb-internal || true
firewall-cmd --permanent --zone=keydb-internal --add-source="${INTERNAL_CIDR}"
firewall-cmd --permanent --zone=keydb-internal --add-port=6379/tcp
firewall-cmd --permanent --zone=keydb-internal --add-port=16379/tcp
firewall-cmd --permanent --zone=public --remove-service=cockpit || true
firewall-cmd --reload

echo "==> fail2ban + automatic security updates"
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban

cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
download_updates = yes
apply_updates = yes
[emitters]
emit_via = stdio
DNFAUTO
systemctl enable --now dnf-automatic.timer

echo "==> Installing KeyDB"
# KeyDB ships an EL9 RPM
dnf -y install https://download.keydb.dev/pkg/open_source/rpm/almalinux9/keydb-latest-1.el9.x86_64.rpm

# In some downstreams the package name is keydb-cli + keydb-server; check what landed
KEYDB_BIN=$(command -v keydb-server || true)
if [[ -z "${KEYDB_BIN}" ]]; then
  echo "KeyDB install did not produce keydb-server in PATH. Investigate."
  exit 2
fi

systemctl stop keydb || true

echo "==> Creating /etc/keydb/tls"
mkdir -p /etc/keydb/tls
cd /etc/keydb/tls

if [[ "${IS_BOOTSTRAP}" == "true" && ! -f ca.crt ]]; then
  openssl req -x509 -newkey rsa:4096 -sha256 -nodes -days 3650 \
    -keyout ca.key -out ca.crt \
    -subj "/CN=KeyDB Cluster CA"
  chmod 600 ca.key
  echo "    Bootstrap CA generated. Copy /etc/keydb/tls/ca.{crt,key} to the peer node before continuing there."
fi

if [[ ! -f keydb.crt ]]; then
  if [[ ! -f ca.crt || ! -f ca.key ]]; then
    echo "ERROR: copy ca.crt + ca.key from the bootstrap node to /etc/keydb/tls/ before running on this peer." >&2
    exit 2
  fi
  openssl req -newkey rsa:2048 -nodes \
    -keyout keydb.key -out keydb.csr \
    -subj "/CN=${NODE_NAME}" \
    -addext "subjectAltName=DNS:${NODE_NAME},IP:${NODE_IP},IP:127.0.0.1"
  cat > keydb.ext <<EXT
subjectAltName=DNS:${NODE_NAME},IP:${NODE_IP},IP:127.0.0.1
extendedKeyUsage=serverAuth,clientAuth
EXT
  openssl x509 -req -in keydb.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
    -out keydb.crt -days 825 -sha256 -extfile keydb.ext
  chmod 640 keydb.key
  chown root:keydb keydb.crt keydb.key ca.crt
fi
cd -

echo "==> Writing /etc/keydb/keydb.conf"
cat > /etc/keydb/keydb.conf <<EOF
# Network
bind ${NODE_IP} 127.0.0.1
protected-mode yes
port 0
tls-port 6379

# TLS
tls-cert-file /etc/keydb/tls/keydb.crt
tls-key-file /etc/keydb/tls/keydb.key
tls-ca-cert-file /etc/keydb/tls/ca.crt
tls-replication yes
tls-auth-clients yes
tls-protocols "TLSv1.2 TLSv1.3"

# Multi-master (active-replica) — every node accepts writes,
# replicates to peers, ignores writes that originated from itself.
active-replica yes
multi-master yes
replicaof ${PEER_IP} 6379
masterauth ${REPL_PASS}
masteruser replication

# Threads — KeyDB is multithreaded by design.
server-threads 4
server-thread-affinity true

# Persistence
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
aof-use-rdb-preamble yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
save 900 1
save 300 10
save 60 10000
dir /var/lib/keydb

# Memory + eviction
maxmemory 4gb
maxmemory-policy allkeys-lru

# Logging
loglevel notice
logfile /var/log/keydb/keydb.log
syslog-enabled no

# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 1024

# ACL
aclfile /etc/keydb/users.acl
EOF

echo "==> Writing /etc/keydb/users.acl"
cat > /etc/keydb/users.acl <<EOF
# default disabled — clients must authenticate as a named user.
user default off nopass nocommands

# admin
user admin on >${ADMIN_PASS} ~* &* +@all

# app — production application
user app on >${APP_PASS} ~* &* -@dangerous -flushdb -flushall -keys -debug -shutdown -script +@all

# replication — peer-to-peer link
user replication on >${REPL_PASS} +@all ~* &*
EOF

chmod 640 /etc/keydb/users.acl /etc/keydb/keydb.conf
chown root:keydb /etc/keydb/users.acl /etc/keydb/keydb.conf
mkdir -p /var/lib/keydb /var/log/keydb
chown -R keydb:keydb /var/lib/keydb /var/log/keydb

echo "==> systemd override"
mkdir -p /etc/systemd/system/keydb.service.d
cat > /etc/systemd/system/keydb.service.d/override.conf <<'OVR'
[Service]
LimitNOFILE=200000
TimeoutStopSec=60
Restart=on-failure
OVR

systemctl daemon-reload
systemctl enable keydb
systemctl start keydb

echo "==> Waiting for KeyDB to accept TLS connections"
for i in {1..60}; do
  if keydb-cli --tls \
       --cacert /etc/keydb/tls/ca.crt \
       --cert /etc/keydb/tls/keydb.crt \
       --key /etc/keydb/tls/keydb.key \
       -h "${NODE_IP}" -p 6379 \
       --user admin -a "${ADMIN_PASS}" \
       ping >/dev/null 2>&1; then
    echo "    KeyDB up."
    break
  fi
  sleep 2
done

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':6379\b' || true
echo
echo "Replication status:"
keydb-cli --tls \
  --cacert /etc/keydb/tls/ca.crt \
  --cert /etc/keydb/tls/keydb.crt \
  --key /etc/keydb/tls/keydb.key \
  -h "${NODE_IP}" -p 6379 \
  --user admin -a "${ADMIN_PASS}" \
  info replication || true
echo
echo "On the other node, run this same script with NODE_NAME / NODE_IP / PEER_IP swapped and IS_BOOTSTRAP=false."
echo
echo "Cross-check from this node to the peer:"
echo "keydb-cli --tls --cacert /etc/keydb/tls/ca.crt --cert /etc/keydb/tls/keydb.crt --key /etc/keydb/tls/keydb.key -h ${PEER_IP} -p 6379 --user admin -a '<admin pass>' ping"

Run on the bootstrap node first to generate the shared CA, then copy /etc/keydb/tls/ca.crt and ca.key to the peer, then run on the peer:

chmod 700 /root/setup-keydb-node.sh
# node 1 (bootstrap)
/root/setup-keydb-node.sh

# copy CA to node 2
scp /etc/keydb/tls/ca.{crt,key} keydb-2:/etc/keydb/tls/

# node 2 (with IS_BOOTSTRAP=false in the script vars)
/root/setup-keydb-node.sh

After both nodes are up, run the read/write cross-check from the verification section. If it passes, do a partition drill: stop KeyDB on node 1 (systemctl stop keydb), continue writing to node 2 for a minute, restart node 1 (systemctl start keydb), confirm the writes you sent during the partition land on node 1 within a few seconds. Watch info replication during the catch-up — the master_sync_in_progress:1 field should briefly appear and clear.

A few operational notes worth knowing before you commit a workload to this:

  • Adding a third or fourth master is possible (replicaof accepts multiple peers in multi-master mode, but each peer must also replicaof every other peer — it’s a full mesh). Operational complexity rises quickly; we don’t recommend going beyond three in this topology.
  • Persistence is per-node. Each node has its own AOF and RDB. A restored snapshot from node 1 will not include writes that landed only on node 2 during a partition.
  • KEYS *, FLUSHALL, DEBUG SLEEP — denied in the app ACL above for good reasons. Don’t take it out.

For an active-active cache tier you don’t have to operate yourself, we run KeyDB and Redis under Managed Cloud Ops on our Cloud Compute with replication-lag alerting in monitoring — book a scoping call to plan the build.