100
Sharding with MongoDB Tyler Brock [email protected] @TylerBrock

Sharding with MongoDB -- MongoNYC 2012

Embed Size (px)

Citation preview

Page 1: Sharding with MongoDB -- MongoNYC 2012

Sharding with MongoDB

Tyler [email protected]@TylerBrock

Page 2: Sharding with MongoDB -- MongoNYC 2012

Philosophy

Concepts

Architecture

Mechanics

Page 3: Sharding with MongoDB -- MongoNYC 2012

Philosophy

Page 4: Sharding with MongoDB -- MongoNYC 2012

Philosophy

MongoDB is a database for developers.

Page 5: Sharding with MongoDB -- MongoNYC 2012

Build

Philosophy

Page 6: Sharding with MongoDB -- MongoNYC 2012

BuildScale

Philosophy

Page 7: Sharding with MongoDB -- MongoNYC 2012

How to Draw an Owl

Philosophy

Page 8: Sharding with MongoDB -- MongoNYC 2012

How to Draw an Owl

Philosophy

Page 9: Sharding with MongoDB -- MongoNYC 2012

> db.runCommand({enablesharding: "<dbname>" })

> db.runCommand({ shardcollection: "<namespace>", key: <shardkeypatternobject> })

Draw Two Circles

Philosophy

Page 10: Sharding with MongoDB -- MongoNYC 2012

Concepts

Page 11: Sharding with MongoDB -- MongoNYC 2012

datastore

app

Read/Write

Simple Web Application

Page 12: Sharding with MongoDB -- MongoNYC 2012

What happens when your working set exceeds memory?

Page 13: Sharding with MongoDB -- MongoNYC 2012

What happens if your write load is enormous?

Page 14: Sharding with MongoDB -- MongoNYC 2012

datastore

app

Vertical Scaling

Page 15: Sharding with MongoDB -- MongoNYC 2012

app

Vertical Scaling

datastore

Page 16: Sharding with MongoDB -- MongoNYC 2012

app

Vertical Scaling

datastore

appapp

68 GB RamRaid10 EBS

Page 17: Sharding with MongoDB -- MongoNYC 2012

datastore

app

Vertical Scaling

appapp

128 GB RamRaid10 SSD

Page 18: Sharding with MongoDB -- MongoNYC 2012
Page 19: Sharding with MongoDB -- MongoNYC 2012

app

datastoredatastoredatastore

Horizontal Scaling

60gb

Page 20: Sharding with MongoDB -- MongoNYC 2012

app

datastoredatastore datastore

20gb 20gb 20gb

Horizontal Scaling

Page 21: Sharding with MongoDB -- MongoNYC 2012

Routing Logic

app

datastoredatastore datastore

20gb 20gb 20gb

Horizontal Scaling

metadata

Page 22: Sharding with MongoDB -- MongoNYC 2012

Routing Logic

app

datastoredatastore datastore

20gb 20gb

Horizontal Scaling

metadata

60gb

Page 23: Sharding with MongoDB -- MongoNYC 2012

app

Routing Logic

Balancer

datastoredatastore datastore

20gb 20gb

Horizontal Scaling

metadata

60gb

Page 24: Sharding with MongoDB -- MongoNYC 2012

app

Routing Logic

Balancer

datastoredatastore datastore

Horizontal Scaling

metadata

30gb 30gb 30gb

Page 25: Sharding with MongoDB -- MongoNYC 2012

Architecture

Page 26: Sharding with MongoDB -- MongoNYC 2012

Really is just a mongod (or replica set)Where your data lives

mongod

Shard

Page 27: Sharding with MongoDB -- MongoNYC 2012

Mongod started with --configsvr optionMust have 3 (or 1 in development)Data is commited using 2 phase commit

config

Config Server

Page 28: Sharding with MongoDB -- MongoNYC 2012

mongos

Acts just like shard router / proxyOne or as many as you wantLight weight -- can run on App serversCaches meta-data from config servers

mongos

Page 29: Sharding with MongoDB -- MongoNYC 2012

Routing Logic

Balancingmetadata

datastore datastoredatastore

Page 30: Sharding with MongoDB -- MongoNYC 2012

metadata

datastore

mongos

datastoredatastore

Page 31: Sharding with MongoDB -- MongoNYC 2012

metadata

datastore

mongos

datastoredatastore

app

Page 32: Sharding with MongoDB -- MongoNYC 2012

datastore

mongos

config

datastoredatastore

app

Page 33: Sharding with MongoDB -- MongoNYC 2012

datastore

mongos

config

datastoredatastore

config

config

app

Page 34: Sharding with MongoDB -- MongoNYC 2012

mongos

config

mongod mongodmongod

config

config

app

Page 35: Sharding with MongoDB -- MongoNYC 2012

mongos

config

mongod mongodmongod

mongod mongodmongod

mongod mongodmongod

RS RS RS

config

config

app

Page 36: Sharding with MongoDB -- MongoNYC 2012

mongos

config

mongod mongodmongod

mongod mongodmongod

mongod mongodmongod

RS RS RS

config

config

app

Page 37: Sharding with MongoDB -- MongoNYC 2012

Mechanics

Page 38: Sharding with MongoDB -- MongoNYC 2012

How does MongoDB balance my data?

Page 39: Sharding with MongoDB -- MongoNYC 2012

{ name: “Joe”, email: “[email protected]”,},{ name: “Bob”, email: “[email protected]”,},{ name: “Tyler”, email: “[email protected]”,}

Keys

test.users

Page 40: Sharding with MongoDB -- MongoNYC 2012

> db.runCommand({

})

{ name: “Joe”, email: “[email protected]”,},{ name: “Bob”, email: “[email protected]”,},{ name: “Tyler”, email: “[email protected]”,}

shardcollection: “test.users”,

Keys

key: { email: 1 }

test.users

Page 41: Sharding with MongoDB -- MongoNYC 2012

{ name: “Joe”, email: “[email protected]”,},{ name: “Bob”, email: “[email protected]”,},{ name: “Tyler”, email: “[email protected]”,}

shardcollection: “test.users”,

Keys

key: { email: 1 }

test.users

Page 42: Sharding with MongoDB -- MongoNYC 2012

{ name: “Joe”, email: “[email protected]”,},{ name: “Bob”, email: “[email protected]”,},{ name: “Tyler”, email: “[email protected]”,}

Keys

key: { email: 1 }

test.users

Page 43: Sharding with MongoDB -- MongoNYC 2012

Chunks

-∞ +∞

Page 50: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 51: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 52: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 53: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 54: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Split this big chunk into 2

chunks

Page 55: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 56: Sharding with MongoDB -- MongoNYC 2012

Splitting

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

These chunks have split

Page 57: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 58: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Shard1, move a chunk to

Shard2

Page 59: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 60: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Shard1, move another chunk

to Shard3

Page 61: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 62: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Shard1, move another chunk

to Shard4

Page 63: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 64: Sharding with MongoDB -- MongoNYC 2012

Balancing

config

config

config

mongos

Shard 1 Shard 2 Shard 3 Shard 4

Page 65: Sharding with MongoDB -- MongoNYC 2012

How does MongoDB route my queries?

Page 66: Sharding with MongoDB -- MongoNYC 2012

Routed Request

mongos

shard shard shard

Page 67: Sharding with MongoDB -- MongoNYC 2012

Routed Request1

mongos

shard shard shard

1. Query arrives at Mongos

Page 68: Sharding with MongoDB -- MongoNYC 2012

Routed Request1

2

mongos

shard shard shard

1. Query arrives at Mongos

2. Mongos routes query to a single shard

Page 69: Sharding with MongoDB -- MongoNYC 2012

Routed Request1

2

3

mongos

shard shard shard

1. Query arrives at Mongos

2. Mongos routes query to a single shard

3. Shard returns results of query

Page 70: Sharding with MongoDB -- MongoNYC 2012

Routed Request1

2

3

4

mongos

shard shard shard

1. Query arrives at Mongos

2. Mongos routes query to a single shard

3. Shard returns results of query

4. Results returned to client

Page 71: Sharding with MongoDB -- MongoNYC 2012

Scatter Gather Request

shard shard shard

mongos

Page 72: Sharding with MongoDB -- MongoNYC 2012

Scatter Gather Request1

1. Query arrives at Mongos

shard shard shard

mongos

Page 73: Sharding with MongoDB -- MongoNYC 2012

Scatter Gather Request1

1. Query arrives at Mongos

2 22

shard shard shard

mongos2. Mongos broadcasts queryto all shards

Page 74: Sharding with MongoDB -- MongoNYC 2012

Scatter Gather Request1

1. Query arrives at Mongos

2 22

3 33

shard shard shard

mongos2. Mongos broadcasts queryto all shards

3. Each shard returns resultsfor query

Page 75: Sharding with MongoDB -- MongoNYC 2012

Scatter Gather Request1

41. Query arrives at Mongos

2 22

3 33

shard shard shard

mongos2. Mongos broadcasts queryto all shards

3. Each shard returns resultsfor query

4. Results combined andreturned to client

Page 76: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.

shard shard shard

Page 77: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

shard shard shard

1. Query arrives at Mongos

Page 78: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

22 2

shard shard shard

1. Query arrives at Mongos

