mongosv 2011 - sharding

32
Sharding Jared Rosoff (@forjared)

Upload: jared-rosoff

Post on 19-May-2015

1.669 views

Category:

Technology


1 download

DESCRIPTION

Sharding talk from MongoSV 2011.

TRANSCRIPT

Page 1: Mongosv 2011 - Sharding

Sharding

Jared Rosoff (@forjared)

Page 2: Mongosv 2011 - Sharding

Overview

• Architecture• How it works• Use Cases

Page 3: Mongosv 2011 - Sharding

SHARDING ARCHITECTURE

Page 4: Mongosv 2011 - Sharding

Architecture

Page 5: Mongosv 2011 - Sharding

mongos

• Shard Router• Acts just like a MongoD• 1 or as many as you

want• Can run on App

Servers• Caches meta-data from

config servers

Page 6: Mongosv 2011 - Sharding

Config Server

• 3 of them• Changes use 2 phase

commit • If any are down, meta

data goes read only • System is online as

long as 1/3 is up

Page 7: Mongosv 2011 - Sharding

HOW IT WORKS

Page 8: Mongosv 2011 - Sharding

Keys

{ name: “Jared”, email: “[email protected]”,}{ name: “Scott”, email: “[email protected]”,}{ name: “Dan”, email: “[email protected]”,}

> db.runCommand( { shardcollection: “test.users”, key: { email: 1 }} )

Page 9: Mongosv 2011 - Sharding

Chunks

-∞ +∞

Page 12: Mongosv 2011 - Sharding

Chunks

-∞ +∞

[email protected]

[email protected]

[email protected]

Split!This is a chunk

This is a chunk

Page 16: Mongosv 2011 - Sharding

ChunksMin Key Max Key Shard

-∞ [email protected] 1

[email protected] [email protected] 1

[email protected] [email protected] 1

[email protected] +∞ 1

• Stored in the config servers• Cached in MongoS • Used to route requests and keep cluster

balanced

Page 17: Mongosv 2011 - Sharding

Balancing

Shard 1 Shard 2 Shard 3 Shard 4

5

9

1

6

10

2

7

11

3

8

12

4

17

21

13

18

22

14

19

23

15

20

24

16

29

33

25

30

34

26

31

35

27

32

36

28

41

45

37

42

46

38

43

47

39

44

48

40

mongos

balancerconfig

config

config

Chunks!

Page 18: Mongosv 2011 - Sharding

Balancingmongos

balancerconfig

config

config

Shard 1 Shard 2 Shard 3 Shard 4

5

9

1

6

10

2

7

11

3

8

12

4

21 22 23 24 33 34 35 36 45 46 47 48

ImbalanceImbalance

Page 19: Mongosv 2011 - Sharding

Balancingmongos

balancer

Move chunk 1 to Shard 2

config

config

config

Shard 1 Shard 2 Shard 3 Shard 4

5

9

1

6

10

2

7

11

3

8

12

4

21 22 23 24 33 34 35 36 45 46 47 48

Page 20: Mongosv 2011 - Sharding

Balancingmongos

balancerconfig

config

config

Shard 1 Shard 2 Shard 3 Shard 4

5

9

6

10

2

7

11

3

8

12

4

21 22 23 24 33 34 35 36 45 46 47 48

1

Page 21: Mongosv 2011 - Sharding

Balancingmongos

balancer

Chunk 1 now lives on Shard 2

config

config

config

Shard 1 Shard 2 Shard 3 Shard 4

5

9

16

10

2

7

11

3

8

12

4

21 22 23 24 33 34 35 36 45 46 47 48

Page 22: Mongosv 2011 - Sharding

ROUTING

Page 23: Mongosv 2011 - Sharding

Routed Request

mongos

Shard 1 Shard 2 Shard 3

1

2

3

41. Query arrives at

MongoS2. MongoS routes query

to a single shard3. Shard returns results

of query4. Results returned to

client

Page 24: Mongosv 2011 - Sharding

Scatter Gather

mongos

Shard 1 Shard 2 Shard 3

1

4 1. Query arrives at MongoS

2. MongoS broadcasts query to all shards

3. Each shard returns results for query

4. Results combined and returned to client2 2

33

2

3

Page 25: Mongosv 2011 - Sharding

Distributed Merge Sort

mongos

Shard 1 Shard 2 Shard 3

1

3

6 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 merge sorts individual results

6. Combined sorted result returned to client

2 2

3 3

4 4

5

2

4

Page 26: Mongosv 2011 - Sharding

Writes

Inserts Requires shard key

db.users.insert({ name: “Jared”, email: “[email protected]”})

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

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

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

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

Page 27: Mongosv 2011 - Sharding

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:”CA”})

Sorted by non shard key

Distributed merge sort

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

Page 28: Mongosv 2011 - Sharding

EXAMPLES

Page 29: Mongosv 2011 - Sharding

User Profiles{ name: “Jared”, email: “[email protected]”, addresses: [ {state: “CA”} ]}

• Shard by email• Lookup by email hits

1 node • Index on

{“addresses.state”:1}

Page 30: Mongosv 2011 - Sharding

Activity Stream{ user_id: “[email protected]”, event_id: “Logged in”, data: “…”}

• Shard by user_id• Looking up a stream

hits 1 node• Writing is evenly

distributed• Index on {“event_id”:1}

for deletes

Page 31: Mongosv 2011 - Sharding

Photos{ photo_id: ???, data: BinData(…)}

• What’s the right key? – Auto Increment?– MD5( data )– Now() + MD5(data)– Month() + MD5(data)

Page 32: Mongosv 2011 - Sharding