Azure Cosmos DB

Azure Cosmos DB Documentation

The globally distributed, multi-model database service for modern applications.

What is Azure Cosmos DB?

Azure Cosmos DB is a globally distributed, multi-model database service that enables you to rapidly build and scale modern applications. It supports multiple data models (document, key-value, graph, and column-family) and is designed for high availability, low latency, and elastic scalability.

With Cosmos DB, you can leverage:

  • Global Distribution: Distribute your data across any number of Azure regions worldwide.
  • Multi-Model Support: Use the API that best suits your application, including SQL (Core) API, MongoDB API, Cassandra API, Gremlin API, and Table API.
  • Elastic Scalability: Scale throughput and storage elastically and independently.
  • Guaranteed Performance: Enjoy predictable throughput, low latency, and high availability with comprehensive SLAs.
  • High Availability: Achieve 99.999% availability for multi-region write scenarios.

Core Concepts

Understanding these core concepts is crucial for working effectively with Azure Cosmos DB:

  • Account: A Cosmos DB account is the top-level resource.
  • Database: A logical namespace for resources. A database can contain multiple containers.
  • Container: The fundamental unit of scalability and throughput. Data is stored in containers.
  • Item: The basic unit of data in a container. For document databases, an item is a document.
  • Partition Key: A property within an item that determines which partition the item is stored in.
  • Throughput: Measured in Request Units (RUs), it represents the amount of processing power required to perform database operations.
  • Indexing: Cosmos DB automatically indexes all data written to a container, with no schema or index management required.

Getting Started with Cosmos DB

Follow these steps to begin using Azure Cosmos DB:

1. Create a Cosmos DB Account

You can create an account using the Azure portal, Azure CLI, PowerShell, or Azure Resource Manager (ARM) templates.

Example using Azure CLI:


az cosmosdb create \
    --name <your-cosmos-account-name> \
    --resource-group <your-resource-group-name> \
    --kind GlobalDocumentDB
                

2. Create a Database

Databases act as logical containers for your containers.

Example using Azure CLI:


az cosmosdb sql database create \
    --account-name <your-cosmos-account-name> \
    --resource-group <your-resource-group-name> \
    --name <your-database-name>
                

3. Create a Container

Containers store your data and are the unit of throughput provisioning.

Example using Azure CLI for SQL API:


az cosmosdb sql container create \
    --account-name <your-cosmos-account-name> \
    --resource-group <your-resource-group-name> \
    --database-name <your-database-name> \
    --name <your-container-name> \
    --partition-key-path "/categoryId" \
    --throughput 400
                

Supported APIs

Azure Cosmos DB provides multiple API options to cater to different development needs:

  • SQL (Core) API: A rich, document database API that supports JSON documents and JavaScript procedural logic.
  • MongoDB API: Compatible with the MongoDB wire protocol, allowing you to use existing MongoDB drivers and tools.
  • Cassandra API: Compatible with Apache Cassandra, enabling you to leverage existing Cassandra applications.
  • Gremlin API: A graph database API for building and querying graph data structures.
  • Table API: Compatible with Azure Table Storage, allowing migration of existing Table storage applications.

SDKs

Azure Cosmos DB offers SDKs for popular programming languages to interact with your data programmatically:

  • .NET
  • Java
  • Node.js
  • Python
  • Go
  • Azure Cosmos DB for MongoDB (various language drivers)
  • Azure Cosmos DB for Cassandra (Java, .NET drivers)
  • Azure Cosmos DB for Gremlin (Java, Python, .NET drivers)

Refer to the SDK documentation for detailed usage and examples.

Pricing

Azure Cosmos DB pricing is based on the following:

  • Provisioned throughput (Request Units per second - RU/s).
  • Consumed storage (GB/month).
  • Optional features like continuous backup, multi-region writes, etc.

You can choose between provisioned throughput or serverless capacity. For detailed information, visit the Azure Cosmos DB pricing page.