INSERT INTO users(name) VALUES('HA user from PostgreSQL Wire Protocol');
SELECT*FROM users;
Connect to another server and check the values
PGPASSWORD="ha" psql -h localhost -U ha -p 5433
SELECT*FROM users;
2.1 Loading existed database to memory
ha -m mydatabase.db
2.2 Use database in disk
ha file:mydatabase.db?_journal=WAL&_busy_timeout=500
2.3 Load database from latest snapshot
ha --from-latest-snapsot
3. Local Read Replicas
Use ha-sync SQLite extension to create local embedded replicas from a remote HA database.
4. Using Docker
docker run --name ha \
-e HA_MEMORY=true \
-p 5432:5432 -p 8080:8080 -p 4222:4222 \
ghcr.io/litesql/ha:latest
- Set up a volume at /data to store the NATS streams state.
curl -d '[{ "sql": "INSERT INTO users(name) VALUES(:name)", "params": {"name": "new HA user"}},{ "sql": "DELETE FROM users WHERE name = :name", "params": {"name": "new HA user"}},{ "sql": "SELECT * FROM users"}]' \
http://localhost:8080
The replication is not invoked when conflicting rows are deleted because of an ON CONFLICT REPLACE clause.
Use idempotents DDL commands (CREATE IF NOT EXISTS and DROP IF EXISTS)
Writing to any node in the cluster improves availability, but it can lead to consistency issues in certain edge cases. If your application values Consistency more than Availability, it's better to route all write operations through a single cluster node.
8. Configuration
Flag
Environment Variable
Default
Description
-n, --name
HA_NAME
random
Node name
-p, --port
HA_PORT
8080
HTTP API tcp port
-m, --memory
HA_MEMORY
false
Store database in memory
--from-latest-snapsot
HA_FROM_LATEST_SNAPSHOT
false
Use the latest database snapshot from NATS JetStream Object Store (if available at startup)
--snapshot-interval
HA_SNAPSHOT_INTERVAL
0s
Interval to create database snapshot to NATS JetStream Object Store (0 to disable)
--nats-logs
HA_NATS_LOGS
false
Enable embedded NATS Server logging
--nats-port
HA_NATS_PORT
4222
Embedded NATS server port (0 to disable)
--nats-store-dir
HA_NATS_STORE_DIR
/tmp/nats
Embedded NATS server store directory
--nats-user
HA_NATS_USER
Embedded NATS server user
--nats-pass
HA_NATS_PASS
Embedded NATS server password
--nats-config
HA_NATS_CONFIG
Path to embedded NATS server config file (override other nats configurations)
--pg-port
HA_PG_PORT
5432
Port to PostgreSQL Wire Protocol server
--pg-user
HA_PG_USER
ha
PostgreSQL Auth user
--pg-pass
HA_PG_PASS
ha
PostgreSQL Auth password
--pg-cert
HA_PG_CERT
Path to PostgreSQL TLS certificate file
--pg-key
HA_PG_KEY
Path to PostgreSQL TLS key file
--concurrent-queries
HA_CONCURRENT_QUERIES
50
Number of concurrent queries (DB pool max)
--extensions
HA_EXTENSIONS
Comma-separated list of SQLite extensions path to load
--replicas
HA_REPLICAS
1
Number of replicas to keep for the stream and object store in clustered jetstream
--replication-timeout
HA_REPLICATION_TIMEOUT
15s
Replication publisher timeout
--replication-stream
HA_REPLICATION_STREAM
ha_replication
Replication stream name
--replication-max-age
HA_REPLICATION_MAX_AGE
24h
Replication stream max age
--replication-url
HA_REPLICATION_URL
Replication NATS url (defaults to embedded NATS server)