Skip to main content

MongoDB read replica setup with Ec2 instances

MongoDB setup  with ec2 instances, odd no of ec2 instances should be used which helps in polling when the instances are down. Backup and restore will be easily managed when ebs storage volume is used and mongodb is setup on a independent instance.

Let's say we decide to go with 1 primary and 2 secondary instance as read replicas.
--Install mongodb on all the instances or install on one instance and launch similar instances using aws console.
--Attach ebs volumes for db,log and journal files in each of the instance. Change the path to these folders in /etc/mongodb.conf. Make sure mongod has read and write permission on these folders.

Now restart mongodb server on each of the instance to make sure changes done are working fine.
$sudo /etc/init.d/mongodb restart

Replication Steps:
Copy this line at the bottom of  mongodb.conf file,  replSet = rs0, and specify the same replica set name on same instance.

Now, start each mongod instance

        mongod --config /etc/mongodb.conf

Open mongo shell in any of the instance by hitting $mongo in the terminal and execute the commands below.

$ rs.initiate()   -- Initiates the replica set
$ rs.conf()        -- Displays the current replica set configuration

Add the other members to the replica,
rs.add("ec2-address:27017")  --secondary

For more basic steps, Refer Deploy a Replica Set- MongoDB 

When Mongodb setup on ec2 instances setup on primary and secondary with read replicas.

If any of the replica stops working,

cfg = rs.conf()

"_id" : "rs0",
"version" : 2,
"members" : [
"_id" : 0,
"host" : "ip-10-128-149-4:27017"
"_id" : 1,
"host" : ""

rs0:SECONDARY> cfg.members=[cfg.members[0]]
[ { "_id" : 0, "host" : "ip-10-128-149-4:27017" } ]
rs0:SECONDARY> rs.reconfig(cfg, {force : true})
{ "ok" : 1 }
rs0:SECONDARY> rs.status()

"set" : "rs0",
"date" : ISODate("2013-11-13T21:04:24Z"),
"myState" : 1,
"members" : [
"_id" : 0,
"name" : "ip-10-128-149-4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 15772,
"optime" : Timestamp(1384376658, 1),
"optimeDate" : ISODate("2013-11-13T21:04:18Z"),
"self" : true
"ok" : 1
rs0:PRIMARY> rs.add("")
{ "ok" : 1 }
rs0:PRIMARY> rs.status


Popular posts from this blog

Starting with Ruby Metaprogamming

Metaprogramming is a programming technique in which computer programs have the ability to treat programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running.(wikipedia)
 Using simple code samples let's give basic start to metaprogramming which helps to relate how code base can be simplified.
 Considering the below class.

Now, using some metaprogramming concepts above code can be modified, notice the assign_attributes method used while the o/p in both the examples is same

Adding some more examples, below simple code helps to generate methods on the fly instead of defining in individually

Metaprogramming should be used with caution and not make the code complicated and difficult to read. It should be easy to understand and modular to change in future.

upload images to AWS::S3 in ruby using aws sdk gem

Using gem aws-sdk for a ROR application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application. The s3 keeps a limitation on the number of buckets creation whereas there is no limitation for content inside a bucket. This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public, so that the images uploaded are directly accessible. The input is takes is the image complete path where it is present, folder in which it should be uploaded and user_id for whom it should be uploaded. def save_screenshot_to_s3(image_location, folder_name,user_id) service = => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY) bucket_name = "app-images" if(service.buckets.include?(bucket_name)) bucket = service.buckets[bucket_name] else bucket = service.buckets.create(bucket_name) …