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"
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.
4. Once creation completed, select the created Cosmos DB.
5. Select "Quick start" from menu, and click ".NET Core" tab. Then click "create 'items' collection.
6. Click "Download" to download sample code.
7. Then go to "Keys" and note URI and PRIMARY KEY.
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.
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();
}
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;
}
Deploy to Azure and GCP
Try deploy both platform, and it simply works like Azure Storage.
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.
You can also run SQL query. This is very hand for me.
Other Explorers
I can also see the data from "collection" point of view. There are several tools under COLLECTIONS menu.
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)
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)
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!