Cloud SQL + Spring Boot Developer Guide
## Cloud SQL + Spring Boot Developer Guide
What is Cloud SQL?
Cloud SQL is a fully managed relational database service on Google Cloud. It supports MySQL, PostgreSQL,
and SQL Server. Cloud SQL automates backups, replication, patches, and maintenance, making it ideal for
cloud-native apps.
Why Use Cloud SQL with Spring Boot?
- Fully managed database with built-in high availability
- Scales vertically and supports read replicas
- Easy integration with Spring Data JPA and JDBC
- Works seamlessly with Cloud Run, GKE, App Engine
- Automatic failover with high availability configuration
Other Managed Relational Databases on Google Cloud
#### AlloyDB
- Googles fully managed PostgreSQL-compatible database
- Offers superior performance and availability over standard PostgreSQL
- Ideal for analytics, hybrid transactional/analytical workloads (HTAP)
- Supports vector search and advanced memory caching
#### Cloud Spanner
- Globally distributed, horizontally scalable relational DB
- Ideal for massive, mission-critical applications needing strong consistency
- Offers SQL support with unlimited scale
#### Comparison Table
| Feature | Cloud SQL | AlloyDB | Cloud Spanner |
|--------|-----------|---------|----------------|
Cloud SQL + Spring Boot Developer Guide
| Compatibility | MySQL, PostgreSQL, SQL Server | PostgreSQL | Custom SQL dialect |
| Scale | Vertical (manual) | Vertical + memory-optimized | Horizontal, global |
| Performance | Moderate | High (4x vs. standard PG) | High throughput |
| HA Options | Regional | Zonal/regional | Multi-region |
| Use Case | Web apps, APIs, CMS | Analytics + transactional apps | Global fintech, ecommerce |
Architecture Diagram
_(Diagram placeholder to be inserted)_
Creating a Cloud SQL Instance
```bash
gcloud services enable sqladmin.googleapis.com
gcloud sql instances create spring-db --database-version=POSTGRES_14 --tier=db-f1-micro
--region=us-central1
gcloud sql databases create springdb --instance=spring-db
gcloud sql users set-password postgres --instance=spring-db --password=your-password
```
Access Types
- Public IP: Simple for quick access, allowlisted IPs
- Private IP: Secure, used with VPC networks for internal communication
- Cloud SQL Auth Proxy: Best for local dev & production, handles IAM + encryption
Connecting from Spring Boot
```properties
spring.datasource.url=jdbc:postgresql://localhost:5432/springdb
spring.datasource.username=postgres
spring.datasource.password=your-password
spring.datasource.driver-class-name=org.postgresql.Driver
Cloud SQL + Spring Boot Developer Guide
```
```bash
./cloud-sql-proxy spring-db --port 5432
```
```properties
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.connection-timeout=30000
```
Authentication & IAM
- Assign Cloud SQL Client role to GCP service accounts
- Use IAM conditions for time-based and IP-based access control
- Leverage Secret Manager to store credentials securely
- Rotate DB passwords regularly with automation scripts
Monitoring, Backups & Logs
- Enable automated backups with retention settings
- Enable binary logging for PITR (Point-in-Time Recovery)
- Use Query Insights to visualize slow queries & CPU usage
- Export logs to Cloud Logging and integrate with Cloud Monitoring dashboards
High Availability (HA)
- Enable high availability (regional instance) to replicate across zones
- Failover happens automatically in case of zone failure
- Best practice for mission-critical applications
Cloud SQL + Spring Boot Developer Guide
Performance Tuning
- Use SSD for low-latency access
- Adjust flags (e.g. work_mem, shared_buffers in PostgreSQL)
- Optimize schema with indexes and constraints
- Monitor slow query logs and use EXPLAIN ANALYZE
- Avoid using persistent connections without pooling
Security Best Practices
- Enforce SSL connections using client certs
- Restrict inbound IPs for public access
- Prefer Private IP with IAM-based access
- Use VPC Service Controls for data exfiltration prevention
- Enable Deletion Protection for prod instances
Pricing & Tier Options
| Tier | vCPU | RAM | Storage | Use Case |
|------|------|-----|---------|----------|
| db-f1-micro | Shared | 0.6 GB | HDD/SSD | Dev/Test |
| db-g1-small | Shared | 1.7 GB | HDD/SSD | Small workloads |
| db-custom-* | Custom | Up to 416 GB | SSD | Production apps |
| High Availability | Multi-zone | Same | SSD | Mission-critical apps |
Best Practices
- Use read replicas for read-heavy workloads
- Use connection pooling (e.g. HikariCP, PgBouncer)
- Enable deletion protection for production DBs
- Regularly test failover if HA is enabled
- Monitor query latency and IOPS in dashboards
- Limit max connections to prevent overload
Cloud SQL + Spring Boot Developer Guide
Cloud SQL Command Cheat Sheet
| Command | Description |
|---------|-------------|
| gcloud sql instances create | Create a new instance |
| gcloud sql users set-password | Set user password |
| gcloud sql connect | Connect via CLI |
| gcloud sql databases create | Create database |
| gcloud sql export sql | Export DB to Cloud Storage |
| gcloud sql import sql | Import SQL from Cloud Storage |
| gcloud sql instances patch | Update DB settings |
| gcloud sql operations list | List operations/status |
| gcloud sql ssl client-certs create | Create SSL client cert |
| gcloud sql users list | List DB users |
| gcloud sql backups list | View backup history |
| gcloud sql instances describe | Full instance details |
2025 - Cloud Spring Series