Tunable Read Latency

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

With YugabyteDB, you can choose different consistency levels for performing reads. Relaxed consistency levels lead to lower latencies since the DB now has less work to do at read time including serving the read from the tablet followers. Reading from the followers is similar to a reading from a cache, which can give more read IOPS with low latency. In this tutorial, we will update a single key-value over and over, and read it from the tablet leader. While that workload is running, we will start another workload to read from a follower and verify that we are able to read from a tablet follower.

If you haven’t installed YugabyteDB yet, do so first by following the Quick Start guide.

1. Setup - create universe

If you have a previously running local universe, destroy it using the following.

  1. $ ./bin/yb-ctl destroy

Start a new local universe with the default 3 nodes and default replication factor 3.

  1. $ ./bin/yb-ctl create

Add 1 more node.

  1. $ ./bin/yb-ctl add_node

2. Write some data

By default, the key-value sample application runs with strong read consistency where all data is read from the tablet leader. We are going to populate exactly one key with a 10KB value into the system. Since the replication factor is 3, this key will get replicated to only 3 of the 4 nodes in the universe.

Let us run the Cassandra sample key-value app to constantly update this key-value, as well as perform reads with strong consistency against the local universe.

  1. $ java -jar ./java/yb-sample-apps.jar --workload CassandraKeyValue \
  2. --nodes 127.0.0.1:9042 \
  3. --nouuid \
  4. --num_unique_keys 1 \
  5. --num_threads_write 1 \
  6. --num_threads_read 1 \
  7. --value_size 10240

In the above command, we have set the value of num_unique_keys to 1, which means we are overwriting a single key key:0. We can verify this using cqlsh:

  1. $ ./bin/cqlsh 127.0.0.1
  1. Connected to local cluster at 127.0.0.1:9042.
  2. [cqlsh 5.0.1 | Cassandra 3.9-SNAPSHOT | CQL spec 3.4.2 | Native protocol v4]
  3. Use HELP for help.
  1. cqlsh> SELECT k FROM ybdemo_keyspace.cassandrakeyvalue;

  1. k

key:0

(1 rows)

3. Strongly consistent reads from tablet leaders

When performing strongly consistent reads as a part of the above command, all reads will be served by the tablet leader of the tablet that contains the key key:0. If we browse to the tablet-servers page, we will see that all the requests are indeed being served by one tserver:

Reads from the tablet leader

4. Timeline consistent reads from tablet replicas

Let us stop the above sample app, and run the following variant of the sample app. This command will do updates to the same key key:0 which will go through the tablet leader, but it will reads from the replicas.

  1. $ java -jar ./java/yb-sample-apps.jar --workload CassandraKeyValue \
  2. --nodes 127.0.0.1:9042 \
  3. --nouuid \
  4. --num_unique_keys 1 \
  5. --num_threads_write 1 \
  6. --num_threads_read 1 \
  7. --value_size 10240 \
  8. --local_reads

This can be easily seen by refreshing the tablet-servers page, where we will see that the writes are served by a single TServer that is the leader of the tablet for the key key:0 while multiple TServers which are replicas serve the reads.

Reads from the tablet follower

5. Clean up (optional)

Optionally, you can shutdown the local cluster created in Step 1.

  1. $ ./bin/yb-ctl destroy

1. Setup - create universe

If you have a previously running local universe, destroy it using the following.

  1. $ ./bin/yb-ctl destroy

Start a new local universe with the default 3 nodes and default replication factor 3.

  1. $ ./bin/yb-ctl create

Add 1 more node.

  1. $ ./bin/yb-ctl add_node

2. Write some data

By default, the key-value sample application runs with strong read consistency where all data is read from the tablet leader. We are going to populate exactly one key with a 10KB value into the system. Since the replication factor is 3, this key will get replicated to only 3 of the 4 nodes in the universe.

Let us run the Cassandra sample key-value app to constantly update this key-value, as well as perform reads with strong consistency against the local universe.

  1. $ java -jar ./java/yb-sample-apps.jar --workload CassandraKeyValue \
  2. --nodes 127.0.0.1:9042 \
  3. --nouuid \
  4. --num_unique_keys 1 \
  5. --num_threads_write 1 \
  6. --num_threads_read 1 \
  7. --value_size 10240

In the above command, we have set the value of num_unique_keys to 1, which means we are overwriting a single key key:0. We can verify this using cqlsh:

  1. $ ./bin/cqlsh 127.0.0.1
  1. Connected to local cluster at 127.0.0.1:9042.
  2. [cqlsh 5.0.1 | Cassandra 3.9-SNAPSHOT | CQL spec 3.4.2 | Native protocol v4]
  3. Use HELP for help.
  1. cqlsh> SELECT k FROM ybdemo_keyspace.cassandrakeyvalue;

  1. k

key:0

(1 rows)

3. Strongly consistent reads from tablet leaders

When performing strongly consistent reads as a part of the above command, all reads will be served by the tablet leader of the tablet that contains the key key:0. If we browse to the tablet-servers page, we will see that all the requests are indeed being served by one tserver:

Reads from the tablet leader

4. Timeline consistent reads from tablet replicas

Let us stop the above sample app, and run the following variant of the sample app. This command will do updates to the same key key:0 which will go through the tablet leader, but it will reads from the replicas.

  1. $ java -jar ./java/yb-sample-apps.jar --workload CassandraKeyValue \
  2. --nodes 127.0.0.1:9042 \
  3. --nouuid \
  4. --num_unique_keys 1 \
  5. --num_threads_write 1 \
  6. --num_threads_read 1 \
  7. --value_size 10240 \
  8. --local_reads

This can be easily seen by refreshing the tablet-servers page, where we will see that the writes are served by a single TServer that is the leader of the tablet for the key key:0 while multiple TServers which are replicas serve the reads.

Reads from the tablet follower

5. Clean up (optional)

Optionally, you can shutdown the local cluster created in Step 1.

  1. $ ./bin/yb-ctl destroy