DEV Community

Cover image for Azure vs GCP part 7: NoSQL Database (Azure)
Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Azure vs GCP part 7: NoSQL Database (Azure)

In part 5 and part 6, 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 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 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 and click "Create a resource". Then search "Cosmos DB"

portal

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

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

portal

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

portal

6. Click "Download" to download sample code.

portal

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

portal

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

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
public static void Initialize()
{
    client = new DocumentClient(new Uri(Endpoint), Key);
    CreateDatabaseIfNotExistsAsync().Wait();
    CreateCollectionIfNotExistsAsync().Wait();
}
Enter fullscreen mode Exit fullscreen mode
Get item by using LINQ
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;
}
Enter fullscreen mode Exit fullscreen mode

Deploy to Azure and GCP

Try deploy both platform, and it simply works like Azure Storage.

gcp

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

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

portal

Other Explorers

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

portal

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
Azure Cosmos DB, design patterns and case studies for globally distributed applications

Ken

Top comments (2)

Collapse
 
kayis profile image
K • Edited

Very interesting series.

I'm just getting into the whole cloud thing and had the impression Google and Microsoft offerings are basically the same. Just AWS differs, because they have new stuff first. (judging from forum comments I read)

Collapse
 
kenakamu profile image
Kenichiro Nakamura

Great input :)
I personally didn't play with AWS yet so I cannot comment on that. I declare myself as Azure expert and now learning GCP to compare. Hope you can put more AWS insights!