Skip to content

Deploying QuestDB on Ubuntu 22.04 with WAL replication to a read replica

Stand up a self-hosted QuestDB primary on Ubuntu 22.04, plus a read replica using WAL shipping to object storage — including the operational checks that detect replication drift before the dashboard does.

The trading-desk dashboard runs against QuestDB on a single VM, and the analytics team has started running heavy GROUP BY queries over the last 30 days during the same hours as ingest. Latency on the realtime quote view climbs from 12 ms to 800 ms, and the head of trading sends you a Slack at 9:47 a.m. asking why the chart is “lagging again.” The textbook fix is a read replica: ingest stays on the primary, the analytics queries land on the replica, and you stop the noisy-neighbor problem at the source. QuestDB’s WAL-based replication is the right tool, but the installer is mostly undocumented in one place.

What’s happening

QuestDB 8.x supports primary-replica replication via WAL shipping to object storage. The primary writes its Write-Ahead Log to a local directory and uploads completed segments to an S3-compatible bucket; the replica pulls those segments from the bucket and replays them. The replica is read-only — INSERT and UPDATE on the replica will be refused. The replication path is not peer-to-peer; both primary and replica need network access to the bucket, and the bucket is the single source of truth for the WAL stream. This has consequences:

  • If the bucket is in another region, WAL upload latency dominates replication lag.
  • If the replica goes offline for a day, it catches up by re-reading the relevant WAL prefix on restart — provided the lifecycle policy hasn’t deleted those segments.
  • The replica’s server.conf must specify replication.role=replica and match the primary’s cairo.snapshot.instance.id — otherwise the replica refuses to start.

Two common silent failures: the replica falls behind because the object-storage upload thread on the primary is starved (CPU pressure or a slow bucket), and the operator only notices when the dashboard shows stale numbers. And: a “replica” that was promoted to primary during a failover but never had its replication role reset stays in replica mode and rejects writes after the original primary comes back, leaving you with no writable node at all.

How to verify

On the primary, confirm WAL segments are uploading:

SELECT table_name, last_segment_id, last_committed_txn,
       last_uploaded_segment_id, lag_segments
FROM wal_replication();

If lag_segments is non-zero and growing, your upload thread is behind.

Confirm the object-storage path is being populated:

aws --endpoint-url "${S3_ENDPOINT}" s3 ls "s3://questdb-wal/${INSTANCE_ID}/" \
  --recursive | tail -20

On the replica:

SELECT table_name, last_applied_segment_id, last_remote_segment_id,
       last_applied_timestamp
FROM wal_replication();

The difference between last_remote_segment_id and last_applied_segment_id is your replication lag in segments. A healthy replica is ≤ 1 segment behind.

Spot-check write rejection on the replica:

-- on the replica, expect an error
INSERT INTO trades VALUES (now(), 'ACME', 42.0, 1000);
-- ERROR: cannot write to read-only replica

The fix

The installer below deploys QuestDB 8.x on Ubuntu 22.04 in either primary or replica role. Object storage is assumed to be reachable at S3_ENDPOINT with bucket S3_BUCKET. The INSTANCE_ID shared between primary and replica must be identical or replication will refuse.

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

### QuestDB primary/replica installer for Ubuntu 22.04
### Run on the primary first, then on each replica.

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

# === EDIT PER NODE ===
ROLE="primary"                # "primary" or "replica"
NODE_NAME="qdb-prim"
NODE_IP="10.50.0.81"
CLUSTER_NETWORK="10.50.0.0/24"

INSTANCE_ID="qdb-trading-prod"   # MUST match between primary + replicas

# Object storage (S3-compatible)
S3_ENDPOINT="https://s3.internal:9000"
S3_REGION="local"
S3_BUCKET="questdb-wal"
S3_ACCESS_KEY="CHANGE_ME_AK"
S3_SECRET_KEY="CHANGE_ME_SK"

QDB_VERSION="8.2.3"
QDB_HEAP_SIZE_GB="8"

# Pulled by replica only — set to the primary's IP for replica
PRIMARY_HTTP="http://10.50.0.81:9000"
# =====================

echo
echo "========================================"
echo "QuestDB ${ROLE} on ${NODE_NAME} (${NODE_IP})"
echo "========================================"
echo

echo "==> Base packages"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y install \
  openjdk-17-jre-headless curl wget ca-certificates jq socat \
  ufw chrony fail2ban unattended-upgrades \
  vim lsof tcpdump net-tools iproute2

systemctl enable --now chrony
systemctl enable --now fail2ban

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

cat > /etc/security/limits.d/99-questdb.conf <<'LIMITS'
questdb soft nofile 1048576
questdb hard nofile 1048576
questdb soft memlock unlimited
questdb hard memlock unlimited
LIMITS

echo "==> SSH hardening"
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 default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow from "${CLUSTER_NETWORK}" to any port 9000 proto tcp   # HTTP + REST
ufw allow from "${CLUSTER_NETWORK}" to any port 9009 proto tcp   # ILP TCP
ufw allow from "${CLUSTER_NETWORK}" to any port 8812 proto tcp   # PG wire
ufw --force enable

echo "==> Creating questdb user + dirs"
id -u questdb >/dev/null 2>&1 || \
  useradd --system --no-create-home --shell /sbin/nologin questdb

install -d -o questdb -g questdb -m 0750 /var/lib/questdb
install -d -o questdb -g questdb -m 0750 /var/lib/questdb/conf
install -d -o questdb -g questdb -m 0750 /var/log/questdb

