Skip to content
NordBaseDocs

Search the docs

Find a page or a section of the NordBase docs

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

InstanceWhereWhat it does
PrimaryThe Home regionThe one Instance that accepts Writes. Every Database has exactly one
StandbyThe Home region, on another machineKept in step with the Primary, synchronously, so it can take over by Failover
ReplicaUsually another RegionA read-only copy that serves nearby Reads. Can be promoted to Primary
PlanReplicasStandby (high availability)
StartNoNo
Hobby, ProYesNo
Team, Scale, BusinessYesYes

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:

bash
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 select without locking clauses or functions that change data, show, and explain without analyze of 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 only may 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:

ConsistencyWhere Reads goUse it when
nearest (default)The nearest healthy InstancePages, lists, search, anything where milliseconds of lag do not matter
strongThe PrimaryA 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:

ts
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}`
python
from nordbase import connect

db = connect()                            # nearest
primary = connect(consistency="strong")   # every Read from the Primary

The 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.

ts
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 Primary

The window is the stickyAfterWriteMs option (sticky_after_write_ms in Python), 2000 by default:

ts
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

PortPoolingInstanceGood for
5432Transaction poolingRouted: nearest for Reads, Primary for WritesApp queries
5433Session poolingAlways the PrimaryMigrations, 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 local inside a transaction instead of set;
  • do not rely on temporary tables, listen or 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

FailoverPromotion
WhatA Standby becomes the PrimaryA Replica becomes the Primary
Who starts itNordBase, automatically, when the Primary failsYou, on purpose
Home regionStays the sameMoves to the Replica's Region
PlansTeam, 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:

bash
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.