Manage Azure Cosmos DB

This tutorial guides you through the essential management operations for your Azure Cosmos DB resources. You'll learn how to create, configure, and monitor your databases and containers.

Prerequisites

  • An Azure subscription. If you don't have one, create a free account.
  • An Azure Cosmos DB account. If you don't have one, you can create it using the Azure portal or Azure CLI.

Creating a Cosmos DB Account

Follow these steps to create a new Azure Cosmos DB account in the Azure portal:

Step 1: Navigate to Cosmos DB

Sign in to the Azure portal. In the search bar, type "Azure Cosmos DB" and select it from the services list.

Step 2: Create Account

Click the "+ Create" or "+ Add" button to start the creation process. Select "Azure Cosmos DB" from the database options.

Step 3: Configure Settings

Fill in the required details such as Subscription, Resource group, Account name, API (e.g., Core (SQL)), Location, and Capacity mode (Provisioned throughput or Serverless).

For this tutorial, we'll use the Core (SQL) API and Provisioned throughput for predictable performance.

Step 4: Review and Create

Review your configuration and click "Create". Deployment may take a few minutes.

Creating a Database and Container

Once your Cosmos DB account is deployed, you can create databases and containers within it.

Step 1: Access Your Cosmos DB Account

Navigate to your newly created Azure Cosmos DB account in the Azure portal.

Step 2: Add Database

In the left-hand menu, under "Data Explorer," click "New Container." This action will prompt you to create a new database if one doesn't exist.

Enter a Database ID (e.g., ToDoList).

Step 3: Add Container

Configure the container details:

  • Container ID: Enter a name for your container (e.g., Items).
  • Partition Key: Define a partition key (e.g., /categoryId). This is crucial for scaling and performance.
  • Throughput: Set the request units per second (RU/s). Start with a low value like 400 RU/s for testing.

Click "OK" to create the container.

Monitoring Your Cosmos DB Account

Azure Cosmos DB provides robust monitoring tools to track performance and usage.

Step 1: Navigate to Metrics

Within your Cosmos DB account in the Azure portal, select "Metrics" under the "Monitoring" section.

Step 2: Explore Key Metrics

Commonly monitored metrics include:

  • Total Requests: The number of requests processed by your account.
  • Data Storage: The amount of data stored.
  • Throughput (RU/s): The actual consumption of Request Units.
  • Latency: The time taken to process requests.

Step 3: Set up Alerts

You can configure alerts based on these metrics to notify you of performance degradation or unusual activity. Go to "Alerts" under "Monitoring" to set up new alert rules.

Managing Keys and Endpoints

Accessing your Cosmos DB account requires keys and endpoints.

Step 1: Access Keys Section

In your Cosmos DB account menu, select "Keys" under the "Settings" section.

Step 2: Retrieve Keys and Endpoint

Here you'll find:

  • URI: The endpoint for your Cosmos DB account.
  • Primary Key and Secondary Key: Use these for authentication when connecting from your applications.

Important: Treat your primary keys as secrets. Do not embed them directly in client-side code.

Next Steps

Congratulations! You have successfully learned the basics of managing an Azure Cosmos DB account. Continue exploring by: