High Availability

Always listening, never missing a beat. MynaDB supports Master-Slave replication for high availability and fault tolerance. The master node handles all writes while replicating data to one or more slave nodes in real-time. If the master fails, a slave can be promoted to take over.

💡 Key Benefits
  • Fault Tolerance: Continue serving reads even when master is down
  • Read Scaling: Distribute read queries across slave nodes
  • Data Safety: Real-time replication ensures data durability
  • Zero Downtime Failover: Promote slave to master without data loss

Architecture Overview

The HA system uses a simple master-slave architecture with WAL (Write-Ahead Log) streaming:

                    +-----------------+
                    |     Clients     |
                    +--------+--------+
                             |
              +--------------+--------------+
              |                             |
      +-------v-------+             +-------v-------+
      |    MASTER     |             |     SLAVE     |
      |   (Active)    |------------>|   (Standby)   |
      |               | WAL Stream  |               |
      +-------+-------+             +-------+-------+
              |                             |
      +-------v-------+             +-------v-------+
      |  Parquet      |             |  Parquet      |
      |  Files        |             |  Files        |
      +---------------+             +---------------+
        

How It Works

  1. Write Path: All writes go to the master node
  2. WAL Replication: Master streams WAL entries to connected slaves via TCP
  3. Slave Replay: Slaves apply WAL entries to their local storage
  4. Heartbeat: Master sends periodic heartbeats; slaves monitor for failures
  5. Failover: On master failure, slave can be promoted (manual or automatic)

Configuration

Master Node Configuration

config-master.toml
[server]
http_port = 7000
flight_port = 7001

[storage]
data_dir = "/data/master"

[ha]
enabled = true
role = "master"
node_id = "master-01"

[ha.replication]
mode = "async"                    # "async" or "sync"
listen_addr = "0.0.0.0:7003"      # Replication port

[ha.heartbeat]
interval_secs = 5                 # Send heartbeat every 5 seconds
timeout_secs = 15                 # Slave timeout threshold

Slave Node Configuration

config-slave.toml
[server]
http_port = 7100                  # Different port from master
flight_port = 7101

[storage]
data_dir = "/data/slave"

[ha]
enabled = true
role = "slave"
node_id = "slave-01"

[ha.replication]
master_addr = "master-host:7003"  # Master's replication address
reconnect_interval_secs = 5       # Retry interval on disconnect

[ha.heartbeat]
interval_secs = 5
timeout_secs = 15

[ha.failover]
mode = "manual"                   # "manual", "automatic", or "disabled"
failure_threshold = 3             # Missed heartbeats before failover
promotion_delay_secs = 10         # Wait before auto-promotion

Configuration Options

Option Description Default
ha.enabled Enable HA mode false
ha.role Node role: "master" or "slave" "master"
ha.node_id Unique identifier for this node "node-{pid}"
ha.replication.mode "async" (faster) or "sync" (safer) "async"
ha.replication.listen_addr Master: address to listen for slaves "0.0.0.0:7003"
ha.replication.master_addr Slave: master's replication address required
ha.failover.mode "manual", "automatic", or "disabled" "automatic"

HA API Endpoints

GET /ha/status

Returns the current HA status of the node.

bash
curl http://localhost:7000/ha/status

Response:

{
  "enabled": true,
  "node_id": "master-01",
  "role": "master",
  "state": "running",
  "replication": {
    "role": "master",
    "current_sequence": 12345,
    "slave_count": 1,
    "connected": true
  },
  "promotion": {
    "role": "master",
    "state": "running",
    "can_promote": false,
    "failover_mode": "manual"
  }
}

POST /ha/promote

Manually promote a slave to master (requires confirmation).

bash
curl -X POST http://localhost:7100/ha/promote \
  -H "Content-Type: application/json" \
  -d '{"confirm": true}'

Response:

{
  "status": "success",
  "message": "Promoted to master",
  "new_role": "master"
}

Operational Guide

Starting a Cluster

bash
# Terminal 1: Start master
./tsdb-stats -c config-master.toml

# Terminal 2: Start slave
./tsdb-stats -c config-slave.toml

# Verify connection
curl http://localhost:7000/ha/status | jq '.replication.slave_count'
# Should return: 1

Writing Data

Always write to the master node. Writes to slave nodes are rejected:

# Write to master (succeeds)
curl -X POST http://localhost:7000/write/mydb \
  -d 'cpu,host=server1 usage=75.5'

# Write to slave (fails with 503)
curl -X POST http://localhost:7100/write/mydb \
  -d 'cpu,host=server1 usage=75.5'
# Error: This node is a slave; writes must be sent to the master

Reading Data

Both master and slave can serve read queries:

# Query master
curl -X POST http://localhost:7000/query/mydb \
  -d 'SELECT * FROM cpu LIMIT 10'

# Query slave (same data after replication)
curl -X POST http://localhost:7100/query/mydb \
  -d 'SELECT * FROM cpu LIMIT 10'

Manual Failover

When the master is unavailable, promote a slave:

# 1. Check slave status
curl http://localhost:7100/ha/status | jq '{role, state, can_promote: .promotion.can_promote}'

# 2. Promote slave to master
curl -X POST http://localhost:7100/ha/promote \
  -H "Content-Type: application/json" \
  -d '{"confirm": true}'

# 3. Verify new role
curl http://localhost:7100/ha/status | jq '.role'
# Should return: "master"

# 4. Update your application to point to the new master

Replication Modes

Async Replication (Default)

Sync Replication

[ha.replication]
mode = "sync"
timeout_ms = 5000  # Wait up to 5 seconds for slave ACK

Failover Modes

Mode Description Use Case
manual Operator must call /ha/promote API Production with human oversight
automatic Slave auto-promotes after detecting master failure Unattended operation
disabled Promotion is not allowed Read-only replicas

Local Testing

Test master-slave mode on a single machine:

bash
# Use the included test script
./scripts/ha_setup_local.sh setup   # Create configs
./scripts/ha_setup_local.sh master  # Terminal 1
./scripts/ha_setup_local.sh slave   # Terminal 2
./scripts/ha_setup_local.sh test    # Terminal 3 - Run tests

# Or run automated tests
./scripts/test_ha_local.sh

Monitoring

Key Metrics to Monitor

# Monitor script example
while true; do
  echo "=== Master ==="
  curl -s http://master:7000/ha/status | jq '{seq: .replication.current_sequence, slaves: .replication.slave_count}'

  echo "=== Slave ==="
  curl -s http://slave:7100/ha/status | jq '{seq: .replication.last_sequence, connected: .replication.connected, lag: .replication.seconds_since_heartbeat}'

  sleep 5
done

Best Practices

⚠ Production Recommendations
  • Network: Use low-latency network between master and slave
  • Storage: Use separate storage volumes for each node
  • Monitoring: Set up alerts for replication lag and connection status
  • Backup: Regularly backup data from slave node
  • Testing: Regularly test failover procedures

Deployment Topology

Troubleshooting

Slave Not Connecting

Replication Lag

Promotion Fails

See Also