Sneaking into Azure Cosmos DB 🌎
Tags: azure, msftstudentambassador, mlsa, microsoft
Published: 2021-04-15 13:57:34

## Azure Cosmos DB

Microsoft's Azure Cosmos DB service is a massively distributed, multi-model database. It allows for elastic and autonomous scaling of throughput. It also has storage in all of Azure's global territories. Throughput, latency, affordability, and performance assurances are all covered by Cosmos DB's service level agreements.

For modern app creation, Azure Cosmos DB is a completely maintained NoSQL database. The speed, at any rate, is guaranteed by single-digit millisecond response times and automatic and immediate scalability.🤓

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmu7uqkhbv97frxxhkmg.gif)

## Entity Hierarchy in Cosmos DB

Cosmos DB accounts, databases, containers, and items all have separate entities. Stored protocols, user specified tasks, causes, and disputes are some of the other entities found in Cosmos DB.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlztq5qezl8n51p25nlq.jpg)

### Databases

Under your username, you can build one or more Azure Cosmos databases. A namespace is similar to a dictionary. A database is a management unit for a group of Azure Cosmos containers.

One or more databases may be added to a Cosmos DB account.


| Azure Cosmos DB | Database | Container | Item |
| ------------- |:-------------:| -----:| -----:|
| SQL API     | Database| Container |	Document |
| Cassandra API     | Database | Table | Row |
| Azure Cosmos DB API for MongoDB | Database |Collection | Document|
| Gremlin API | Database     |  Graph | Node or Edge|
| Table API | N/A    | Table|Item |

### Containers

A container can be horizontally partitioned using a partition key, and the data can then be distributed through Azure regions. A container will scale flexibly depending on whether private or shared throughput settings are used.

Multiple containers may exist in a database. For both provisioned throughput and storage, the Azure Cosmos DB container is a scalability unit.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak0o8fekaj6buyfmk3bw.png)

One of the most distinguishing features of containers is the lack of schema handling. You can build objects of different schemas in a container.😮 By default, all objects you add to a container are instantly indexed, so there's no need to handle indexes or schemas.

You may also set a timer on an object or a container to delete them after a certain amount of time has passed. When an item expires, Azure Cosmos DB deletes it immediately. It also ensures that a check on the container does not return expired objects within a predetermined time frame.

For your Azure Cosmos container, you can register stored procedures, causes, user-defined functions (UDFs), and merge procedures.

On your Azure Cosmos container, you should define a special key restriction. You may guarantee the uniqueness of one or more values per logical partition key by designing a special key policy. No new or modified objects with values that repeat the values defined by the specific key restriction can be generated if you create a container with a unique key policy.

The system properties of each container are listed below.

| Property | Purpose |
| ------------- |:-------------:| 
| _rid | The unique resource identifier for container| 
| _etag | Entity tag for optimistic concurrency| 
| _ts | Last updated timestamp of container| 
| _self | Addressable URI of container| 
| id | User defined unique name for the container| 
| indexingPolicy | Provides the ability to change the index path, index type, and index mode| 
| TimeToLive| Provides the ability to delete items automatically from a container after a set time period| 
| changeFeedPolicy| Used to read changes made to items in a container | 
| uniqueKeyPolicy| Used to ensure the uniqueness of one or more values in a logical partition| 

### Items

A document in a collection, a row in a table, or a node or edge in a graph may all be represented by an Azure Cosmos item, depending on the API you use.

To connect items to the container, you can use any of the APIs. Any item in the container has properties that are either created by the system or specified by the user. User defined properties are the properties required for your application.

| Property | Purpose |
| ------------- |:-------------:| 
| _rid | Unique identifier of the item| 
| _etag | Entity tag used for optimistic concurrency control| 
| _ts | Timestamp of the last update of the item| 
| _self | Addressable URI of the item | 
| id | User-defined unique name in a logical partition.| 
| Arbitrary user-defined properties | User-defined properties represented in API-native representation (including JSON, BSON, and CQL) |

## Any Key Benefits?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smqxrvg82e9yj0onxzt3.gif)

#### Speed Guarantee at any scale

- SLA-backed real-time connectivity with fast read and write latencies around the world, as well as throughput and accuracy.
- With a single click, multi-region writes and data distribution can be sent to any Azure region.
- Size storage and throughput through Azure regions in a flexible and independent manner.

#### Simple application development

- Choose from multiple database APIs including the native Core (SQL) API, API for MongoDB, Cassandra API, Gremlin API, and Table API.
- Azure Functions, IoT Hub, AKS (Azure Kubernetes Service), App Service, and other Azure services used in current (cloud-native) app creation are all deeply embedded.
- The schema-less service in Azure Cosmos DB instantly indexes all of your files, regardless of the data model, so you can get blazing fast queries.

#### Mission-critical

- Azure Cosmos DB comes with a robust set of SLAs, including industry-leading global availability.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uskyfo2rw9esbljet5g4.jpg)

- Role-based access management in Azure keeps the data secure and gives you more control.
- With automated data replication, you can easily disperse data to any Azure area. When using multi-region writes or RPO 0 there would be no downtime. Consistency is essential.

#### Absolutely self-managed and cost-effective

- Database infrastructure that is fully operated. Maintenance, patching, and updates are all automated, saving developers time and resources.
- Developers can get underway quickly without having to schedule or maintain capacity with these cost-effective solutions for irregular or intermittent workloads of any size or scale.
- The serverless model provides an automated and responsive service for spiky workloads to handle traffic bursts on request.