What is the difference between a Replica Set and a Cluster?
I’m assuming that a Replica Set represents the group of mongod processes maintaining a data set, something like the ‘software’ behind the nodes, whereas the Cluster would be the physical group of database servers itself (e.g.: primary, secondary, secondary).
Plus, what are Heartbeats? I couldn’t understand the definition as it is in the documentation.
Database cluster is just multiple nodes running that database. It’s same as usual clusters.
Now these nodes are not necessarily replicas, which means that the data stored by each node might be difference from other nodes. And this is what the difference between a general cluster and a replica-set is.
When the load is too high, you would want to split (distribute) that load over multiple nodes. And since too much data might take longer to search, so you store parts of data on different nodes. You usually choose yourself how the data is distributed between nodes, and since you know what data is contained in which node, you can direct your code to that node making the database lookup much faster.
Besides this, you also want to have a backup node (or multiple backup nodes) in case some node goes down. That’s where you have replication. Multiple nodes contain same data and behave the same way (whenever a new data is added to some node, it synchronizes with other nodes).
This is what you call as distributed computing and high availability. The question is slightly off in the sense that both distribution and availability come as a “cluster”. So a cluster includes distributing the load and making sure it’s highly available.
Heartbeats work the same way as a ping request in between every node in a replica set.
Consider a replica set with 3 nodes: RS1, RS2 and RS3. How these three will know that everything is working properly? By pinging every node every 2 seconds.
RS1 will receive heartbeats from RS2 and RS3:
RS1 <- RS2 and RS1 <- RS3
RS2 will receive heartbeats from RS1 and RS3:
RS2 <- RS1 and RS2 <- RS3
RS3 will receive heartbeats from RS1 and RS2:
RS3 <- RS1 and RS3 <- RS2
If one replica set node does not receive heartbeat from any other node, it considers the node has been shutdown and calls for an election.
You can experiment that yourself. Fire up a replica set for 3 nodes. Check rs.status(), then shutdown any node by kill pid or run db.shutDownServer(), then check mongod logs to see how the election is taking place and how it come to a conclusion that the node has been down.