TheRobotLearns Platform · ProxySQL: from first install to production
Read/Write Split Choices and Digest-Based Routing
Learning objectives
- Compare port-based, broad regex, and workload-driven routing for primary and replica hostgroups
- Use stats_mysql_query_digest views to identify expensive, frequent, or long-running SELECT candidates
- Create targeted mysql_query_rules entries using digest values or focused match_digest patterns
- Apply LOAD and SAVE workflow to make routing changes effective and persistent
Split traffic without app edits
You put a primary MySQL server in hostgroup 1 and replicas in hostgroup 2. ProxySQL can route queries based on multiple criteria without requiring application modifications. Port-based routing uses separate listening ports, and the incoming port determines the target hostgroup. Generic regex routing, which matches query text by regular expression, sends SELECT statements to replicas and other statements to the primary. Intelligent routing starts by sending all traffic to the primary, analyses query patterns, and then adds targeted rules. Port-based rules have a limitation: the application must already distinguish reads from writes and connect to the appropriate port. Generic regex rules cause problems with transactions, session state, and consistency, and they are not for production. The recommended path is workload-driven: route everything to the primary first, identify statements that can safely run on replicas, then create focused rules. Separate ports appear in mysql-interfaces, while port rules map proxy_port to a destination hostgroup. The generic example also sets mysql_users.default_hostgroup=1 so user defaults can reach the primary. Takeaway: Port routing depends on client ports, generic regex routing is not for production, and query-analysis routing targets selected SELECTs.
Digests reveal expensive SELECTs
After routing all traffic to the primary, you need SELECT statements that can safely move to replicas. stats_mysql_query_digest is the view ProxySQL analyses to identify expensive SELECT statements. A digest is a query pattern hash shown with digest_text, count_star, and timing values. You can order SELECT rows by sum_time to find total-time candidates. You can order by count_star to find frequent candidates. You can compute sum_time/count_star as avg_time and filter by average execution time. The sample views also show min_time and max_time for individual execution limits. A total-time query can filter min_time > 1000. Another query can compute a percentage named pct by dividing sum_time by the total sum_time, rounded to three places. A SELECT workload share query uses the same division while filtering digest_text LIKE 'SELECT%'. The sample views show SUBSTR(digest_text,0,25), and each result set is limited to five rows. A selected row gives the digest value you can copy into a rule. Takeaway: Use stats_mysql_query_digest columns sum_time, count_star, avg_time, min_time, and max_time to select candidates.
Focused rules choose replicas
You choose a row from stats_mysql_query_digest and put it into mysql_query_rules. The first analysis query sorts SELECT digests by total time. For a high-value individual query, use the digest column and set destination_hostgroup to the replica group. For a broad pattern, use a focused match_digest value. One sample match_digest pattern routes all COUNT(*) queries to hostgroup 2. Prefer match_digest over match_pattern, because digests are smaller and faster to evaluate. Regex rules suit broad patterns, and digest rules suit high-value individual queries. The sample rule sets active=1 and apply=1 with the target hostgroup. The sample inserts also include rule_id. The proxy_port column belongs to port-based rules, not to these query-analysis rules. After inserting rules, LOAD MYSQL QUERY RULES TO RUNTIME makes them effective. Then SAVE MYSQL QUERY RULES TO DISK makes the routing persist. You can repeat the analysis as workloads evolve, and you should always test rules against a representative workload before applying them in production. Takeaway: Insert digest or focused match_digest values in mysql_query_rules, then run LOAD MYSQL QUERY RULES TO RUNTIME and SAVE MYSQL QUERY RULES TO DISK.
Pitfalls in split choices
Port-based routing looks simple because the incoming port maps to a hostgroup. The limitation is that the application must already distinguish reads from writes and choose the right port. ProxySQL can route queries based on criteria without requiring application modifications, but port rules require the application to choose a port. The port rules use proxy_port, not digest. Broad regex routing sends SELECT statements to replicas, but it causes problems with transactions, session state, and consistency. The pattern is not for production. The generic example also sets mysql_users.default_hostgroup=1, then loads users with LOAD MYSQL USERS TO RUNTIME. The recommended path begins with all traffic on the primary, then moves only safe SELECTs. Even targeted digest rules need care: re-evaluate query patterns as workloads evolve, and test rules against a representative workload before applying them in production. LOAD MYSQL QUERY RULES TO RUNTIME activates the rules. SAVE MYSQL QUERY RULES TO DISK persists them. Takeaway: Port rules depend on proxy_port, broad regex is not for production, and targeted rules need testing, LOAD, and SAVE.
Route a heavy SELECT by digest
SELECT digest, SUBSTR(digest_text,0,25), count_star, sum_time
FROM stats_mysql_query_digest
WHERE digest_text LIKE 'SELECT%'
ORDER BY sum_time DESC LIMIT 5;
INSERT INTO mysql_query_rules (rule_id,active,digest,destination_hostgroup,apply)
VALUES (1,1,'0x38BE36BDFFDBE638',2,1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;The SELECT lists the top five SELECT digests by total execution time. The INSERT places one rule in mysql_query_rules that sends digest 0x38BE36BDFFDBE638 to hostgroup 2. LOAD MYSQL QUERY RULES TO RUNTIME activates the rule. SAVE MYSQL QUERY RULES TO DISK persists the rule.
Try it: query_rule_playground
You start with all traffic on hostgroup 1. Run the total-execution-time query against stats_mysql_query_digest and view the top five SELECT rows. Pick a digest value. Insert a rule into mysql_query_rules using that digest, active=1, apply=1, and destination_hostgroup=2. Run LOAD MYSQL QUERY RULES TO RUNTIME, then SAVE MYSQL QUERY RULES TO DISK. As an alternative, insert the sample COUNT(*) rule by using match_digest='^SELECT COUNT\(\)\*\)' instead of a digest value.
Sources
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
- How to Set Up ProxySQL Read/Write Split
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.