Replica sets in MongoDB

By Pramod Sadalage on October 31, 2010 3:29 PM

Replica sets is a feature of MongoDB for Automatic Failover, in this setup there is a primary server and the rest are secondary servers. If the primary server goes down, the rest of the secondary servers choose a new primary via an election process, each server can also be assigned number of votes, so that you can decide the next primary based on data-center location, machine properties etc, you can also start mongo database processes that act only as election tie-breakers these are known as arbiters, these arbiters will never have data, but just act as agents that break the tie.

All operations are directed at the primary server, the primary server writes the operations to its operation log (also known as opslog), the secondary servers get updates from the primary server. The data is written to the primary server and later replicated to the other secondary servers, so when the write happens at the primary and before the write is replicated to the secondary servers, if the primary server goes down you will loose the data that was written to the primary but never replicated to the secondary servers, you can get around this by specifying how many servers should have the data, before the write is considered good

db.runCommand( { getlasterror : 1 , w : 3 } )
in the above command, you are saying that the write to the database is considered good, only if the write has been propagated to at least 3 servers, off course doing this for every write is going to be very expensive, so you should batch all your writes for a user action and then issue getlasterror

This is how you start the the mongod servers, in a replica set, the can run on any machine any port, as long as they can talk to each other over the network and all of them have the same "--replSet" parameter, in the example below its "prod"

mongod --replSet prod --port 27017 --dbpath /data/node1 
mongod --replSet prod --port 27027 --dbpath /data/node2 
mongod --replSet prod --port 27037 --dbpath /data/node3 

Once the three servers are up, you have to create a replica configuration as shown below, if you use localhost as a server name, then all the members of the replica set have to be on localhost, if the mongo servers are on different servers, you should use distinct machine names and not localhost for anyone of them, once the replica config is defined, you then initiate the replica using the configuration as shown below

replica_config = {_id: 'prod', members: [
                          {_id: 0, host: 'localhost:27017'},
                          {_id: 1, host: 'localhost:27027'},
                          {_id: 2, host: 'localhost:27037'}]}
#Now initiate the replica_config
rs.initiate(replica_config);
When you are connecting to a replica set, you have to connect to atleast one server which is alive, using the ruby driver you can connect to more than one server using the "multi" method, one part you should be careful about is, lets say you define all the servers in the replica set as your connection string, but one of the members of the replica set is down, you will get connection failures, so the best thing to do is give members of the replica set that are up and the drivers will discover the other servers when they come online or go offline. Here is a sample ruby program to find a doc in a loop.
#!/usr/bin/env ruby
require 'mongo'
begin
  @connection = Mongo::Connection.multi([
                                       ['localhost',27017],
                                       ['localhost',27027],
                                       ['localhost',27037]])
  @collection = @connection.db("sales").collection("products")
  product = { "name" => "Refactoring", 
              "code" => "023XX3",
              "type" => "book", 
              "in_stock" => 100}
  @collection.insert(product)
  100.times do
    sleep 0.5
    begin
  	  product = @collection.find_one "code" => "023XX3"
  	  puts "Found Book: "+product["name"]
  	rescue Exception => e
    	puts e.message
    	next
  	end
  end
end
While the ruby program is running, you can kill the current primary and you will see that the program gets connection exceptions, while the replica set is figuring out the next master, once the next master is picked, the program starts going about its way finding the same data from the newly elected primary, here is a screen cast of the replica sets in action. Replica Sets screencast

Categories:

  • MongoDB,
  • NoSQL

Tags:

  • failover,
  • mongodb,
  • replica sets
  • spacer Atom feed
  • spacer
  • spacer

My Books

spacer spacer spacer

Reading list

Switch: How to Change Things When Change Is Hard
The Power of Now
A New Earth
SQL Anti patterns
The 4-Hour Workweek
Loving Our Kids On Purpose
Tuesdays with Morrie
The Secret
Presentation Zen
Programming Ruby 1.9
Release It!
The Toyota Way
Agile Data Warehousing
Outliers: The Story of Success
India After Gandhi
Why Does Software Cost So Much?:
It Happened in India
Oracle Tuning: The Definitive Reference
Ship it!
Service-Oriented Architecture
My Job Went to India
Let My People Go Surfing
Practices of an Agile Developer
Rails Recipes
Programming Ruby
The Art of SQL
Execution: The Discipline of Getting Things Done
The Monk Who Sold His Ferrari
The Greatness Guide
Who Will Cry When You Die?
Zen and the Art of Motorcycle Maintenance
Freakonomics
Head First Design Patterns (Head First)
Agile Database Techniques
Hibernate in Action (In Action series)
The World Is Flat
Guns, Germs, and Steel

Search

Tag Cloud

  • design
  • mongodb
  • agile dba
  • BI
  • database
  • nosql
  • oracle
  • Agile Data
  • automation
  • books
  • constraints
  • Data
  • data conversion
  • database link
  • dba
  • development
  • documentdb
  • EDW
  • failover
  • java
Tweets by @pramodsadalage

Categories

  • Agile DBA (7)
    • Best Practices (12)
    • Improving Design (6)
  • BI (2)
  • Broadcast (13)
  • JDBC (1)
  • Learning (11)
    • Mistakes I Learnt from (2)
  • MongoDB (4)
  • NoSQL (2)
  • Open Source (2)
  • Oracle (6)
  • Ruby (2)

All Content Copyright © 2012 Pramod Sadalage. All Rights Reserved.
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.