For modern, globally distributed enterprises, deploying a single relational database instance across multiple continents is a technical necessity. However, this technical necessity often collides directly with severe geopolitical and legal restrictions. Regulations such as the European Union’s GDPR or India’s Personal Data Protection Bill mandate strict Data Residency—meaning data belonging to citizens of those regions must be physically stored on servers located within their geographic borders.
Historically, achieving relational data residency required deploying completely separate, isolated database clusters for each region, leading to fragmented applications and massively complex application logic to route users to the correct database.
Google Cloud Spanner, a fully managed, horizontally scalable relational database, solves this with Geo-Partitioning. By utilizing Spanner’s distributed architecture and specifically configuring row-level placement policies, architects can maintain a single global database (with a single global schema and endpoint) while mathematically guaranteeing that specific rows of data never leave specific physical geographies.
Understanding Geo-Partitioning Architecture
Spanner achieves geo-partitioning by extending its core architectural concept: the Split. Spanner automatically shards (splits) data into chunks and distributes them across nodes to balance load. By default, in a multi-region configuration, these splits are replicated globally for high availability.
With Geo-Partitioning, you manipulate the schema to instruct Spanner where to place specific splits.
- You define a Placement Strategy mapping a logical region (e.g., “EU”) to physical Google Cloud regions (e.g.,
europe-west1). - You define a partition key in your table schema (e.g.,
user_region). - You instruct Spanner to map specific values of the partition key directly to the Placement Strategy.
When an application inserts a row with user_region = 'EU', Spanner dynamically routes that row to a split physically located on a server in Europe, and restricts its replication exclusively to European zones.
Step 1: Creating a Custom Multi-Region Instance Configuration
Geo-partitioning requires a custom instance configuration, as you must define the exact physical regions your database will span and establish the base placement policies.
Using the gcloud CLI, create a custom configuration defining a topology that spans the US and the EU:
gcloud spanner instance-configs create custom-global-residency \
--base-config=nam-eur-asia1 \
--display-name="Custom Global Residency Topology" \
--replica-regions=europe-west1,europe-west4,us-central1,us-east4
Next, create the Spanner instance utilizing this custom configuration:
gcloud spanner instances create global-residency-db \
--config=custom-global-residency \
--description="Global Database with Data Residency" \
--nodes=3
Step 2: Defining Placements in DDL
Once the instance is running, you must modify your database schema using Google Standard SQL Data Definition Language (DDL) to define the Placements.
Open the Spanner Studio in the Google Cloud Console (or use the gcloud spanner databases execute-sql command) and execute the following DDL statements to create the Placement objects:
CREATE PLACEMENT eu_placement
OPTIONS (
default_leader = 'europe-west1',
regions = ['europe-west1', 'europe-west4']
);
CREATE PLACEMENT us_placement
OPTIONS (
default_leader = 'us-central1',
regions = ['us-central1', 'us-east4']
);
These commands create two distinct logical buckets. Any data assigned to eu_placement is guaranteed by the Spanner control plane to only exist on disks within europe-west1 and europe-west4.
Step 3: Creating the Partitioned Table
Now, you create the actual table and bind the rows to the placements using a partitioning key. The partition key must be the first column in the Primary Key definition.
CREATE TABLE Users (
Region STRING(2) NOT NULL,
UserId INT64 NOT NULL,
FullName STRING(100),
Email STRING(255)
) PRIMARY KEY (Region, UserId),
ROW DELETION POLICY (OLDER_THAN(Timestamp, INTERVAL 30 DAY))
PLACEMENT KEY (Region)
PLACEMENT MAP (
'EU' => eu_placement,
'US' => us_placement
);
This DDL creates a Users table. The PLACEMENT MAP explicitly defines the residency rules. If a row is inserted where Region = 'EU', it is routed to the European disks. If Region = 'US', it routes to the American disks.
Step 4: Executing Queries and Understanding Routing
From the application’s perspective, there is only one database connection string and one Users table. The application does not need to know about the complex underlying storage topology.
When the application executes an insert:
INSERT INTO Users (Region, UserId, FullName, Email)
VALUES ('EU', 1042, 'Hans Schmidt', '[email protected]');
Spanner transparently routes this write to the eu_placement leader in europe-west1, ensuring immediate GDPR compliance.
When the application executes a read query:
SELECT FullName FROM Users WHERE Region = 'EU' AND UserId = 1042;
The Spanner query optimizer recognizes that the Region column is the placement key. It aggressively prunes the execution plan, completely ignoring the US regions and routing the query exclusively to the European nodes, resulting in massive performance improvements and lower cross-region latency.
Conclusion
Google Cloud Spanner Geo-Partitioning resolves one of the most complex architectural challenges in global software engineering. By defining strict DDL placement policies, organizations can leverage a unified, strongly consistent, global database schema while mathematically enforcing localized data residency compliance for specific rows, satisfying both engineering scale and geopolitical legalities.