TheRobotLearns Platform · ProxySQL: from first install to production

Explain the threading model and the life of a query

  • about 20 min read

Learning objectives

  • Describe ProxySQL as an event-driven, asynchronous C++ proxy with high-level design principles.
  • Map ProxySQL thread roles across main/worker, admin, monitoring/cluster, and idle subsystems.
  • Explain ProxySQL hostgroup pooling, multiplexing, latency routing, and read_only monitoring.
  • Summarize ProxySQL query processor routing, rewriting, caching, and blocking responsibilities.
  • Trace a client query from worker thread through query processor, hostgroups, backend, and result.
  • Explain ProxySQL query digests as fingerprints for statistics and rule matching.
  • Summarize ProxySQL performance techniques: lock-free statistics, jemalloc, and cached regex.
  • Explain ProxySQL multi-layer SQLite configuration separation and peer-to-peer cluster synchronization.

Why separate threads matter

Client traffic uses MySQL worker threads, while health checks run on Monitor threads. ProxySQL splits threads by task to avoid contention and ensure predictability.

ProxySQL is written in C++ around an asynchronous, multi-threaded, event-driven design. The event-driven model uses libev to handle thousands of concurrent connections per thread. ProxySQL also supports maximal uptime, because configuration changes happen at runtime without restarts.

Different thread types handle specific tasks to avoid contention and ensure predictability. Protocol awareness supports routing and rewriting, and resilience adds integrated monitoring and auto-shunning of unhealthy backend nodes.

The threading diagram groups MySQL worker threads, PgSQL worker threads, Admin thread, Monitor threads, Cluster threads, and optional Idle threads under the Main thread. MySQL worker threads handle authentication, query parsing, and result set relaying. PgSQL worker threads handle the PostgreSQL protocol.

The Admin thread supports MySQL protocol on port 6032 and PostgreSQL protocol on port 6132. Monitor threads run health checks on backend servers, and cluster threads handle inter-node synchronization. Idle threads are optional.

Takeaway: Worker, Admin, Monitor, Cluster, and optional Idle threads keep data traffic and control traffic on separate paths.

How routing chooses servers

A client query arrives, and ProxySQL must choose a backend group before contacting a database. The Query Processor makes that choice, because routing is one of its decisions for every query.

ProxySQL groups backend servers into hostgroups. Each hostgroup maintains its own pool of persistent connections. Multiplexing lets multiple frontend sessions share a smaller number of backend connections.

Two behaviors shape server choice. Latency awareness routes traffic to the fastest responding servers. Replication tracking monitors read_only status to distinguish Writers and Readers.

In the query lifecycle, the worker thread identifies the user and schema, and the Query Processor matches query rules against the query. If a cached result exists, the path can return it before contacting a backend.

For every query, the Query Processor decides routing, rewriting, caching, and blocking. Rewriting can modify SQL, such as adding index hints. Caching checks whether a valid cached result exists in memory. Blocking denies a query based on security rules. The Query Processor then requests a connection for a hostgroup from the HostGroups Manager.

Takeaway: The Query Processor selects a hostgroup, and the HostGroups Manager supplies pooled, latency-aware connections that use read_only status.

Trace one query through

A client sends an SQL query, and the result returns through named thread and component roles. The path starts with the worker thread and ends with the same worker thread.

The client sends the query to the worker thread. The worker thread identifies the user and schema, then passes the query to the Query Processor. The Query Processor matches query rules against the query.

If the Query Processor has a valid cached result, it returns that result to the worker thread. If not, it requests a connection for Hostgroup X from the HostGroups Manager. The HostGroups Manager sends the SQL to the backend database. The backend returns a result set, the pool relays it to the worker thread, and the worker returns results to the client.

ProxySQL also computes a query digest, a unique fingerprint made by normalizing values. The digest supports high-performance statistics tracking and rule matching based on query types rather than specific data.

Takeaway: A query moves from worker to Query Processor, then to cache or backend, with a digest fingerprint for tracking.

Performance and cluster state

You handle thousands of connections, many rules, and long-running processes. ProxySQL keeps those paths fast without making every worker wait for one global lock. Integrated monitoring supports auto-shunning of unhealthy backend nodes.

For statistics, ProxySQL uses thread-local storage (__thread) for counters. Worker threads update metrics without expensive global locks, and this ensures performance scales linearly with CPU cores.

Memory management uses jemalloc to reduce fragmentation and allocate efficiently for long-running processes that handle large result sets. ProxySQL compiles and caches query matching patterns in memory, and RE2 and PCRE engines support high-speed matching across thousands of rules.

ProxySQL changes configuration at runtime without restarts. ProxySQL stores all configuration in an internal SQLite database, which allows transactional updates. This can separate the persistent state (disk) from the active state (runtime). ProxySQL Cluster uses a peer-to-peer model with checksum-based synchronization, and nodes compare configuration versioning and epochs to converge on the same state without a single point of failure.

Takeaway: Lock-free counters, jemalloc, cached regex, SQLite disk/runtime separation, and checksum-based peer-to-peer sync keep ProxySQL scalable and convergent.

Check the Admin interface ports

-- Admin Thread supports MySQL protocol on port 6032.
-- Admin Thread supports PostgreSQL protocol on port 6132.
SELECT 1;

This block records the Admin thread ports and sends a simple SQL statement to the Admin interface. The first comment records the MySQL protocol port, 6032. The second comment records the PostgreSQL protocol port, 6132. SELECT 1; sends an SQL statement to the interface the Admin thread manages.

Sources

Do more with an account

Explain the threading model and the life of a query — ProxySQL: from first install to production