DBTrail

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.

Engine Routing puts DBTrail between your application and its MySQL for reads. This page is what that means on a bad day, and what each error is telling you.

Before production

Read these once before an application depends on this port.

To stop using it, point your application back at your MySQL. To have the copy answer everything again, press Turn routing off on the panel.

It only reads, unless you say otherwise

As it comes, a statement that is not a read is refused with error 1290 and is never sent to your MySQL. To let writes through, set this where DBTrail's settings are, then restart the service that answers the port:

In .env, next to docker-compose.yml:

ROUTER_READ_ONLY=0
docker compose up -d

In /etc/bintrail/bintrail-console.env:

BINTRAIL_CONSOLE_ROUTE_READ_ONLY=0
sudo systemctl restart bintrail-router

Writes then run on your MySQL as the forwarding user, for anyone who holds the port's password. Give that user exactly the privileges you mean it to have first.

Your MySQL enforces it too. From 0.139.1, every connection a read-only port opens to your MySQL starts with SET SESSION TRANSACTION READ ONLY. A write the port cannot see in the text, such as a SELECT that calls a stored function that writes, is refused by your MySQL with error 1792. If your MySQL refuses that first statement, the client gets the error and no connection is opened.

A driver or a pool that switches a connection back from read-only keeps working. A Java application does this after each read-only transaction (Connection.setReadOnly). The port answers SET SESSION TRANSACTION READ WRITE, SET TRANSACTION READ WRITE and a SET of transaction_read_only or tx_read_only with OK, does not send them, and writes stay refused.

  • SELECT @@session.transaction_read_only answers 1, even after such a SET, because that is what the session on your MySQL is.
  • Those SET statements are answered only as plain statements, alone or in a list of unquoted settings. As a prepared statement, or in a SET that also holds quoted text, they get error 1290.
  • START TRANSACTION READ WRITE gets error 1290, as on a MySQL server with read_only=ON.
  • A read-only session still lets a function do what is not a write to a table: take a named lock, sleep, or write to the session's own temporary tables. The forwarding user's grants are what bounds that.

When the router stops

An application that connects to this port depends on the router, which is one process. While it is down the application gets errors on this port with your MySQL up: nothing on the port sends it to your MySQL another way. Your MySQL itself is not touched.

Measured in one run on one host: DBTrail 0.137.0 from the Docker Compose stack, a MySQL 8.0.46 source, the router letting writes through (ROUTER_READ_ONLY=0), and a Python client (PyMySQL) that inserted a row and read one by key ten times a second, plus a GROUP BY over 300,000 rows once a second, which the copy answers. The application connected straight to the port.

What was done to the routerWhat the client gotFor how long
Stopped (docker compose stop router), started 30 seconds laterThe statement in flight: 2013 Lost connection to MySQL server during query. A long SELECT in flight: 2006 MySQL server has gone away (the port's connection to the source was lost: context canceled); reconnect to continue. Every new connection: 2003 Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused), answered in 1 msUntil it was started. It took connections again within the same second
Killed (kill -9 of its process)2013 on the statement in flight, then two failed connections: one refused, one cut during the handshake (2013)Under a second. Docker restarted the container in 0.3 seconds (the stack's restart: unless-stopped) and the client's third attempt connected
Frozen for 30 seconds (docker compose pause router): the process does not run, the port still accepts connectionsNo error from the port. A statement waited until the client's own read timeout, 20 seconds there (2013 Lost connection to MySQL server during query (timed out)), and a new connection waited 10 seconds, until the router ran again. On clients with a 60 second timeout, statements waited 22 seconds and were then answeredAs long as the freeze, or the client's timeout
  • Open connections are closed, not left waiting, when the router stops or is killed: the next statement on an idle connection failed with 2013 at once. Only the frozen router left clients waiting, so give the application a connect timeout and a read timeout.
  • A transaction that was open is rolled back when the router stops or is killed. Its next statement failed with 2013, and none of its rows were on MySQL. Across the freeze it stayed open, and committed once the router ran again.
  • Every write the client had an answer for was on MySQL: 694 of 694 with the stop, 995 of 995 with the kill. The one INSERT in flight each time ended in 2013 and was not on MySQL. That is what this run saw, not a rule: treat a write that ended in 2013 as unknown, as with any MySQL connection lost during a statement.
  • MySQL was not touched. A second client writing straight to MySQL during each of those runs had 1,000 of 1,000 inserts answered, the slowest in 16 ms.
  • With the Linux packages it is systemd that starts the router again (Restart=always, RestartSec=5 in bintrail-router.service). That was not part of the run.

DBTrail does not send the application to your MySQL when its router stops. A proxy the application connects to can: Fall back to MySQL when the router stops.

If something goes wrong

On this page