←Back to Documentation

Replication and Transactions

How commits are replicated and validated in a multi-node cluster.

Transactions in a cluster

In a three-node cluster (docker-compose.cluster.yml), every commit is a single entry in a Raft log replicated with MicroRaft. Followers forward writes to the leader. Each entry carries the keys and ranges the transaction read; every replica checks them against its own copy before applying the entry, so all replicas accept or reject a commit identically. A rejected commit returns 409, just like on a single node.

  • A commit is acknowledged once a majority of nodes have it, so one node can fail without losing it.
  • When the leader fails, writes resume after a new leader is elected (about 10 seconds in testing).
  • Reads on a follower can be slightly behind the leader.
  • There is no sharding and no multi-region support: every node holds all data.

Full details and the list of what was tested are in docs/deployment/multi-node.md in the repository.

Using a transaction

Begin a transaction, pass its ID with each operation (any of the six data models), then commit or roll back. Nothing is visible to other clients until the commit.

TX=$(curl -s "${H[@]}" -X POST localhost:8080/api/v1/transaction/begin -d '{}' | jq -r .transactionId)

curl -s "${H[@]}" -X POST localhost:8080/api/v1/query/sql \
  -d '{"database":"demo","sql":"UPDATE acct SET balance = balance - 100 WHERE id = 1","transactionId":"'$TX'"}'
curl -s "${H[@]}" -X POST localhost:8080/api/v1/query/sql \
  -d '{"database":"demo","sql":"UPDATE acct SET balance = balance + 100 WHERE id = 2","transactionId":"'$TX'"}'
curl -s "${H[@]}" -X PUT localhost:8080/api/v1/kv/last-transfer \
  -d '{"database":"demo","value":"1->2","transactionId":"'$TX'"}'

curl -s "${H[@]}" -X POST localhost:8080/api/v1/transaction/$TX/commit     # or .../rollback

The SDKs wrap this: with db.transaction(): ... in Python and await db.transaction(async (tx) => ...) in Node.js. A statement sent without a transaction ID runs in its own single-statement transaction.

What is guaranteed

  • Atomicity. Writes are buffered and applied as one batch at commit, across all six data models. A rollback, or a client that never commits, leaves nothing behind.
  • Constraints. Primary keys, CHECK constraints and foreign keys are enforced; a violating statement fails with HTTP 409.
  • Isolation. Concurrency control is optimistic. At commit, the transaction is rejected (HTTP 409, TRANSACTION_CONFLICT) if anything it read or wrote, including the ranges it scanned, was changed by another commit after it began. This prevents lost updates, write skew and phantoms. Run a rejected transaction again.
  • Durability. The write-ahead log is fsynced before a commit is acknowledged (ENTROPYDB_SYNC_WRITES=true, the default). With replication, a commit is also written to a majority of nodes first.

Limits

  • Under heavy contention on the same rows, commits are retried and some may fail with 409.
  • On a single node, commits are serialized through one lock, so each waits for the previous fsync.
  • There is no sharding. In a cluster every node holds all data; see Replication and transactions.