Cloud Spanner for global distribution in GCP - Commands & Configuration
apiVersion: spanner.cnrm.cloud.google.com/v1beta1 kind: SpannerInstance metadata: name: global-spanner-instance spec: config: regional-us-central1 displayName: "Global Spanner Instance" nodes: 3 labels: environment: production --- apiVersion: spanner.cnrm.cloud.google.com/v1beta1 kind: SpannerDatabase metadata: name: global-database spec: instanceRef: name: global-spanner-instance ddlStatements: - "CREATE TABLE Users (UserId STRING(36) NOT NULL, UserName STRING(1024)) PRIMARY KEY (UserId)" - "CREATE TABLE Orders (OrderId STRING(36) NOT NULL, UserId STRING(36), OrderDate TIMESTAMP) PRIMARY KEY (OrderId)"
This file creates a Cloud Spanner instance named global-spanner-instance in the us-central1 region with 3 nodes for capacity. It also creates a database called global-database inside that instance. The database has two tables: Users and Orders. The tables have primary keys defined to organize data efficiently.
The config field sets the instance location. The nodes field controls how much power the instance has. Labels help identify the environment.
gcloud spanner instances create global-spanner-instance --config=regional-us-central1 --description="Global Spanner Instance" --nodes=3
--config - Sets the geographic location of the instance.--nodes - Specifies the number of nodes for capacity.gcloud spanner databases create global-database --instance=global-spanner-instance --ddl='CREATE TABLE Users (UserId STRING(36) NOT NULL, UserName STRING(1024)) PRIMARY KEY (UserId)' --ddl='CREATE TABLE Orders (OrderId STRING(36) NOT NULL, UserId STRING(36), OrderDate TIMESTAMP) PRIMARY KEY (OrderId)'
--instance - Specifies the instance where the database will be created.--ddl - Defines the schema for tables in the database.gcloud spanner instances describe global-spanner-instance
gcloud spanner databases list --instance=global-spanner-instance
--instance - Specifies the instance to list databases from.If you remember nothing else from this pattern, remember: Cloud Spanner lets you store and access your data quickly and safely across the world by creating instances and databases with the right location and capacity.
Practice
Solution
Step 1: Understand global distribution purpose
Cloud Spanner stores data in multiple regions to improve speed and reliability.Step 2: Compare options
Only It keeps applications fast and reliable by storing data across multiple regions. correctly describes the benefit of global data storage for performance and reliability.Final Answer:
It keeps applications fast and reliable by storing data across multiple regions. -> Option AQuick Check:
Global data storage = fast and reliable apps [OK]
- Thinking global distribution saves storage cost
- Believing data is deleted automatically
- Assuming data is limited to one region
Solution
Step 1: Identify multi-region config names
Cloud Spanner uses specific multi-region config names like "nam3" for North America multi-region.Step 2: Check options
Only A is a valid multi-region config. Options B, C, and D are single-region names.Final Answer:
multi_region_config = "nam3"-> Option DQuick Check:
Multi-region config uses names like nam3 [OK]
- Using single region names instead of multi-region codes
- Confusing region names with zone names
- Misspelling multi-region config strings
instance_config = "nam3"
nodes = 3What is the expected behavior of this setup?
Solution
Step 1: Interpret instance_config "nam3"
"nam3" is a multi-region config for North America, so data is distributed across multiple regions there.Step 2: Understand nodes setting
3 nodes means the instance has 3 processing units to handle workload.Final Answer:
Data is globally distributed across North America with 3 nodes handling requests. -> Option AQuick Check:
nam3 = North America multi-region + 3 nodes [OK]
- Assuming single region storage with multi-region config
- Ignoring the meaning of nodes
- Confusing region names with multi-region configs
instance_config = "nam3" but your app experiences high latency in Europe. What is the likely cause?Solution
Step 1: Understand "nam3" coverage
"nam3" is a multi-region config limited to North America regions only.Step 2: Analyze latency cause
High latency in Europe happens because data is not stored close to European users.Final Answer:
The multi-region config "nam3" only covers North America, not Europe. -> Option CQuick Check:
nam3 excludes Europe, causing latency there [OK]
- Assuming multi-region covers all continents
- Thinking node count fixes geographic latency
- Believing "nam3" is invalid
Solution
Step 1: Identify multi-region config covering Asia, Europe, North America
"nam-eur-asia1" is a multi-region config spanning these continents.Step 2: Understand node allocation
Choosing enough nodes ensures capacity and low latency across regions.Step 3: Compare other options
Single-region configs or multiple instances increase complexity or limit coverage.Final Answer:
Use the multi-region config "nam-eur-asia1" with enough nodes. -> Option BQuick Check:
nam-eur-asia1 covers all needed continents [OK]
- Choosing single-region config for global users
- Using nam3 which excludes Europe and Asia
- Deploying multiple instances instead of one multi-region
Ready to prove it?
Pass the test and this pattern moves unknown
