Replication

Since 4.6.5 several DianaDB servers can hold the same data, and each of them accepts reads and writes (multi-master). A change made on one server is sent to the others right after it is confirmed to the client. The ODM and your queries stay the same.

Setting up a cluster

Every server lists the others and shares one secret. For three servers:


# /etc/diana-db/diana-db.conf on server 1 (10.0.0.1)
server_id=dianadbnode1
cluster_secret=<the same on every server, 16 or more characters>
cluster_peers=10.0.0.2:34569,10.0.0.3:34569

# server 2 (10.0.0.2)
server_id=dianadbnode2
cluster_secret=<the same secret>
cluster_peers=10.0.0.1:34569,10.0.0.3:34569

# server 3 (10.0.0.3)
server_id=dianadbnode3
cluster_secret=<the same secret>
cluster_peers=10.0.0.1:34569,10.0.0.2:34569
        
  • server_id is required and must differ on every server: exactly 12 lowercase letters or digits.
  • A cluster needs store_strategy=local and the write-ahead log (both are the defaults).
  • Servers connect to each other on cluster_port (34569): open it between the servers, not to the public.
  • Create users and grant access on every server (diana-db add-user, diana-db grant-access): users and access rights are not replicated.

A server with a missing or invalid cluster setting, or a busy cluster port, does not start and names the problem. A server with another secret or with a server_id already in the cluster is refused, and both sides log why.

Adding a server: start it with an empty data directory and the settings above, and add it to cluster_peers of the others (restart them one by one). It receives the whole state from a peer and then follows the changes.

Changes made on several servers

SituationResult on every server
Two servers change different fields of one documentBoth changes are kept
Two servers change the same fieldThe newest write wins. Equal times are decided by server_id
One server removes a document, another changes itThe document is removed
Collections, schema changes and viewsReplicated; the newest definition of a collection or view wins
TransactionsReplicated when they are committed. A rolled back transaction is never sent

"Newest" compares server clocks, so keep them synchronized (NTP). A server whose clock is behind still orders its own writes after the ones it has seen. The metricdiana_db_cluster_clock_ahead_seconds shows the difference.

Servers that were away

  • A server that was down, or cut off from the others, receives what it missed when it returns, and sends what was written to it meanwhile.
  • Each server keeps its last cluster_oplog_size changes (100,000) for that. A server further behind, and a new server, receives the whole state instead and merges it by the rules above.
  • Removals are remembered for cluster_tombstone_ttl (7 days). A server that was away for longer must start with an empty data directory, otherwise documents removed meanwhile can come back.
  • Subscribers on any server hear about changes made on every server.

Limits

  • A read on another server may not see a write for a moment.
  • unique is checked on the server that accepts the write. Two servers can accept the same unique value at the same moment, and both documents then exist. Send writes that depend on uniqueness to one server.
  • Updates with commands replicate as the resulting value. Two servers adding to the same number at the same moment end with one of the two results, not the sum.
  • Transactions are atomic on the server that runs them. Other servers receive their changes one by one, and locks are not shared between servers.
  • Users and access rights are per server.
  • The ODM connects to one server and does not switch to another by itself. Put a TCP load balancer in front, or choose the server in your application.
  • The cluster is a full mesh of servers that list each other; it is meant for a handful of servers.

Settings

KeyDefaultDescription
cluster_peers–The other servers: host:port,host:port
cluster_secret–16 or more characters, the same on every server
cluster_port34569Port the other servers connect to
cluster_host0.0.0.0Address the cluster port listens on
cluster_tombstone_ttl7dHow long removals are remembered: days (7d) or hours (48h)
cluster_oplog_size100000Changes kept in memory for servers that reconnect

The state of the cluster is on /health and /metrics: see Monitoring.