database

Proxysql On Google Cloud: Database Scaling Guide

Managing high traffic database workloads on cloud infrastructure often hits a wall when primary database instances take on the full weight of heavy read queries. Scaling up instances vertically become

Proxysql On Google Cloud: Database Scaling Guide
In this story

Managing high-traffic database workloads on cloud infrastructure often hits a wall when primary database instances take on the full weight of heavy read queries. Scaling up instances vertically becomes expensive fast, while standard cloud load balancers don't understand SQL query routing. Deploying ProxySQL on Google Cloud solves this bottleneck by inserting an intelligent, high-performance proxy layer between your application servers and your backend MySQL databases, such as Cloud SQL or Compute Engine instances.

When configured correctly, this setup handles query routing, connection pooling, and failover automatically without requiring application code rewrites. Let's look at how to architect, deploy, and tune ProxySQL on Google Cloud to maximize database throughput and reliability.

Architecture and Core Concepts

Deploying a database proxy in a cloud environment requires careful consideration of network topology and failure domains. In a typical Google Cloud deployment, ProxySQL instances run on dedicated Compute Engine virtual machine instances placed in the same Virtual Private Cloud (VPC) network as your backend databases.

[ Application Servers ]
         │
         ▼
  [ ProxySQL Node ]
    ├── Read Queries ──> [ Read Replica 1 ]
    ├── Read Queries ──> [ Read Replica 2 ]
    └── Write Queries ─> [ Primary Database ]

Your application connects to the ProxySQL virtual IP or load-balanced endpoints rather than talking directly to MySQL. ProxySQL inspects incoming statements on the fly. Writes route automatically to the primary database instance, while read-only queries distribute across configured read replicas based on weighted distribution or least-connections algorithms. More information on this are explored by Associated Press.

Network Topology and Security

Place your ProxySQL instances in a private subnet using VPC internal IP addresses. Never expose ProxySQL management or SQL listening ports directly to the public internet.

  • Use Google Cloud Firewall rules to restrict incoming traffic on port 6033 (ProxySQL query port) to only allow traffic originating from your application server subnets or internal load balancers.
  • Secure port 6032 (ProxySQL admin interface) strictly behind internal bastion hosts or Identity-Aware Proxy (IAP) tunnels.
  • Configure TLS encryption between your application and ProxySQL, as well as between ProxySQL and your Cloud SQL backend instances, using custom CA certificates.

Step-by-Step Deployment on Google Cloud

Setting up a production-ready ProxySQL instance involves provisioning a reliable Compute Engine VM, installing the package, and connecting it to your existing MySQL infrastructure.

1. Provision the Compute Engine Instance

Create a dedicated Compute Engine instance. For production workloads, choose an instance type with balanced CPU and memory resources (such as e2-standard-4 or higher), and attach a high-throughput persistent SSD for the ProxySQL SQLite database backend which stores runtime configurations.

gcloud compute instances create proxysql-node-01 \
    --zone=us-central1-a \
    --machine-type=e2-standard-4 \
    --subnet=default \
    --private-network-ip=10.128.0.20 \
    --image-family=ubuntu-2204-lts \
    --image-project=ubuntu-os-cloud \
    --boot-disk-size=50GB \
    --boot-disk-type=pd-balanced

2. Install ProxySQL

SSH into your newly created instance and install the official ProxySQL release package matching your Linux distribution.

sudo apt-get update
sudo apt-get install -y wget lsb-release gnupg
wget -O - https://repo.proxysql.com/ProxySQL/repo-key.pub | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/proxysql.gpg
echo "deb https://repo.proxysql.com/ProxySQL/proxysql-2.8.x/$(lsb_release -sc)/ ./" | sudo tee /etc/apt/sources.list.d/proxysql.list
sudo apt-get update
sudo apt-get install -y proxysql

Start the service and verify that the admin interface is accessible locally:

sudo systemctl enable proxysql
sudo systemctl start proxysql
mysql -u admin -padmin -h 127.0.0.1 -P 6032

Configuring Query Routing and Connection Pooling

ProxySQL relies on database tables stored in an internal SQLite database for all operational parameters. You configure routing rules by connecting to the admin port (6032) and updating runtime tables before saving changes to disk.

Defining Server Groups

First, register your backend database instances into mysql_servers. Suppose your primary database sits at 10.128.0.10 and your read replica sits at 10.128.0.11:

INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '10.128.0.10', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (20, '10.128.0.11', 3306);

LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;

Here, hostgroup 10 represents your writer group, and hostgroup 20 represents your reader group.

Setting Up Query Rules

