Skip to content

Instantly share code, notes, and snippets.

@sithumonline
Last active October 11, 2023 00:41
Show Gist options
  • Select an option

  • Save sithumonline/d0995099fb76213add94902b35094a53 to your computer and use it in GitHub Desktop.

Select an option

Save sithumonline/d0995099fb76213add94902b35094a53 to your computer and use it in GitHub Desktop.

Case for Using NewSQL over RDBMS & NoSQL

Timeline of NewSQL databases:

graph LR
    A["Google Spanner
     2012"] --> B["CockroachDB 
     2015"]
    B --> C["YugabyteDB
    2016"]
    C --> D["TiDB 
    2017"]
Loading

What is the Difference Between SQL and NoSQL?

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.

Why Choose NewSQL?

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.

NewSQL vs. NoSQL

Utilizing Google Spanner / CockroachDB

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
Loading

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.

Implementing a Cloud Solution

To implement the above case using Google Spanner for an e-commerce platform scenario, follow these steps:

  1. 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
  1. 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
  1. 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);'
  1. 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");'
  1. 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:

  1. Log in or sign up for CockroachCloud.
  2. Create a Cluster from the Clusters page.
  3. Select "Serverless" as the deployment type.
  4. Click "Create Cluster."
  5. Create an SQL user.
  6. Provide a name for the SQL user.
  7. Click "Generate & save password."
  8. Copy the SQL user name and password.
  9. Click "Next."
  10. For application development, you can develop it similar to how you would for PostgreSQL.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment