Skip to content

ProxySQL query rules — routing, mirroring, and rewriting in production

How we use ProxySQL query rules to route, mirror, throttle, rewrite, and shed traffic — the columns that matter, the precedence model, and the production patterns we deploy.

ProxySQL’s query rules are the SQL-level handle on a database fleet — match a fingerprint, route it to a hostgroup, mirror it to a canary, rewrite it on the fly, throttle a tenant. This article is the production playbook for mysql_query_rules: what each column does, how the precedence model decides which rule wins, the patterns we deploy most, and the operational discipline that keeps the rules table from becoming an undebuggable thicket.

How to verify

Query rules live in mysql_query_rules (memory), runtime_mysql_query_rules (active), and disk.mysql_query_rules (persisted). The matching counters live in stats_mysql_query_rules.

mysql -h 127.0.0.1 -P 6032 -u admin -p -e \
  "SELECT rule_id, active, match_pattern, destination_hostgroup, apply FROM runtime_mysql_query_rules ORDER BY rule_id;"
mysql -h 127.0.0.1 -P 6032 -u admin -p -e \
  "SELECT rule_id, hits FROM stats_mysql_query_rules ORDER BY hits DESC LIMIT 20;"
mysql -h 127.0.0.1 -P 6032 -u admin -p -e \
  "SELECT * FROM stats_mysql_query_digest ORDER BY count_star DESC LIMIT 10;"

stats_mysql_query_rules.hits shows which rules actually matched. A rule with zero hits over a sustained period is either misordered, misregexed, or matching traffic that does not arrive.

What’s happening

A query rule is a row in mysql_query_rules. When a query arrives, ProxySQL walks the table in rule_id order and tests each rule. The relevant matching columns are:

  • username, schemaname, client_addr — match by who, where, and to which schema.
  • match_digest — the regex matches the query digest (the fingerprint, with literals stripped). Faster than match_pattern because the digest is already computed.
  • match_pattern — the regex matches the full query text. Use when literal values matter.
  • digest — exact match on a specific digest hex string. The fastest of all.

The action columns are:

  • destination_hostgroup — route here.
  • mirror_hostgroup / mirror_flagOUT — copy of the query is also sent here. Asynchronous, no waiting on the response.
  • replace_pattern — regex rewrite of the query text. Pair with match_pattern.
  • cache_ttl — cache the result for this many milliseconds. ProxySQL becomes a tiny read cache.
  • reconnect / timeout / retries / delay — per-rule failure handling and throttling.

The crucial column is apply. With apply=0, ProxySQL continues evaluating later rules after this one matches; the later rule’s destination wins. With apply=1, the rule’s destination is final and evaluation stops. Production rules almost always have apply=1; cascading rules with apply=0 are a debugging trap.

The procedure

  1. Start with the classic read/write split, but make every rule explicit about whether it stops evaluation.

    -- mysql -h 127.0.0.1 -P 6032 -u admin -p
    INSERT INTO mysql_query_rules
      (rule_id, active, match_digest, destination_hostgroup, apply) VALUES
      (10, 1, '^SELECT.*FOR UPDATE',  10, 1),
      (20, 1, '^SELECT.*FOR SHARE',   10, 1),
      (30, 1, '^(SET|CALL|BEGIN|COMMIT|ROLLBACK|START TRANSACTION)', 10, 1),
      (40, 1, '^SELECT',              20, 1);

    Rule 10 catches SELECT ... FOR UPDATE (a write in disguise) and pins it to the writer; rule 30 pins transaction control to the writer; rule 40 sends remaining selects to the readers.

  2. Add a per-tenant route. Match by username to send a heavyweight tenant to its own dedicated hostgroup.

    INSERT INTO mysql_query_rules
      (rule_id, active, username, destination_hostgroup, apply) VALUES
      (50, 1, 'tenant_big_co', 30, 1);
  3. Mirror writes to a canary while still going to production. The mirror is fire-and-forget; the response is ignored.

    INSERT INTO mysql_query_rules
      (rule_id, active, match_digest, destination_hostgroup, mirror_hostgroup, apply) VALUES
      (60, 1, '^UPDATE orders', 10, 40, 1);
  4. Cache a slow read for a short time. Useful for a query digest that runs every second and only changes every minute.

    INSERT INTO mysql_query_rules
      (rule_id, active, digest, cache_ttl, apply) VALUES
      (70, 1, '0xC0FFEE12345', 60000, 1);
  5. Rewrite a query in flight — kill a heavy SELECT * from a tool you cannot modify.

    INSERT INTO mysql_query_rules
      (rule_id, active, match_pattern, replace_pattern, apply) VALUES
      (80, 1, 'SELECT \* FROM big_log_table', 'SELECT id, ts, status FROM big_log_table', 1);
  6. Load, verify, save.

    LOAD MYSQL QUERY RULES TO RUNTIME;
    SAVE MYSQL QUERY RULES TO DISK;
    SELECT rule_id, hits FROM stats_mysql_query_rules ORDER BY rule_id;
  7. Identify the queries you have not yet ruled on. The digest table is the menu.

    SELECT digest_text, count_star, sum_time, hostgroup
    FROM stats_mysql_query_digest
    ORDER BY sum_time DESC LIMIT 20;

Common pitfalls

  • Rules out of order — rule_id=100 “SELECT to readers” comes before rule_id=200 “SELECT FOR UPDATE to writers”; the second rule never runs because the first one already matched. Always order specific rules before general ones.
  • apply=0 chains — feel clever, debug awful. Two rules can both match a query and both contribute; the destination becomes a function of which rules are present in what order. Use apply=1 on every production rule unless you have a measured reason.
  • match_pattern everywhere — match_pattern runs against the full query text, which is more work than match_digest. Use match_digest when literal values do not matter.
  • Cache TTL on a query whose result varies per user — cache_ttl keys on the digest, not on the user. A query SELECT * FROM cart WHERE user_id = ? cached for 60s will return one user’s cart to another. Cache only globally-stable queries.
  • Mirror traffic to a non-existent hostgroup — ProxySQL silently swallows mirror failures; you do not notice until you try to verify the canary received anything.

Query rules are the lever that turns “the database is the wrong shape for this workload” into a fix the operator can ship in seconds, without an application deploy. In the engagements we run, the mysql_query_rules table is in version control, additions go through review with a hits baseline before and after, and the digest table feeds our weekly review of which queries to shape next. For the broader pattern of operating MySQL fleets with ProxySQL like this, see /en/services/managed-operations/.