Developers are often asking me how to “version” documents with Couchbase 2.0. The short answer is: the clients and server do not expose such feature, but it is quite easy to implement.
In this article I will use a basic approach, and you will be able to extend it depending of your business requirements.
The first thing to do is to select how to “store/organize” the versions of your document, and for this you have different designs:
- copy the versions the document into new documents
- copy the versions of the document into a list of embedded documents
- store the list of attributes that have been changed into a embedded element (or new documents)
- store the “delta”
You will have to chose the design based on your application requirements (business logic, size of the dataset, …). For this article, let’s use one of the most simplistic approach: create new document for each version with the following rules for the keys:
- The current version is is a simple Key/Document, no change to the key.
- The version is a copy of the document, and the version number is added to the key.
This looks like:
1 2 3 4
With this approach, existing applications will always use the current version of the document, since the key is not changed. But this approach creates new documents that will be indexed by existing views.
For example, in the Beer Sample application, the following view is used to list the beer by name:
1 2 3 4 5
It is quite simple to “support” versioning without impacting the existing code, except the view itself. The new view needs to emit keys,value only for the current version of the document. This is the new view code:
1 2 3 4 5
With this change the existing applications that are using this view will continue to work with the same behavior.
Implementing the versioning
Based on this design, when the application needs to version the document, the following logic should happen:
- Get the current version of the document
- Increment the version number (for example using another key that maintains the version number for each document)
- Create the version with the new key “mykey::v1”
- Save the document current version
Let’s look at the code in Java
1 2 3 4 5 6 7 8 9 10 11 12
Quite simple isn’t?
The application can access the document using the key, but also get one version or the list of all versions, this is one of the reasons why it is interesting to create a key (mykey_version), and use it also to delete documents and related versions.
Based on the previous comment, the delete operation looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
As an example, I have created a small library available on GitHub https://github.com/tgrall/couchbase-how-to-versioning, this library extends the Couchbase Client and overrides some of the operations : set, replace and delete. (the basic one: no TLL, no durability) As I said before this is just an example.
Build and Install
1 2 3
Then add this library to your project in addition to Couchbase Java Client, for example in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
Code your application
Create a document and version it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- Line 5: instead of using the
CouchbaseClient, the application uses the extended
- Line 7: create a new entry
- Line 9: create a new version, the boolean value to “true” force the versioning of the document
- The application use other methods such as get a specific version (line 11), get all versions (line 13), delete a specific version (line 14), and finally delete the key and all versions (line 16).
So using this approach the developer controls explicitly when to create a version, since he has to add the boolean parameter in the set operation. In this small sample library it is also possible to do auto versioning, in this case all set and replace calls will create a version, to achieve that the developer just needs to call the setAutoVersioning(true) method. Something like:
With this approach you can provide versioning to your application with minimal code change. You can test it in the Beer Sample application, just do not forget to change the views as documenter above to only return current version of the documents.
As you can see doing versioning in Couchbase is not that complicated, but it is something that must be done by your application based on its requirements and constraints. You have many different solution and none of these options is perfect for all use cases.
In this specific sample code, I am working with a simple design where I create a copy of the documents for each version. With this approach also, it is interesting to mention that you can version “anything”, not only JSON document but also any values. As I said before, this is one possible approach, and like any design, it has some impact on the application or database, in this case most the database:
- Increase the number of keys and documents
- Double - or more- the number of operations, for example when updating a document, the application needs to get the current value, create a version, save the current version.
- Consistency management when adding new version and incrementing the version number (need to deal with errors when creating a new version, deleting the versions and counter….)
Many features could be added to this easily, for example:
- Limit to a specific number of version,
- Enable the versioning only of replace() operation
- Add specific attribute about versions in JSON document (for example date of the version)
If you are using versioning in your Couchbase application feel free to comment or write a small article that describes the way your are doing it.