Before you begin
Make sure you have already .Step 1. Start a 6-node cluster
-
In separate terminal windows, use the command to start six nodes:
-
Use the command to perform a one-time initialization of the cluster:
Step 2. Set up load balancing
In this tutorial, you run a sample workload to simulate multiple client connections. Each node is an equally suitable SQL gateway for the load, but it’s always recommended to . This section shows how to set up the open-source HAProxy load balancer.-
Install HAProxy.
If you’re on a Mac and use Homebrew, run:
If you’re using Linux and use apt-get, run:
-
Run the command, specifying the port of any node:
This command generates an
haproxy.cfgfile automatically configured to work with the nodes of your running cluster. -
In
haproxy.cfg, changebind:26257tobind:26000. This changes the port on which HAProxy accepts requests to a port that is not already in use by a node. -
Start HAProxy, with the
-fflag pointing to thehaproxy.cfgfile:
Step 3. Run a sample workload
Use the command to run CockroachDB’s built-in version of the YCSB benchmark, simulating multiple client connections, each performing mixed read/write operations.-
Load the initial
ycsbschema and data, pointing it at HAProxy’s port:The--splitsflag tells the workload to manually split ranges a number of times. This is not something you’d normally do, but for the purpose of this tutorial, it makes it easier to visualize the movement of data in the cluster. -
Run the
ycsbworkload, pointing it at HAProxy’s port:This command initiates 3 concurrent client workloads for 20 minutes, but limits the total load to 1000 operations per second (since you’re running everything on a single machine). You’ll see per-operation statistics print to standard output every second:After the specified duration (20 minutes in this case), the workload will stop and you’ll see totals printed to standard output.
Step 4. Check the workload
Initially, the workload creates a new database calledycsb, creates the table public.usertable in that database, and inserts rows into the table. Soon, the load generator starts executing approximately 95% reads and 5% writes.
- Go to the DB Console at http://localhost:8080.
-
To check the SQL queries getting executed, click Metrics on the left, and hover over the SQL Statements graph at the top:

-
To check the client connections from the load generator, select the SQL dashboard and hover over the Open SQL Sessions graph:
You’ll notice 3 client connections from the load generator. If you want to check that HAProxy balanced each connection to a different node, you can change the Graph dropdown from Cluster to specific nodes.
-
To see more details about the
ycsbdatabase and thepublic.usertabletable, click Databases in the upper left and click ycsb: You can also view the schema and other table details of
public.usertableby clicking the table name:
-
By default, CockroachDB replicates all data 3 times and balances it across all nodes. To see this balance, click Overview and check the replica count across all nodes:

Step 5. Simulate a single node failure
When a node fails, the cluster waits for the node to remain offline for 5 minutes by default before considering it dead, at which point the cluster automatically repairs itself by re-replicating any of the replicas on the down nodes to other available nodes.-
In a new terminal, to reduce the amount of time the cluster waits before considering a node dead to the minimum allowed of 1 minute and 15 seconds:
-
Get the process IDs of the nodes:
-
Gracefully shut down the node stored in
fault-node5, specifying its process ID (in this example,53708):
Step 6. Check load continuity and cluster health
Go back to the DB Console, click Metrics on the left, and verify that the cluster as a whole continues serving data, despite one of the nodes being unavailable and marked as Suspect:
Step 7. Watch the cluster repair itself
Click Overview on the left:
Step 8. Prepare for two simultaneous node failures
At this point, the cluster has recovered and is ready to handle another failure. However, the cluster cannot handle two near-simultaneous failures in this configuration. Failures are “near-simultaneous” if they are closer together than theserver.time_until_store_dead plus the time taken for the number of replicas on the dead node to drop to zero. If two failures occurred in this configuration, some ranges would become unavailable until one of the nodes recovers.
To be able to tolerate 2 of 5 nodes failing simultaneously without any service interruption, ranges must be replicated 5 times.
-
In the terminal window where you started
fault-node5initially, start it again using the same command you used to start the node initially: -
Use the command to change the cluster’s
defaultreplication factor to 5: -
In the DB Console Overview dashboard, watch the replica count increase and even out across all 6 nodes:
This shows the cluster up-replicating so that each range has 5 replicas, one on each node.
Step 9. Simulate two simultaneous node failures
Gracefully shut down 2 nodes, specifying the process IDs you retrieved earlier:Step 10. Check cluster status and service continuity
-
Click Overview on the left, and verify the state of the cluster:

-
To verify that the cluster still serves data, use the
cockroach sqlcommand to interact with the cluster. Count the number of rows in theycsb.public.usertabletable to see that it serves reads:Insert data to see that it serves writes:This shows that when all ranges are replicated 5 times, the cluster can tolerate 2 simultaneous node outages because the surviving nodes have a majority of each range’s replicas (3/5).
Step 11. Clean up
- In the terminal where the YCSB workload is running, press CTRL + c.
-
Terminate HAProxy:
-
Gracefully shut down the remaining 4 nodes, specifying the process IDs you retrieved earlier and the new process ID for the node stored in
fault-node5:
For the final 2 nodes, the shutdown process will take longer (about a minute each) and will eventually force the nodes to stop. This is because, with only 2 of 5 nodes left, a majority of replicas are not available, and so the cluster is no longer operational.
-
To restart the cluster at a later time, run the same
cockroach startcommands as in Step 1. Start a 6-node cluster from the directory containing the nodes’ data stores. -
If you do not plan to restart the cluster, you may want to remove the nodes’ data stores and the HAProxy config files:

