mongodb by tonny

14
@agatestudio Replication and Sharding on MongoDB Tonny K Publishing Agate Studio

Upload: agate-studio

Post on 10-May-2015

1.134 views

Category:

Education


1 download

DESCRIPTION

Presentasi dari Tonny Kusdarwanto, Crew dari Agate Studio dalam event Talent Development Saturday Agate Studio. http://agatestudio.com Talent Development Saturday adalah acara Agate Studio crew sharing berbagai topik. Mulai dari Art, Programming, Game Production dan General Business/Management. TDS ini dilakukan tanggal 8 Februari 2014 di Bandung Digital Valley.

TRANSCRIPT

Page 1: MongoDB by Tonny

@agatestudio

Replication and Sharding on MongoDB

Tonny KPublishing

Agate Studio

Page 2: MongoDB by Tonny

@agatestudio

CONTENT

Introduction

Replication

• Replication: Concepts

• Replication: How to

Sharding

• Sharding: Concepts

• Sharding: How to

Page 3: MongoDB by Tonny

@agatestudio

Introduction

• Replication increases data availability

With multiple copies of data on different database servers, replication protects a database from the loss of a single server

• Sharding addresses the challenge of scaling to support large data sets

As the size of the data increases, a single machine may not be sufficient to store the data. With Sharding we can distribute data across multiple servers

Page 4: MongoDB by Tonny

@agatestudio

Replication: Concepts

• Replication is the process of synchronizing data across multiple servers

• You can use replication to increase read capacity

• Provide high availability using automatic failover

Page 5: MongoDB by Tonny

Replication: Concepts• Replica set Members

– Primary: accepts all write operations from clients

– Secondary: maintains a copy of the primary’s data set.

Page 6: MongoDB by Tonny

Replication: Concepts• Automatic Failover

The loss of a primary triggers an election where one of the secondaries becomes the new primary

Page 7: MongoDB by Tonny

@agatestudio

Replication: How to1. Install MongoDB

– # yum install mongo-10gen mongo-10gen-server

2. Configure – port = 27017 – bind_ip = 192.168.56.107– dbpath = /srv/mongodb/ – fork = true – replSet = rs1

3. Start Instances– # mongod --config /etc/mongodb.conf

4. Connect To Instance– $ mongo <host>

5. Initiate and Add Replica set member– rs.initiate()– rs.add("<hostname>:<port>") – rs.conf()

Page 8: MongoDB by Tonny

@agatestudio

Sharding: Concepts

• Sharding is the process of storing data records across multiple machines

• Sharding reduces the amount of data that each server needs to store

• MongoDB distributes data, or shards, at the collection level

• Sharding partitions a collection’s data by the shard key.

Page 9: MongoDB by Tonny

@agatestudio

Sharding: Concepts

• Shard Key

– A shard key is either an indexed field or an indexed compound field that exists in every document in the collection

– MongoDB divides the shard key values into chunks and distributes the chunks evenly across the shards

Page 10: MongoDB by Tonny

Sharding: Concepts

• Data Partitioning– Range Based Sharding

– Hash Based Sharding

Page 11: MongoDB by Tonny

Sharding: Concepts

• Components

– Router

routes the reads and writes from applications to the shards

– Config Server

holds metadata about the cluster

– Shard

holds a subset of a collection’s data

Page 12: MongoDB by Tonny

Sharding: Concepts

• Maintaining a Balanced Data Distribution

Ch

un

k Migratio

n

Chunk Splitting

Page 13: MongoDB by Tonny

@agatestudio

Sharding: How to

1. Create Data directory for each Config Server & start instances– mkdir /data/configdb

– mongod --configsvr --dbpath <path> --port <port>

2. Start Router (mongos instance)– mongos --configdb <config server hostnames>

3. Connect To Mongos Instance

– mongo --host <hostname> --port <port>

4. Add each shard to the cluster– sh.addShard( "mongodb0.example.net:27017" )

5. Enable Sharding for A Database– sh.enableSharding("<database>")

6. Enable Sharding for Collections– sh.shardCollection("<database>.<collection>", shard-key-pattern)

Page 14: MongoDB by Tonny

THANK YOU