MongoDB: Setting up a Raspberry Pi Cluster

James Judd
Nov 17, 2018 · 3 min read

My self-assigned task for today in the Netsells Hackathon was to set up a MongoDB replica set on three Raspberry Pis. There are two ways to scale using MongoDB, via sharding (splitting your data set), or via replication.

Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database server.

My first step was to get the replica set running virtually, Docker being the obvious choice, this would skip any networking / hardware issues, and just allow me to configure Mongo and create a quick proof-of-concept.

The important part to note in the docker-compose file:

command: --replSet 'rs0'

This starts each member and defines its replica set name.

Then connect to one of the nodes and configure it:

mongo> rs.initiate( ... )

You’ll then be able to check the status of the replica set:

rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-11-17T16:27:18Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.0.1.28:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4215,
"optime" : Timestamp(1542469358, 1),
"optimeDate" : ISODate("2018-11-17T15:42:38Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.0.1.249:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3342,
"optime" : Timestamp(1542469358, 1),
"optimeDate" : ISODate("2018-11-17T15:42:38Z"),
"lastHeartbeat" : ISODate("2018-11-17T16:27:16Z"),
"lastHeartbeatRecv" : ISODate("2018-11-17T16:27:16Z"),
"pingMs" : 2,
"syncingTo" : "10.0.1.28:27017"
},
{
"_id" : 2,
"name" : "10.0.1.247:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3469,
"optime" : Timestamp(1542469358, 1),
"optimeDate" : ISODate("2018-11-17T15:42:38Z"),
"lastHeartbeat" : ISODate("2018-11-17T16:27:18Z"),
"lastHeartbeatRecv" : ISODate("2018-11-17T16:27:16Z"),
"pingMs" : 3,
"syncingTo" : "10.0.1.28:27017"
}
],
"ok" : 1
}

We can see that the two secondary members are connected to the primary. Success!

Getting the Replica Set running on the Raspberry Pis

The Pis are all connected via ethernet to a switch which is connected to the office network.

I ran the following on each of the devices:

sudo apt-get install mongodb-server

Then add the replication set name to the /etc/mongodb.conf file, and removing the restriction to only list on 127.0.0.1:

replSet=rs0# Mongo listens on all IP addresses by default
#bindAddress=127.0.0.1

Restart the service:

sudo service mongodb restart

Then find the IPs of each device (ifconfig | grep inet), modify the the configuration and run rs.initiate( … ) on the primary device.

The final step is to test we can write to the primary, and confirm the data reaches all members, we can do this using write concerns.

Write Concerns

When inserting into a collection, we can specify a write concern, this can be used to give us a guarantee that our data has been acknowledged by members in the set or even guarantee that the data has been written to disk on all members.

{ w: <value>, j: <boolean>, wtimeout: <number> }
  • w: <number>

Requests acknowledgement that the write operation has propagated to the specified number of mongod instances

  • w: “majority”

Requests acknowledgement that write operations have propagated to the majority of voting nodes, including the primary.

  • j: true

If j: true, requests acknowledgement that the mongod instances, as specified in the w: <value>, have written to the on-disk journal. j: true does not by itself guarantee that the write will not be rolled back due to replica set primary failover.

Testing writing to all members

Given the definitions above we can use:

> db.products.insert( 
{ "name": "Samsung", value: 199.99 },
{ writeConcern: { w: 3, j: true} }
)
WriteResult({ "nInserted" : 1 })

Success! ✅

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade