Azure vs GCP part 7: NoSQL Database (Azure)
Tags: azure, gcp, cloud
Published: 2018-03-06 06:05:38

---
title: "Azure vs GCP part 7: NoSQL Database (Azure)"
published: true
description: In this series, I compare Azure and GCP as C# developer
tags: azure,gcp,cloud
cover_image: https://thepracticaldev.s3.amazonaws.com/i/m57mmv4hgz9xrskaital.PNG
---

In [part 5](https://dev.to/kenakamu/azure-vs-gcp-part-5-storage-options-4k8p) and [part 6](https://dev.to/kenakamu/azure-vs-gcp-part-6-storage-gcp-4f50/), I compare storage between Azure and GCP, where I stored blob object. I compare NoSQL database services in this and next article.

# Azure Databases
Azure offers many database services, such as SQL, NoSQL, Cache, etc. See [Azure Databases list](https://azure.microsoft.com/en-us/services/#databases) for complete lists.

### SQL Databases
You can use Azure SQL, which is very similar to Microsoft SQL Server but cloud version, MySQL and PostgreSQL. If you are into RDBMS, check out these services.

### NoSQL
**Azure Cosmos DB** is Azure version of NoSQL service. This is the one I look into this article. Oh you wonder what happens to Document DB? That's Cosmos DB now :)

### Other
Azure also offers Redis Cache, Table Storage, or Warehouse type of Database.

In this article, I look into Azure Cosmos DB.

# Azure Cosmos DB
Refer to [this document](https://docs.microsoft.com/en-us/azure/cosmos-db/introduction) for detail about Cosmos DB, but simply put, it's:

- globally scale
- multiple data model/api
- always-on
- high-performance
- low-cost solution, etc.

Sounds good. 

### Multiple data models 
There is one thing a bit confusing about Cosmos DB, which is Multiple data models support. As a developer, I see "Cosmos DB" as a brand, and there are multiple products in it, which shares same capability from SLA, cost, performance, scalability. 

At the moment, it supports following models and APIs.

- SQL API: This is same as document db.
- MongoDB API: As name infers, it's MongoDB compatible.
- Cassandra API: As name infers, it's Cassandra compatible.
- Graph (Gremlin) API: It's Open Graph API base.
- Table API: It's compatible with Azure Table Storage.

I use SQL (document DB) in this article.

### SDK
Each API offers SDK for multiple languages and REST endpoint. Therefore, if SDK is not available for your preferred language, you can still call REST endpoint.

For SQL, .NET, Java, Node.js, Python and Xamarin SDK available. Other API may support different set of languages.

# Get code
This time, rather I write code from scratch, I take sample code.

1\. Go to [Azure Portal](https://portal.azure.com) and click "Create a resource". Then search "Cosmos DB"

![portal](https://thepracticaldev.s3.amazonaws.com/i/qsmkp5s4cyx7178b6m7f.PNG)

2\. Select Azure Cosmos DB and click "Create".

3\. Give globally unique name and select "SQL" API. Then create. You can select "Enable geo-redundancy" if you want. Of course you can "pin" the tile to dashboard as well.

![portal](https://thepracticaldev.s3.amazonaws.com/i/l5apccjuzek0bae25rzq.PNG)

4\. Once creation completed, select the created Cosmos DB.

![portal](https://thepracticaldev.s3.amazonaws.com/i/p4xf3lxb6ebwahi33pqo.PNG)

5\. Select "Quick start" from menu, and click ".NET Core" tab. Then click "create 'items' collection.

![portal](https://thepracticaldev.s3.amazonaws.com/i/bah8xsh2gzv35woxcx2n.PNG)

6\. Click "Download" to download sample code.

![portal](https://thepracticaldev.s3.amazonaws.com/i/vt4oio4xcqrzwsn6mvge.PNG)

7\. Then go to "Keys" and note URI and PRIMARY KEY.

![portal](https://thepracticaldev.s3.amazonaws.com/i/ovzg9qk13vbrhj92vqt1.PNG)

8\. Once download completed, unzip the file and open quickstartcore.sln solution with Visual Studio.

9\. Open DocumentDBRepository.cs and replace endpoint and key, then press F5 to test the application. You should store the key in separate, safe place though.

![app](https://thepracticaldev.s3.amazonaws.com/i/cty8tnjp10i8p6zyu45k.PNG)

## How it works
All the code where interact with Cosmos DB is stored in DocumentDBRepository.cs. It supports LINQ query, which I love.

##### Connect and create database and collection
```csharp
public static void Initialize()
{
    client = new DocumentClient(new Uri(Endpoint), Key);
    CreateDatabaseIfNotExistsAsync().Wait();
    CreateCollectionIfNotExistsAsync().Wait();
}
```
##### Get item by using LINQ
```csharp
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
{
    IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
        UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
        new FeedOptions { MaxItemCount = -1 })
        .Where(predicate)
        .AsDocumentQuery();

    List<T> results = new List<T>();
    while (query.HasMoreResults)
    {
        results.AddRange(await query.ExecuteNextAsync<T>());
    }

    return results;
}
```
# Deploy to Azure and GCP
Try deploy both platform, and it simply works like Azure Storage.

![gcp](https://thepracticaldev.s3.amazonaws.com/i/3cgp2suh4xa060brz5rw.PNG)

# Explorer Tools
As a developer, I may need to check the data from outside of the application. There are several explorer tools are available. All the tools can be accessed via Azure Portal.

## Data Explorer
It shows you database and collections, as well as each items. You can create, update delete items, too.

![portal](https://thepracticaldev.s3.amazonaws.com/i/64ua83a4vlzpc5cwy9t5.PNG)

You can also run SQL query. This is very hand for me.

![portal](https://thepracticaldev.s3.amazonaws.com/i/g427nieeydffrtprfx4i.PNG)

## Other Explorers
I can also see the data from "collection" point of view. There are several tools under COLLECTIONS menu.

![portal](https://thepracticaldev.s3.amazonaws.com/i/w3kmunllq8ssjqd67hy0.PNG)

# Summary
I feel the development experience is similar to Azure Storage. Portal GUI and tools are easy to understand, enough documentation, SDK and samples. It also just work when I deploy to GCP without changing any code, which is good.

I will try NoSQL solution on GCP in the next article.

# References
[Azure Cosmos DB: Build planet scale mobile apps in minutes](https://azure.microsoft.com/en-us/resources/videos/build-2017-azure-cosmos-db-build-planet-scale-mobile-apps-in-minutes/)
[Azure Cosmos DB, design patterns and case studies for globally distributed applications](https://www.youtube.com/watch?v=F0wEDdxQER0)

Ken