DBTrail

How routing decides

Which statements your MySQL answers and which ones DBTrail's copy answers on port 3309, what the copy cannot answer, what happens when it is busy, and how old the copy is.

With Engine Routing on, every statement that reaches port 3309 is answered by your MySQL or by the copy. This page is how that is decided.

Who answers what

For most SELECTs, DBTrail asks your MySQL one question: how would you run this? MySQL answers with its plan and runs nothing.

Light read
SELECT * FROM orders WHERE id = 42;
your MySQL

The plan finds a few rows with an index. Live, to the last write.

Heavy read
SELECT status, COUNT(*) FROM orders GROUP BY status;
the copy

The plan reads a whole big table. Your MySQL does no work for it.

Write
UPDATE orders SET status = 'paid' WHERE id = 42;
refused · 1290

As it comes, the port only reads. The write never reaches your MySQL.

You do not keep a list. A query that is light today and heavy next year moves over by itself. The rules, in plain words:

  • Heavy means MySQL's own plan says so: a cost of 10,000 or more, or a whole table of 100,000 rows or more read from end to end. On a MariaDB source the cost is not used: the rows read decide, whether the plan reads the whole table, a whole index or a range of one. Page one of a large range, a LIMIT with no sort or grouping before it, stays on MariaDB.
  • On MySQL 9.x the plan is asked for in the 8.0 layout. MySQL 9.7 writes EXPLAIN FORMAT=JSON in a new layout by default, which the router does not read. So each new connection to a MySQL 9 source first sends SET SESSION explain_json_format_version = 1 (0.133.0 or newer; before, every statement stayed on MySQL with the reason explain_failed). Your own EXPLAIN FORMAT=JSON through the port comes back in that layout. A client that sets the variable back to 2 keeps its statements on MySQL.
  • Everything that is not a SELECT goes to your MySQL: SHOW, SET, USE, BEGIN.
  • Inside a transaction, everything goes to your MySQL, so a read after your own write sees that write.
  • A query the copy would answer differently goes to your MySQL. Being right comes before saving work.
  • A result over 10,000 rows goes to your MySQL. The copy answers reports, not exports.
  • Column names are your MySQL's, whoever answers. COUNT(*) is called COUNT(*) either way, so code that reads a result by column name keeps working.
  • A server with no forwarding user is not routed. The copy answers it alone, the light reads too, and the panel says so.

What the copy cannot answer

The copy runs DuckDB, not MySQL. Your application keeps writing MySQL's SQL and always gets its answer, because what the copy cannot answer correctly, your MySQL answers.

  • A few things are translated for the copy: names in backticks, which most ORMs write; DATE_FORMAT and DATEDIFF over a date, so a report that groups its rows by month stays on the copy; and CURRENT_TIMESTAMP, CURRENT_DATE, LOCALTIME and LOCALTIMESTAMP, which the copy is sent as NOW() and CURDATE(). Nothing else. The dates and the clock words are translated since 0.132.0; before, they went to your MySQL.
  • What the copy does not have, your MySQL answers: STR_TO_DATE and TIMESTAMPDIFF, for example.
  • What the copy would answer differently, with no error, is kept on your MySQL: GROUP_CONCAT, NOW(3), CURRENT_TIME, JSON functions, and more.
  • Some tables are kept on your MySQL too: a statement that names a TIME or a YEAR column, or a generated column, and a SELECT * over a table whose columns the copy cannot return as MySQL does.

Correct, but not taken off your MySQL. How much moves to the copy depends on how your reports are written. sql-compare, in step 7 of the setup, tells you which of yours go where, and Why each side on the panel counts them by reason. The known differences: SQL on the copy.

The copy alone, for one connection

Add @copy to the user, and the copy answers everything on that connection, whatever the routing switch says. The other connections stay routed.

mysql -h 127.0.0.1 -P 3309 -u shop@copy -p

Nothing is sent to your MySQL on that connection, and nothing is translated: write DuckDB's SQL there. sql-compare needs such a connection while routing is on.

When it is busy

Heavy queries are run by a small number of workers. DBTrail works out how many the machine can carry. When all of them are busy, the next heavy query waits its turn, in the order it arrived, the way a query waits on a busy MySQL. It is never handed to your MySQL because the copy is busy. Any number of applications can stay connected: 256 connections sending heavy queries to a copy that runs 4 at once are all answered, more slowly. Each connected application also holds one connection to your MySQL, so its max_connections has to allow them.

0wait for lightA light query does not wait in that line. It goes straight to your MySQL.
No limiton the waitA heavy query waits for as long as its application stays connected. If the application leaves, the query leaves the line and runs nowhere.
4,096can waitOne more gets error 1040, too many connections, at once.
2cores per workerAnd about 2.5 GB of memory each. 4 cores and 8 GB run two heavy queries at once, unless you set the number.

