Databases
Replicas and Consistency
Primary, Standby and Replica, how Reads and Writes are routed, nearest and strong Consistency, pooling on the two SQL ports, and Promotion versus Failover.
A Database can have several running copies, called Instances. Each one runs in one Region with one Size and uses its share of your Pool.
Primary, Standby and Replica
| Instance | Where | What it does |
|---|---|---|
| Primary | The Home region | The one Instance that accepts Writes. Every Database has exactly one |
| Standby | The Home region, on another machine | Kept in step with the Primary, synchronously, so it can take over by Failover |
| Replica | Usually another Region | A read-only copy that serves nearby Reads. Can be promoted to Primary |
| Plan | Replicas | Standby (high availability) |
|---|---|---|
| Start | No | No |
| Hobby, Pro | Yes | No |
| Team, Scale, Business | Yes | Yes |
Each Standby and Replica counts as an Instance and uses its Size from your Pool, and storage counts every copy. A Database of 3 GB with one Replica uses 6 GB of storage.
Adding a Replica
In the console, open the Database, go to Replicas and choose a Region. Or through the API:
curl -X POST "https://api.nordbase.<base>/v1/databases/db-k7q2x9m4p1/replicas" \
-H "Authorization: Bearer $NORDBASE_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 8d1e6b2a-3f4c-4a7e-9b0d-5c2f1e8a7d36" \
-d '{ "region": "de-fsn", "size": "micro" }'The answer is an Operation you can follow while the Replica copies the data and catches up. When it is available, the Database has an Endpoint in that Region, and the TXT record lists it, so the libraries find it on their next start.
If the Replica does not fit your Pool, or your Plan has no Replicas, the API answers 402 with the smallest Plan that fits.
How Reads and Writes are routed
On the routed port, 5432, the Router in each Region classifies every statement with PostgreSQL's own parser:
- Read: a
selectwithout locking clauses or functions that change data,show, andexplainwithoutanalyzeof a Write. It goes to the nearest healthy Instance, which may be a Replica. - Write: everything else. It goes to the Primary, even when the Primary is in another Region.
- Transactions: a transaction that contains a Write is sent to the Primary as a whole. A transaction started with
begin read onlymay use a Replica.
Any Region's Endpoint takes Writes. If your app connects to the Falkenstein Endpoint and the Primary is in Helsinki, Writes are forwarded to Helsinki for you.
Consistency: nearest and strong
Replicas apply the Primary's changes a little later, normally within milliseconds. The API shows each Replica's lag in lag_ms. Consistency says whether a Read may be served by a Replica:
| Consistency | Where Reads go | Use it when |
|---|---|---|
nearest (default) | The nearest healthy Instance | Pages, lists, search, anything where milliseconds of lag do not matter |
strong | The Primary | A Read must see every committed Write, for example a balance check before a payment |
You can set Consistency per client and per call. Per client:
import { createClient } from 'nordbase'
const db = createClient() // nearest
const primary = createClient(process.env.NORDBASE_URL, { consistency: 'strong' }) // every Read from the Primary
const [account] = await primary.sql`select balance from accounts where id = ${accountId}`from nordbase import connect
db = connect() # nearest
primary = connect(consistency="strong") # every Read from the PrimaryThe form for a single call is coming soon to the library pages.
The libraries add a routing hint to the statement to send it to the Primary. Do not write that hint yourself: its form may change without notice. With another driver or ORM, connect to the Direct port, 5433, when every Read must come from the Primary.
Read-your-writes
After a client makes a Write, that client's Reads go to the Primary for the next 2,000 ms. So a page that saves a form and then reloads it shows the saved data, even if a Replica is a few milliseconds behind.
await db.sql`update customers set name = ${name} where id = ${id}`
const [customer] = await db.sql`select name from customers where id = ${id}` // served by the PrimaryThe window is the stickyAfterWriteMs option (sticky_after_write_ms in Python), 2000 by default:
const db = createClient(process.env.NORDBASE_URL, { stickyAfterWriteMs: 5000 })It covers one client in one process. When one process writes and another reads, for example a worker that picks up an order a web request just created, the reader should use strong.
Transaction pooling and the Direct port
| Port | Pooling | Instance | Good for |
|---|---|---|---|
| 5432 | Transaction pooling | Routed: nearest for Reads, Primary for Writes | App queries |
| 5433 | Session pooling | Always the Primary | Migrations, LISTEN/NOTIFY, session SET, advisory locks, temporary tables |
With transaction pooling, your connection to the Router is shared: between transactions it may be served by a different PostgreSQL connection. Session state does not survive from one transaction to the next. On port 5432:
- use
set localinside a transaction instead ofset; - do not rely on temporary tables,
listenor session-level advisory locks across statements.
Prepared statements work on both ports.
Use the Direct port for migrations: tools such as prisma migrate and drizzle-kit expect a session that stays on one connection. Change 5432 to 5433 in the Connection URL, nothing else.
Promotion and Failover
| Failover | Promotion | |
|---|---|---|
| What | A Standby becomes the Primary | A Replica becomes the Primary |
| Who starts it | NordBase, automatically, when the Primary fails | You, on purpose |
| Home region | Stays the same | Moves to the Replica's Region |
| Plans | Team, Scale, Business (needs a Standby) | Any Plan with Replicas |
Failover happens in about 30 seconds. The Standby is synchronous, so a Write that was committed is not lost. During the switch, connections drop with connection errors. The libraries retry Reads, and transactions that never reached the Database, for up to 30 seconds. A Write whose outcome is unknown is never retried automatically; the error says so. See Errors.
Without a Standby, if the machine under a Primary fails, NordBase rebuilds the Database on another machine from its archive automatically. This takes minutes rather than seconds, and grows with the size of the Database. At most the last 60 seconds of Writes can be lost.
Promotion turns a Replica into the Primary and moves the Home region to its Region, for example to move a Database to another country:
curl -X POST "https://api.nordbase.<base>/v1/databases/db-k7q2x9m4p1/replicas/in_3v8q1m0x7k2p9d4f/promote" \
-H "Authorization: Bearer $NORDBASE_ACCESS_TOKEN"Promotion is an Operation: follow it until it succeeds. Afterwards, Writes from every Region's Endpoint go to the new Primary.
Document Databases
For now, every Read and Write on a Document Database goes to the Primary, so Consistency does not apply yet. You can still add Replicas, for Promotion and to keep a copy in another Region. Reads from Replicas for Document Databases are coming later.
Functions that write
The libraries decide read-your-writes from the statement's first keyword, so select archive_old_orders() counts as a Read even if the function writes. After calling a function that writes, read with consistency: 'strong' until the change has replicated.