Sunday, November 1, 2015

Configuring CouchDB uuid generation algorithm

CouchDB can generate _id field of documents for you when you store the documents using POST instead of PUT. The algorithm used by CouchDB to generate the _id fields can be changed although the options are small. We can pick below algorithms for couch db to use to generate the _id fields of our documents: (http://wiki.apache.org/couchdb/HttpGetUuids)


  1. sequential: 26 hex character random prefix, followed by 6 hex characters of sequence, which is incremented by random amounts. When the 6 character sequence overflows, a new random prefix is chosen. There are no guarantees of ordering, but most inserted documents will be sequentially ordered. This improves insert speed as most B-tree edits do not happen randomly. Also, if the documents are likely to be accessed sequentially, this improves access speeds.
  2. random: 32 hex characters generated completely at random.
  3. utc_random: First 14 hex characters are microseconds since Jan 1, 1970 (Unix epoch), followed by 18 random hex characters.
To Update the setting ew can use REST api of the couchDB, 

[rozaydin@RO ~]$ curl -X PUT http://localhost:5984/_config/uuids/algorithm -d '"sequential"'
"sequential"
[rozaydin@RO ~]$

As you can see CouchDB responds with the old algorithm it was using to generate the _id fields prior the update. The same configuration should be able to be done using local.ini file however i failed to find the proper name for the setting if you know how please leave a comment :)