Skip to content

Deploying Meilisearch in an active/passive HA pattern on Ubuntu 22.04

Meilisearch has no native clustering. Here is the active/passive deployment we use in production — full installer with a WAL replication strategy, snapshot replay, and Keepalived failover.

Your storefront search is now Meilisearch, indexing six million product documents, sub-100ms p99 against the live catalog — until the single VM hosting it had a kernel panic during Black Friday prep and the restore from a six-hour-old snapshot lost every search analytics event in between. The product team asks the inevitable: “can we just put two of these behind a load balancer?” The answer, honestly, is no — Meilisearch is single-node by design, and the supported HA story is active/passive with a follower that replays the leader’s write log (the “task queue”), promoted via floating IP. This is the installer and the replication strategy that actually works.

What’s happening

Meilisearch (as of 1.8) is single-node software. The official position is that horizontal scaling is on the roadmap; today, what you get is one writer plus N read replicas via an experimental feature, and “true” HA is achieved by running an active node that takes writes, a passive node that periodically pulls the leader’s full data directory snapshot, replays the task queue diff, and is promoted by Keepalived when the leader fails. The two cluster-y options in the docs — the experimental --experimental-replication-parameters flag and the snapshot+sync pattern — are not interchangeable. The replication-parameters path is for read scale-out from a frozen base; it does not handle write failover. The snapshot+task-queue pattern is what you use for HA. The trade-off is a non-zero RPO (typically 30-120 seconds, matching your snapshot cadence) and a deliberate, observed promotion — not the millisecond-grade leader-election of Patroni-style systems.

The data layout per node: /var/lib/meilisearch/data.ms holds the index, the task queue lives inside, snapshots are periodic tarballs of the entire directory written to /var/lib/meilisearch/snapshots/, and dumps (a logical export, slower, safer for restore) live in /var/lib/meilisearch/dumps/. The follower’s job is to fetch the latest snapshot from the leader over rsync-over-SSH every minute, replay it into a hot-standby Meilisearch instance, and answer health checks. When Keepalived flips the floating IP, the application sees a Meilisearch with the leader’s state from up to 60 seconds ago — acceptable for catalog search, unacceptable for billing-grade data.

How to verify

Both nodes healthy:

MS_KEY=$(cat /etc/meilisearch/master.key)
curl -sk -H "Authorization: Bearer ${MS_KEY}" \
  http://10.10.0.41:7700/health
# {"status":"available"}
curl -sk -H "Authorization: Bearer ${MS_KEY}" \
  http://10.10.0.42:7700/health

Floating IP held by exactly one node:

ip -br addr show | grep "10.10.0.40"
# eth0   UP   10.10.0.41/24 10.10.0.40/32

Replication lag — task queue head on leader vs follower:

curl -sk -H "Authorization: Bearer ${MS_KEY}" \
  "http://10.10.0.41:7700/tasks?limit=1" | jq '.results[0].uid'
curl -sk -H "Authorization: Bearer ${MS_KEY}" \
  "http://10.10.0.42:7700/tasks?limit=1" | jq '.results[0].uid'

The follower’s max task UID should trail the leader by less than your SYNC_INTERVAL_SEC. If it trails by more than 3x, the rsync pipe is failing — check the timer log on the follower.

The fix

Save the script as /root/setup-meilisearch-node.sh and run it on both nodes after editing the variables. The installer handles: base hardening on Ubuntu 22.04, the official Meilisearch APT package, a systemd unit running as the meilisearch user, snapshot generation every minute on the leader, an SSH key for the follower to pull snapshots, a meili-sync@.service template unit with a per-minute timer on the follower, Keepalived in active/passive mode with a vrrp_script health check pointing at /health, and a promotion script that flips the snapshot writer flag on the new leader.

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

### Ubuntu 22.04 Meilisearch active/passive HA installer
### Run on ms01 (leader) first, then ms02 (follower)
### Floating VIP is held by ms01 normally; Keepalived flips it to ms02 on failure

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

# === EDIT PER NODE ===
NODE_NAME="ms01"               # ms01 (leader) or ms02 (follower)
NODE_IP="10.10.0.41"
PEER_IP="10.10.0.42"            # the other node
FLOATING_IP="10.10.0.40"
ROLE="leader"                   # leader | follower
KEEPALIVED_PRIORITY="100"       # 100 on ms01, 90 on ms02
KEEPALIVED_AUTH_PASS="CHANGE_ME_VRRP"

MS_VERSION="1.8.3"
MASTER_KEY="CHANGE_ME_MASTER_KEY_LONG_RANDOM"
SYNC_INTERVAL_SEC="60"
SNAPSHOT_INTERVAL_SEC="60"
# =====================

echo
echo "========================================"
echo "Meilisearch ${ROLE} node ${NODE_NAME} (${NODE_IP})"
echo "========================================"
echo

echo "==> Updating base OS"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
apt-get -y install curl wget gnupg ufw fail2ban unattended-upgrades \
  chrony jq socat rsync openssh-client keepalived

systemctl enable --now chrony

echo "==> Kernel/sysctl"
cat > /etc/sysctl.d/99-meilisearch.conf <<'SYSCTL'
fs.file-max = 1048576
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
SYSCTL
sysctl --system

cat > /etc/security/limits.d/99-meilisearch.conf <<'LIMITS'
meilisearch soft nofile 65535
meilisearch hard nofile 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 ssh

echo "==> UFW"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow from "$PEER_IP" to any port 7700 proto tcp
ufw allow from "$PEER_IP" to any port 22 proto tcp
ufw allow from "$PEER_IP" proto vrrp
ufw allow 7700/tcp comment "Meilisearch HTTP — restrict to app subnet in prod"
ufw --force enable

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

echo "==> Creating meilisearch user"
id -u meilisearch >/dev/null 2>&1 || useradd -r -s /usr/sbin/nologin -d /var/lib/meilisearch meilisearch
mkdir -p /var/lib/meilisearch/{snapshots,dumps} /etc/meilisearch
chown -R meilisearch:meilisearch /var/lib/meilisearch

echo "==> Installing Meilisearch ${MS_VERSION}"
wget -qO /tmp/meilisearch.deb \
  "https://github.com/meilisearch/meilisearch/releases/download/v${MS_VERSION}/meilisearch.deb"
dpkg -i /tmp/meilisearch.deb || apt-get -f install -y
rm -f /tmp/meilisearch.deb

echo "${MASTER_KEY}" > /etc/meilisearch/master.key
chown root:meilisearch /etc/meilisearch/master.key
chmod 640 /etc/meilisearch/master.key

echo "==> /etc/meilisearch/meilisearch.toml"
cat > /etc/meilisearch/meilisearch.toml <<EOF
db_path = "/var/lib/meilisearch/data.ms"
env = "production"
http_addr = "${NODE_IP}:7700"
http_payload_size_limit = "100 MiB"
log_level = "INFO"

# Snapshot scheduling — the leader writes snapshots; the follower disables this
schedule_snapshot = ${SNAPSHOT_INTERVAL_SEC}
snapshot_dir = "/var/lib/meilisearch/snapshots"
dump_dir = "/var/lib/meilisearch/dumps"

# Loaded on first boot only; rotated via API afterwards
master_key = "${MASTER_KEY}"
EOF

if [[ "$ROLE" == "follower" ]]; then
  sed -i "s|^schedule_snapshot.*|schedule_snapshot = false|" /etc/meilisearch/meilisearch.toml
fi

chown root:meilisearch /etc/meilisearch/meilisearch.toml
chmod 640 /etc/meilisearch/meilisearch.toml

echo "==> systemd unit"
cat > /etc/systemd/system/meilisearch.service <<'MS'
[Unit]
Description=Meilisearch
After=network.target

