Today’s applications are required to be highly available and responsive. In order to guarantee low latency and high availability, the application should be deployed in the data centers which is nearest to the consumer of the application.
Azure Cosmos DB is globally distributed, multi model database service offered by Azure. With a single click of button, you can elastically scale the throughput and storage across any numbers of Azure regions.
Cosmos DB provides comprehensive service level agreements (SLAs) for throughput, latency, availability, and consistency guarantees, something no other database service offers.
Let’s have a look at some basic terminology related to Cosmos DB. We will also have a look at how can we create Azure resources.
Basic Concepts and Terminology
Also, the Cosmos DB service instance can be created in almost all of the Azure regions, allowing you to reduce the latency to the minimum.
Turnkey Global Distribution
Azure Cosmos DB allows you to configure global distribution, meaning the data is replicated in many azure regions, bringing it closer to the users of applications. This replication process is completely transparent to you.
Cosmos DB allows you to add or remove any of the Azure regions to your Cosmos account at any time, with a click of a button.
Azure Cosmos DB allows you to configure the data replication across regions. You can also invoke the regional failover either progromatically or using portal, in case of regional disaster, making sure your application is still available.
Elastic scalability of throughput and storage, worldwide
Cosmos DB is designed with horizontal partitioning and multi-master replication. Cosmos DB offers unprecedented elastic scalability for your writes and reads, all around the globe. With single API call, you can elastically scale up from hundreds to thousands of requests per second. And yet you pay for only throughput and storage that you need.
No Schema or Index Management
Many of us might already have worked on the relational databases like SQL Server or oracle. These databases have schema management. You have to define schema of the table. If you have to change the schema after it is deployed to production, it may be very painful. Because of this, some feature updates become very difficult.
With Cosmos DB, you do not need to deal with schema or index management. The database engine is fully schema-agnostic.
Wide variety of API choices
SQL API is the core API set supported by Cosmos DB. In addition to the SQL API, Cosmos DB also implements APIs for the Cassandra, MongoDB, Gremlin and Azure Table Storage. So if you have an application which is developed using MongoDB, then you can easily migrate to Cosmos DB with almost no code changes.
This wide variety of API choices can be very helpful for you if you want your application code to be cloud vendor platform agnostic.
Multiple Consistency Choices
Cosmos DB’s multi-master replication protocol is carefully designed to offer five well-defined consistency choices – strong, bounded staleness, session, consistent prefix, and eventual.
These consistency level can help you to have reasonable tradeoff between your applications response time and consistency of data. You can decide the consistency level based on need of your application.
Cosmos DB Account Using Portal
If you do not have Azure subscription, I would suggest to create a free Azure account.
Login to Azure and click on create a resource in left side menu navigation. You can either search for Cosmos DB or you can search under Databases as shown in below snapshot. Click on Azure Cosmos DB.
The create Cosmos DB account panel looks as shown in below snapshot. In first tab, you need to provide below details:
- Subscription, under which the resource will be created
- Resource Group, under which resource will be placed
- Account Name, unique account name for the Cosmos DB account
- API, the API you want to use. The default is SQL API. Other options are MongoDB, Cassandra, Gremlin.
- Apache Spark, let this be default value (i.e. None)
- Location, the azure region where the account will be physically created
- Geo-Redundancy, if you want to replicate the data across azure regions.
- Multi-Regions write, enable this if you want your application to write data to nearest Cosmos DB account.
Then you have Networking tab, where you can specify which networks can access the Cosmos DB account. For the scope of this article, let’s ignore this step, keep it to default and click on Next.
Then you can go on Tags tab and there you can specify the name-value pairs. These can be useful to logically organize the resource. e.g. you can set Environment to Dev.
Then you can click on Create and the Cosmos DB account will be created in few of seconds. That’s it, you just have created a new Cosmos DB account.
You can also click on download a template for automation link from above dialogue to download the ARM template for automating the creation of Cosmos DB account.
Create Database in Cosmos DB
Under Cosmos DB Account, you can create several databases. For creating database, navigate to the newly created Cosmos DB account. Go to Data Explorer and click on New Container option as shown in below snapshot.
On Add container dialog, you need to provide below details:
- Create New to create new database ID
- Database ID, the identifier for your database, logical group of containers
- Throughput, let this be 400 for now. The unit is RU (Request Units)
- Container ID, the name of your container
- Partition Key, the key which will be used by Cosmos DB to create the partitions.
Once you add values as shown below, and click on OK, the container will be created instantaneously.
Add new container under Cosmos DB
Once container is created, you can try adding the records / items in the container. For that you can click on “New Item” button and add a JSON as shown below. Please note that there is no schema so you can enter any JSON you want.
Once you are done adding JSON, you can click on save button to save the record. After saving the record, the data will be refreshed and it will show you some additional properties in the JSON record e.g. id, etag, self, etc. Let’s have a look at them in some other blog article.
I am sure this article helps you to get started with Azure Cosmos DB Service. Let me know your thoughts.