Timeline of NewSQL databases:
graph LR
A["Google Spanner
2012"] --> B["CockroachDB
2015"]
B --> C["YugabyteDB
2016"]
C --> D["TiDB
2017"]
NoSQL databases are primarily referred to as non-relational. Database structures are mostly organized into key-value pairs or document-based structures.
The main difference between SQL and NoSQL lies in the way they scale. SQL databases are vertically scalable, meaning you can increase the load on a single server by upgrading components like CPU, RAM, or SSD. On the other hand, NoSQL databases are horizontally scalable, meaning you can handle more traffic by sharing data or adding more servers to your NoSQL database.
NewSQL databases are designed to combine the scalability of NoSQL with the ACID guarantees of traditional RDBMS. NewSQL databases are designed to scale out on commodity hardware in a manner similar to NoSQL databases, but they also support SQL and ACID guarantees. Therefore, they build a SQL layer on top of the NoSQL database.
graph LR
b & A["Google Spanner
2012"] --> B["CockroachDB
2015"]
B --> C["YugabyteDB
2016"]
C & c --> D["TiDB
2017"]
a(["LevelDB
2011"]) --> b(["RocksDB
2012"])
b --> c(["TiKV
2016"])
c --> d(["Pebble
2020"])
d --> B
As mentioned earlier, NewSQL databases integrate a SQL layer on top of the NoSQL database. Initially, CockroachDB was built on top of RocksDB, which is a key-value store based on LevelDB and optimized for write-heavy workloads. RocksDB is inspired by LevelDB and has been used by Facebook for Facebook Messages and Facebook Search. LevelDB, on the other hand, is a key-value store optimized for read-heavy workloads. It draws inspiration from Bigtable, a distributed storage system for structured data developed by Google. Later, CockroachDB switched to Pebble, a LevelDB/RocksDB-inspired key-value store focused on performance.
To implement the above case using Google Spanner for an e-commerce platform scenario, follow these steps:
- Create a Spanner instance using the Google Cloud Console or the Google Cloud CLI. This instance will serve as the database server.
gcloud spanner instances create test-instance --config=regional-us-east1 --description="My Instance" --nodes=2- Create a database in the Spanner instance. This database will hold the e-commerce platform's data.
gcloud spanner databases create ecommerce --instance=test-instance- Create tables in the database to hold different types of data, such as products, customers, and transactions. You can use SQL commands to create these tables.
gcloud spanner databases ddl update ecommerce \
--instance=test-instance --ddl='CREATE TABLE Customers (
CustomerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024)
) PRIMARY KEY(CustomerId);'- Insert data into the tables and execute queries as required by the e-commerce platform.
gcloud spanner databases execute-sql ecommerce --instance=test-instance \
--sql='INSERT INTO Customers (CustomerId, FirstName, LastName)
VALUES (1,"John", "Doe");'- For application development, Google provides client libraries in several languages like Java, Python, Node.js, etc. These libraries can be used to interact with Spanner from the application.
To implement the above case using CockroachDB, follow these steps:
- Log in or sign up for CockroachCloud.
- Create a Cluster from the Clusters page.
- Select "Serverless" as the deployment type.
- Click "Create Cluster."
- Create an SQL user.
- Provide a name for the SQL user.
- Click "Generate & save password."
- Copy the SQL user name and password.
- Click "Next."
- For application development, you can develop it similar to how you would for PostgreSQL.
