Follower Read

This document introduces how to use Follower Read to optimize query performance.

Introduction

TiDB uses Region as the basic unit to distribute data to all nodes in the cluster. A Region can have multiple replicas, and the replicas are divided into a leader and multiple followers. When the data on the leader changes, TiDB will update the data to the followers synchronously.

By default, TiDB only reads and writes data on the leader of the same Region. When a read hotspot occurs in a Region, the Region leader can become a read bottleneck for the entire system. In this situation, enabling the Follower Read feature can significantly reduce the load of the leader and improve the throughput of the whole system by balancing the load among multiple followers.

When to use

Reduce read hotspots

You can visually analyze whether your application has a hotspot Region on the TiDB Dashboard Key Visualizer Page. You can check whether a read hotspot occurs by selecting the “metrics selection box” to Read (bytes) or Read (keys).

For more information about handling hotspot, see TiDB Hotspot Problem Handling.

You can visually analyze whether your application has a hotspot Region on the TiDB Cloud Key Visualizer Page. You can check whether a read hotspot occurs by selecting the “metrics selection box” to Read (bytes) or Read (keys).

For more information about handling hotspot, see TiDB Hotspot Problem Handling.

If read hotspots are unavoidable or the changing cost is very high, you can try using the Follower Read feature to better load the balance of reading requests to the follower Region.

Reduce latency for geo-distributed deployments

If your TiDB cluster is deployed across districts or data centers, different replicas of a Region are distributed in different districts or data centers. In this case, you can configure Follower Read as closest-adaptive or closest-replicas to allow TiDB to prioritize reading from the current data center, which can significantly reduce the latency and traffic overhead of read operations. For implementation details, see Follower Read.

Enable Follower Read

  • SQL
  • Java

To enable Follower Read, set the variable tidb_replica_read (default value is leader) to follower, leader-and-follower, prefer-leader, closest-replicas, or closest-adaptive:

  1. SET [GLOBAL] tidb_replica_read = 'follower';

For more details about this variable, see Follower Read Usage.

In Java, to enable Follower Read, define a FollowerReadHelper class.

  1. public enum FollowReadMode {
  2. LEADER("leader"),
  3. FOLLOWER("follower"),
  4. LEADER_AND_FOLLOWER("leader-and-follower"),
  5. CLOSEST_REPLICA("closest-replica"),
  6. CLOSEST_ADAPTIVE("closest-adaptive"),
  7. PREFER_LEADER("prefer-leader");
  8. private final String mode;
  9. FollowReadMode(String mode) {
  10. this.mode = mode;
  11. }
  12. public String getMode() {
  13. return mode;
  14. }
  15. }
  16. public class FollowerReadHelper {
  17. public static void setSessionReplicaRead(Connection conn, FollowReadMode mode) throws SQLException {
  18. if (mode == null) mode = FollowReadMode.LEADER;
  19. PreparedStatement stmt = conn.prepareStatement(
  20. "SET @@tidb_replica_read = ?;"
  21. );
  22. stmt.setString(1, mode.getMode());
  23. stmt.execute();
  24. }
  25. public static void setGlobalReplicaRead(Connection conn, FollowReadMode mode) throws SQLException {
  26. if (mode == null) mode = FollowReadMode.LEADER;
  27. PreparedStatement stmt = conn.prepareStatement(
  28. "SET GLOBAL @@tidb_replica_read = ?;"
  29. );
  30. stmt.setString(1, mode.getMode());
  31. stmt.execute();
  32. }
  33. }

When reading data from the Follower node, use the setSessionReplicaRead(conn, FollowReadMode.LEADER_AND_FOLLOWER) method to enable the Follower Read feature, which can balance the load between the Leader node and the Follower node in the current session. When the connection is disconnected, it will be restored to the original mode.

  1. public static class AuthorDAO {
  2. // Omit initialization of instance variables...
  3. public void getAuthorsByFollowerRead() throws SQLException {
  4. try (Connection conn = ds.getConnection()) {
  5. // Enable the follower read feature.
  6. FollowerReadHelper.setSessionReplicaRead(conn, FollowReadMode.LEADER_AND_FOLLOWER);
  7. // Read the authors list for 100000 times.
  8. Random random = new Random();
  9. for (int i = 0; i < 100000; i++) {
  10. Integer birthYear = 1920 + random.nextInt(100);
  11. List<Author> authors = this.getAuthorsByBirthYear(birthYear);
  12. System.out.println(authors.size());
  13. }
  14. }
  15. }
  16. public List<Author> getAuthorsByBirthYear(Integer birthYear) throws SQLException {
  17. List<Author> authors = new ArrayList<>();
  18. try (Connection conn = ds.getConnection()) {
  19. PreparedStatement stmt = conn.prepareStatement("SELECT id, name FROM authors WHERE birth_year = ?");
  20. stmt.setInt(1, birthYear);
  21. ResultSet rs = stmt.executeQuery();
  22. while (rs.next()) {
  23. Author author = new Author();
  24. author.setId( rs.getLong("id"));
  25. author.setName(rs.getString("name"));
  26. authors.add(author);
  27. }
  28. }
  29. return authors;
  30. }
  31. }

Read more