<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: arthurolga</title>
    <description>The latest articles on DEV Community by arthurolga (@arthurolga).</description>
    <link>https://dev.to/arthurolga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F66566%2Fe114d588-780a-4284-91bb-7c7c6a0f9847.png</url>
      <title>DEV Community: arthurolga</title>
      <link>https://dev.to/arthurolga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arthurolga"/>
    <language>en</language>
    <item>
      <title>NewSQL: An Implementation with Google Spanner</title>
      <dc:creator>arthurolga</dc:creator>
      <pubDate>Mon, 10 Aug 2020 16:01:25 +0000</pubDate>
      <link>https://dev.to/arthurolga/newsql-an-implementation-with-google-spanner-2a86</link>
      <guid>https://dev.to/arthurolga/newsql-an-implementation-with-google-spanner-2a86</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;What is actually NewSQL? You’ve probably already dealt with SQL and NoSQL databases. Each with its own advantages and disadvantages. In this article, I’m going to present the main characteristics of NewSQL databases and a simple implementation using Google Spanner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ymvt4oZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4000/1%2AE7OJvlW5O6SD1KRWPpeeFA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ymvt4oZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4000/1%2AE7OJvlW5O6SD1KRWPpeeFA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly, a little recap about the main topics concerning relational e and no-relational databases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt; is known for being sturdy and organized, dealing with a set of properties called ACID (Atomicity, Consistency, Isolation, Durability), which is the main reason it is so popular, bringing a lot of these sturdiness. But this also means that it only scales vertically, so large companies might need a single powerful machine to support all their requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NoSQL&lt;/strong&gt;, on the other hand, is very flexible, without the need for the relational structure of the regular SQL, being able to support non-structured forms of data. This type of database is known for being able to scale horizontally, which means that companies can build database servers close the global clients. But, these non-relational databases don’t support ACID transactions, which means they cannot provide the same consistency as the regular relational.&lt;/p&gt;

&lt;p&gt;NewSQL comes with the idea of bringing the major advantages in each SQL and NoSQL to the same service. These databases use various techniques to provide these functionalities, as such:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Partitioning/Sharding:&lt;/strong&gt; In order to be able to scale horizontally, NewSQL uses a system of dividing itself in various shards/nodes/partitions. Differently form the fragmentation of the NoSQL databases, the shards run partial parts of the whole databases, although the whole works as a distributed application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Main Memory Storage:&lt;/strong&gt; This characteristic provides the ability for the database to run on the memory, instead of a hard disc or flash memory. This provides the NewSQL a lot of leverage in terms of speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Replication and Consistency:&lt;/strong&gt; NewSQL replicates itself through transparent nodes using the Paxos or Raft consensus algorithm. They are families of complex protocols able to assure consensus in a network of unreliable processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Concurrency Control:&lt;/strong&gt; The NewSQL applies Multi-Version Concurrency Control (MVCC), Timestamp methods and T/O to assure the access is granted to the necessary nodes given a certain operation. This together with the consensus algorithm provides the ability of the database to support ACID transactions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This all makes NewSQL databases capable of OLTP, unlike any other databases, considering even SQL supports it partially.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R1NWUeFU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3496/1%2AdIqzTt5dmTC8vj4HwskUzQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R1NWUeFU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3496/1%2AdIqzTt5dmTC8vj4HwskUzQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the best ways to implement a NewSQL database is using Google's DBaaS solution, called Spanner. Which can start a instance ready in minutes.But this all comes with a cost, a large cost. A simple Google Spanner Database with two nodes can cost about 2.00 USD per hour, a lot more than regular services. The main reason this service costs that much is because of the in-memory implementation, which is a lot pricier than a normal hard disc. Also, as the transparent shards work with redundancy, the space needed to store data can sometimes be very large to provide the consistency needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tutorial
&lt;/h2&gt;

&lt;p&gt;We are going to build a Google Spanner Instance running with two nodes. Be careful as this will result in charges of around &lt;strong&gt;2.00 USD per hour&lt;/strong&gt;, so you can delete it after this tutorial. For this, we're going to need Python 2.7 and a Google Cloud account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring your Google Cloud Console
&lt;/h3&gt;

&lt;p&gt;Supposing you already have a Google Account with a credit card. Enter in your Google Cloud console, specifically at the Projects page: &lt;a href="https://console.cloud.google.com/cloud-resource-manager"&gt;https://console.cloud.google.com/cloud-resource-manager&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vVancDCb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AxaAq4BsezdwAuZGa1Pu1lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vVancDCb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AxaAq4BsezdwAuZGa1Pu1lw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the "Create Project" button. We are going to create a new project for this tutorial, but you can also use one of your own.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m4kRrD1n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2152/1%2AY4Tq5y0WhOBh9buUYvLjhA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m4kRrD1n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2152/1%2AY4Tq5y0WhOBh9buUYvLjhA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose a Project Name that suits you, but you are going to have to remember it for the later parts. Click on "Create" and wait for your project to be ready.&lt;/p&gt;

&lt;p&gt;Now, activate de Google Spanner API on your project: &lt;a href="https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com"&gt;https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing the Google Cloud CLI
&lt;/h3&gt;

&lt;p&gt;We are going to deploy the instance using the CLI. You can also do this using the &lt;strong&gt;Google Cloud Console&lt;/strong&gt; on the web and switch to the command line at any moment.&lt;/p&gt;