[Service]
Type=simple
User=meilisearch
Group=meilisearch
ExecStart=/usr/bin/meilisearch --config-file-path /etc/meilisearch/meilisearch.toml
Restart=on-failure
RestartSec=5
LimitNOFILE=65535
WorkingDirectory=/var/lib/meilisearch

[Install]
WantedBy=multi-user.target
MS

systemctl daemon-reload
systemctl enable --now meilisearch

echo "==> Waiting for /health"
for i in {1..30}; do
  if curl -sk -H "Authorization: Bearer ${MASTER_KEY}" "http://${NODE_IP}:7700/health" >/dev/null; then break; fi
  sleep 2
done

if [[ "$ROLE" == "follower" ]]; then
  echo "==> Follower: SSH key + sync timer"
  if [[ ! -f /root/.ssh/id_ed25519 ]]; then
    ssh-keygen -t ed25519 -N "" -f /root/.ssh/id_ed25519
    echo
    echo "==> COPY the pubkey below into ms01:/home/meilisync/.ssh/authorized_keys"
    echo "    (or root@ms01 with a forced-command in authorized_keys)"
    cat /root/.ssh/id_ed25519.pub
    echo
  fi

  cat > /usr/local/bin/meili-sync.sh <<'SYNC'
#!/usr/bin/env bash
set -euo pipefail
LEADER="${LEADER_IP:-10.10.0.41}"
SRC_DIR="/var/lib/meilisearch/snapshots/"
DST_DIR="/var/lib/meilisearch/snapshots/"
DATA_DIR="/var/lib/meilisearch/data.ms"

# 1) pull the latest snapshot from leader
rsync -az --delete \
  -e "ssh -o StrictHostKeyChecking=accept-new -i /root/.ssh/id_ed25519" \
  "root@${LEADER}:${SRC_DIR}" "${DST_DIR}"

# 2) find the newest .snapshot file
LATEST=$(ls -t /var/lib/meilisearch/snapshots/*.snapshot 2>/dev/null | head -1 || true)
if [[ -z "$LATEST" ]]; then
  echo "No snapshot yet."
  exit 0
fi

LAST_APPLIED=$(cat /var/lib/meilisearch/.last_applied 2>/dev/null || echo "")
if [[ "$LATEST" == "$LAST_APPLIED" ]]; then
  exit 0
fi

# 3) stop, replay, restart
systemctl stop meilisearch
rm -rf "${DATA_DIR}.old"
[[ -d "$DATA_DIR" ]] && mv "$DATA_DIR" "${DATA_DIR}.old"
meilisearch \
  --config-file-path /etc/meilisearch/meilisearch.toml \
  --import-snapshot "$LATEST" \
  --ignore-snapshot-if-db-exists &
MS_PID=$!
sleep 5
kill -INT $MS_PID 2>/dev/null || true
wait $MS_PID 2>/dev/null || true
chown -R meilisearch:meilisearch "$DATA_DIR"
rm -rf "${DATA_DIR}.old"

echo "$LATEST" > /var/lib/meilisearch/.last_applied
systemctl start meilisearch
SYNC
  chmod 750 /usr/local/bin/meili-sync.sh

  cat > /etc/systemd/system/meili-sync.service <<EOF
[Unit]
Description=Meilisearch follower sync
After=meilisearch.service

[Service]
Type=oneshot
Environment=LEADER_IP=${PEER_IP}
ExecStart=/usr/local/bin/meili-sync.sh
EOF

  cat > /etc/systemd/system/meili-sync.timer <<EOF
[Unit]
Description=Run meili-sync every ${SYNC_INTERVAL_SEC}s

[Timer]
OnBootSec=2min
OnUnitActiveSec=${SYNC_INTERVAL_SEC}s
AccuracySec=5s

[Install]
WantedBy=timers.target
EOF

  systemctl daemon-reload
  systemctl enable --now meili-sync.timer
fi

echo "==> Keepalived (VRRP)"
KA_STATE="MASTER"
if [[ "$ROLE" == "follower" ]]; then
  KA_STATE="BACKUP"
fi

cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
  router_id ${NODE_NAME}
  enable_script_security
  script_user root
}

vrrp_script chk_meili {
  script "/usr/local/bin/check-meili.sh"
  interval 2
  timeout 2
  rise 2
  fall 3
  weight -20
}

vrrp_instance VI_MEILI {
  state ${KA_STATE}
  interface eth0
  virtual_router_id 51
  priority ${KEEPALIVED_PRIORITY}
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass ${KEEPALIVED_AUTH_PASS:0:8}
  }
  virtual_ipaddress {
    ${FLOATING_IP}/32 dev eth0
  }
  track_script {
    chk_meili
  }
  notify /usr/local/bin/keepalived-notify.sh
}
EOF

cat > /usr/local/bin/check-meili.sh <<EOF
#!/usr/bin/env bash
curl -fs -m 1 -H "Authorization: Bearer ${MASTER_KEY}" \
  "http://${NODE_IP}:7700/health" >/dev/null
EOF
chmod 750 /usr/local/bin/check-meili.sh

cat > /usr/local/bin/keepalived-notify.sh <<'NOTIFY'
#!/usr/bin/env bash
# $1 = INSTANCE | GROUP, $2 = name, $3 = MASTER|BACKUP|FAULT
TYPE="$1"; NAME="$2"; STATE="$3"
logger -t keepalived "VRRP state change: ${TYPE} ${NAME} -> ${STATE}"

if [[ "$STATE" == "MASTER" ]]; then
  # Promoted to leader: re-enable snapshot writing
  sed -i "s|^schedule_snapshot.*|schedule_snapshot = 60|" /etc/meilisearch/meilisearch.toml
  systemctl restart meilisearch
  systemctl stop meili-sync.timer 2>/dev/null || true
elif [[ "$STATE" == "BACKUP" ]]; then
  sed -i "s|^schedule_snapshot.*|schedule_snapshot = false|" /etc/meilisearch/meilisearch.toml
  systemctl restart meilisearch
  systemctl enable --now meili-sync.timer
fi
NOTIFY
chmod 750 /usr/local/bin/keepalived-notify.sh

systemctl enable --now keepalived

echo
echo "========================================"
echo "DONE — ${NODE_NAME} (${ROLE})"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(7700)\b' || true
echo
echo "Floating IP (only on leader):"
ip -br addr show | grep "${FLOATING_IP}" || echo "  (not held on this node)"
echo
echo "Meilisearch /health:"
curl -sk -H "Authorization: Bearer ${MASTER_KEY}" "http://${NODE_IP}:7700/health" || true
echo

Run on ms01 first with ROLE=leader and KEEPALIVED_PRIORITY=100, then on ms02 with ROLE=follower and KEEPALIVED_PRIORITY=90. Drop the follower’s SSH pubkey into the leader’s authorized_keys (a command= restriction limiting it to rsync is wise). Watch the floating IP flip with journalctl -u keepalived -f while you stop meilisearch.service on the leader — the VIP should appear on ms02 within five seconds and meili-sync.timer should stop.

The replication strategy is RPO-bound by SNAPSHOT_INTERVAL_SEC plus rsync time. For a 6M-doc catalog at ~3GB on disk, expect ~10-30s of lag and a 30-90s promotion window. If you need tighter RPO, switch to a queued ingestion pattern: the application writes to a durable queue (RabbitMQ or NATS JetStream) and a small consumer pushes documents to whichever Meilisearch is currently behind the floating IP — that pushes the “missed writes” problem out of Meilisearch and into a system that has actual durability guarantees.

For a Meilisearch deployment you don’t have to babysit — including promotion drills and snapshot integrity checks — our team runs it under Managed Cloud Ops on Cloud Compute. Start a scoping call.