Search code examples
performancecouchdbreplication

CouchDB - Mobile application architecture - Replication performance


I built a mobile application based on CouchDB.

For security reason, i have to make sure that a document can be read only by the users allowed to do do it. As i cannot manage the access right at document level, i create one couchdb database per user, and i replicate documents from my main couchDB database in each user database with a filtered replication.

This model work very well, but today i faced huge performance issues.

I tried to have all my replications continuous, filtered and bi-directionnal, but after 80 users (so 81 databases and 160 simultanous continuous replications), there was too much replications and my couchDB service start to slow down and even crashed sometimes. Notices that all the databases are on the same server (and i could not have more than one server)

I tried to put in place a "manual" replications, but even this way when i need to replicate a document from my main database to all my 80 users databases, each filtered replication from my main database to a user database take around 30 seconds.

Maybe i have an issue with my replication filter, i store for each document a list of users allowed to see it. As each user has it own database, i replicate only the document the user is allowed to see in its database. Here is my replication function :

function(doc, req) {
  if(doc.userList) {
    if(doc.userList.indexOf(req.query.username) > 1) {
      return true;
    }
  }
  return false;
}

The goal of my application is to get around 1000 users, that is totally impossible with the current architecture / performance.

I have three questions : 1. Even if i think that it's not possible, Is it possible to get about 1000 databases in continuous replication on the same server? 2. Is there anything wrong with my replication filter? Is there any way to improve it to have fast databases replications? 3. If the current architecture is not good at all, what kind of architecture would you advise in my case?

Thank you very much !


Solution

  • We finally changed our global project architecture. The main server cannot handle more than 100 replicated databases even if the configuration limits can be changed, after 80 synchronied databases couchdb logs start to explode. I may wrong, but i think that this kind of architecture is not possible on a single server.

    Here is the solution we put in place. We removed all the users databases and we plugged all our mobile applications directly on the main database and do a filtered replication directly on the main database : http://pouchdb.com/api.html#replication by using this solution : Example 3: filter function inside of a design document

    This new model is now working we did some stress tests and we didn't get any issue until 1000 simultaneous users.

    Just be aware that pouchDB, to replicate a database, ask couchdb all the modifications applied on the main database since the last synchronisation (even for filtered replication). So when you create a new pouchdb database and synchronise it, if your main couchDB is old and has a big historical (check couchdb _changes API), it can take a very (very) long time !