To split traffic effectively, configure query rules that inspect incoming SQL statements. Send all statements starting with SELECT (unless they contain explicit locking clauses like FOR UPDATE) to the reader hostgroup:

INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, comment) 
VALUES (1, 1, '^SELECT.*FOR UPDATE$', 10, 'Route explicit locking reads to writer');

INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, comment) 
VALUES (2, 1, '^SELECT', 20, 'Route standard reads to reader replica');

LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;

Optimizing Connection Pooling

Cloud databases have strict connection limits. ProxySQL prevents connection exhaustion by maintaining a persistent pool of open connections to Cloud SQL. Configure connection limits in mysql_users and global variables:

UPDATE global_variables SET variable_value='2048' WHERE variable_name='mysql-max_connections';
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

Ensure your application users are defined in ProxySQL with matching credentials:

INSERT INTO mysql_users (username, password, default_hostgroup) VALUES ('app_user', 'SecurePassword123!', 10);
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;

High Availability and Monitoring on Google Cloud

A single ProxySQL instance represents a potential single point of failure. In production environments, deploy at least two ProxySQL nodes behind an Internal TCP/UDP Load Balancer in Google Cloud.

[ Internal Load Balancer ]
       │
       ├─> [ ProxySQL Node 1 ] ──> [ Cloud SQL Primary / Replicas ]
       └─> [ ProxySQL Node 2 ] ──> [ Cloud SQL Primary / Replicas ]

Health Checking and Failover

Google Cloud Internal Load Balancers require health check probes to determine instance availability. You can configure a lightweight HTTP health check script on your ProxySQL instances that queries port 6032 to ensure the proxy process is responsive. If a ProxySQL node fails, the load balancer automatically reroutes application traffic to the healthy node.

For backend failover between primary and replica databases, configure ProxySQL's built-in monitor daemon:

UPDATE global_variables SET variable_value='3000' WHERE variable_name='mysql-monitor_ping_interval';
UPDATE global_variables SET variable_value='cloud_sql_mon_user' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='MonitorPassword' WHERE variable_name='mysql-monitor_password';
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

ProxySQL continuously pings backend nodes. If the primary instance goes down, ProxySQL can promote a replica or trigger external orchestration tools to handle promotion safely.

Monitoring Metrics

Integrate ProxySQL metrics into Cloud Monitoring by scraping the admin statistics tables. Key metrics to track include:

  • mysql_counter.Client_Connections_Connected: Active connections from application servers.
  • mysql_servers.Conn_Used: Connections currently busy executing queries against backend databases.
  • mysql_query_rules.hits: Number of queries matching specific routing rules.
  • Backend query latency distributions across hostgroups.

Common Pitfalls and Troubleshooting

Database proxy layers introduce abstraction that can complicate debugging if misconfigured.

  • Connection Timeout Mismatch: If your application experiences dropped connections during idle periods, verify that ProxySQL's mysql-wait_timeout setting aligns with your backend database and application connection pooling timeout configurations.
  • Prepared Statement Errors: Some application frameworks use server-side prepared statements that can fail if routed across different backend nodes in a read hostgroup. Enable multiplexing restrictions or disable server-side prepared statements in your database driver configuration if you encounter erratic errors.
  • Configuration Drift: Always execute LOAD ... TO RUNTIME followed by SAVE ... TO DISK after making changes in the admin console. Forgetting the save step means configuration changes vanish when the ProxySQL service restarts.

For official documentation on cloud networking setups, review the Google Cloud VPC Documentation, and consult the ProxySQL Official Manual for detailed syntax references.

Frequently Asked Questions

Can ProxySQL replace a database load balancer entirely?

Yes, ProxySQL acts as an application-aware SQL load balancer. It understands database protocols, parses SQL text, and routes traffic based on query types, making standard TCP load balancers unnecessary for SQL traffic routing.

Does ProxySQL require changes to application code?

In most cases, no. Applications connect to ProxySQL using standard MySQL connection strings. You only need to adjust connection strings to point to the ProxySQL IP address and port instead of the direct database instance.

How does ProxySQL handle database failovers?

ProxySQL monitors backend health checks. When a primary instance fails, ProxySQL detects the outage and can reject new write traffic or work alongside orchestration tools to redirect writes to a promoted replica.

Is ProxySQL compatible with Google Cloud SQL?

Yes. ProxySQL integrates seamlessly with Cloud SQL for MySQL as well as self-hosted MySQL instances running on Compute Engine.

IC

Written by

Isabella Carter

As a veteran correspondent, Isabella Carter has reported from across the globe, bringing firsthand perspectives to international stories and local issues.

More from database