Hands-on Tutorial on Google Cloud Spanner: A Globally Distributed Relational Database
Cloud Spanner is a managed, horizontally scalable, and globally distributed relational database service from Google Cloud Platform (GCP).
Features
- Cloud Spanner is designed to be fully managed, which means that Google manages the infrastructure and ensures high availability, automatic scaling, and backups for customers. This allows customers to focus on their application code rather than managing the database infrastructure.
- Cloud Spanner is highly available with zero downtime, as it automatically replicates data across multiple zones and regions.
- Cloud Spanner is a globally distributed database, which means that it can span multiple geographic locations. This allows customers to achieve high performance and low latency for their users, regardless of where their users are located.
- Cloud Spanner is designed to be highly scalable, with the ability to scale to petabytes of data and thousands of nodes.
- Cloud Spanner is a relational database that supports SQL queries, and provides features such as ACID transactions, foreign keys, and SQL-compatible schemas. This makes it easy for customers to migrate their existing applications and databases to Cloud Spanner.
- Cloud Spanner also provides built-in features for security and compliance, such as encryption at rest and in transit, IAM integration, and compliance with various industry standards such as HIPAA, SOC 2, and ISO 27001.
Cloud Spanner can be used as a fully-managed database service for mission-critical transactional workloads, or as a highly available read-replica for other databases. The database is designed to provide consistent performance and high availability, even for large-scale workloads, across multiple regions and continents.
In this blog, we shall create spanner instances, databases & tables (using UI, Cloud Shell & Terraform script) and then query those.
Create spanner instances, databases and tables through GCP Console
Step 1: Create a spanner instance.
(1) Go to spanner on GCP console and select the following instance:
(2) Provide a name for your instance and select a region. Under compute capacity, you can provide the quantity in terms of nodes or processing unites. 1000 processing unit is equivalent to 1 node.
In Google Cloud Spanner, the number of databases that can be created and hosted within a Spanner instance is theoretically unlimited. The number of databases that can be created within a Spanner instance is limited only by the available storage capacity and the maximum number of nodes that can be added to an instance.
Step 2: Create a database in your instance.
Now let’s create a database in our moona-spanner-instance
(1) Select the Create database
option
(2) Give your database a name and click on CREATE
Step 3: Create a table in your database.
Now let’s create our first table inside our database
(1) Select the CREATE TABLE
option
(2) Write the following DDL statement to create MoonaTable1
and click SUBMIT
CREATE TABLE MoonaTable1 (
id INT64 NOT NULL,
FirstName STRING(200) NOT NULL,
LastName STRING(200) NOT NULL,
BirthDate DATE
) PRIMARY KEY(id);
(3) Once the table is created, select the table name to view the schema definition.
Step 4: Insert records in this table.
Let’s insert a couple of records in our table.
INSERT INTO
MoonaTable1 ( id,
FirstName,
LastName,
BirthDate)
VALUES
(1, 'Dipan', 'Saha', '1985-01-01');
INSERT INTO
MoonaTable1 ( id,
FirstName,
LastName,
BirthDate)
VALUES
(2, 'Moona', 'Saha', '1985-02-01');
Go to the Query
tab and run the above 2 queries.
Run a SELECT query against your table to confirm that the records have been inserted
Create spanner instances, databases and tables through Cloud Shell
Step 1: Activate cloud shell.
Step 2: Create a spanner instance, a database and a table
# Ensure that you are using the correct account and project
gcloud auth list
gcloud config list project
# Create a spanner instance
gcloud spanner instances create moona-spanner-instance-two --description=moona-spanner-instance-two --config=regional-us-central1 --nodes=2
gcloud spanner instances list
# Create a database in moona-spanner-instance-two
gcloud spanner databases create moona-database-1 --instance=moona-spanner-instance-two
# Update the number of nodes for the spanner
gcloud spanner instances update moona-spanner-instance-two --nodes=1
# Create a table in this database
gcloud spanner databases ddl update moona-database-1 --ddl='CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), SingerInfo BYTES(MAX) ) PRIMARY KEY (SingerId)' --instance moona-spanner-instance-two
And now, you should be able to see the new table under the new spanner database through the UI —
Create spanner instances, databases and tables using a terraform script
Step 1: Validate the terraform version in Google Cloud Shell
terraform -version
touch spanner.tf
As terraform comes pre-installed in the Cloud Shell, you should see something like this —
Step 2: Create a terraform file and write a code to create a spanner instance
(1) Create a blank terraform file called spanner.tf
touch spanner.tf
(2) Open the Cloud Shell Editor and copy paste the below code.
resource "google_spanner_instance" "moona-spanner-instance-three" {
name = "moona-spanner-instance-three"
config = "regional-us-central1"
display_name = "Moonsa Spanner Instance Three"
num_nodes = 1
labels = {
}
}
Step 3: Execute the terraform script
# Ensure that the all the terraform service providers are available
terraform init
# Ask Terraform to create an execution plan
terraform plan
# Execute the terraform script
terraform apply
# Test that the instance was created
gcloud spanner instances list
This should show you all the instances which are currently available —
Clean up
gcloud spanner instances delete moona-spanner-instance
gcloud spanner instances delete moona-spanner-instance-two
gcloud spanner instances delete moona-spanner-instance-three
Conclusion
Hope you found this blog useful! Let me know what you think about this, if you have any suggestion of a topic you would love to see here get in touch.
If you enjoyed the writings leave your claps 👏 to recommend this article so that others can see it.