Just because CouchDB uses Multi Versioning Concurrency Control (or MVCC) doesn’t mean that you’ll also get document versioning support by default. So, how would you go if you need document versioning in CouchDB?
This article takes a look at 4 different approaches and details the first one as it looks to be both simple, scalable and supported by CouchDB replication:
- storing as a binary attachment the previous version
- append old versions to an array embedded in the current document
- keep each version as a separate document
- skip compaction
How does storing previous document version as a binary attachment to the new version work?
[…] when the document is loaded from the CouchDB server, the string representation is saved before being parsed into JSON. Later, when the document is saved, the string representation is attached as a new binary attachment, with the corresponding rev as it’s name, and a content type of application/json. This way any CouchDB library can just open the stored rev, and see it as a normal document.
This means that each time the document is updated, the client will also store the previous version as an attachment to the latest version. At any time, a user can load any of the old versions.