echo "==> Downloading QuestDB ${QDB_VERSION}"
TMP=$(mktemp -d)
cd "${TMP}"
wget -q \
  "https://github.com/questdb/questdb/releases/download/${QDB_VERSION}/questdb-${QDB_VERSION}-no-jre-bin.tar.gz" \
  -O questdb.tgz
tar xzf questdb.tgz
install -d -o questdb -g questdb -m 0755 /opt/questdb
cp -r questdb-${QDB_VERSION}-no-jre-bin/* /opt/questdb/
chown -R questdb:questdb /opt/questdb
rm -rf "${TMP}"

echo "==> Writing server.conf"
SERVER_CONF=/var/lib/questdb/conf/server.conf
cat > "${SERVER_CONF}" <<CONF
# Listeners
http.bind.to=0.0.0.0:9000
line.tcp.net.bind.to=0.0.0.0:9009
pg.net.bind.to=0.0.0.0:8812

# Identity
cairo.snapshot.instance.id=${INSTANCE_ID}

# Performance
shared.worker.count=8
http.worker.count=4
line.tcp.writer.worker.count=4
pg.worker.count=4

# Storage
cairo.commit.lag=2000
cairo.max.uncommitted.rows=500000
cairo.sql.default.partition.by=DAY

# Object store for WAL
replication.object.store=s3::endpoint=${S3_ENDPOINT};region=${S3_REGION};bucket=${S3_BUCKET};access_key=${S3_ACCESS_KEY};secret_key=${S3_SECRET_KEY};

# Role
replication.role=${ROLE}
CONF

if [[ "${ROLE}" == "replica" ]]; then
  cat >> "${SERVER_CONF}" <<CONF
replication.primary.url=${PRIMARY_HTTP}
CONF
fi

chown -R questdb:questdb /var/lib/questdb
chmod 600 "${SERVER_CONF}"

echo "==> systemd unit"
cat > /etc/systemd/system/questdb.service <<EOF
[Unit]
Description=QuestDB (${ROLE})
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=questdb
Group=questdb
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
Environment="QDB_PACKAGE=/opt/questdb"
ExecStart=/opt/questdb/questdb.sh start -d /var/lib/questdb -t -f -j /usr/lib/jvm/java-17-openjdk-amd64
ExecStop=/opt/questdb/questdb.sh stop -d /var/lib/questdb
Restart=on-failure
RestartSec=5
LimitNOFILE=1048576
LimitMEMLOCK=infinity
TimeoutStopSec=60

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now questdb

echo "==> Waiting for HTTP listener"
for i in {1..60}; do
  if curl -fs http://127.0.0.1:9000/exec?query=SELECT%201 >/dev/null 2>&1; then break; fi
  sleep 2
done

if [[ "${ROLE}" == "primary" ]]; then
  echo "==> Sample table + auth user"
  curl -fs "http://127.0.0.1:9000/exec" \
    --data-urlencode "query=CREATE TABLE IF NOT EXISTS trades (
      ts TIMESTAMP, symbol SYMBOL, price DOUBLE, size LONG
    ) TIMESTAMP(ts) PARTITION BY DAY WAL;" >/dev/null || true
fi

echo
echo "========================================"
echo "DONE — ${NODE_NAME} (${ROLE})"
echo "========================================"
echo
ss -lntp | grep -E ':(9000|9009|8812)\b' || true
echo
curl -fs "http://127.0.0.1:9000/status" || true
echo
systemctl --no-pager status questdb | head -15 || true

Make it executable and run primary first, then each replica:

chmod 700 /root/setup-questdb-node.sh
/root/setup-questdb-node.sh

Once both nodes are up, push some data into the primary and watch it replicate:

PRIMARY=10.50.0.81
REPLICA=10.50.0.82

# write via ILP TCP
for i in {1..10000}; do
  echo "trades,symbol=ACME price=$((100 + RANDOM % 50)).$((RANDOM % 100)),size=$((100 + RANDOM % 5000)) $(date +%s%N)"
done | nc -q1 "${PRIMARY}" 9009

# wait for the WAL to ship
sleep 10

# count on primary
curl -fs -G "http://${PRIMARY}:9000/exec" \
  --data-urlencode "query=SELECT count(*) FROM trades" | jq .

# count on replica
curl -fs -G "http://${REPLICA}:9000/exec" \
  --data-urlencode "query=SELECT count(*) FROM trades" | jq .

# replica should refuse a write
curl -fs "http://${REPLICA}:9000/exec" \
  --data-urlencode "query=INSERT INTO trades VALUES(now(),'BADWRITE',1.0,1)" | jq .

The two counts should match within a few seconds, and the replica insert must error. If counts don’t match, check wal_replication() on both sides — the most common cause is the replica’s object-storage credentials not having read access to the WAL prefix the primary is writing to.

Add an alert on lag_segments > 5 from the primary’s wal_replication() view. Add a second alert on the replica’s last_applied_timestamp falling more than 60 seconds behind wall clock. Configure a 30-day lifecycle on the WAL bucket — long enough to allow a replica to catch up after an extended outage, short enough that you’re not paying for indefinite WAL retention you don’t need.

For QuestDB clusters we deploy and operate — replication lag alerting, WAL bucket lifecycle, read-fleet sizing for analytics workloads — see Cloud Compute under Managed Cloud Ops, or book a discovery call if you’re scaling out of single-node ingest.