Open In App

How To Configure Cloud Spanner In GCP ?

In today’s era when data is everywhere, the demand for databases which can seamlessly accumulate massive amount of data is increasing. Databases also have to provide consistency, reliability and high availability. Google Cloud Platform is one of famous cloud providers in the market providing cloud solutions including storage and databases. Cloud Spanner is a fully managed, scalable, distributed database service, offered by Google Cloud Platform. Cloud Spanner combines the benefits of a traditional relational database system with the scale and flexibility of a NoSQL database. In this article we will be learning about, how we can create and configure Cloud Spanner in Google Cloud Platform.

What Is Cloud Spanner?

Cloud Spanner is a fully managed, distributed database service provided by Google Cloud Platform. Cloud spanner provides reliability, high availability and high scalability. Cloud spanner combines the traditional relational databases i.e. Strong consistency, high availability, SQL Queries with the agility and scalability of NoSQL databases. Cloud Spanner also provides fully automatic data replication and server redundancy within all available instances. Google also uses Cloud Spanner for it’s products like Gmail to internally manage workloads. The features provided by Cloud Spanner includes,



  1. Write and Read scalability with no limits
  2. Horizontal scalability
  3. SQL compatibility
  4. Automatic Management
  5. Vector Search

Create And Configure Cloud Spanner: A Step-By-Step Guide

Step 1: Login To Google Cloud Console

Step 2: Enable Spanner API

Once we are logged into the Cloud Console, we have to enable the Spanner API. Spanner API is an application programming interface (API) provided by Google Cloud Platform that allows developers to interact with Cloud Spanner and create, manage, and query databases withing Cloud Spanner.



Step 3: Search For Cloud Spanner

Step 4: Configure Cloud Spanner Instance

a) Configure Instance details:

b) Configure Region:

c) Configure Compute Capacity:

Step 5: Review And Create

Using Cloud Spanner Database: A Step-By-Step Guide

Spanner Database relies on the concepts of Primary Keys, Table interleaving, and Foreign Keys to maximize database efficiency and performance. In this example we will create a new database and a table then performing some basic database operations.

Step 1: Create Database

Step 2: Create Table

CREATE TABLE
<table_name> ( <col_name> <col_type>,
)
PRIMARY KEY
(<col_name>);
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
BirthDate DATE
) PRIMARY KEY(SingerId);

Output:

Step 3: Perform Database Operations

a) Insert Data: We will be using Spanner Studio to perform all database operations. To insert data use the following structure for query.

INSERT INTO
Singers (SingerId,
BirthDate,
FirstName,
LastName,
SingerInfo)
VALUES
(<SingerId>, -- type: INT64
<BirthDate>, -- type: DATE
<FirstName>, -- type: STRING(1024)
<LastName>, -- type: STRING(1024)
<SingerInfo> -- type: BYTES(MAX)
);

b) Read Operation: We have inserted two more records in the table. Let’s check all the rows in the database. Following is the format to check all the records available in the table.

select * from <table_name>

c) Update Operation: Let’s query again to add birthdate to “Jayesh Haldar” using query. We can also change other values in the same update statement. Let’s change the Singer Name and birthday.

UPDATE
Singers
SET
BirthDate='2000-01-01',
FirstName='Raj'
WHERE
SingerId=3;

Output:

d) Delete operation: Let’s try deleting the first singer from the table.

DELETE from Singers WHERE SingerId = 1

Output:

Conclusion

The Google Cloud Spanner offering features of RDBMS with flexibility of NoSQL system stands out from the other database solutions in the market. The spanner API further enhances features of Cloud Spanner by providing seamless programable access to Cloud spanner’s functionalities. This enables developers to integrate the database service in their applications. With Cloud Spanner, developers can build and deploy critical applications with effortless scaling, high availability and high performance.

Configure Cloud spanner in GCP – FAQ’s

How Does Cloud Spanner Ensures High Availability?

Cloud Spanner provides high availability through automatic replication of data across the available regions. This helps providing data is case of any regional outage happens.

What Is The Difference Between Cloud Sql And Cloud Spanner?

Cloud SQL and Cloud Spanner, both are database services provided by Google Cloud Platform. Cloud SQL is a fully managed relational database service that supports popular database engines like MySQL, PostgreSQL, and SQL Server while Cloud Spanner is a globally distributed, horizontally scalable relational database service.

Is Cloud Spanner Serverless ?

No, Cloud Spanner is not serverless. Cloud spanner uses managed instance as used by Cloud SQL . Cloud Spanner runs on a virtual server and it’s not available to the users.

What Is Cost Of Using Cloud Spanner?

Cloud spanner pricing is based on the resource consumption, which includes storage, nodes, etc. As it is a cloud service, it also uses the pay-as-you-go model. For the configurations we have made in our Cloud Spanner, you can check the pricing in the summary screenshot attached for a reference.


Article Tags :