MongoDB Sharded Cluster - How to Vinodh Krishnaswamy ... · 3 © 2019 Percona Agenda Sharded...

Post on 19-Jul-2020

5 views 0 download

Transcript of MongoDB Sharded Cluster - How to Vinodh Krishnaswamy ... · 3 © 2019 Percona Agenda Sharded...

© 2019 Percona1

Vinodh Krishnaswamy

MongoDB Sharded Cluster - How to design your topology

Support Engineer - MongoDB & MySQL Percona

© 2019 Percona2

◆ Started as MySQL DB DBA

◆ Support for MongoDB

◆ Trainer

◆ Scripting, Reading, Driving

◆ “Guruji” - who shares knowledge◆ Now here I am - Perconian!!!

Who am I

© 2019 Percona3

Agenda▪ Sharded Cluster - components

▪ Ranged Sharding

▪ Hashed Sharding

▪ Zones / Tags

© 2019 Percona4

Agenda

▪ HA

▪ ReplicaSet - Key points

▪ Topology

▪ Q&A

© 2019 Percona5

Sharded Cluster

© 2019 Percona6

Sharded Cluster▪ Horizontal Scaling

•Data across multiple nodes

• Reduce stress for larger working set

• Ability to add more servers

© 2019 Percona7

Architecture

mongos

© 2019 Percona8

Sharded Cluster

▪ Shard Key

▪ Chunks

▪ Balancer - Chunk distribution

© 2019 Percona9

POINTS TO NOTE

TARGET vs BROADCAST

© 2019 Percona10

Primary Shard

A

A

© 2019 Percona11

Primary Shardsh.status()

© 2019 Percona12

Primary Shard▪ Routing - sharded vs unsharded

▪ Keep at right shard

db.adminCommand( { movePrimary:

<databaseName>, to: <newPrimaryShard> } )

© 2019 Percona13

Ranged Sharding

© 2019 Percona14

Strategy

▪ Ranged Sharding

© 2019 Percona15

Strategy

▪ Ranged Sharding

sh.shardCollection( "database.collection", { <shard

key> } )

Example:

sh.shardCollection( "vinodh.testData", { empNo: 1 } )

© 2019 Percona16

Monotonically Changing Shard Key

© 2019 Percona17

© 2019 Percona18

© 2019 Percona19

Ranged Sharding▪ Contiguous ranges determined by Shard Key

▪ Default method

▪ Efficient when

• High cardinality

• Low Shard Key frequency

• Non-Monotonically Changing Shard key

© 2019 Percona20

Hashed Sharding

© 2019 Percona21

Strategy

▪ Hashed Sharding

© 2019 Percona22

Strategy▪ Hashed Sharding uses hashed index

sh.shardCollection( "database.collection", {

<field> : “hashed” } )

Example:

sh.shardCollection( "vinodh.testData",

{ UUIDfield: “hashed” } )

© 2019 Percona23

Monotonically Changing Shard Key - Hash

© 2019 Percona24

Hashed Sharding▪ Provides equal distribution

▪ Target vs Broadcast operations

▪ Ideal for fields that changes monotonically like

ObjectID, timestamp etc

© 2019 Percona25

Zones

© 2019 Percona26

Why Zones?

▪ Isolate data set

▪ Geographically closest

▪ Route data to desired Shard h/w

▪ Distribute Writes based on DC

© 2019 Percona27

How to...▪ First create tagRange with Shards

sh.addShardTag("shard01", "tag1")

sh.addShardTag("shard02", "tag2")

▪ Assign tagRanges to collections

sh.addTagRange( "<database>.<collection>",

{ "gender" : "Male", "userid" : MinKey },

{ "gender" : "Male", "userid" : MaxKey },

"tag1")

© 2019 Percona28

Zone

© 2019 Percona29

Zone

© 2019 Percona30

HA overview

© 2019 Percona31

HA

• Instance down

• Loss of DC

•Network Partition

•Backup

•Etc.

© 2019 Percona32

HA

▪ Sharded Cluster itself is NOT HA

▪ SC + RS = Complete HA

▪ Failover Scenarios - Do your Exercise!

© 2019 Percona33

ReplicaSet - Key Points

© 2019 Percona34

Replicaset

© 2019 Percona35

Replicaset

© 2019 Percona36

Replicaset - ARBITER

© 2019 Percona37

replicaSet Configuration

▪ Priority

▪ Hidden

▪ Delayed

▪ Arbiter

© 2019 Percona38

© 2019 Percona39

Replicaset - Election+Priority

ELECTION - VOTING & CHOOSING

Priority: 5Priority: 1

Priority: 1 Priority: 5

© 2019 Percona40

Points to Note

▪ Set proper {Priority:n} to elect a member as

Primary

•Avoid setting Priority for all members

▪ Set {writeConcern:majority} to avoid data loss

▪ Set readPreference for reads

© 2019 Percona41

Topology- Application Servers

© 2019 Percona42

Applications and mongos in one server

© 2019 Percona43

mongos/router in each Application servers

© 2019 Percona44

Sharded Cluster in Private network

© 2019 Percona45

Topology- Shards

© 2019 Percona46

Shard instances deployed in dedicated servers design

© 2019 Percona47

Multiple Shard members in each Server + HA design

© 2019 Percona48

Production DC and DR DC + HA design

© 2019 Percona49

Using Zones / Tags + HA design

© 2019 Percona50

Points to Note

© 2019 Percona51

POINTS TO NOTE▪ SHARD KEY - Pain Point!

•Monotonically increased key

•Do your tests!

▪ SHARD EARLY!

▪ ZONES

© 2019 Percona53

Question?vinodh.krishnaswamy@percona.comhttps://in.linkedin.com/in/vinodhkrish

© 2019 Percona54

THANK YOU