Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Users last-access time with CouchDB

Tags:

couchdb

I am new to CouchDB, but that is not related to the problem. The question is simple, yet not clear to me.

For example: Boris was on the site 5 seconds ago and viewing his profile Ivan sees it.

How to correctly implement this feature (users last-access time)?

The problem is that, if we update users profile document in CouchDB, for ex. property last_access_time, each time a page is refreshed, than we will have the most relevant information (with MySQL we did it this way), but on the other hand, we will have _rev of the document somewhere about 100000++ by the end of the day.

So, how do you do that or do you have any ideas?

like image 322
unno Avatar asked Feb 03 '26 22:02

unno


2 Answers

This is not a full answer but a possible optimization. It will work in addition to any other answers here.

Instead of storing the latest timestamp, update the timestamp only if it has changed by e.g. 5 seconds, or 60 seconds.

Assume a user refreshes every second for a day. That is 86,400 updates. But if you only update the timestamp at 5-second intervals, that is 17,280; for 60-seconds it is 1,440.

You can do this on the client side. When you want to update the timestamp, fetch the current document and check the old timestamp. If it is less than 5 seconds old, don't do anything. Otherwise, update it normally.

You can also do it on the server side. Write an _update function in CouchDB, which you can query like e.g. POST /db/_design/my_app/_update/last-access/the_doc_id?time=2011-01-31T05:05:31.872Z. The update function will do the same thing: check the old timestamp, and either do nothing, or update it, depending on the elapsed time.

like image 65
JasonSmith Avatar answered Feb 05 '26 12:02

JasonSmith


If there was (a large) part of a document that is relatively static, and (a small) part that is highly dynamic, I would consider splitting it into two different documents.

Another option might be to use something more suited to the high write throughput of small pieces of data of that nature such as Redis or possibly MongoDB, and (if necessary) have a background task to occasionally write the info to CouchDB.

like image 28
Evan Avatar answered Feb 05 '26 11:02

Evan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!