TheRobotLearns Platform · ProxySQL: from first install to production
Rewrite, Cache, and Mirror Traffic with Query Rules
Learning objectives
- Alter matched statements using query rewrite for validation or routing outcomes
- Configure cache_ttl values so selected query patterns reuse results for a bounded time
- Set mirror_hostgroup and mirror_flagOUT to duplicate traffic into alternative paths or test hostgroups
- Compare mirrored and original query behavior in stats_mysql_query_digest metrics
- Identify the prepared statement limitation for mirroring
Match, cache, and rewrite digests
Match, cache, and rewrite digests mysql_query_rules defines query rules. Rules are processed in rule_id order, and only rules with active=1 are considered and loaded into runtime data structures.
A rule can match a query digest with match_digest or query text with match_pattern. A matched pattern can be replaced by setting replace_pattern, which must be paired with match_pattern; substitution and capture groups are possible.
The multiplex column has the values 0, 1, and 2. With 0, multiplexing is disabled. With 1, multiplexing could be re-enabled if there aren't any other conditions preventing it. With 2, multiplexing is disabled for the life of the session. Example rules set multiplex=0 for ^SET SQL_SELECT_LIMIT=? and multiplex=1 for ^SET SQL_SELECT_LIMIT=DEFAULT; another example sets multiplex=2 and cache_ttl=60000 for ^SELECT @@max_allowed_packet.
cache_ttl is the number of milliseconds for which to cache the result of the query. OK_msg is a column where the specified message will be returned for a query that uses the defined rule. mirror_flagOUT and mirror_hostgroup are settings related to mirroring.
Takeaway: mysql_query_rules rows can match by digest or text, replace matched text when match_pattern is set, control multiplex, set cache_ttl, return an OK_msg, and configure mirroring settings.
Mirror queries for safe testing
Mirroring duplicates queries to multiple host groups for performance testing without impacting live traffic. ProxySQL exposes active rule configuration in runtime_mysql_query_rules after a relevant LOAD command. The feature uses two columns added to mysql_query_rules: mirror_flagOUT and mirror_hostgroup. When either parameter is set, ProxySQL creates a new MySQL session with properties identical to the original. That mirrored session inherits credentials, the schema name, and the default hostgroup.
If mirror_hostgroup is specified, it becomes the new session's default hostgroup. If mirror_flagOUT is not set, the original query executes against mirror_hostgroup. If mirror_flagOUT is set, the query matches rules starting from that flag value, enabling further modifications.
Performance testing uses this to mirror SELECT statements to test environments while production traffic continues. Query rewrite validation uses mirror_flagOUT to route mirrored queries through separate rules. Firewall testing can combine mirroring with error_msg to process queries without execution, allowing verification of rewrites without server impact.
Takeaway: mirror_hostgroup sends the copy to an alternate path, and mirror_flagOUT lets mirrored queries enter another rule set.
Compare metrics and know limits
In mysql_query_rules, mirror_flagOUT and mirror_hostgroup are listed as settings related to mirroring.
Metrics are collected for both original and mirrored queries in stats_mysql_query_digest. Query rules can also match queries by digest, using the digest value returned by stats_mysql_query_digest.
Rules are processed in rule_id order. Only rules with active=1 are considered by the query processing module and loaded into runtime data structures. An incoming query is tested against rules with flagIN=0; if a matching rule sets flagOUT, the query is re-evaluated against rules with flagIN equal to that value, and if apply=1 is set, the query is executed immediately.
Takeaway: Compare original and mirrored queries in stats_mysql_query_digest, and verify that the relevant rules are active and ordered as intended.
Match, cache, and stop a query
INSERT INTO mysql_query_rules (active,match_digest,multiplex) VALUES
('1','^SET SQL_SELECT_LIMIT=?',0), (1,'^SET SQL_SELECT_LIMIT=DEFAULT',1);
INSERT INTO mysql_query_rules (active,match_digest,multiplex,cache_ttl) VALUES
('1','^SELECT @@max_allowed_packet',2,60000);
INSERT INTO mysql_query_rules (active,match_digest,OK_msg) VALUES
('1','^SET @@wait_timeout = ?','');The first statement inserts two active rules into mysql_query_rules. The first row matches ^SET SQL_SELECT_LIMIT=? and sets multiplex=0. multiplex=0 disables multiplexing. The second row matches ^SET SQL_SELECT_LIMIT=DEFAULT and sets multiplex=1. multiplex=1 enables multiplexing. The second statement inserts one active rule matching ^SELECT @@max_allowed_packet. It sets multiplex=2. multiplex=2 prevents multiplexing from being disabled for that query containing @. It sets cache_ttl=60000. That may cache the query. The third statement inserts one active rule matching ^SET @@wait_timeout = ?. It sets OK_msg to an empty string. This returns OK without executing the query. A change made to mysql_query_rules must be loaded to the runtime before it takes effect.
Try it: query_rule_playground
This adds the rule examples to mysql_query_rules; each INSERT shows columns that affect a matched digest.
INSERT INTO mysql_query_rules (active,match_digest,multiplex) VALUES
('1','^SET SQL_SELECT_LIMIT=?',0), (1,'^SET SQL_SELECT_LIMIT=DEFAULT',1);
INSERT INTO mysql_query_rules (active,match_digest,multiplex,cache_ttl) VALUES
('1','^SELECT @@max_allowed_packet',2,60000);
INSERT INTO mysql_query_rules (active,match_digest,OK_msg) VALUES
('1','^SET @@wait_timeout = ?','');The first statement inserts rules with multiplex=0 and multiplex=1. The second statement sets multiplex=2 and cache_ttl=60000, which may cache the query. The third statement sets OK_msg='', which returns OK without executing the query. Mirroring uses mirror_hostgroup and mirror_flagOUT in the same table. A relevant LOAD command must run before these rows become active runtime configuration.
Sources
Do more with an account
- Graded quizzes is available on your account.
- Saving your progress is available on your account.
- Hands-on labs is available on your account.
- AI tutoring is available on your account.