ProxySQL Support and Consulting

ProxySQL is a routing layer that sits in front of the database: it separates read and write traffic without changing the application's connection string, lowers the connection count, and moves traffic to a working node at the moment of failover.

$ mysql -u admin -P 6032 -e 'SELECT hostgroup,srv_host,status FROM stats_mysql_connection_pool'
10 db-write-01 ONLINE
20 db-read-01 ONLINE
20 db-read-02 SHUNNED
β†’ read traffic routed to the working node

We design and operate the ProxySQL layer

The value of ProxySQL lies not in installing it but in designing the query rules correctly. A single badly written rule sends a write query to a replica and breaks the application silently.

Connection pooling and multiplexing

We serve thousands of connections from the application servers over a few dozen connections to the database, by multiplexing them. On systems pressed against the max_connections limit, this opens room without growing the hardware.

Read and write splitting

We separate traffic into hostgroups and design the query rules (mysql_query_rules) on query patterns and digests. Queries needing read-after-write consistency are marked separately, so replication lag does not reach the application.

Routing during failover

The monitor module continuously watches the reachability of the backend nodes and their read_only flag. When the primary changes, the writer hostgroup is repointed; the application's connection string does not change.

Query rules and traffic control

Query rewriting, result caching, rate limiting and blocking specific query patterns are all configured at the ProxySQL layer. A query threatening production can be stopped without waiting for an application release.

What ProxySQL does, and what it does not

ProxySQL is a routing layer. It holds no data, performs no replication, and provides no high availability on its own. Availability is provided by the cluster architecture behind it; ProxySQL is that architecture’s application-facing side.

The mistaken expectation we meet most often in the field is that installing ProxySQL will remove outages by itself. Putting ProxySQL in front of a single-node MySQL server changes nothing when that server becomes unreachable. In front of a cluster that already exists, on the other hand, ProxySQL is the component that determines how many errors the application receives at the moment of failover.

The connection count limits the database before the hardware does

Each application server opens its own connection pool. Ten application servers, each with a fifty-connection pool, have opened five hundred connections to the database. The database allocates memory for every connection; as the number of concurrently running threads rises, the management cost starts to outgrow the work itself. The picture that appears once the max_connections limit is reached is a familiar one: the server’s resources sit idle, and no new connection is accepted.

ProxySQL gathers those connections at a single point and reaches the backend with far fewer. The same backend connection is shared between queries arriving in turn β€” that behaviour is called multiplexing.

Multiplexing does not work in every case, and that has to be known before the installation. On connections carrying session state β€” an open transaction, a temporary table, a user variable, or a SET command run at session level β€” ProxySQL has to keep the connection bound to that session; otherwise the query runs in the wrong context. A ProxySQL built without measuring how often the application uses those patterns does not deliver the expected gain. We take that measurement before the installation and state the expected gain as a number.

Query rules: the real work in a ProxySQL installation

Routing is done through backend groups called hostgroups. In a typical installation the primary sits in one hostgroup and the read replicas are gathered in another. Which query goes to which group is determined by the rules in the mysql_query_rules table; rules are matched on the query text or on the query’s digest value, and evaluated in order.

Designing the rule set correctly takes far more effort than the installation itself. Three points have to be understood from the start:

  • Every query inside an open transaction has to stay on the primary. A SELECT that lands on a replica mid-transaction cannot see changes that have not yet been sent.
  • SELECT ... FOR UPDATE is not a read query. It takes a lock and has to go to the primary. A rule set that looks only at whether the query starts with SELECT sends this one to a replica.
  • Patterns needing read-after-write consistency have to be marked. An application that reads a record immediately after updating it will see data as stale as the replication lag, if that read lands on a replica.

As an additional measure against the third point, we configure the setup to take replicas whose lag exceeds a defined threshold out of the hostgroup. A lagging node stops receiving traffic by itself, and returns once the lag closes.

Query rules are not used only for routing. Query rewriting, result set caching and the outright blocking of specific query patterns are configured at the same layer. Being able to stop a query that threatens production without waiting for an application release is one of the most useful capabilities in incident response.

ProxySQL in cluster architectures

ProxySQL watches the topology behind it not with a generic health check but by reading that architecture’s own state information.

On Galera Cluster and Percona XtraDB Cluster installations the mysql_galera_hostgroups configuration is used. The joining and synchronisation state of the nodes is watched, and a node that has not finished synchronising receives no traffic. In these architectures, which can run multi-writer, concentrating write traffic on a single node reduces certification conflicts markedly β€” and ProxySQL does that without the application noticing.

