Distributed

Topology-aware routing

Route server-side reads and writes across a replication group and refresh discovered addresses after authority failures.

TopologyAwareClient selects a node for each read and sends writes to the primary. Static discovery uses configured addresses, while coordinator discovery obtains routing metadata from the group's HTTP servers.

Keep @delali/sirannon-db/client/topology in trusted server-side processes. This client can obtain internal node addresses and uses a credential authorised to read cluster metadata. Browser applications use SirannonClient.

Route to addresses you configure

Static discovery uses primary and replicas. It provides no automatic promotion or discovery of a replacement primary. An operator or deployment system must update those addresses when authority changes.

readPreference: 'replica' chooses a reachable configured replica at random and falls back to the primary. 'primary' uses the primary, while 'nearest' measures health-request latency and caches the measurements for 60,000 milliseconds.

Let the cluster say where to go

Coordinator discovery treats endpoints as seed HTTP addresses and requests GET /db/:id/cluster. The response supplies the primary address, primary term, and read endpoints with their supported concerns.

The complete routing.ts file connects to the local three-node example in coordinator failover. Provide the same SIRANNON_OPERATOR_TOKEN after those nodes become ready.

routing.ts
import { operationRef } from '@delali/sirannon-db'
import { TopologyAwareClient } from '@delali/sirannon-db/client/topology'
 
const token = process.env.SIRANNON_OPERATOR_TOKEN
if (!token) throw new Error('Set SIRANNON_OPERATOR_TOKEN to the cluster operator credential')
 
const listOrders = operationRef<Record<string, never>, { id: number; total: number }>('listOrders')
const client = new TopologyAwareClient({
  endpoints: ['http://127.0.0.1:9876', 'http://127.0.0.1:9877', 'http://127.0.0.1:9878'],
  discovery: 'coordinator',
  readPreference: 'replica',
  readConcern: 'majority',
  headers: { Authorization: `Bearer ${token}` },
})
 
try {
  console.log(await client.database('orders').query(listOrders, {}))
} finally {
  client.close()
}

The group's data and authority determine the response, so this network example has no fixed result panel.

The server's authorizeClusterStatus must permit this credential. If no seed can supply usable routing metadata, discovery fails with ROUTING_ERROR. Invalid metadata fails with INVALID_RESPONSE.

These HTTP seed addresses differ from replication peer addresses. In coordinator mode, peers discover their addresses through sessions, and the replication engine refuses a static transportConfig.endpoints list.

Where each call goes

Writes use the primary. Coordinator discovery without a current primary fails a write with NO_SAFE_PRIMARY.

For coordinator reads, the client filters advertised endpoints by the client-wide concern before applying its preference. replica prefers an eligible non-primary node. nearest uses the first advertised eligible endpoint; it does not measure latency in this discovery mode.

A linearizable read goes to the current primary. Other selection paths can fall back to the primary or a locally readable endpoint. The selected server must enforce the requested concern and can refuse the read if it cannot satisfy it. Selecting an address alone provides no consistency guarantee.

Configure readConcern on this client. Coordinator mode uses majority selection when the option is absent. A per-call readConcern fails with INVALID_ARGUMENT, because the topology transport accepts the client-wide policy.

The single-server HTTP and WebSocket transports accept a per-call concern and send it to their server. Distributed replication defines the levels.

When the client's view goes stale

Coordinator mode refreshes metadata after STALE_PRIMARY, AUTHORITY_LOST, COORDINATOR_UNAVAILABLE, NO_SAFE_PRIMARY, or CONNECTION_ERROR. These codes can indicate changed authority or a dependency failure.

A failed write triggers refresh but does not replay the write. Its outcome may be uncertain, so reconcile it before requesting another non-idempotent write.

A failed read can retry once after refresh. An unreachable endpoint is set aside for 5,000 milliseconds before selection can use it again. Open subscriptions and live queries can move when refreshed metadata changes routing.

In static mode, an unreachable non-primary read endpoint can be set aside for the same interval and the read retried elsewhere. That fallback omits a read concern. A primary read failure propagates to the caller. Use coordinator mode when the application needs its authority-aware read policy.

Options

OptionDefaultBehaviour
endpointsnoneCoordinator discovery starts with these HTTP addresses.
primarynoneStatic routing sends writes to this address.
replicasnoneStatic routing can select these addresses for reads.
discovery'static'The client also accepts 'coordinator'.
readPreference'primary'The client also accepts 'replica' and 'nearest'.
readConcernabsentCoordinator selection treats absence as 'majority'.

The constructor also accepts the client options, including credentials, transport, reconnect policy, and request timeout.

SirannonClient refuses topology options with INVALID_ARGUMENT. Keep the topology client and its operator credential in the service that needs routing, and expose authorised application operations to end users.