简体   繁体   中英

How to update multiple MongoDB servers simultaneously - direct mode or replica set?


I am interested in using MongoDB as a cache servers. I am relatively new to Mongo so bear with me ...
One of the tasks I should perform is the following:
Retrieve records from database and update 2 MongoDB servers. This task only writes to the Mongo servers and doesn't perform "read" actions. The task should run every 250 ms and the amount of the documents to be updated is between 0 - 180 records (relatively small records - each record is a document).

I would like to know how can perform this task using one update in the code ? (I do not want to loop over the servers and perform an update for each of them)

One point to mention: the data in the mongo servers should be read locally at each machine in order to gain as small latency as possible.

Should I create a replica set? if so, will the replica set reflect the up-to-date data in each server given frequency of 250 ms?

Or should I connect to them directly like mongodb://server1,server2/?connect=direct and then perform one update in the code which will be translated to 2 updates - one for each server?

Replies inline..

I would like to know how can perform this task using one update in the code? (I do not want want to loop over the servers and perform an update for each of them)

You can do a bulk insert or multi update. But at the mongod server side, they will be applied in sequence.

Should I create a replica set? if so, will the replica set reflect the up-to-date data in each server given frequency of 250 ms?

You can't be sure that everything is replicated to secondaries in 250 ms. But, by default all the read goes to Primary ( replica-set ) so you get the up-to-date data.

Or should I connect to them directly like mongodb://server1,server2/?connect=direct and then perform one update in the code which will be translated to 2 updates - one for each server?

You have to connect like this only 'mongodb://server1,server2?..options'. Update only goes to the primary, and this update is pulled by the secondary from the primary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM