microservices_on_cloudsql_whitepaper
microservices_on_cloudsql_whitepaper
January 2023
Cloud SQL
Architecture
Patterns for
Microservices
Table of Contents
Chapter 1
Definition of 4
Microservices
Chapter 2
Database 7
Properties
Chapter 3
Deployment 10
Architectures
Chapter 4
Architecture 22
Selection
Introduction
There is no
one-size-fits all
database
What should the database deployment architecture look
like for an application that is implemented according to a deployment option
microservices paradigm? Should it be a single centralized
database? Are several databases more adequate?
We try to help you the application developer to understand We try to help you
the complexity within the context of database
deployments, for database architects to rationalize the
decision process, and for database administrators to
the application
understand implications of the various database
deployment possibilities.
developer to
understand the
complexity within
the context of
database
deployments
Chapter 1
Definition of
There is no single
Microservices accepted definition
What is a microservice?
of the term
Database Properties
Instance is the
deployment unit
● Data consistency
● Data sharding
● Database size
● Backup/restore
● Distributed transactions
● Fleet management
● Disaster recovery
● Vertical and horizontal scaling An instance might
Instance vs. database contain one or
Instance is the deployment unit, while database is a data more databases
container and does not correspond to a unit of deployment.
In this sense the headers of this section are all wrong and
probably should not contain “database.” The
instance-database discussion is a point of confusion (still
after 50+ years of databases) and we need to point that
out. For example, you cannot run a database as a container
in K8s, but you can in an instance.
In this architecture each microservice persists data in its own Cloud SQL instance. Cloud SQL (PostgreSQL or
MySQL) can have multiple databases per instance. However a single database within an instance is deployed for
each microservice.
Isolation:
Provides the greatest level of isolation. Databases and
Instances do not share resources and can be managed,
maintained, and deployed separately.
Agility:
Cloud SQL Instances are easy and fast to provision and
deprovision. Compared to the other architectures this
requires the most effort and time. The number of steps
involved and the configuration required is commonly
automated for efficiency and consistency. Instance
provisioning and de-provisioning can be automated using
Terraform.
Configuration:
Each microservice database can be configured
independently at the database or instance level. For
example, database flags can be changed at the instance
level specifically to meet the requirements of the service.
The database can be tuned without undue consideration
for the impact on other databases or services.
Scalability:
A microservice database can consume the entire instance.
As a result there are no restrictions within the instance
itself and the database service scales to the full
capabilities of a Cloud SQL instance.
Performance:
There is no resource sharing within the instance and
therefore there is no possibility of resource contention
between the databases or noisy neighbor effect. The
database for the service can perform to the full capability
and scale of a single instance.
Security:
Data for each microservice can be secured separately and
different levels of access provided for each function or
user. Data access is not shared by the services. Cloud SQL reduces
Additionally, data can be easily secured with access
provided only through the services themselves via service your database
APIs.
instance costs
Instances can be configured separately for each service
and deployed in different Data Centers or Regions
depending on regulation and compliance requirements.
Availability:
Instances can be configured for different levels of
availability depending on the requirements for the service.
Cloud SQL provides HA and non-HA configurations for each
instance. Services that do not require 99.95% availability
can be configured without HA.
Cost:
Allocating a single instance for each microservice would
incur a higher cost relative to other architecture options.
Cloud SQL instances are vertically scalable. However,
sufficient vCPUs and memory must be initially assigned to
account for small increases in demand and sudden spikes
in load. Factor in possible over provisioning and idle
instances.
Microservices in this architecture can share a Cloud SQL instance but are deployed within a single isolated
database. Cloud SQL (PostgreSQL, MySQL, SQL Server) can have multiple databases per instance. This
potentially maximizes the density and the utilization of resources allocated to each instance. This architecture
leverages the feature of Cloud SQL database engines that allows multiple databases per instance as shown in
the diagram below.
Isolation:
Complete isolation at the database level. However
databases share the same resources in a Cloud SQL
instance (memory & CPU). Configurations such as flag
settings potentially affect all databases, including
operational activities.
Agility:
Cloud SQL databases can be easily provisioned and
deleted from an instance. This process would generally be
faster compared to when an instance must be created first.
Database provisioning and deletion can be automated and
included in the CI/CD pipelines.
Configuration:
Databases for each service can be configured at the
database level. For example, connections. Configurations
done at the instance level affect all databases. Most
configurations for MySQL, PostgreSQL are done at the
instance level.
Scalability:
Databases share the same resources within a Cloud SQL
instance. Each database will be limited to the available
resources available up to the maximum Cloud SQL resource
allocations (e.g. CPU, memory, and storage).
Performance:
There is resource sharing within the instance and the
possibility of resource contention between the databases.
For example, databases needing to access CPU resources
at the same time or the Write Ahead Log (WAL) processes
simultaneously. Overprovisioning of capacity or over
allocation of processes is required to mitigate conflicts.
Security:
It is easy to securely configure all databases at the instance
level. Conversely a lapse in the configuration of the security
or a delay in patching can affect all databases. Database
level and object level security can be configured separately
to isolate each database.
Availability:
Database and the microservice that they support share the
same level of Availability. For example if the Cloud SQL
instance is configured for HA then all databases share the
HA configuration. Whenever the instance fails over to
another zone all databases will be protected.
Cost:
Allocating a single database for each microservice
increased the density of each Cloud SQL instance.
Resources allocated are shared and used more efficiently.
This can reduce the overall cost of Cloud SQL across all
microservices.
In the Schema per service architecture the microservice is deployed and constrained to only a single schema
within a database. All Cloud SQL platforms allow for the creation of multiple schemas within a database. This
architecture leverages this feature and ensures that the objects (tables, views, indexes) for the microservice are
constrained to a single schema. Monitoring and controlling technical depth in this architecture is particularly
important since it is easy to create SQL statements that cross schemas belonging to multiple microservices.
Over time this could lead to the deployment developing into a monolithic architecture where services are tightly
coupled and expensive to update and deploy independently.
Isolation:
Logical isolation for database objects (tables, indexes,
views) is provided within the database at the schema level.
Each schema shares a single database and instance.
Operations and changes at the database and instance level
affect all schemas and hence the services supported by
those schemas. Additionally, resource consumption or
changes within each schema can affect other schemas or
services.
Agility:
Schemas can be quickly created and deleted within a
database and instance that was previously provisioned.
Database provisioning and deletion can be automated and
included in the CI/CD pipelines using tools such as
Liquibase.
Configuration:
Configuration at the database and Instance level affects all
schemas. For example, connections or database flags. Most
configurations for MySQL, PostgreSQL are done at the
instance level. Services requiring specific database
configurations must be deployed in separate databases or
instance.
Scalability:
Schemas share the same resources within a database and a
Cloud SQL instance. Each schema will be limited by the
amount of resources available and remaining within the
database.
Performance:
There is resource sharing within the instance and the
possibility of resource contention between the processes
accessing each schema on behalf of individual services. For
example, connections from different services needing to
access CPU resources at the same time or the WAL
processes needing to replicate transactions from multiple
services simultaneously. Overprovisioning of capacity or
over allocation of processes is required to mitigate conflicts.
Security:
It is easy to secure every service at the database or instance
level. Conversely a lapse in the configuration of the security
or a delay in patching can affect every service. Security can
be configured at the schema and object level separately to
isolate each service data.
Availability:
Each schema and the microservice that it supports share
the same level of availability. For example, if the Cloud SQL
instance is configured for HA then all databases share that
HA configuration and SLA. Whenever the instance fails over
to another zone every service will failover and be protected.
Cost:
Allocating a single database for each microservice
increased the density of each Cloud SQL instance.
Resources allocated are shared and used more efficiently.
This can reduce the overall cost of Cloud SQL across all
microservices.
Architecture
Deploying a
Selection microservice
architecture can
How do you select a database architecture for your
microservices?
require different
Deploying each
microservice in a
separate Cloud
SQL instance will
provide the most
flexibility, agility,
Deploying multiple databases within a single Cloud SQL
instance can provide the most density and resource scalability, and
utilization. This architecture type will also provide upfront
cost optimization without introducing operational
complexity and technical debt. Deploying each microservice
best performance
in a separate Cloud SQL instance will provide the most
flexibility, agility, scalability, and best performance in the in long term
long term.
Customer
References and
Examples
Auto Trader (UK) : Migrated from Oracle to PostgreSQL,
and using microservices.
"Our architecture consists of around 500 logical services,"
says Karl Stoney, Technical Architect at Autotrader. "Around
140 of those are already in production on GCP, and we plan
to migrate the rest over the next year so everything is built,
managed, and monitored in the same way."