Tech Interview RoomStudy guides

Cloud SQL

How do applications connect securely to Cloud SQL? Explain public vs private IP, the Auth Proxy and connectors.

Intermediate · 2 min read · Lesson 2 of 5 in Cloud SQL

Network paths

  • Private IP: the instance gets an address inside your VPC (via Private Service Access or Private Service Connect). Traffic never crosses the internet. Preferred for production apps on GKE, Compute Engine and Cloud Run (with Direct VPC egress or a Serverless VPC connector).
  • Public IP: reachable from the internet, but only from authorised networks you list, or through the Auth Proxy/connectors. Avoid opening it widely.

Cloud SQL Auth Proxy and language connectors

The Auth Proxy (a small binary or GKE sidecar) and the Cloud SQL connectors (Java, Python, Go, Node.js libraries) open an encrypted TLS 1.3 tunnel using IAM credentials, so you do not manage SSL certificates or IP allowlists. The app connects to localhost (proxy) or uses a socket factory (Java connector).

Authentication

  • Built-in database users and passwords (store passwords in Secret Manager).
  • IAM database authentication: service accounts or users log in with short-lived OAuth tokens instead of passwords; works well with the proxy's --auto-iam-authn.

Connection limits

Cloud SQL caps connections by instance memory. Serverless platforms (Cloud Run, Cloud Functions) can scale to many instances quickly, so cap the pool size per instance and the maximum number of instances, or add a pooler (PgBouncer, or the Managed Connection Pooling offered for some editions).

Example

yaml
# GKE: the Auth Proxy as a sidecar, using Workload Identity (no key files)
containers:
  - name: orders-api
    image: asia-south1-docker.pkg.dev/my-proj/apps/orders-api:1.4.2
    env:
      - name: SPRING_DATASOURCE_URL
        value: jdbc:postgresql://127.0.0.1:5432/orders
  - name: cloud-sql-proxy
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.0
    args:
      - "--private-ip"
      - "--auto-iam-authn"
      - "--structured-logs"
      - "my-proj:asia-south1:orders-db"
    securityContext:
      runAsNonRoot: true
properties
# Spring Boot with the Java connector instead of a proxy
spring.datasource.url=jdbc:postgresql:///orders?cloudSqlInstance=my-proj:asia-south1:orders-db&socketFactory=com.google.cloud.sql.postgres.SocketFactory&enableIamAuth=true&user=orders-api@my-proj.iam
spring.datasource.hikari.maximum-pool-size=5

Follow-up questions interviewers ask

Try answering each one before you open it.

Why is the Auth Proxy still useful with private IP?

It still provides IAM-based authorisation (who may connect), automatic TLS encryption without certificate management, and IAM database login; private IP only controls the network path.