Schema Routing Rules

The Vitess routing rules feature is a powerful mechanism for directing query traffic to the right keyspaces, shards, and tablet types in Vitess Gateways (vtgate). Their primary usage today is for the following use case:

  • Routing traffic during data migrations: during e.g. MoveTables and Reshard operations, routing rules dictate where to send reads and writes. These routing rules are managed automatically by VReplication. You can see an example of their usage in the MoveTables user guide.

Understanding the routing rules can help you debug migration related issues as well as provide you with another powerful tool as you operate Vitess.

Viewing Routing Rules

The routing rules are global and can be viewed using the GetRoutingRules client command.

Updating Routing Rules

You can update the routing rules using the ApplyRoutingRules client command.

Syntax

Routing rules are managed using the JSON format. Here’s an example, using the routing rules that are put in place by MoveTables in the local examples where the customer and corder tables are being moved from the commerce keyspace to the customer keyspace and we have not yet switched traffic from the commerce keyspace to the customer keyspace — so all traffic, regardless of which keyspace a client uses, are sent to the commerce keyspace:

  1. $ vtctldclient --server=localhost:15999 GetRoutingRules
  2. {
  3. "rules": [
  4. {
  5. "from_table": "customer.customer",
  6. "to_tables": [
  7. "commerce.customer"
  8. ]
  9. },
  10. {
  11. "from_table": "commerce.corder@replica",
  12. "to_tables": [
  13. "commerce.corder"
  14. ]
  15. },
  16. {
  17. "from_table": "customer.customer@rdonly",
  18. "to_tables": [
  19. "commerce.customer"
  20. ]
  21. },
  22. {
  23. "from_table": "commerce.corder@rdonly",
  24. "to_tables": [
  25. "commerce.corder"
  26. ]
  27. },
  28. {
  29. "from_table": "corder@replica",
  30. "to_tables": [
  31. "commerce.corder"
  32. ]
  33. },
  34. {
  35. "from_table": "commerce.customer@replica",
  36. "to_tables": [
  37. "commerce.customer"
  38. ]
  39. },
  40. {
  41. "from_table": "commerce.customer@rdonly",
  42. "to_tables": [
  43. "commerce.customer"
  44. ]
  45. },
  46. {
  47. "from_table": "customer.corder@replica",
  48. "to_tables": [
  49. "commerce.corder"
  50. ]
  51. },
  52. {
  53. "from_table": "customer.corder@rdonly",
  54. "to_tables": [
  55. "commerce.corder"
  56. ]
  57. },
  58. {
  59. "from_table": "customer.customer@replica",
  60. "to_tables": [
  61. "commerce.customer"
  62. ]
  63. },
  64. {
  65. "from_table": "customer.corder",
  66. "to_tables": [
  67. "commerce.corder"
  68. ]
  69. },
  70. {
  71. "from_table": "corder@rdonly",
  72. "to_tables": [
  73. "commerce.corder"
  74. ]
  75. },
  76. {
  77. "from_table": "customer@replica",
  78. "to_tables": [
  79. "commerce.customer"
  80. ]
  81. },
  82. {
  83. "from_table": "customer@rdonly",
  84. "to_tables": [
  85. "commerce.customer"
  86. ]
  87. },
  88. {
  89. "from_table": "corder",
  90. "to_tables": [
  91. "commerce.corder"
  92. ]
  93. },
  94. {
  95. "from_table": "customer",
  96. "to_tables": [
  97. "commerce.customer"
  98. ]
  99. }
  100. ]
  101. }

When Routing Rules Are Applied

In the above example, we send all query traffic for the customer and corder tables to the commerce keyspace regardless of how the client specifies the database/schema and table qualifiers. There is, however, one important exception and that is when the client explicitly requests the usage of a specific shard, also known as “shard targeting”. For example, if the client specifies the database as customer:0 or customer:0@replica then the query will get run against that shard in the customer keyspace.

You should exercise extreme caution when executing ad-hoc write queries during this time as you may think that you’re deleting data from the target keyspace, that is as of yet unused, when in reality you’re deleting it from the source keyspace that is currently serving production traffic.

You can leverage shard targeting to perform ad-hoc read-only queries against the target and source keyspace/shards to perform any additional data validation or checks that you want (beyond VDiff). You can also use this shard targeting to see how your data is distributed across the keyspace’s shards.

Additional Details

There are some key details to keep in mind if you will be creating and managing your own custom routing rules.

  • The to_tables field must contain only one entry and the table name must be fully qualified.

  • If the from_table is qualified by a keyspace, then a query that references that table will get redirected to the corresponding target table. The reference need not be explicit. For example, if you are connected to the customer keyspace, then an unqualified reference to the customer table is interpreted as a qualified reference to customer.customer.

  • You may further add a tablet type to the from_table field using the @<type> syntax seen in the example above. If so, only queries that target that tablet type will get redirected. Although you can qualify a table by its keyspace in a query, there is no equivalent syntax for specifying the tablet type. The only way to choose a tablet type is through the use statement, like use customer@replica, or by specifying it in the connection string.

  • The more specific rules supercede the less specific one. For example, customer.customer@replica is chosen over customer.customer if the current tablet type is a replica.

  • If the to_tables have special characters that need escaping, you can use the mysql backtick syntax to do so. As for the from_tables, the table name should not be escaped. Instead, you should just concatenate the table with the keyspace without the backticks. In the following example, we are redirecting the b.c table to the c.b table in keyspace a:

    1. {
    2. "rules": [
    3. {
    4. "from_table": "a.b.c",
    5. "to_tables": [
    6. "a.`c.b`"
    7. ]
    8. }
    9. ]
    10. }