最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent CouchDB to create document revisions when updating simple counters - Stack Overflow

programmeradmin5浏览0评论

I want to store counters in a CouchDB document, incremented on each page view. CouchDB will create a plete revision of this document for just 1 counter update.

Wouldn't this consume too much space? Considering that I have 1M hits in a day, I might be looking at 1M revisions to the document in a day.

Any thoughts on this...

Thanks!

I want to store counters in a CouchDB document, incremented on each page view. CouchDB will create a plete revision of this document for just 1 counter update.

Wouldn't this consume too much space? Considering that I have 1M hits in a day, I might be looking at 1M revisions to the document in a day.

Any thoughts on this...

Thanks!

Share Improve this question asked Sep 12, 2011 at 12:43 Mayank JainMayank Jain 3,2052 gold badges25 silver badges20 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

CouchDB is very explicit about the trade-offs it makes. In this particular case, we are talking about having a crash proof database that, sadly, can and will use a lot of disk until paction.

You get with this reliability and a lot concurrency for reads. You will also get the ability to replicate seamlessly with any other nodes. This is the bacon of it. Having to pact because of bumped counters is the suck of it. Forget about mucking around with _rev_limit. You will screw yourself doing it because revisions are so foundational to Couch.

One possibility that you have is logging some info, the date and time, IP's and other stuff. You'd then create a view emitting the data you need and using _count as your reduce function. You'll get the info you need and some other possibly valuable stuff for analytics. This is the "just create a view" solution.

The second possibility would be using [redis] (http://redis.io/mands/incr). Redis is quite nice and would fit well with this use case (http://ai.mee.nu/is_couchdb_the_anti-redis). This would be the "the right tool for the right job" solution.

The third possibility would be to simply ignore it. It might not be a problem at all (if you pact often). This would be the "just relax" solution.

You have to take the good with the bad and make sure the advantages outweighs the disadvantages. Measure everything twice before you cut/optimize.

I don't think it's possible.

An alternative solution would be to place the counter in a small document, and run paction on it periodically. This isn't optimal, but it minimizes the space occupied.

You may also want to consider using something like memcached (or Membase) to serve as your "counter storage". That will let you update these counters without creating extra revisions in CouchDB. I assume that you don't actually need to keep all of the intermediate states of the counter (since you say that you don't want the revisions kept around) so putting them in something better suited for this use case seems to make sense.

If you don't need replication you can save the Counter in a _local doc. Local documents do not have a version history. You also can save them without knowing their revision. They don't replicate, last write wins always.

To create/update a _local doc just use PUT /db/_local/[DOCID]

You can retrieve your _local doc with GET /db/_local/[DOCID]

We were doing a little experiment...

The document had default 1000 revs limit, had about 100kb of attachments, 1 integer counter, which we kept incrementing

We ended up with about 4GB of disk used for about 200,000 increments. Used paction & it got reduced to about 6KB.

Now that's a bummer!

My serious concerns are now - running frequent paction (maybe hourly/twice-daily/etc) on a write heavy couch instance!

发布评论

评论列表(0)

  1. 暂无评论