2. Mongos broadcasts query to all shards

Page 79: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

22 2

shard shard shard3 3 3

1. Query arrives at Mongos

2. Mongos broadcasts query to all shards

3. Each shard locally sorts results

Page 80: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

22 2

4 44

shard shard shard3 3 3

1. Query arrives at Mongos

2. Mongos broadcasts query to all shards

3. Each shard locally sorts results

4. Results returned to mongos

Page 81: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

5

22 2

4 44

shard shard shard3 3 3

1. Query arrives at Mongos

2. Mongos broadcasts query to all shards

3. Each shard locally sorts results

4. Results returned to mongos

5. Mongos merges sorted results

Page 82: Sharding with MongoDB -- MongoNYC 2012

mongos

Distributed Merge Sort Req.1

6

5

22 2

4 44

shard shard shard3 3 3

1. Query arrives at Mongos

2. Mongos broadcasts query to all shards

3. Each shard locally sorts results

4. Results returned to mongos

5. Mongos merges sorted results

6. Combined results returned to client

Page 83: Sharding with MongoDB -- MongoNYC 2012

Queries

By Shard Key Routed db.users.find({email: “[email protected]”})

Sorted by shard key

Routed in order db.users.find().sort({email:-1})

Find by non shard key

Scatter Gather db.users.find({state:”NY”})

Sorted by non shard key

Distributed merge sort

db.users.find().sort({state:1})

Page 84: Sharding with MongoDB -- MongoNYC 2012

Writes

Inserts Requires shard key db.users.insert({ name: “Bob”, email: “[email protected]”})

Removes Routed db.users.delete({ email: “[email protected]”})

Removes

Scattered db.users.delete({name: “Bob”})

Updates Routed db.users.update( {email: “[email protected]”}, {$set: { state: “NY”}})

Updates

Scattered db.users.update( {state: “CA”}, {$set:{ state: “NY”}} )

Page 85: Sharding with MongoDB -- MongoNYC 2012

How do I choose my shard key?

Page 86: Sharding with MongoDB -- MongoNYC 2012

Choose a field that is common to your queries.

Rule of Thumb

Page 87: Sharding with MongoDB -- MongoNYC 2012

Write Scaling

Writes should be distributed.

Page 88: Sharding with MongoDB -- MongoNYC 2012

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken"}

Bad { time : 1 }

Writes should be distributed

Page 89: Sharding with MongoDB -- MongoNYC 2012

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken"}

Bad { time : 1 }

Better {node:1, application:1, time:1}

Writes should be distributed

Page 90: Sharding with MongoDB -- MongoNYC 2012

Query Isolation & Data Locality

Queries should be routed to one shard.

Page 91: Sharding with MongoDB -- MongoNYC 2012

Bad {msg: 1, node: 1}

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken”}

Queries should be routed to one shard

Page 92: Sharding with MongoDB -- MongoNYC 2012

Better {node: 1, time: 1}

Bad {msg: 1, node: 1}

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken”}

Queries should be routed to one shard

Page 93: Sharding with MongoDB -- MongoNYC 2012

Cardinality

Chunks should be able to split.

Page 94: Sharding with MongoDB -- MongoNYC 2012

Bad {node: 1}

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken"}

Chunks should be able to split

Page 95: Sharding with MongoDB -- MongoNYC 2012

Better {node:1, time:1}

Bad {node: 1}

{ node: "ny153.example.com", application: "apache", time: "2011-01-02T21:21:56Z", level: "ERROR", msg: "something is broken"}

Chunks should be able to split

Page 96: Sharding with MongoDB -- MongoNYC 2012

Configuration

Page 97: Sharding with MongoDB -- MongoNYC 2012

mongodmongodmongod

Bring up mongods or Replica Sets

mongod mongodmongod

mongod mongodmongod

RS RS RS

mongod --shardsvrmongod --replSet --shardsvr

Page 98: Sharding with MongoDB -- MongoNYC 2012

config

mongodmongodmongod

mongod mongodmongod

mongod mongodmongod

RS RS RS

Bring up Config Servers

config

config

mongod --configsvr

Page 99: Sharding with MongoDB -- MongoNYC 2012

config

mongodmongodmongod

mongod mongodmongod

mongod mongodmongod

RS RS RS

Bring up Mongos

config

config

mongos

mongos --configdb <list of configdb uris>

Page 100: Sharding with MongoDB -- MongoNYC 2012

> use admin> db.runCommand({"addShard": <shard uri>})

Connect to Mongos+ Add Shards

Enable Sharding

> db.runCommand( { enablesharding : "<dbname>" } );

> db.runCommand( { shardcollection : "<namespace>", key : <key> });

Shard a Collection