By: Rajendra Gupta | Updated: 2022-01-26 | Comments (1) | Related: > Azure Cosmos DB
Problem
Microsoft aims to help you access and update your data more efficiently with a PaaS (Platform as a Service) solution such as Azure Cosmos DB, a multi-model database service. You can store all of your data in a single global database that automatically scales and replicates itself wherever your users are on any platform.
In this article we look at how to implement Azure Cosmos DB for free to learn and explore the document store technology.
Solution
This tutorial is an overview of Microsoft Azure Cosmos DB and covers how to implement it using the Azure Cosmos DB for Free offering.
Azure Cosmos DB Overview
Today's customer-centric applications need to run on a scalable, low-latency infrastructure designed to distribute the application's workload across multiple datacenter locations. To achieve high availability, these applications need to be operational 99.999% of the time without considering a spike in traffic and infrastructure issues.
One way to make your application always online is to make sure that you deploy instances of it in datacenters that are close by, so that in case anything happens you'll be able to respond quickly and efficiently. Applications require a response in real-time to significant changes in usage at peak hours, store considerable amounts of data, with an emphasis on both their latency and speed, so you'll want to be able to access the data as fast as possible.
Azure Cosmos DB is one of the first databases of its kind – a globally distributed real-time NoSQL database designed to interoperate with any application and deliver seamless performance at every scale. It is designed for automatic scaling, eliminates pre-plan capacity, or manage resources in advance. Azure Cosmos DB automatically adapts to dynamic workloads, allowing applications to cost-effectively serve peak demand without over-provisioning resources and achieve low latency at a massive scale with region stretching for more consistency in latency anywhere in the world.
Image Reference: Source
An Azure Cosmos DB databaes has the following use cases:
- Azure Cosmos DB supports millisecond read\write data access in a globally distributed database.
- It can replicate data in data centers worldwide without complex configuration.
- The Cosmos DB can independently scale resources for supporting millions of transactions per second.
- Azure Cosmos DB supports multiple data models, including documents, key-value pair, graph, and column-family databases.
- You can use SDKs for the .NET, Java, Python, Node.js, JavaScript, native Core (SQL API), API for MongoDB, Cassandra API, Gremlin API and Table API.
- The Cosmos DB automatically indexes data on all document fields irrespective of the model.
- It provides 99.999% availability (SLA) and enterprise-level security for every application. You can leverage zero downtime with multi-region writes or RPO 0 by the Strong consistency. Cosmos provides automatic failover capabilities in case of a regional disaster.
- It enables enterprise-grade encryption at rest with self-managed keys.
- Cosmos DB supports multi-master architecture. It means that it can scale writes elastically across any Azure region.
How to deploy Azure Cosmos DB
You will need an Azure subscription to deploy Azure Cosmos DB. If you do not have a subscription, you can deploy in two ways:
- You can use - Try Azure Cosmos DB for Free for deploying Cosmos DB without any cost and commitment.
- Alternatively, use the Azure Cosmos DB free tier that provides 1000 RU/s and 25 GB of storage free.
Select API and Data Model
The first step is to select an API and data model to create a database. The available options are below:
- SQL
- MongoDB
- Table
- Graph
- Cassandra
Let's choose SQP API for this tip and click on Create. You need to login first to create a Cosmos DB account. Click on the Microsoft icon and signup\login with your Microsoft account.
Once logged in, you get a message – Welcome! Your database is ready. The free Cosmos DB account expires in 719h:59min as shown below.
Start Working with Cosmos DB
To start working with the Cosmos DB, click on the button – Open in the Azure portal. This opens the Quickstart page that gives the following steps for working with Cosmos DB.
Step1:1: Choose a platform – Available options .NET, Xamarin, Java, Node.JS, and Python
Step2: Download and run your .Net app
To view the Cosmos DB general information, click on the overview page. The overview pages the following details:
- Status: Online
- Resource group p
- Subscription
- Read Location
- Write location
- URI: The URL you can use to connect with the database account. As shown here, the URI is https://eb5429c7-0ee0-4-231-b9ee.documents.azure.com:443/ for lab cosmos Database.
- Capacity mode: Provisioned throughput
Azure Cosmos DB has two different capacity modes:
- Provisioned throughput: Is suitable for workloads with sustained traffic
that requires predictable performance. Provisioned has the following
characteristics:
- Geo-distribution n
- Unlimited (maximum) storage per container
- Less than 10 ms latency for point-reads and writes
- The Billing is on a per-hour basis for the RU/s provisioned
- Serverless: This is for workloads with intermittent or
unpredictable traffic. You do not need to provision any capacity for your
database operations. It has certain limitations:
- No Geo-distribution n
- Maximum storage per container is 50 GB
- Less than 10 ms latency for point-reads and < 30 ms for writes covered by SLO
- Azure bills serverless architecture Cosmos DB per hour for RUs consumed
You can refer to https://youtu.be/CgYQo6uHyt0 to compare Provisioned throughput and Serverless capacity mode.
- Replicate data globally: Click on the Replicate data globally page to configure multi-region writes. By default, the multi-region write is disabled.
Create New Container
Let's create a new container and database in the Azure Cosmos DB account. Click on Quick start and Create Items container.
The Azure Cosmos DB account is ready, and we can create a database under that account and add data containers. This step creates an 'Items' container with 400 Request Units per second (RU/s) throughput capacity for up to 400 reads/sec as shown below.
Once the Items container is created, click on Data Explorer to view your Cosmos DB.
Adding Records
Click on the New Item to add new records to the Cosmos DB.
Let's add a sample record in JSON document form. Click on the Save button to insert the record.
{ "id": "1", "category": "personal", "name": "groceries", "description": "Pickupapplesandstrawberries.", "isComplete": false }
Once the record is inserted successfully, you can see additional metadata columns such as _rid, _self,_etag, _attachments, and _ts.
Similarly, add another document with the following JSON. The documents can have any structure because Cosmos DB doesn't impose any schema on your data.
{ "id": "2", "category": "Official", "name": "admin", "description": "Pickupofficechairs.", "isComplete": true };
Query Data
To query data from Cosmos DB, we can click on the New SQL Query. It shows the default query – SELECT * FROM c. If we execute this query, it retrieves all documents.
Click on Execute Query and view all items. As shown below, it retrieved both JSON documents inserted earlier.
We can modify the query to filter records. For example, we want to retrieve a document with ID value 2.
SELECT * FROM c WHERE c.id="2"
The query filters the records based on the WHERE predicate.
Similarly, the following query will return the document whose category value is personal.
SSELECT * FROM c WHERE c.category="personal"
That's it. Now you have a understanding of how quickly you can setup Cosmos DB to begin working with it.
Next Steps
- We can use Data Explorer for creating stored procedures, UDFs, and triggers for server-side business logic. Try different things to continue your learning.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2022-01-26