Database / CouchDB Interview Questions
What is the CouchDB winning revision algorithm for conflict resolution?
When CouchDB has two or more conflicting revisions of the same document, it must deterministically pick one as the winning revision — the one returned by a normal GET request without ?conflicts=true. The algorithm is deterministic so that all cluster nodes independently arrive at the same winner without any coordination.
The winning revision is chosen by these rules in order:
- Prefer non-deleted revisions over deleted ones. A live document always beats a tombstone, regardless of generation count. This prevents a delete from silently "winning" over an edit that arrived at a replica later.
- Among non-deleted (or all-deleted) revisions, prefer the one with the higher generation number (the integer prefix in
_rev). Generation 5 beats generation 3. - If generation numbers tie, compare the revision hash strings lexicographically. The hash that sorts later (higher string order) wins. This is the tiebreaker of last resort and is arbitrary from a business logic perspective — which is why applications should not rely on the winning revision for semantically meaningful merges.
The consequence of this algorithm is that the winner is not necessarily the revision with the "most recent" wall-clock timestamp, nor the one with the most recent change. It is entirely possible for an older edit to "win" if its revision chain is longer. This is why CouchDB recommends application-level conflict detection and resolution rather than trusting the automatic winner for data that matters.
You can trigger a re-evaluation of which revision wins by deleting the current winner — the next-highest-generation conflict becomes the new winner automatically.
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...