With no ceiling set, the service that answers the port takes half of the machine and leaves the rest for capture. To give it more, or less, set a ceiling. It then uses all of what you gave it.

These settings are new in DBTrail 0.104.1. Before it the number could not be set, and a heavy query waited 30 seconds at most.

In .env, next to docker-compose.yml, then docker compose up -d:

ROUTER_CPUS=4
ROUTER_MEMORY=8g

Run sudo systemctl edit bintrail-router and set, under [Service]:

CPUQuota=400%
MemoryMax=8G

Say how many run at once

DBTrail works the number out: one heavy query for every two cores, as many as the memory holds. To set it yourself, from 1 to 64, the way innodb_thread_concurrency does on a MySQL server:

In .env, with the current docker-compose.yml, then docker compose up -d:

ROUTER_CONCURRENCY=8

Run sudo systemctl edit bintrail-router and set, under [Service]:

Environment=BINTRAIL_ROUTER_CONCURRENCY=8

Then sudo systemctl restart bintrail-router.

A number above one for every two cores is taken as you wrote it. Each query then runs with fewer threads, down to one: more run at once, and each one is slower. A high number has three costs:

  • Memory. Each query may use about 2.5 GB. If the number can ask for more than the port has, the service prints a line that begins Router: WARNING: when it starts, with the sum.
  • Temporary disk. The disk a query may use when it runs out of memory is shared among them. With many, each gets little or none, and a query that used to finish slowly can fail.
  • Idle processes. The service keeps that many workers ready, a few tens of MB each.

Put a limit on the wait

There is none by default. To refuse a heavy query that has waited too long, with error 1040, which drivers and pools retry:

ROUTER_MAX_WAIT=30s
Environment=BINTRAIL_ROUTER_MAX_WAIT=30s

Write the time with its unit, from 1s to 24h.

How current the copy is

A heavy query reads the copy, and the copy is seconds behind your MySQL.

2.5 s95 of 100 answersWere under this in the run drawn above: 0.129.0, about 1,200 rows changed per second on the source, 15 minutes.
20 sthe worstOne answer in that run.
9 smedian, heavier loadAn earlier version at 500 transactions per second. The worst was 289 seconds.
Livelight queriesThey read your MySQL, so this is not about them.

Each answer says how far behind it was. Watch it the way you watch a replica's lag; the panel shows it as Lag, with Max lag since the router started.

Fresh costs something: each query also reads the recent changes, so it takes somewhat longer than in fast mode, and the router keeps them in memory. Choose Fast for a server whose reports can be minutes old, on its Snapshots page, How current SQL answers are. For every server at once:

.env
SQL_FRESH=off   # every server fast. on: every server fresh. Unset: each server's choice

Then docker compose up -d router (BINTRAIL_SQL_TAILS outside Compose). Before 0.129.0 the default was Fast. The heavier load is this benchmark.

A limit on how far behind

New in 0.132.0, and off unless you save it. Give a server a limit, say 5 minutes, and a heavy query whose data would be more than 5 minutes behind your MySQL is answered by your MySQL instead of the copy. Under the limit, nothing changes.

Save it from the terminal, on the machine DBTrail runs on (the dbtrail command):

dbtrail port max-behind shop 5m     # or 300, in seconds
dbtrail port max-behind shop        # say the limit saved
dbtrail port max-behind shop off    # remove it

It changes the limit only: the server's fresh or fast choice stays as it was. dbtrail port status lists the limit of each server. The page has no control for it yet; from a script, the same setting is max_behind_source_seconds (0 removes it):

curl -X PUT -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  http://127.0.0.1:8090/api/servers/<id>/sql-mode -d '{"max_behind_source_seconds": 300}'

What happens past the limit:

  • On a routed connection, your MySQL answers. Why each side on the panel counts it ("the copy was further behind the source than this server's limit"), and SHOW ROUTER STATUS as copy_behind_source.
  • On a connection only the copy answers (shop@copy, or routing off), there is no one else to answer, so the client gets error 1105 with how far behind the copy is and the limit.
  • When how far behind cannot be told, for example because the router cannot reach the server's index, the limit counts as passed: your MySQL answers, and SHOW ROUTER STATUS counts it as copy_age_unknown.

How it is measured:

  • It is the lag each answer reports, read before the statement runs. In fresh mode an old copy whose recent changes are caught up is within the limit, and a quiet source whose capture is caught up is 0 behind.
  • Pick minutes, not seconds. Capture's state is read every 30 seconds, so a limit under about a minute and a half is not kept to the second. The command says so when you save one.
  • A change reaches open connections within a few seconds. No connection is closed.
  • A query that reads only the change log (events) is not held to it.

Every detail: A lag limit per server, in the reference.

More

Every rule and setting: Engine Routing, in the reference.

On this page