English translation
Initialize the Cosmos DB client
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous article, we explored the features and advantages of Azure SQL Database, gaining insight into its strengths as a relational database solution. In this article, we shift our focus to Azure’s non-relational (NoSQL) database offerings—specifically Azure Cosmos DB—and also introduce several common NoSQL services. Through concrete use cases, we’ll better understand the application scenarios and practical usage of these services.
1. What Is Cosmos DB?
Azure Cosmos DB is a globally distributed, multi-model database service provided by Microsoft. It supports multiple APIs—including the SQL API, MongoDB API, Cassandra API, Gremlin API, and Table API—offering exceptional flexibility to meet diverse application requirements.
1.1 Core Features of Cosmos DB
- Global Distribution: Cosmos DB enables multi-region replication, allowing seamless data replication across geographically dispersed regions to minimize latency.
- Elastic Scalability: Supports horizontal scaling—resources can be dynamically increased on demand to boost performance without downtime.
- Multi-Model Support: Stores data in various formats—including documents, key-value pairs, graphs, and wide-column stores—to accommodate heterogeneous workloads.
- High Availability & Consistency: Guarantees 99.999% availability and offers configurable consistency models (e.g., strong, bounded staleness, session, eventual).
2. Use Cases
2.1 E-Commerce Application
Suppose you’re building an e-commerce platform that handles orders, product catalogs, and user profiles. You might choose Cosmos DB to store this data using its document model. Each order can be represented as a JSON document—for example:
{
"orderId": "12345",
"userId": "u6789",
"items": [
{
"productId": "p001",
"quantity": 2
},
{
"productId": "p002",
"quantity": 1
}
],
"totalPrice": 29.99,
"status": "shipped"
}
This schema-free structure enables flexible storage and querying of heterogeneous data.
2.2 Social Networking Application
When developing a social media platform, you may need to store posts, comments, and friendship relationships—all of which can be efficiently modeled and queried in Cosmos DB. Using the Gremlin API, for instance, you can rapidly traverse social graphs. The following Gremlin query retrieves all users directly connected to u6789 via a knows relationship:
g.V().hasLabel('user').has('userId', 'u6789').out('knows')
This enables fast, low-latency retrieval of friend lists and other graph-based insights.
3. Getting Started with Cosmos DB
3.1 Creating a Cosmos DB Instance
Creating a Cosmos DB instance in the Azure portal is straightforward. Follow these steps:
- Sign in to the Azure Portal.
- Select Create a resource from the left-hand menu.
- Search for Azure Cosmos DB, then select it.
- Click Create.
- Choose your preferred API (e.g., Core (SQL)).
- Fill in required configuration details (e.g., resource group, account name, region).
- Click Review + create, then confirm deployment.
3.2 Connecting to Cosmos DB Using the Azure SDK
You can interact with Cosmos DB programmatically using Azure SDKs—for example, in Python or Node.js—to perform basic CRUD (Create, Read, Update, Delete) operations. Below is a Python example demonstrating how to connect to Cosmos DB and insert a new order document:
from azure.cosmos import exceptions, CosmosClient, PartitionKey
# Initialize the Cosmos DB client
url = 'YOUR_COSMOS_DB_URL'
key = 'YOUR_COSMOS_DB_KEY'
client = CosmosClient(url, credential=key)
# Create database and container if they don't exist
database_name = 'ECommerceDB'
container_name = 'Orders'
database = client.create_database_if_not_exists(id=database_name)
container = database.create_container_if_not_exists(
id=container_name,
partition_key=PartitionKey(path='/userId'),
offer_throughput=400
)
# Insert a new order document
new_order = {
'orderId': '12345',
'userId': 'u6789',
'items': [{'productId': 'p001', 'quantity': 2}],
'totalPrice': 29.99,
'status': 'shipped'
}
container.upsert_item(new_order)
In this example, we instantiate a CosmosClient, create a database and container (with /userId as the logical partition key), and upsert a new order document.
4. Summary
In the preceding sections, we examined Azure Cosmos DB—a modern, fully managed NoSQL database—highlighting its core capabilities and real-world applicability. Its global distribution, flexible data models, and predictable low-latency performance make it an ideal choice for scalable, mission-critical applications.
In the next article, we’ll explore best practices for efficient database migration and management—ensuring data integrity, security, and operational reliability. Until then, we encourage you to experiment hands-on with Cosmos DB and discover its full potential in your own projects.
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Initialize the Cosmos DB client?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue