Skip to main content
In a multi-region deployment, are a good choice for tables with the following requirements:
  • Read latency must be low, but write latency can be higher.
  • Reads can be historical.
  • Rows in the table, and all latency-sensitive queries, cannot be tied to specific geographies (e.g., a reference table).
  • Table data must remain available during a region failure.
If reads can use stale data, use . If reads must be exactly up-to-date, use to achieve . Up-to-date reads are required by tables referenced by , for example.

Before you begin

Fundamentals

  • Multi-region topology patterns are almost always table-specific. If you haven’t already, to ensure you choose the right one for each of your tables.
  • Review how data is replicated and distributed across a cluster, and how this affects performance. It is especially important to understand the concept of the “leaseholder”. For a summary, see . For a deeper dive, see the CockroachDB .
  • Review the concept of , which CockroachDB uses to place and balance data based on how you define .
  • Review the recommendations and requirements in our .
  • This topology doesn’t account for hardware specifications, so be sure to follow our and perform a POC to size hardware for your use case. For optimal cluster performance, Cockroach Labs recommends that all nodes use the same hardware and operating system.
  • Adopt relevant to ensure optimal performance.

Cluster setup

Each assumes the following setup: Multi-region hardware setup

Hardware

  • 3 regions
  • Per region, 3+ AZs with 3+ VMs evenly distributed across them
  • Region-specific app instances and load balancers
    • Each load balancer redirects to CockroachDB nodes in its region.
    • When CockroachDB nodes are unavailable in a region, the load balancer redirects to nodes in other regions.

Cluster startup

Start each node with the flag specifying its region and AZ combination. For example, the following command starts a node in the west1 AZ of the us-west region:

Configuration

With each node started with the flag specifying its region and zone combination, CockroachDB will balance the replicas for a table across the three regions: Follower reads table replication

Summary

You configure your application to use by adding an clause when reading from the table. With this clause CockroachDB reads slightly historical data from the closest replica so as to avoid being routed to the leaseholder, which may be in an entirely different region. Writes, however, will still leave the region to get consensus for the table.

Steps

  1. Create the postal_codes table:
  2. Insert data into the postal_codes table:
  3. Decide which type of follower read to perform: exact staleness or bounded staleness. For more information about when to use each type of read, see and .
    • To use , configure your app to use with the whenever reading from the table:
      You can also set the AS OF SYSTEM TIME value for all operations in a read-only transaction:
Using the statement as shown in the preceding example will make it easier to use exact staleness follower reads from .
  • To use , configure your app to use with the whenever reading from the table. Note that only single-row point reads in single-statement (implicit) transactions are supported.

Characteristics

Latency

Reads

Reads retrieve historical data from the closest replica and, therefore, never leave the region. This makes read latency very low but slightly stale. For example, in the following diagram:
  1. The read request in us-central reaches the regional load balancer.
  2. The load balancer routes the request to a gateway node.
  3. The gateway node routes the request to the closest replica for the table. In this case, the replica is not the leaseholder.
  4. The replica retrieves the results as of your preferred staleness interval in the past and returns to the gateway node.
  5. The gateway node returns the results to the client.
Follower reads topology reads

Writes

The replicas for the table are spread across all 3 regions, so writes involve multiple network hops across regions to achieve consensus. This increases write latency significantly. For example, in the following animation:
  1. The write request in us-central reaches the regional load balancer.
  2. The load balancer routes the request to a gateway node.
  3. The gateway node routes the request to the leaseholder replica for the table in us-east.
  4. Once the leaseholder has appended the write to its Raft log, it notifies its follower replicas.
  5. As soon as one follower has appended the write to its Raft log (and thus a majority of replicas agree based on identical Raft logs), it notifies the leaseholder and the write is committed on the agreeing replicas.
  6. The leaseholder then returns acknowledgement of the commit to the gateway node.
  7. The gateway node returns the acknowledgement to the client.
Follower reads topology writes

Resiliency

Because this pattern balances the replicas for the table across regions, one entire region can fail without interrupting access to the table: Follower reads topology region failure

See also

    • Single-region patterns
    • Multi-region patterns