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.
The plan finds a few rows with an index. Live, to the last write.
The plan reads a whole big table. Your MySQL does no work for it.
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
LIMITwith 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=JSONin a new layout by default, which the router does not read. So each new connection to a MySQL 9 source first sendsSET SESSION explain_json_format_version = 1(0.133.0 or newer; before, every statement stayed on MySQL with the reasonexplain_failed). Your ownEXPLAIN FORMAT=JSONthrough 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
SELECTgoes 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 calledCOUNT(*)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_FORMATandDATEDIFFover a date, so a report that groups its rows by month stays on the copy; andCURRENT_TIMESTAMP,CURRENT_DATE,LOCALTIMEandLOCALTIMESTAMP, which the copy is sent asNOW()andCURDATE(). 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_DATEandTIMESTAMPDIFF, 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
TIMEor aYEARcolumn, or a generated column, and aSELECT *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 -pNothing 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.
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=8gRun sudo systemctl edit bintrail-router and set, under [Service]:
CPUQuota=400%
MemoryMax=8GSay 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=8Run sudo systemctl edit bintrail-router and set, under [Service]:
Environment=BINTRAIL_ROUTER_CONCURRENCY=8Then 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=30sEnvironment=BINTRAIL_ROUTER_MAX_WAIT=30sWrite 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.
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:
SQL_FRESH=off # every server fast. on: every server fresh. Unset: each server's choiceThen 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 itIt 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 STATUSascopy_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 STATUScounts it ascopy_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.
Fast queries on your app's connection
Point your application's reads at port 3309. Heavy queries come back in seconds from DBTrail's copy, lookups are still answered by your MySQL, and you do not change a query.
Routing in production
What to know before an application depends on port 3309 with Engine Routing on, how to let writes through, and what each error means.