On Group Replication and InnoDB Cluster installations the mysql_group_replication_hostgroups configuration takes the group’s own membership information as its source. Nodes that lose quorum are left out of the traffic.

Which architecture to build and how to determine the node count is a separate piece of work: High Availability and Cluster Solutions

The proxy layer must not be a single point of failure

Putting ProxySQL between the application and the database adds a new component to the architecture. If a single ProxySQL server is installed, the problem solved does not disappear β€” it merely moves. We use one of two deployment shapes:

  • Local ProxySQL on every application server. The application connects over localhost, no extra network round trip is introduced, and losing one copy affects only its own server.
  • A separate ProxySQL layer. Run redundantly behind a virtual IP; preferred where the number of application servers is very high, or where the configuration has to be managed centrally.

In both shapes we keep the configuration consistent between copies with ProxySQL Cluster: a rule change made on one node propagates to the others, with no manual copying.

Monitoring: the proxy layer is where the query profile shows

ProxySQL keeps its own statistics in the stats schema of its admin interface. Connection usage per hostgroup, the state of the backend nodes, replication lag and query digests are all read from there. We collect the metrics through Percona Monitoring and Management (PMM) and watch them on Grafana dashboards.

The stats_mysql_query_digest table has a value of its own: it shows every query pattern arriving from the application together with its call count and total time. Where the slow query log records only queries above a threshold, ProxySQL sees all of the traffic. A query called thousands of times a second that looks fast on its own is only noticed in this table.

The equivalent work on the database side is a separate engagement: Performance Tuning and Monitoring

How the installation proceeds

  1. Measurement. The current connection profile, the query patterns and the use of session state are established. The gain to expect from multiplexing rests on that measurement.
  2. Rule design. The hostgroup scheme and the query rules are written in a test environment and verified against the application’s real traffic.
  3. Phased cutover. Read traffic is moved through ProxySQL first; once the behaviour is confirmed, write traffic follows.
  4. Failover rehearsal. The primary is taken out deliberately; the number of errors the application receives and the time to reroute are measured.
  5. Handover. The rule set, the hostgroup scheme and the monitoring dashboard are documented and handed over.

ProxySQL is part of connection and user management; backend credentials and access control are planned at this layer too: Database Security and Technical Measures

Taking backup traffic from the read replicas is designed together with the routing rules: Database Backup and Recovery

Our database support services

On the databases we put a ProxySQL layer in front of, we provide the full range of our support and consulting services.

Frequently asked questions

No change to the application code is needed; only the server address and port in the connection string are updated to point at ProxySQL. The application connects to ProxySQL with the standard MySQL protocol and does not have to know the topology behind it. Before the installation, though, we measure the application's query patterns: queries running inside an open transaction, temporary tables and session variables all directly affect the routing rules.

It does not. ProxySQL routes traffic; it does not replicate data. Availability is provided by the cluster architecture behind it β€” InnoDB Cluster, Group Replication or Galera. Putting ProxySQL in front of a single-node MySQL server changes nothing when that server becomes unreachable. ProxySQL is the application-facing side of a cluster that already exists.

They do if the rules are not designed carefully. An application that reads a record immediately after updating it will see data as stale as the replication lag, if that read lands on a replica. We apply three measures together: pinning query patterns that need consistency to the writer hostgroup, taking replicas whose lag exceeds a threshold out of the hostgroup, and keeping every query inside an open transaction on the primary.

An application pool works only for its own process. Ten application servers, each with a fifty-connection pool, open five hundred connections to the database; the database allocates memory for each one and the thread management cost rises. ProxySQL gathers those connections at a single point and reaches the backend with far fewer. It does not replace the application pool; it sits in front of it.

It does if it is installed on one server, and that means the problem solved has merely moved. We use one of two deployment shapes: installing ProxySQL locally on every application server, or running a separate ProxySQL layer redundantly behind a virtual IP. To keep the configuration consistent between copies we use ProxySQL Cluster; a rule change made on one node propagates to the others.

There is none. ProxySQL is open source licensed, and our right to install, configure and integrate comes from that. We have no commercial relationship with the organisation that develops ProxySQL; what we provide is the installation, the rule design and the operational maintenance that follows.

Get in touch about a ProxySQL installation