nickponline
nickponline

Reputation: 25914

Can I keep two mongo databases synced?

I have an app that can run in offline mode. If offline it uses a local mongo database, if it has a data connection it will use a remote mongo database.

Is there an easy way to sync these two databases and make sure they both have the union of their collections and documents?

EDIT: Effectively there are two databases that could both have insertions and deletions happening on them that aren't happening on the other. At fixed points in time I would like to have both databases show the union of them both.

For example over a period of time.

DB1.insert(A)
DB1.insert(B)
DB2.insert(C)
DB1.remove(A)

RUN SYNC

DB1 = DB2 = {B, C}

EDIT2: Been doing some reading. It's not the intended purpose but could they be set up as slaves replica sets of the remote and used that way? Problem is that I think replicas need to have a replica hosts must be accessible by way of resolvable DNS. Not sure how the remote could access local host.

Upvotes: 4

Views: 9130

Answers (1)

zero323
zero323

Reputation: 330063

You could use replica set but MongoDB doesn’t support master-master replication. Let's assume if you have setup like this:

  • two nodes with priority 1 which will be used as remote servers
  • single arbiter to ensure majority if one of remotes dies
  • 5 local dbs with priority set as 0

When your application goes offline, it will stay secondary so you won't be able to perform writes. When you go online it will sync changes from remote dbs but you still need some way of syncing local changes. One of dealing with could be using local fallback db which will be used for writes when you are offline. When you go online, you push all new records to master. A little bit trickier could be dealing with updates but it is doable.

Another problem is that it won't scale up if you'll need to add more applications. If I remember correctly, there is a 12 nodes per replica set limit. For small cluster DNS resolution could be solved by using ssh tunnels.

Another way of dealing with a problem could be using small restful service and document timestamps. Whenever app is online it can periodically push local inserts to remote and pull data from remote db.

Upvotes: 2

Related Questions