TheRobotLearns Platform · ProxySQL: from first install to production

Multiplexing, Session State, and Routing Pitfalls

  • about 22 min read

Learning objectives

  • Describe how ProxySQL shares backend connections and why multiplexing improves scalability
  • List session constructs that disable multiplexing, including transactions, GET_LOCK, and temporary tables
  • Tune multiplexing behavior through mysql_query_rules columns such as multiplex, cache_ttl, and OK_msg
  • Explain why routing must be considered when session state or temporary objects depend on a specific backend

Why shared backend connections help

You open many client sessions to ProxySQL. MySQL uses a thread per connection, so each backend connection gets its own software thread. Idle connections still consume resources, and performance gradually degrades as connections increase.

ProxySQL uses a thread pool rather than MySQL's thread-per-connection approach. Multiplexing lets multiple frontend connections re-use the same backend connection. A frontend connection is the client session that reaches ProxySQL. A backend connection is the session ProxySQL holds to MySQL.

This sharing reduces the number of resources allocated and managed by the database backends. ProxySQL tracks certain session-specific variables to decide whether an in-use backend connection can be shared. It can greatly improve performance because fewer backend resources are needed.

Sharing is not always possible. Certain session conditions implicitly disable it, so a connection cannot be shared while those conditions hold, and some conditions never allow sharing again.

Takeaway: Multiplexing re-uses backend connections and reduces resources, but session conditions can disable sharing.

What stops connection sharing

Some conditions only pause sharing. An active transaction disables multiplexing until the transaction commits or rolls back. A table lock disables it until UNLOCK TABLES runs. SQL_LOG_BIN=0 disables it until SQL_LOG_BIN returns to 1.

Other conditions stop sharing and never enable it again on the connection. GET_LOCK() disables multiplexing and never enables it again. SQL_CALC_FOUND_ROWS, CREATE TEMPORARY TABLE, and PREPARE have the same effect.

All queries that have @ in their query_digest disable multiplexing, and it is never enabled again. Two exceptions are hardcoded: SELECT @@tx_isolation and SELECT @@version. Many applications run those on every connection.

Some SET commands also disable sharing, including SET SQL_SAFE_UPDATES=?, SET FOREIGN_KEY_CHECKS, and SET UNIQUE_CHECKS. If you set mysql-multiplexing in configuration, multiplexing is globally disabled for all sessions.

Delay parameters also disable sharing. If mysql-auto_increment_delay_multiplex or mysql-connection_delay_multiplex_ms has a non-zero value, multiplexing is disabled on a connection for a number of queries or milliseconds. Both are global variables and affect all connections and hostgroups on a ProxySQL host.

Takeaway: Transactions, GET_LOCK, temporary tables, user variables, locks, and configuration can stop sharing.

Tuning sharing with query rules

mysql_query_rules lets you change multiplexing for matched queries. The multiplex field accepts 0 to disable multiplexing, 1 to enable it, and 2 to avoid disabling it for a specific query containing @.

If your application uses SET SQL_SELECT_LIMIT, you can create one rule that disables sharing for that SET. A second rule can enable sharing when the application uses DEFAULT. The rules use matching patterns rather than global configuration.

If your application sends SELECT @@max_allowed_packet, you can prevent multiplexing from being disabled because of that @ query. The 2 value covers that case. The example rule also adds cache_ttl to cache the matched statement.

You can also filter statements that would otherwise disable sharing. If that variable can be filtered, create a rule with an empty OK_msg. That filter returns OK without executing the query.

This stores two rules: one keeps @@max_allowed_packet shared, and one filters a SET @@wait_timeout.

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 = ?','');

Takeaway: Use multiplex, cache_ttl, and OK_msg in mysql_query_rules to control sharing per query.

When routing breaks session state

Disabling multiplexing does not disable routing. If CREATE TEMPORARY TABLE runs, multiplexing is disabled, but the next SELECT can still be routed differently. If the two statements go to two different hostgroups, ProxySQL returns an error that the table does not exist. To prevent that, use query routing.

Frontend session variables are not automatically issued to a backend. ProxySQL relies on mysql-default_XXX globals as assumed backend defaults and propagates a different value only when the client sets one. Set common values as mysql-default_XXX global variables for best performance.

When a node enters OFFLINE_SOFT, a disabled-multiplexing connection can stay on that node, and queries continue routed there. Application pools should recycle connections before failover. If an active transaction caused the disable, ProxySQL moves the connection after the transaction finishes.

The SELECT extended_info FROM stats_mysql_processlist query returns detailed session information and helps identify whether multiplexing is disabled and why.

Takeaway: Keep routing aligned with session state, and expect OFFLINE_SOFT to hold disabled sessions.

Inspect and tune a variable query

SELECT extended_info FROM stats_mysql_processlist;
INSERT INTO mysql_query_rules (active,match_digest,multiplex,cache_ttl) VALUES
('1','^SELECT @@max_allowed_packet',2,60000);

SELECT extended_info FROM stats_mysql_processlist returns highly detailed session information and can show whether multiplexing is disabled and why. INSERT INTO mysql_query_rules stores a rule that sets multiplex=2, so a matching @ query does not disable multiplexing. The same row sets cache_ttl to the example value, matching the cache rule. The returned JSON can include get_lock, user_variable, temporary_table, lock_tables, no_multiplex, and found_rows under status.

Try it: query_rule_playground

Run SELECT extended_info FROM stats_mysql_processlist and look for fields such as get_lock, user_variable, or temporary_table. Then insert the SELECT @@max_allowed_packet rule with multiplex=2 and cache_ttl=60000. The rule tells ProxySQL not to disable multiplexing for that @ query.

Sources

Do more with an account

Multiplexing, Session State, and Routing Pitfalls — ProxySQL: from first install to production