couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Metson <>
Subject Re: distributed counters
Date Wed, 29 Jan 2014 21:26:54 GMT

On Wednesday, 29 January 2014 at 21:19, Jens Alfke wrote:

> On Jan 29, 2014, at 5:48 AM, Benoit Chesneau < (>
> > 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.

Thats fragile/brittle in a distributed system. Imagine you had a large number of conflicts,
with a very complicated revision history - the simple merge isn’t practical. You need the
inc/dec’s to be standalone documents and do the summing in the database via a view.  

A similar pattern can work for sets, I think, but requires a more complex view, and possibly
a bit more logic outside of the DB.

View raw message