How to load balance/failover with pouchdb/couchdb?

2019-07-16 13:04发布

问题:

Just looking for info on what strategies people use for this?

In the examples I see it is really easy to make a remote database connection that I will call get() on.

var remoteDb = new PouchDB('https://myhost.com/db');
var data = remoteDb.get("myId");

But what if I have several remote databases set up that are syncing with each other. I want the clients to be able to use them all without changing the code.

I am considering doing this through dns. For example have db.myhost.com with multiple records pointing to the other servers. If I do this and one server goes down will pouchDB realize and reconnect to another?

Potentially I could write some javascript to give users a random server or assign a server based on region, etc. Then I could set up error handlers to switch servers on any errors. Is this necessary or is it already implemented in some library I could use?

回答1:

You can use nginx as a reverse proxy for that, as explained here: http://karmi.tumblr.com/post/247255194/simple-couchdb-multi-master-clustering-via-nginx

Basically you configure nginx to listen on one address and then pass the traffic to your CouchDB instances (adapted from the original post):

http {
  # ...
  upstream couchdb_cluster {
    server couchdb1.example.com:5984;
    server couchdb2.example.com:5984;
  }

  server {
    listen 80;
    server_name db.example.com;

    location / {
      proxy_pass http://couchdb_cluster;
      break;
    }

  }
  # ...
}