Search code examples
mongodbreplicationmongodb-replica-set

MongoDB standalone vs replica set and how to migrate data from a standalone to a replica set


I have a few questions about MongoDB standalone and Replica sets, I don't really get it.

  • When should I use either of them
  • Why all the replica sets tutorials show 3 connections, is there a reason?
  • Can I create a replica set for 1 instance only? and in that case how is it different than the standalone mongodb instance?
  • How to Migrate data from a standalone instance to replica sets?

All these questions I'm asking because recently I was trying to implement transactions and sessions can only start on "replica sets" I don't really get the difference at all.


Solution

    • When should I use either of them?

    Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.

    • To keep your data safe
    • High (24*7) availability of data
    • Disaster recovery
    • No downtime for maintenance (like backups, index rebuilds, compaction) Read scaling (extra copies to read from)
    • Replica set is transparent to the application
    • Why all the replica sets tutorials show 3 connections, is there a reason?

    The basic implementation to take full advantage of replication specifies you should have at least one primary node with two secondary nodes. So the examples are always with 3 nodes. Not only this if from 3 the Primary node goes down you still have 2 nodes (mongoDB will assign using arbiter rule) and one primary and one secondary for high availability

    • Can I create a replica set for 1 instance only? and in that case how is it different than the standalone mongodb instance?

    It does not make sense to have single instance with mongo replication.

    • How to Migrate data from a standalone instance to replica sets?

    Convert a Standalone to a Replica Set . Your existing data will be migrated to all replication instances once they are up and running when converted to replication sets from standalone.