Database / Azure Cosmos DB interview questions
1. What is Azure Cosmos DB and what problems does it solve?
Azure Cosmos DB is Microsoft's globally distributed, multi-model NoSQL database service built for modern cloud-native applications. It is designed to solve two of the hardest problems in distributed systems: global low-latency reads and writes and horizontal scalability without any schema or infr...
2. What are the different APIs available in Azure Cosmos DB?
Azure Cosmos DB is a multi-model database, meaning a single back-end engine exposes different wire-protocol-compatible APIs so you can interact with data using the client libraries and query syntax you already know. As of 2024, six APIs are available: Azure Cosmos DB API Overview API Data Model B...
3. What is a partition key in Azure Cosmos DB and why is choosing it correctly so important?
A partition key is a property you designate on every item in a Cosmos DB container. Its value is hashed to determine which logical partition the item belongs to. Cosmos DB distributes logical partitions across physical partitions (server nodes), and this is how it achieves horizontal scaling — mo...
4. What are Request Units (RU/s) in Azure Cosmos DB?
Request Units (RU/s) are the currency of throughput in Azure Cosmos DB. Rather than expressing capacity in CPU cores, memory, or IOPS — which are hard to reason about at the application level — Cosmos DB normalizes all database operations into a single unit. One RU is defined as the cost of readi...
5. What are the five consistency levels in Azure Cosmos DB?
Consistency levels in Cosmos DB define the tradeoff between data freshness and system availability/latency when data is replicated across multiple regions or replicas. Azure Cosmos DB offers five levels, from strongest to weakest: Cosmos DB Consistency Levels Level Guarantee Latency Impact Strong...
6. How does global distribution work in Azure Cosmos DB?
Cosmos DB is built from the ground up for global distribution. Every account has a single write region by default (or multiple write regions with multi-master enabled), and you can add any number of read regions at any time. Cosmos DB automatically replicates all data to every configured region u...
7. What is the Cosmos DB Change Feed and what are its main use cases?
The Change Feed is a persistent, ordered log of every insert and update that occurs in a Cosmos DB container. It records changes in the order they occur within each logical partition, and this log is available for consumption indefinitely (it is not ephemeral like a message queue). Deletes are no...
8. What is provisioned throughput vs autoscale vs serverless in Cosmos DB?
Cosmos DB offers three throughput provisioning models, and choosing the right one significantly affects cost and performance: Provisioned Throughput (Manual) — You pre-set a fixed RU/s number (minimum 400) that is guaranteed and always available. The cost is flat per hour regardless of actual usa...
9. How does indexing work in Azure Cosmos DB?
By default, Cosmos DB automatically indexes every property in every JSON item you insert — no schema definition, no CREATE INDEX statement required. This is called the automatic indexing policy . When you write a document, the indexing engine traverses the JSON tree and adds every field path, arr...
10. What is Time to Live (TTL) in Cosmos DB and how do you configure it?
Time to Live (TTL) is a built-in expiry mechanism that automatically deletes items after a specified number of seconds from their last modification timestamp. This is useful for session data, temporary tokens, audit logs with retention windows, or any data with a known shelf life. Cosmos DB's TTL...
11. What is a stored procedure in Cosmos DB and what are its limitations?
Stored procedures in Cosmos DB are JavaScript functions registered on a container and executed server-side on the Cosmos DB engine itself. They run atomically within a single partition — meaning all operations inside the stored procedure either all commit or all roll back, giving you ACID transac...
12. What is the difference between a point read and a query in Cosmos DB?
A point read is the most efficient read operation in Cosmos DB. It fetches a single item by specifying both the item's id and its partition key value exactly. Cosmos DB can route this operation directly to the specific replica holding that item without scanning any index. The RU cost is approxima...
13. What is the Cosmos DB NoSQL query language and how does it differ from standard SQL?
The Cosmos DB NoSQL API supports a SQL-like query language (sometimes called Cosmos DB SQL or the NoSQL query language) that is syntactically close to ANSI SQL but adapted for JSON documents. You use familiar keywords — SELECT, FROM, WHERE, ORDER BY, GROUP BY, JOIN — but with several important ex...
14. What is the Cosmos DB transactional batch API?
The transactional batch API is the way to execute multiple operations (up to 100) atomically on items that share the same partition key. Unlike stored procedures (which also provide ACID atomicity but require JavaScript and have size limits), the transactional batch runs from the client SDK and d...
15. What is Cosmos DB Integrated Cache and how does it reduce RU consumption?
The Cosmos DB Integrated Cache is a server-side in-memory cache built into the dedicated gateway tier. When enabled, it caches the results of point reads and queries at the gateway layer so that repeated identical requests can be served from memory without going to the backend storage partitions ...
16. How does optimistic concurrency work in Azure Cosmos DB?
Cosmos DB uses ETags for optimistic concurrency control. Every item in Cosmos DB has a system-generated _etag property that changes on every write. When you read an item, you get its current ETag. When you want to update that item, you include the ETag in the write request using the If-Match HTTP...
17. What is hierarchical partition keys in Cosmos DB and when do you use it?
Hierarchical partition keys (also called sub-partitioning) allow you to define up to three levels of partition key paths on a container, forming a composite key like /tenantId/userId/sessionId . This feature, introduced in GA in 2023, solves the hot partition problem for multi-tenant or hierarchi...
18. What is the Cosmos DB Bulk Executor and how do you use bulk operations in the SDK?
Bulk operations in Azure Cosmos DB allow you to send multiple create, upsert, replace, delete, or read operations in a single SDK call that the library optimizes internally — batching by partition, retrying 429 throttles automatically, and maximizing throughput by saturating available RU/s concur...
19. What are Cosmos DB triggers and user-defined functions (UDFs)?
Cosmos DB supports two additional server-side JavaScript constructs beyond stored procedures: triggers and user-defined functions (UDFs) . Both run on the Cosmos DB compute engine but serve different purposes. Triggers are JavaScript functions attached to a container that fire automatically befor...
20. How does Cosmos DB handle conflicts in multi-region write (multi-master) setups?
When multi-region writes are enabled in Cosmos DB, any region can accept writes simultaneously. If two clients in different regions write to the same item at the same time, a write conflict occurs. Cosmos DB uses a deterministic conflict resolution policy to handle these without blocking either w...
21. What is the Cosmos DB Emulator and how is it used in development?
The Azure Cosmos DB Emulator is a locally running software that faithfully emulates the Cosmos DB NoSQL API on your development machine. It lets you develop and test Cosmos DB applications without an Azure subscription, without incurring costs, and with full offline capability. The emulator runs ...
22. What is Cosmos DB for MongoDB API and what version compatibility does it provide?
The Cosmos DB for MongoDB API is a compatibility layer that implements the MongoDB wire protocol, allowing applications written for MongoDB to connect to Cosmos DB by changing only the connection string. The application code — queries, aggregation pipelines, driver calls — does not change. Cosmos...
23. What is the Cosmos DB analytical store and Azure Synapse Link?
The Cosmos DB analytical store is a fully isolated, column-oriented copy of your operational data maintained automatically by Cosmos DB, designed for analytical queries. When you enable it on a container, Cosmos DB automatically synchronizes every insert, update, and delete from the row-oriented ...
24. What are Cosmos DB materialized views and how do they differ from containers?
Cosmos DB materialized views (in preview as of 2024) are automatically maintained read-only containers that are derived from a source container via a defined query or projection. When data changes in the source container, Cosmos DB updates the materialized view automatically and asynchronously — ...
25. How does Cosmos DB pricing work and what are the key cost drivers?
Cosmos DB billing has three main components: provisioned throughput , storage , and additional features . Understanding each is essential for cost-effective application design. 1. Throughput cost — For provisioned throughput, you are billed per 100 RU/s per hour (around $0.008/hour per 100 RU/s i...
26. What is the Cosmos DB Gremlin API and what is it optimized for?
The Cosmos DB Gremlin API implements Apache TinkerPop's Gremlin graph traversal language, enabling you to model and query data as a property graph — a network of vertices (nodes) and edges (relationships), each of which can carry arbitrary key-value properties. This API is optimized for workloads...
27. How does Cosmos DB backup and restore work?
Cosmos DB offers two backup modes, selected at account creation and switchable in some scenarios: Periodic backup (the legacy default) and Continuous backup (the recommended modern option). Periodic Backup — Takes full snapshots at a configured interval (default every 4 hours, configurable to 1 o...
28. What is the Cosmos DB Patch API and how does it differ from Replace?
The Patch API (partial document update) was introduced in Cosmos DB in 2022 to address a common inefficiency in document updates. Before Patch, the only way to modify a field in a Cosmos DB item was to read the full document, change the field in memory, and replace the entire document. For large ...
29. What is the Cosmos DB Cassandra API and how does CQL map to Cosmos DB concepts?
The Cosmos DB Cassandra API exposes Cosmos DB as a Cassandra-compatible data store using the Cassandra Query Language (CQL) wire protocol. Applications built on Apache Cassandra — using drivers like DataStax Java Driver, Python driver, or the gocql Go driver — can connect to Cosmos DB by changing...
30. How do you model one-to-many relationships in Cosmos DB?
Cosmos DB is a document database with no foreign key enforcement and no JOIN between containers, so one-to-many relationship modeling requires deliberate design decisions based on your access patterns. There are three main strategies, each with different tradeoffs: 1. Embedding (denormalization) ...
31. What is the Cosmos DB free tier and what does it include?
The Cosmos DB free tier is a permanent (not time-limited) offering where every Azure account can have one Cosmos DB account with a perpetual free allocation, making it suitable for prototyping, side projects, and learning without ongoing costs. The free tier allocation includes: 1,000 RU/s provis...
32. What is the Cosmos DB SDK and what are the key client configuration options?
The Azure Cosmos DB SDK is the official client library for interacting with Cosmos DB programmatically. Microsoft provides first-party SDKs for .NET, Java, Python, JavaScript/Node.js, and Go. Each SDK wraps the underlying REST API with connection pooling, retry logic, serialization, and Cosmos-sp...
33. What is the Cosmos DB Table API and when would you migrate from Azure Table Storage to it?
The Cosmos DB Table API is a compatibility layer that exposes Cosmos DB as an Azure Table Storage-compatible endpoint. Applications using the Azure Table Storage SDK can point their connection string at a Cosmos DB Table API account and gain Cosmos DB's global distribution, SLA guarantees, and la...
34. How does Cosmos DB handle security and access control?
Cosmos DB provides multiple layers of security: network isolation, authentication, and fine-grained authorization. Network-level security : By default, Cosmos DB accounts accept connections from all networks. You can restrict access using: IP firewall rules — Allowlist specific IP addresses or ra...