Database / Azure Cosmos DB interview questions
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, array element, and nested object to a set of inverted indexes maintained alongside the data. This is why write costs in Cosmos DB are higher than read costs per RU — every write triggers index updates.
The indexing policy is configured per container in JSON and controls three things:
- indexingMode —
consistent(default, synchronous),lazy(asynchronous, lower write cost, not recommended), ornone(disables indexing entirely). - includedPaths — Explicit paths to index (e.g.,
/address/city/?). Wildcard/*indexes everything. - excludedPaths — Paths to exclude from indexing (e.g., large text fields you never filter on). Excluding high-cardinality or rarely-queried large fields significantly reduces RU cost per write and storage overhead.
Three index types exist:
- Range index — Default for scalar values. Supports
=,<,>,BETWEEN, andORDER BY. - Spatial index — For GeoJSON geometry types. Supports
ST_DISTANCE,ST_INTERSECTS, and other geo queries. - Composite index — Required when an ORDER BY clause references two or more properties, or when a filter and ORDER BY target different properties. You must explicitly define composite indexes.
A common optimization: exclude the /_etag path and any large blob-like string fields from indexing by default to reduce write RU consumption on write-heavy containers.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