&lt;p&gt;Open your terminal and check if you have &lt;strong&gt;Python 2.7&lt;/strong&gt; installed and on your path:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -V
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  On macOS:
&lt;/h3&gt;

&lt;p&gt;You can install it using &lt;strong&gt;Homebrew&lt;/strong&gt; with:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew cask install gcloud
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Alternatively you can install using the interactive installer bellow.&lt;/p&gt;

&lt;h3&gt;
  
  
  On Linux and macOs:
&lt;/h3&gt;

&lt;p&gt;Run:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl https://sdk.cloud.google.com | bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reboot the shell:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exec -l $SHELL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  On Windows:
&lt;/h3&gt;

&lt;p&gt;Use the GUI installer for windows, available at: &lt;a href="https://cloud.google.com/sdk/downloads#windows"&gt;https://cloud.google.com/sdk/downloads#windows&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Configure your Google Cloud CLI
&lt;/h3&gt;

&lt;p&gt;Run on the terminal:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud auth application-default login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Chrome will open a window for you to login with your Google Account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploying a Google Spanner Instance:
&lt;/h3&gt;

&lt;p&gt;Run to show all available projects:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud projects list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The project you just created should appear like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3bwDzXIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIT2a9lSdsgiJyKYm-doM_g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3bwDzXIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIT2a9lSdsgiJyKYm-doM_g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you want to select this project as default. If you choose not to, you will have to pass a project parameter at every command.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud config set project PROJECT_ID
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we are going to run our instances with two nodes. I'm choosing &lt;strong&gt;us-east1&lt;/strong&gt; as the region because it is the cheapest by the time I'm writing this tutorial.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner instances create test-instance --config=regional-us-east1 --description="My Instance" --nodes=2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To see the instance you have created, run:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner instances list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which should return as such:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xRYbZEyx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AmXrXzu97nzWA9u8-qEA4dg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xRYbZEyx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AmXrXzu97nzWA9u8-qEA4dg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The instance is identified by &lt;strong&gt;test-instance&lt;/strong&gt;, and the display name is &lt;strong&gt;My Instance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now we are going to build a database and a table. As I've mentioned earlier, we can straight up use SQL language to work with this Database, so we're going to build some simples queries.&lt;/p&gt;

&lt;p&gt;To create a database called &lt;strong&gt;classroom&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner databases create classroom --instance=test-instance
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We are going to build a simples student table. For this, we are going to run a DDL command with the content of the query being:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Student (
  StudentId   INT64 NOT NULL,
  FirstName  STRING(1024),
  LastName   STRING(1024)
) PRIMARY KEY(StudentId);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To run this on the Google CLI, we are going to run a command on the directory of this file with:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner databases ddl update classroom \
--instance=test-instance --ddl='CREATE TABLE Student (
  StudentId   INT64 NOT NULL,
  FirstName  STRING(1024),
  LastName   STRING(1024)
) PRIMARY KEY(StudentId);'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can use &lt;strong&gt;gcloud spanner databases ddl update DATABASE&lt;/strong&gt; every time you want to alter the database schema.&lt;/p&gt;

&lt;p&gt;Now, to add run a DML we are going to use &lt;strong&gt;execute-sql&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner databases execute-sql classroom --instance=test-instance \
    --sql='INSERT INTO Student (StudentId, FirstName, LastName)
VALUES (1,"Arthur", "Olga");'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To add some more rows:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner databases execute-sql classroom --instance=test-instance \
    --sql='INSERT INTO Student (StudentId, FirstName, LastName)
VALUES (2,"Vitor", "Liu");'

gcloud spanner databases execute-sql classroom --instance=test-instance \
    --sql='INSERT INTO Student (StudentId, FirstName, LastName)
VALUES (3,"Iago", "Mendes");'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we can see the table with:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner databases execute-sql classroom --instance=test-instance \
    --sql='SELECT * FROM Student'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which should return&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--70lrWk9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADA_ktgEHslyCbDkR1F10WQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--70lrWk9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADA_ktgEHslyCbDkR1F10WQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to delete this expensive test instance:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud spanner instances delete test-instance
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you've seen, running commands on the CLI can be useful, but to build a real service, like a REST application, you need to be able to run these commands in any sort of language. So we're going to run a query using &lt;strong&gt;Python 3.7&lt;/strong&gt;,** **but you can choose from a variety of different languages, such as C#, NodeJS or Ruby.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running with Python3
&lt;/h3&gt;

&lt;p&gt;Now we are going to run a query using a program. First You should have &lt;strong&gt;Python3&lt;/strong&gt; and &lt;strong&gt;pip&lt;/strong&gt; installed.&lt;/p&gt;

&lt;p&gt;Run:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install google-cloud-spanner
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now create a file name **test-spanner.py **with the inicialization of the Client and Instance and the run of a simple Select query:&lt;/p&gt;

&lt;p&gt;Running this with &lt;strong&gt;python3 test-spanner.py&lt;/strong&gt; should print on the terminal something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gg1UVniW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AOuhCnG-X4HEO0yNQkPDtog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gg1UVniW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AOuhCnG-X4HEO0yNQkPDtog.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This concludes this Tutorial, entirely based on the Google Spanner Documentation. Which is &lt;strong&gt;awesome!&lt;/strong&gt; If you want to continue using this database, you should really check it out. Thanks for checking this out!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Google Spanner Documentation&lt;/strong&gt;: &lt;a href="https://cloud.google.com/spanner/docs/"&gt;https://cloud.google.com/spanner/docs/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
