SurrealDB Schema Migrations and Multi-Master Replication: Consistency During Hot Upgrade
0 reputation · 31 Mar 2021, 02:54 UTC
0 reputation · 31 Mar 2021, 02:54 UTC
Migrating a small SurrealDB-backed application to a new schema version without downtime requires coordinating online ALTER TABLE operations with the database's multi-master replication layer. The HTTP API permits issuing schema changes while the application continues serving requests, yet adding a NOT NULL column with a default value can trigger a full table rewrite that blocks writes. Meanwhile, SurrealDB's multi-master replication enables blue-green deployment patterns, but the consistency guarantees during a hot upgrade—particularly read-after-write visibility across replicas while a schema change is in progress—are not fully documented. Transactions provide atomicity and isolation within a single transaction boundary, but cross-transaction schema changes may expose temporary inconsistencies. No built-in dry-run mechanism exists to preview the impact of large schema changes before applying them to production traffic.
ALTER TABLE rewrite is in progress?35275 reputation · 31 Mar 2021, 07:03 UTC
ALTER TABLESurrealDB’s documentation states that a transaction is atomic and isolated at the node that executes it. When a ALTER TABLE that rewrites the table is issued, the node that receives the command pauses new writes until the rewrite finishes. Reads from that node are immediately consistent, but reads from other replicas are eventual – they may return stale data or a temporary error until the updated schema is propagated via the multi‑master replication layer.
During a rewrite, concurrent writes that target the same record are resolved by SurrealDB’s last‑writer‑wins rule, which uses a logical timestamp assigned to each write. If a write arrives after the schema has been altered and the record shape no longer matches the new schema, the node that receives the write will reject it with a schema‑violation error. The rejecting node will then propagate the error to the other replicas through the replication channel.
SurrealDB does not ship a dry‑run feature for large schema changes. The most reliable pattern is:
ALTER TABLE on a secondary replica that is not serving production traffic./replication/lag endpoint (aim for < 100 ms).# On the staging replica
curl -sS http://staging:8000/replication/lag
# Run a load test (e.g., wrk or custom script) while the ALTER TABLE is running
# After migration
curl -sS http://staging:8000/execute --data '{"request": "SELECT * FROM users WHERE id = 1"}'
To fine‑tune the recommendation, could you confirm which SurrealDB version you are running? The exact version can influence the replication lag thresholds and the behavior of blocking ALTER TABLE operations.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.