Skip to main content

🌍 Azure Cosmos DB (Interview Guide)

 

🟢 What is Azure Cosmos DB?

  • A globally distributed, multi-model, NoSQL database service.
  • Designed for low latency, high availability, and global scalability.
  • Supports multiple APIs: SQL (Core), MongoDB, Cassandra, Gremlin (Graph), Table API.

👉 Think of it as a database that automatically scales across the globe.


🟢 Key Concepts

  • Account → Database → Container → Items
  • Partition Key → spreads data evenly across physical partitions.
  • Request Units (RUs) → performance currency (every operation consumes RUs).
  • Consistency Levels → trade-off between performance and accuracy:
    • Strong, Bounded Staleness, Session, Consistent Prefix, Eventual.
  • Change Feed → stream of inserts/updates in order.

🟢 Partition Key (Most Asked in Interviews)

  • Good partition key: high cardinality, evenly distributes load.
  • Bad partition key: low cardinality (e.g., true/false, country code).
  • Can use synthetic keys (e.g., CustomerId + Date).

👉 Interviewers love to ask: “What happens if you choose a bad partition key?”
Answer: It creates hot partitions → performance issues.


🟢 Pricing Models

  • Provisioned Throughput (RU/s) → fixed capacity, predictable cost.
  • Autoscale → scales RU/s up/down automatically.
  • Serverless → pay per request (for small workloads).

🟢 Example: Insert Document in .NET

using Azure.Cosmos;

string endpoint = "<COSMOS_ENDPOINT>";
string key = "<COSMOS_KEY>";
CosmosClient client = new CosmosClient(endpoint, key);

var db = await client.CreateDatabaseIfNotExistsAsync("SampleDb");
var container = await db.CreateContainerIfNotExistsAsync("Products", "/category", 400);

var item = new { id = Guid.NewGuid().ToString(), category = "electronics", name = "Laptop", price = 1200 };
await container.CreateItemAsync(item, new PartitionKey(item.category));

Console.WriteLine("✅ Item inserted!");

🟢 Example: Query Documents (SQL-like)

var query = "SELECT * FROM c WHERE c.price > 500";
var iterator = container.GetItemQueryIterator<dynamic>(query);

while (iterator.HasMoreResults)
{
    foreach (var doc in await iterator.ReadNextAsync())
        Console.WriteLine($"{doc.name} - {doc.price}");
}

🟢 Advanced Features

  • Transactional Batch → atomic operations within same partition.
  • Stored Procedures (JS) → run logic close to data.
  • Change Feed → react to changes in real time (e.g., with Azure Functions).

🟢 Monitoring

  • Integrated with Azure Monitor + App Insights.
  • Track RU consumption, latency, throttling (429 errors).
  • Use retry policies with exponential backoff.

🟢 Common Interview Questions

  1. What is Azure Cosmos DB?
  2. What is a partition key? How do you choose one?
  3. What are Request Units (RUs)?
  4. Explain different consistency levels.
  5. What is the Change Feed? Use case?
  6. Difference between Provisioned Throughput and Serverless mode?
  7. How does Cosmos DB achieve global distribution?
  8. What are hot partitions and how do you avoid them?
  9. Difference between Cosmos DB and SQL Database?
  10. How do you monitor performance and troubleshoot 429 errors?

🟢 Pros & Cons

✅ Pros

  • Global distribution, low latency.
  • Multi-model, flexible APIs.
  • Tunable consistency.
  • Change Feed support for event-driven apps.

❌ Cons

  • Cost can grow with poor partition design.
  • Limited support for complex joins (compared to SQL).
  • RU model can be confusing for beginners.

✅ Quick Recap

  • Cosmos DB = globally distributed NoSQL database.
  • Partition Key → most critical design choice.
  • RU/s → performance unit (watch consumption).
  • Consistency Levels → balance between latency & accuracy.
  • Change Feed → real-time stream of changes.


Comments

Popular posts from this blog

☁️ Azure Key vault Short Notes

🟢 What is Azure Key Vault? A cloud service for securely storing and accessing secrets, keys, and certificates . Removes the need to keep secrets (like connection strings, passwords, API keys) inside code or config files. Provides centralized secret management, encryption, and access control . 👉 Think of it like a secure password manager but for your applications. 🟢 Key Features Secrets → store text values (e.g., DB connection string, API key). Keys → store cryptographic keys (RSA, EC) for encryption, signing. Certificates → store/manage SSL/TLS certificates. Access Control → Access Policies (older model). Azure RBAC (modern, preferred). Integration → works with App Service, Functions, AKS, VMs, SQL DB, etc. Logging → audit who accessed secrets via Azure Monitor / Diagnostic Logs. 🟢 Why Use Key Vault? Security → secrets are encrypted with HSM (Hardware Security Modules). Compliance → meet industry standards (PCI-DSS, ISO, GDPR). Automation → aut...

🗑️ Garbage Collection & Resource Management in .NET (C#) — Beginner Friendly Guide

When you start working with .NET and C#, one of the biggest advantages is that you don’t need to manually manage memory like in C or C++. The Garbage Collector (GC) does most of the work for you. But here’s the catch — not everything is managed automatically. Some resources like files, database connections, sockets, and native memory still need special handling. This blog will help you understand: ✔ How the GC works ✔ What are managed vs unmanaged resources ✔ The difference between Dispose , Finalize , and using ✔ The Dispose pattern with examples ✔ Best practices every C# developer should know 1) How Garbage Collection Works in .NET Managed resources → Normal .NET objects (string, List, etc.). GC frees them automatically. Unmanaged resources → External resources like file handles, database connections, sockets, native memory. GC cannot clean them up — you must do it. 👉 GC uses a Generational Model for performance: Gen 0 : Short-lived objects (local variables, t...

🚀 Onboard a New API in Microsoft Entra and Consume It — with Full Examples

This article shows how to: Register your API in Microsoft Entra (Azure AD) and expose a scope. Register clients (daemon and web client) and grant permissions. Protect a .NET Web API (ASP.NET Core) using Microsoft.Identity.Web . Call the API from: a daemon/service using client credentials a backend that performs On-Behalf-Of (OBO) to call a downstream API Replace placeholders like YOUR_TENANT_ID , API_CLIENT_ID , CLIENT_APP_ID , and CLIENT_SECRET with actual values from the portal. ✳️ Portal steps (brief) Register API ( MyProductApi ) Azure Portal → Microsoft Entra ID → App registrations → New registration → MyProductApi Note: Application (client) ID → API_CLIENT_ID , Directory (tenant) ID → YOUR_TENANT_ID Expose an API App → Expose an API → Set Application ID URI to api://{API_CLIENT_ID} (default) or https://contoso.com/my-api Add a scope : Scope name: access_as_user Admin consent display name: Access MyProductApi as user Who can consent: Admins a...