Replication in MongoDB

I completed a section on Architecture of ElasticSearch from an online course and completed two chapters from MongoDB in Action - Replication and Scaling your system with sharding.

The motivation was to learn what these terms mean and what is their utility in application development. I’m writing this post to collate my notes and understanding from the above sources.

What necessitates Replication and Sharding?

availability-and-scalability

How replication adds to availability?

Replication involves storing multiple copies of data across nodes (different servers).

In the event of a failure of the primary data source (node), the operational load of the database is shifted to the other nodes which are alive and retain a copy of the data from the primary data source

replica-set in MongoDB

replica-set-mongodb

How is data synchronized between the primary and the secondary nodes of a replica set?

Every node (primary and secondary) has a database local where it stores all the replica-set metadata and an oplog

An oplog is a collection of entries where each entry corresponds to a write operation and contains enough information to reproduce the write operation. Also these entries in the oplog are idempotent - irrespective of how many times an oplog entry is applied, the result would be the same.

Each entry within an oplog contains

Operations affecting multiple documents are split into multiple entries in the oplog. For multi-updates or mass deletes, a separate entry is created in the oplog for each document affected

How a secondary node updates itself?

Secondary nodes use long polling ie make a long lived request to the primary. When the primary node is modified, it responds to the waiting request immediately. Thus, secondary nodes will usually be almost completely up to date

The concept of an oplog and executing its entries to create the database sounded similar to how state is updated in redux. The equivalent of an entry in oplog is an action which is executed by the appropriate reducer to update the state.

A key difference between actions in redux and the entries in an oplog is that the latter are idempotent. The idempotent nature of oplog entries allows them to be executed multiple times to overcome earlier failed attempts without fear of corrupting the data.

The oplog entries are also similar to commits in a version control system such as Git. By applying the commits, one can generate the code base at a given point in time.

What are the challenges replication introduces?

How replication adds to performance?

Replication can help distribute read queries across secondary nodes with the caveat that the secondary nodes may not always serve the latest data (eventual consistency)

References