Database / Azure Cosmos DB interview questions
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 write.
Three conflict resolution modes are available:
Last-Write-Wins (LWW) — The default mode. Cosmos DB uses a timestamp-based comparison to determine the winner. By default it compares the _ts system property. You can configure a custom numeric property as the tiebreaker (useful if client-side timestamps are more reliable than server-assigned ones). The losing write is discarded.
Custom — Merge Procedure — You register a JavaScript stored procedure as the conflict resolver. When a conflict is detected, Cosmos DB calls the procedure with both conflicting versions. Your code decides the final merged state — useful for counter increments, array merges, or any domain where neither version should simply win over the other.
Custom — Async via Conflicts Feed — Cosmos DB writes all conflicting versions to a special conflicts feed container accessible via the SDK. Your application code processes conflicts asynchronously and uses the API to resolve each one. This is the most flexible mode — you can apply business rules, user notifications, or manual review workflows — but it adds latency between conflict detection and resolution.
In practice, the conflict rate in well-designed multi-region applications is very low if you partition data by region affinity (European users write to the European region, etc.). Conflicts only occur when the same item is written concurrently in two different regions, which is rare if your routing logic is sound.
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...
