SlideShare a Scribd company logo
Database Replication
valikhani@sirangweb.com
@mehdivk
Mehdi Valikhani
Replication
Process of keeping IDENTICAL COPY
of your data on different servers.
Replication offers
Minimum Downtime
in case of hardware failure, software
failure or maintenance
Replication is highly
recommended for all
databases in
production from the
very first day.
Replica Set
MongoDB’s Terminology for Replication
A replica set is a group of servers with one primary,
the server taking client requests, and multiple
secondaries, servers that keep copies of the primary’s
data.
Give A Go To
MongoDB’s Replica Set
In
60 Seconds!
#ToBeTriedOnlyAtHome
Step 1: Setup Replica Set
$: mongo --nodb
$: myReplica = new ReplSetTest({nodes: 3});
$: myReplica.startSet();
$: myReplica.initiate();
Congratulations!
3 member of replica set running on ports 31000, 31001 and 31002
Step 2: Connect To Primary
$: mongo --port 31000
testReplSet:PRIMARY> use surryhills_db
testReplSet:PRIMARY> db.offices.insert({name: ‘MongoDB’});
testReplSet:PRIMARY> show dbs
testReplSet:PRIMARY> show collections
testReplSet:PRIMARY> db.offices.find().pretty()
Pro Tip :]
Auto Format Query Response
Add
DBQuery.prototype._prettyShell = true;
to your ~/.mongorc.js
Step 3: Connect To A Secondary
$: mongo --port 31001
testReplSet:SECONDARY> db.slaveOk()
testReplSet:SECONDARY> show dbs
testReplSet:SECONDARY> use surryhills_db
testReplSet:SECONDARY> show collections
testReplSet:SECONDARY> db.offices.find()
Step 4: Shutdown Replica Set
$: myReplica.stopSet()
Setup Your Replica Set
Part 1: Run replica members
$: mongod --port 31000 --fork --logpath ./path/to/log --dbpath
/path/to/srv1 --replSet myReplica
$: mongod --port 31001 --fork --logpath ./path/to/log --dbpath
/path/to/srv2 --replSet myReplica
$: mongod --port 31002 --fork --logpath ./path/to/log --dbpath
/path/to/srv3 --replSet myReplica
Setup Your Replica Set
Part 2: Configure the set
$: mongo --port 31000
$: rs.initiate();
$: rs.add(‘my-host:31001’);
$: rs.add(‘my-host:31002’);
Setup Your Replica Set
Part 2: Example Configuration
$: rs.config();
{
"_id" : "myReplica",
"version": 3,
"members" : [
{
"_id" : 0,
"host" : "my-host:31000"
},
{
"_id" : 1,
"host" : "my-host:31001"
},
{
"_id" : 2,
"host" : "my-host:31002"
}
]
}
Auto Connect To Current Primary
$: mongo --host myReplica/my-host:31000,my-host:31001,my-host:31002
Replica Set
Good To Knows
1. Always start your instance with `replSet`
2. You can’t initiate a replica set with more than
one member with existing data on it.
3. If one of set members has data, you must run
rs.initiate() against that member
4. `system.replset` collection on `local` database
of all members contains set configuration.
5. Replica sets can have up to 50 members, 7 voting
6. By default, MongoDB reads from primary
Replica Set Reconfiguration
Add new member
$: mongo --port 31000 # current primary
$: rs.add(‘my-host:31003’) # accepts object as well.
Replica Set Reconfiguration
Remove existing member
$: mongo --port 31000 # current primary
$: rs.remove(‘my-host:31003’)
Replica Set Reconfiguration
Update existing member
$: mongo --port 31000 # current primary
$: config = rs.config()
$: config.members[2].priority = 0;
$: rs.reconfig(config);
How Replication Works
MongoDB keeps a log of every write operations that
primary does in database
Logs are saved in “oplog.rs” collection of “local” db
Secondaries apply changes by reading from oplog.rs and
applieing to their data
Secondaries maintain their own “oplog.rs” as well.
“oplog.rs” Facts
“oplog.rs” is a capped collection which means it can
save limited amount of operation logs.
Bulk operations fill “oplog” quickly. A bulk removal of
1K documents adds 1K documents to “oplog”
It’s important to have a “oplog.rs” to keep logs for
atleast last 24 hrs.
“Stale” Secondary :(
Secondary misses a copy of last operation it applied in
syncing source.
Secondary queries other members to find another
source to continue syncing.
Secondary missed some operations permanently.
Admin must resyncs from scratch.
Heartbeat
Members PING each other every 2 seconds.
Members should
reply within 10 seconds
otherwise they’re counted
as unreachable.
Change in replica set configuration
Current primary steps down
Replica Set Election
When It Happens?
Current primary is unreachable
Majority
The Single Most
Important Fact About
Replica Set
More than half of All Voting Members in the set must be reachable.
You need a majority of voting
members to elect a primary.
A primary can only stay
primary so long as it can reach
a majority of voting members.
Majority
Replica Set with two members.
Three Common
Replica Set
Design Mistakes :(
Even number of members in two data center.
A set with more than one arbiter
Keep a majority of members in preferred
data center and rest in the other one.
Two Common
Replica Set
Design Patterns :)
Keep even number of members in two data
center and a tie breaker in third one.
Meet Arbiter
Budget Friendly Member Of Replica Set
Does not hold data
It’s a lightweight process
Can vote on elections
Start An Arbiter Process
$: mongod --port 31005 --replSet myReplica --
nojournal --smallfiles
Arbiter does not save data, no need for journaling
Don’t waste disk space and use --smallfiles
Add An Arbiter To Replica Set
$: rs.addArb(‘arbiter-host-name:port’);
Arbiter
Good To Know
You can’t change an arbiter to non-arbiter member
If possible, use normal members instead of arbiters
At most you need one arbiter in your set
Do not add an arbiter to a set with odd members
Replica Set
Useful Commands
$: rs.config()
$: rs.reconfig()
$: rs.isMaster()
$: rs.status()
$: rs.stepDown()
Rollback :(
Primary does a write and
goes down before at least
one secondary has a chance
to replicate the write.
Rollback
Main Reasons
Network Partitions
Different write rates between
primary and secondary
Rollback Process
1. Former primary reverts write operations
2. Saves affected documents into /rollback directory
3. Admin browses documents using mongorestore
4. Admin applies changes to the current primary
Durability
Write Concerns And Replica Set
1. Unacknowledged
2. Acknowledged
3. Journaled
4. Replica Acknowledged
5. Custom Writer Concern
Improve Durability
db.runCommand({getLastError:1}) options
Returns the error status of the preceding write
operation on the current connection.
j: boolean
w: number | majority | custom role
wtimeout: number (milliseconds)
fsync: boolean
“wtimeout”
GOOD TO KNOW
MongoDB does not “rollback” or ”
undo modifications” made before
the “wtimeout” interval expired.
Custom Write Concerns
Step 1: Tags
$: config = rs.config()
$: config.members[0].tags = {dc: ‘dc-1’};
$: config.members[3].tags = {dc: ‘dc-2’};
$: rs.reconfig(config);
Custom Write Concerns
Step 2: Custom Last Error Mode
$: config = rs.config()
$: config.settings.lastErrorModes = [{
“each-dc”: {dc: 2}
}];
$: rs.reconfig(config);
Custom Write Concerns
Step 3: Use it!
$: db.runCommand({getLastError:1, w: ‘each-dc’})
Replica Set &
Mixed Storage Engines
You can have both
WiredTiger & MMAPv1
● Due to migration
● Due to different use cases
Keen To Go Further?
Register for M102: MONGODB FOR DBAS course!
Starts tomorrow!
It’s awesome!
https://university.mongodb.com/courses/M102/about
Keen To Go Further?
docs.mongodb.org/manual/replication
MongoDB
The Definite Guide
Stay In Touch
● Follow me on Twitter
@mehdivk
● Add me to your Linkedin network https://au.
linkedin.com/in/valikhani
● Subscribe to my blog
blog.mehdivk.net
● Send me an email
valikhani@sirangweb.com
Thank
You!

More Related Content

What's hot

Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0
Mydbops
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
Norberto Leite
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained
Mydbops
 
RocksDB detail
RocksDB detailRocksDB detail
RocksDB detail
MIJIN AN
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to RedisDvir Volk
 
MongoDB performance
MongoDB performanceMongoDB performance
MongoDB performance
Mydbops
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
Sigit52
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
Puneet Behl
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability Practices
Yoshinori Matsunobu
 
MySQL Data Encryption at Rest
MySQL Data Encryption at RestMySQL Data Encryption at Rest
MySQL Data Encryption at Rest
Mydbops
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster TutorialMongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
Jason Terpko
 
Using all of the high availability options in MariaDB
Using all of the high availability options in MariaDBUsing all of the high availability options in MariaDB
Using all of the high availability options in MariaDB
MariaDB plc
 
MariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAsMariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAs
Federico Razzoli
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
Altinity Ltd
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
Tanu Siwag
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Mike Dirolf
 
MyRocks Deep Dive
MyRocks Deep DiveMyRocks Deep Dive
MyRocks Deep Dive
Yoshinori Matsunobu
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
EXEM
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
NexThoughts Technologies
 
MongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTigerMongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTiger
WiredTiger
 

What's hot (20)

Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0Redo log improvements MYSQL 8.0
Redo log improvements MYSQL 8.0
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained
 
RocksDB detail
RocksDB detailRocksDB detail
RocksDB detail
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
MongoDB performance
MongoDB performanceMongoDB performance
MongoDB performance
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability Practices
 
MySQL Data Encryption at Rest
MySQL Data Encryption at RestMySQL Data Encryption at Rest
MySQL Data Encryption at Rest
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster TutorialMongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
 
Using all of the high availability options in MariaDB
Using all of the high availability options in MariaDBUsing all of the high availability options in MariaDB
Using all of the high availability options in MariaDB
 
MariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAsMariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAs
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MyRocks Deep Dive
MyRocks Deep DiveMyRocks Deep Dive
MyRocks Deep Dive
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
 
MongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTigerMongoDB World 2015 - A Technical Introduction to WiredTiger
MongoDB World 2015 - A Technical Introduction to WiredTiger
 

Similar to MongoDB Database Replication

MongoDB Replication and Sharding
MongoDB Replication and ShardingMongoDB Replication and Sharding
MongoDB Replication and ShardingTharun Srinivasa
 
Getting started with replica set in MongoDB
Getting started with replica set in MongoDBGetting started with replica set in MongoDB
Getting started with replica set in MongoDBKishor Parkhe
 
Practical Replication June-2011
Practical Replication June-2011Practical Replication June-2011
Practical Replication June-2011
Chris Westin
 
Production MongoDB in the Cloud
Production MongoDB in the CloudProduction MongoDB in the Cloud
Production MongoDB in the Cloud
bridgetkromhout
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017
Dave Stokes
 
Setting up mongo replica set
Setting up mongo replica setSetting up mongo replica set
Setting up mongo replica setSudheer Kondla
 
advanced Google file System
advanced Google file Systemadvanced Google file System
advanced Google file System
diptipan
 
Scaling with MongoDB
Scaling with MongoDBScaling with MongoDB
Scaling with MongoDB
MongoDB
 
Armitage – The Ultimate Attack Platform for Metasploit
Armitage – The  Ultimate Attack  Platform for Metasploit Armitage – The  Ultimate Attack  Platform for Metasploit
Armitage – The Ultimate Attack Platform for Metasploit
Ishan Girdhar
 
Evolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesEvolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best Practices
Mydbops
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
Ligaya Turmelle
 
Introduction to MySQL InnoDB Cluster
Introduction to MySQL InnoDB ClusterIntroduction to MySQL InnoDB Cluster
Introduction to MySQL InnoDB Cluster
I Goo Lee
 
Evolution Of MongoDB Replicaset
Evolution Of MongoDB ReplicasetEvolution Of MongoDB Replicaset
Evolution Of MongoDB Replicaset
M Malai
 
Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)
MongoDB
 
MongoDB Replica Sets
MongoDB Replica SetsMongoDB Replica Sets
MongoDB Replica Sets
MongoDB
 
Configuring MongoDB HA Replica Set on AWS EC2
Configuring MongoDB HA Replica Set on AWS EC2Configuring MongoDB HA Replica Set on AWS EC2
Configuring MongoDB HA Replica Set on AWS EC2
ShepHertz
 
Exploring the replication in MongoDB
Exploring the replication in MongoDBExploring the replication in MongoDB
Exploring the replication in MongoDB
Igor Donchovski
 
Python mongo db-training-europython-2011
Python mongo db-training-europython-2011Python mongo db-training-europython-2011
Python mongo db-training-europython-2011
Andreas Jung
 
Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014
Michael Renner
 
Replication, Durability, and Disaster Recovery
Replication, Durability, and Disaster RecoveryReplication, Durability, and Disaster Recovery
Replication, Durability, and Disaster Recovery
Steven Francia
 

Similar to MongoDB Database Replication (20)

MongoDB Replication and Sharding
MongoDB Replication and ShardingMongoDB Replication and Sharding
MongoDB Replication and Sharding
 
Getting started with replica set in MongoDB
Getting started with replica set in MongoDBGetting started with replica set in MongoDB
Getting started with replica set in MongoDB
 
Practical Replication June-2011
Practical Replication June-2011Practical Replication June-2011
Practical Replication June-2011
 
Production MongoDB in the Cloud
Production MongoDB in the CloudProduction MongoDB in the Cloud
Production MongoDB in the Cloud
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017
 
Setting up mongo replica set
Setting up mongo replica setSetting up mongo replica set
Setting up mongo replica set
 
advanced Google file System
advanced Google file Systemadvanced Google file System
advanced Google file System
 
Scaling with MongoDB
Scaling with MongoDBScaling with MongoDB
Scaling with MongoDB
 
Armitage – The Ultimate Attack Platform for Metasploit
Armitage – The  Ultimate Attack  Platform for Metasploit Armitage – The  Ultimate Attack  Platform for Metasploit
Armitage – The Ultimate Attack Platform for Metasploit
 
Evolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesEvolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best Practices
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
Introduction to MySQL InnoDB Cluster
Introduction to MySQL InnoDB ClusterIntroduction to MySQL InnoDB Cluster
Introduction to MySQL InnoDB Cluster
 
Evolution Of MongoDB Replicaset
Evolution Of MongoDB ReplicasetEvolution Of MongoDB Replicaset
Evolution Of MongoDB Replicaset
 
Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)
 
MongoDB Replica Sets
MongoDB Replica SetsMongoDB Replica Sets
MongoDB Replica Sets
 
Configuring MongoDB HA Replica Set on AWS EC2
Configuring MongoDB HA Replica Set on AWS EC2Configuring MongoDB HA Replica Set on AWS EC2
Configuring MongoDB HA Replica Set on AWS EC2
 
Exploring the replication in MongoDB
Exploring the replication in MongoDBExploring the replication in MongoDB
Exploring the replication in MongoDB
 
Python mongo db-training-europython-2011
Python mongo db-training-europython-2011Python mongo db-training-europython-2011
Python mongo db-training-europython-2011
 
Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014
 
Replication, Durability, and Disaster Recovery
Replication, Durability, and Disaster RecoveryReplication, Durability, and Disaster Recovery
Replication, Durability, and Disaster Recovery
 

Recently uploaded

哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.pptPROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
bhadouriyakaku
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
azkamurat
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 

Recently uploaded (20)

哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.pptPROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
PROJECT FORMAT FOR EVS AMITY UNIVERSITY GWALIOR.ppt
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 

MongoDB Database Replication

  • 2. Replication Process of keeping IDENTICAL COPY of your data on different servers. Replication offers Minimum Downtime in case of hardware failure, software failure or maintenance
  • 3. Replication is highly recommended for all databases in production from the very first day.
  • 4. Replica Set MongoDB’s Terminology for Replication A replica set is a group of servers with one primary, the server taking client requests, and multiple secondaries, servers that keep copies of the primary’s data.
  • 5. Give A Go To MongoDB’s Replica Set In 60 Seconds! #ToBeTriedOnlyAtHome
  • 6. Step 1: Setup Replica Set $: mongo --nodb $: myReplica = new ReplSetTest({nodes: 3}); $: myReplica.startSet(); $: myReplica.initiate();
  • 7. Congratulations! 3 member of replica set running on ports 31000, 31001 and 31002
  • 8. Step 2: Connect To Primary $: mongo --port 31000 testReplSet:PRIMARY> use surryhills_db testReplSet:PRIMARY> db.offices.insert({name: ‘MongoDB’}); testReplSet:PRIMARY> show dbs testReplSet:PRIMARY> show collections testReplSet:PRIMARY> db.offices.find().pretty()
  • 9. Pro Tip :] Auto Format Query Response Add DBQuery.prototype._prettyShell = true; to your ~/.mongorc.js
  • 10. Step 3: Connect To A Secondary $: mongo --port 31001 testReplSet:SECONDARY> db.slaveOk() testReplSet:SECONDARY> show dbs testReplSet:SECONDARY> use surryhills_db testReplSet:SECONDARY> show collections testReplSet:SECONDARY> db.offices.find()
  • 11. Step 4: Shutdown Replica Set $: myReplica.stopSet()
  • 12. Setup Your Replica Set Part 1: Run replica members $: mongod --port 31000 --fork --logpath ./path/to/log --dbpath /path/to/srv1 --replSet myReplica $: mongod --port 31001 --fork --logpath ./path/to/log --dbpath /path/to/srv2 --replSet myReplica $: mongod --port 31002 --fork --logpath ./path/to/log --dbpath /path/to/srv3 --replSet myReplica
  • 13. Setup Your Replica Set Part 2: Configure the set $: mongo --port 31000 $: rs.initiate(); $: rs.add(‘my-host:31001’); $: rs.add(‘my-host:31002’);
  • 14. Setup Your Replica Set Part 2: Example Configuration $: rs.config(); { "_id" : "myReplica", "version": 3, "members" : [ { "_id" : 0, "host" : "my-host:31000" }, { "_id" : 1, "host" : "my-host:31001" }, { "_id" : 2, "host" : "my-host:31002" } ] }
  • 15. Auto Connect To Current Primary $: mongo --host myReplica/my-host:31000,my-host:31001,my-host:31002
  • 16. Replica Set Good To Knows 1. Always start your instance with `replSet` 2. You can’t initiate a replica set with more than one member with existing data on it. 3. If one of set members has data, you must run rs.initiate() against that member 4. `system.replset` collection on `local` database of all members contains set configuration. 5. Replica sets can have up to 50 members, 7 voting 6. By default, MongoDB reads from primary
  • 17. Replica Set Reconfiguration Add new member $: mongo --port 31000 # current primary $: rs.add(‘my-host:31003’) # accepts object as well.
  • 18. Replica Set Reconfiguration Remove existing member $: mongo --port 31000 # current primary $: rs.remove(‘my-host:31003’)
  • 19. Replica Set Reconfiguration Update existing member $: mongo --port 31000 # current primary $: config = rs.config() $: config.members[2].priority = 0; $: rs.reconfig(config);
  • 20. How Replication Works MongoDB keeps a log of every write operations that primary does in database Logs are saved in “oplog.rs” collection of “local” db Secondaries apply changes by reading from oplog.rs and applieing to their data Secondaries maintain their own “oplog.rs” as well.
  • 21. “oplog.rs” Facts “oplog.rs” is a capped collection which means it can save limited amount of operation logs. Bulk operations fill “oplog” quickly. A bulk removal of 1K documents adds 1K documents to “oplog” It’s important to have a “oplog.rs” to keep logs for atleast last 24 hrs.
  • 22. “Stale” Secondary :( Secondary misses a copy of last operation it applied in syncing source. Secondary queries other members to find another source to continue syncing. Secondary missed some operations permanently. Admin must resyncs from scratch.
  • 23. Heartbeat Members PING each other every 2 seconds. Members should reply within 10 seconds otherwise they’re counted as unreachable.
  • 24. Change in replica set configuration Current primary steps down Replica Set Election When It Happens? Current primary is unreachable
  • 25. Majority The Single Most Important Fact About Replica Set More than half of All Voting Members in the set must be reachable.
  • 26. You need a majority of voting members to elect a primary. A primary can only stay primary so long as it can reach a majority of voting members. Majority
  • 27. Replica Set with two members. Three Common Replica Set Design Mistakes :( Even number of members in two data center. A set with more than one arbiter
  • 28. Keep a majority of members in preferred data center and rest in the other one. Two Common Replica Set Design Patterns :) Keep even number of members in two data center and a tie breaker in third one.
  • 29. Meet Arbiter Budget Friendly Member Of Replica Set Does not hold data It’s a lightweight process Can vote on elections
  • 30. Start An Arbiter Process $: mongod --port 31005 --replSet myReplica -- nojournal --smallfiles Arbiter does not save data, no need for journaling Don’t waste disk space and use --smallfiles
  • 31. Add An Arbiter To Replica Set $: rs.addArb(‘arbiter-host-name:port’);
  • 32. Arbiter Good To Know You can’t change an arbiter to non-arbiter member If possible, use normal members instead of arbiters At most you need one arbiter in your set Do not add an arbiter to a set with odd members
  • 33. Replica Set Useful Commands $: rs.config() $: rs.reconfig() $: rs.isMaster() $: rs.status() $: rs.stepDown()
  • 34. Rollback :( Primary does a write and goes down before at least one secondary has a chance to replicate the write.
  • 35. Rollback Main Reasons Network Partitions Different write rates between primary and secondary
  • 36. Rollback Process 1. Former primary reverts write operations 2. Saves affected documents into /rollback directory 3. Admin browses documents using mongorestore 4. Admin applies changes to the current primary
  • 37. Durability Write Concerns And Replica Set 1. Unacknowledged 2. Acknowledged 3. Journaled 4. Replica Acknowledged 5. Custom Writer Concern
  • 38. Improve Durability db.runCommand({getLastError:1}) options Returns the error status of the preceding write operation on the current connection. j: boolean w: number | majority | custom role wtimeout: number (milliseconds) fsync: boolean
  • 39. “wtimeout” GOOD TO KNOW MongoDB does not “rollback” or ” undo modifications” made before the “wtimeout” interval expired.
  • 40. Custom Write Concerns Step 1: Tags $: config = rs.config() $: config.members[0].tags = {dc: ‘dc-1’}; $: config.members[3].tags = {dc: ‘dc-2’}; $: rs.reconfig(config);
  • 41. Custom Write Concerns Step 2: Custom Last Error Mode $: config = rs.config() $: config.settings.lastErrorModes = [{ “each-dc”: {dc: 2} }]; $: rs.reconfig(config);
  • 42. Custom Write Concerns Step 3: Use it! $: db.runCommand({getLastError:1, w: ‘each-dc’})
  • 43. Replica Set & Mixed Storage Engines You can have both WiredTiger & MMAPv1 ● Due to migration ● Due to different use cases
  • 44. Keen To Go Further? Register for M102: MONGODB FOR DBAS course! Starts tomorrow! It’s awesome! https://university.mongodb.com/courses/M102/about
  • 45. Keen To Go Further? docs.mongodb.org/manual/replication MongoDB The Definite Guide
  • 46. Stay In Touch ● Follow me on Twitter @mehdivk ● Add me to your Linkedin network https://au. linkedin.com/in/valikhani ● Subscribe to my blog blog.mehdivk.net ● Send me an email valikhani@sirangweb.com