How do you monitor and tune a Cloud SQL instance?
Observability
- Cloud Monitoring metrics: CPU, memory, disk utilisation and IOPS, connections, replication lag, transaction and query counts. Alert on high CPU, low free memory, disk nearly full, connection count near the limit and replica lag.
- Query Insights: top queries by load, latency over time, query plans, and tags (application, controller, route) so slow queries map back to code; supports PostgreSQL and MySQL.
- Logs: database logs in Cloud Logging (slow query log for MySQL,
log_min_duration_statementfor PostgreSQL), and Recommenders for idle or over- and under-provisioned instances.
Tuning levers
- Database flags (Cloud SQL's way to set parameters):
max_connections,work_mem,log_min_duration_statement,innodb_buffer_pool_size(managed by tier), autovacuum settings. - Right-size the machine tier (vCPU and memory, so the working set fits in the buffer cache), enable the data cache (Enterprise Plus), and remember that disk performance scales with disk size for SSD.
- Usual SQL work: indexes, query rewrites, pagination, connection pooling, removing N+1 queries.
- Move read-heavy and reporting traffic to read replicas; cache hot data in Memorystore (Redis/Valkey).
Example
bash
# Log slow PostgreSQL statements (over 500 ms) and enable pg_stat_statements-style insights
gcloud sql instances patch orders-db \
--database-flags=log_min_duration_statement=500,cloudsql.enable_pg_cron=on \
--insights-config-query-insights-enabled \
--insights-config-record-application-tags \
--insights-config-record-client-addressjava
// Tag queries so Query Insights can group them by endpoint (sqlcommenter)
// application/route/controller tags are added as SQL comments, e.g.
// SELECT ... /*controller='OrderController',route='/orders/{id}'*/gcloud sql instances patch with flags can restart the instance for some flags; apply in a maintenance window.
Follow-up questions interviewers ask
Try answering each one before you open it.
Why can storage auto-increase become a cost problem?
Storage can grow automatically but never shrinks. A runaway table, unbounded logs or a long-held replication slot can permanently increase the disk size and cost. Monitor growth, set an auto-increase limit, and clean up data with retention policies.