Configure replication
Start on the old instance, otherwise data lost
rs.initiate()
rs.status()
rs.add("PK-MBP:27002")
rs.add("PK-MBP:27003")
rs.status()
db.isMaster()
db.test.find()
db.test.insert({ name: "Peter", city: "Steyr" })
db.test.find()
Slide 18
Slide 18 text
Read from secondaries
$ mongo --port 27002
> db.test.find()
> rs.slaveOk()
> db.test.find()
> db.test.insert({ name: "Dieter", city: "Graz" })
slaveOk only valid for the current connection
Slide 19
Slide 19 text
Failover
Kill primary with [Ctrl]+[C]
Write to new primary
> rs.status()
> db.test.insert({ name: "Dieter", city: "Graz" })
> db.test.find()
Election
Candidate node asks for a vote
Others can veto
Slide 28
Slide 28 text
Election
One yes for one node within 30s
Majority yes elects a new primary
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
Issues
Slide 31
Slide 31 text
CAP
Select Availability or Consistency
Partition-tolerance is a prerequisite
for distributed systems
"The network is reliable":
http://aphyr.com/posts/288-the-network-is-reliable
Slide 32
Slide 32 text
Rollback
Old primary rolls back unreplicated
changes once it rejoins the replica set
Slide 33
Slide 33 text
Rollback file
rollback/ in data folder
File name:
..
.bson
Slide 34
Slide 34 text
Election time
At times 5 to 7 minutes
http://www.tokutek.com/2014/07/explaining-ark-
part-2-how-elections-and-failover-currently-work/
Slide 35
Slide 35 text
Missing synchronization
during election
Old primary sends last changes to a
single node
If not new primary: rollback
Slide 36
Slide 36 text
Remember
Replication is
asynchronous
Slide 37
Slide 37 text
Multiple primaries
Unlikely but possible
Bugs: https://jira.mongodb.org/browse/SERVER-9765
Test script with no replies: https://groups.google.com/
forum/#!topic/mongodb-dev/-mH6BOYyzeI
http://aphyr.com/posts/284-call-me-
maybe-mongodb
05/2013 version 2.4
Up to 42% data lost
Data written to old primary: rollback
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
WriteConcern
Configure durability vs performance
https://github.com/mongodb/mongo-java-driver/blob/
master/src/main/com/mongodb/WriteConcern.java
Slide 42
Slide 42 text
WriteConcern.
UNACKNOWLEDGED
w=0, j=0
Fire and forget
Default until 11/2012
Slide 43
Slide 43 text
No content
Slide 44
Slide 44 text
WriteConcern.
ACKNOWLEDGED
w=1, j=0
Current default
Operation successful in memory
Slide 45
Slide 45 text
WriteConcern.
JOURNALED
w=1, j=1
Operation written to the journal file
Since 1.8, single server durability
Slide 46
Slide 46 text
WriteConcern.FSYNCED
w=1, fsync=true
Operation written to disk
Slide 47
Slide 47 text
WriteConcern.
REPLICA_ACKNOWLEDGED
w=2, j=0
Acknowledged by primary and at least
one secondary
w is the server number
Slide 48
Slide 48 text
WriteConcern.
MAJORITY
w=majority, j=0
Acknowledgement by the majority of
nodes
wtimeout recommended
Slide 49
Slide 49 text
WriteConcern.
MAJORITY
Nearly no data lost, but high overhead
Slide 50
Slide 50 text
Write concern performance
https://blog.serverdensity.com/mongodb-on-google-
compute-engine-tips-and-benchmarks/
3 x 1,000 inserts on GCE
Local 10GB system disk
Dedicated 200GB disk
Dedicated 200GB for data and journal