couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jens Alfke <>
Subject Re: distributed counters
Date Wed, 29 Jan 2014 21:19:09 GMT

On Jan 29, 2014, at 5:48 AM, Benoit Chesneau <> wrote:

> I wonder if someone already thought to implement distributed counters using
> couchdb? 

Quick and dirty idea: Implement the counter as a document storing a map from UUIDs to deltas
(1 or -1). To modify the count, add a new UUID key whose value is the amount to increment/decrement
by. The value of the counter is the sum of all the increment values. When there's a conflict,
just merge together the two documents keeping all key/value pairs that occur in either map.

You can store more info if you want by using something descriptive in place of the UUID, for
example a username+timestamp. Just as long as it's guaranteed to be unique.

View raw message