Top Banner
Scaling with mongoDB
62

Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Jun 07, 2018

Download

Documents

dinhque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Scaling with mongoDB

Page 2: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Ross LawleyPython Engineer @ 10genWeb developer since 1999

Passionate about open sourceAgile methodology

email: [email protected]: RossC0

Page 3: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Today's Talk• Scaling• Understanding mongoDB's architecture

• Schema design and usage• Replication• Sharding

Page 4: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Scaling• Operations/sec go up

• Storage needs go up• Capacity• IOPs

• Complexity goes up• Caching

Page 5: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

• Optimization & Tuning• Schema & Index Design• O/S tuning• Hardware configuration

• Vertical scaling• Hardware is expensive• Hard to scale in cloud

How do you scale now?

$$$

throughput

Page 6: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

mongoDB Scaling - Single Node

write

read

node_a1

Page 7: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Read scaling - add Replicas

write

read

node_b1

node_a1

Page 8: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Read scaling - add Replicas

write

read

node_c1

node_b1

node_a1

Page 9: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Write scaling - Sharding

shard1

write

read

node_c1

node_b1

node_a1

Page 10: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Write scaling - add shards

write

read

shard1

node_c1

node_b1

node_a1

shard2

node_c2

node_b2

node_a2

Page 11: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Write scaling - add shards

write

read

shard1

node_c1

node_b1

node_a1

shard2

node_c2

node_b2

node_a2

shard3

node_c3

node_b3

node_a3

Page 12: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Understanding mongoDB's architecture

http://www.flickr.com/photos/tragiclyflawed/867687742

Page 13: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

mongod architecture• mongod memory maps the numbered & .ns files

• Memory mapping makes in-place updates effective

• File page residency decisions left to operating system

Page 14: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

mongod Data Files• Fixed-size extents in data files store records,

indexes

• Records contain documents

• Unused records get placed in free lists

• Indexes are B-Trees

Deleted Record (Size, Offset, Next)

BSON Data

Header (Size, Offset, Next, Prev)

Padding

...

...

Page 15: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Collection 1

Index 1

Page 16: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Collection 1

Index 1

Page 17: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Collection 1

Index 1 This is your virtual memory size

(mapped)

Page 18: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Physical RAM

Collection 1

Index 1

Page 19: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Physical RAM

Collection 1

Index 1

This is your resident

memory size

Page 20: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Physical RAM

DiskCollection 1

Index 1

Page 21: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Physical RAM

Disk

Virtual Address Space 2

Collection 1

Index 1

Page 22: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Virtual Address Space 1

Physical RAM

DiskCollection 1

Index 1

100 ns

10,000,000 ns

=

=

Page 23: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

mongod Concurrency• Readers block writers• A writer blocks everything• Everybody yields periodically• When a new writer queues up, new readers block

• In v2.0 and earlier, this concurrency model is global to the mongod

• In v2.2, this model will be scoped to the database

Page 24: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Architecture Summary• Uses memory mapped files - RAM• Faster disks - more IOPS (SSDs are good!)• CPU usage low

Page 26: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Schema• Data model effects performance• Embedding versus Linking• Roundtrips to database• Disk seek time• Size of data to read & write

• Partial versus full document writes• Partial versus full document reads

Page 27: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Indexes• Index common queries• Do not over index• (A) and (A,B) are equivalent, choose one

Page 28: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Query for {a: 7}

{...} {...} {...} {...} {...} {...} {...} {...} {...} {...} {...}

[-∞, 5) [5, 10) [10, ∞)

[5, 7) [7, 9) [9, 10) [10, ∞) buckets[-∞, 5) buckets

With Index

Without index - Scan

Page 29: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Picking an a Index

db.col.find({x: 10, y: "foo"})

scan

index on x

index on y remember

terminate

Page 30: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Random Index AccessHave to keep entire index in ram

randomemail address hash

Page 31: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Right-Balanced Index AccessOnly have to keep small portion in ram

Time BasedObjectIdAuto Increment

Page 32: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> db.users.ensureIndex({uname: 1, first: 1, last: 1})

> db.users.find( { uname: "RossC0" }, {_id: 0, first: 1, last: 1})

Covered IndexesUse just the index

Page 33: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Schema• Schema and data usage critical for scaling and

performance

• Understand data access patterns

• Use indexes but don't over index

Page 34: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Replication

http://www.flickr.com/photos/10335017@N07/4570943043

Page 35: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Replication• mongoDB replication like MySQL replication• Asynchronous master/slave

• Replica sets• A cluster of N servers• All writes to primary• Reads can be to primary (default) or a secondary• Any (one) node can be primary• Consensus election of primary• Automatic failover• Automatic recovery

Page 36: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

Member 1

Member 2

Member 3

• Set is made up of 2 or more nodes

Page 37: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

• Election establishes the PRIMARY• Data replication from PRIMARY to SECONDARY

Member 1

Member 2

Primary

Member 3

Page 38: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

• PRIMARY may fail• Automatic election of new PRIMARY if majority exists

Member 1

Member 2

DOWN

Member 3

negotiate new master

Page 39: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

• New PRIMARY elected• Replica Set re-established

Member 1

Member 2

DOWN

Member 3

Primary

Page 40: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

• Automatic recovery

Member 1

Member 3

Primary

Member 2Recovering

Page 41: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

How mongoDB Replication works

• Replica Set re-established

Member 1

Member 3

Primary

Member2

Page 42: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> cfg = { _id : "myset", members : [ { _id : 0, host : "germany1.acme.com" }, { _id : 1, host : "germany2.acme.com" }, { _id : 2, host : "germany3.acme.com" } ] }

> use admin> db.runCommand( { replSetInitiate : cfg } )

Creating a Replica Set

Page 43: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Replica Set Member Types

• Normal {priority: 1}• Passive {priority: 0}• Cannot be elected as PRIMARY

• Arbiters• Can vote in an election• Do not hold any data

• Hidden {hidden: True}• Tagging:• {tags: {"dc": "germany", "rack": r23s5}}

Page 44: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Safe writesdb.runCommand({getLastError: 1, w : 1})

• ensure write is synchronous• command returns after primary has written to memory

w: n or w: 'majority'• n is the number of nodes data must be replicated to• driver will always send writes to Primary

w: 'my_tag'• Each member is "tagged" e.g. "allDCs"• Ensure that the write is executed in each tagged "region"

j: true• Ensures changes are flushed to the Journal

Page 45: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Replication features• Reads from Primary are always consistent

• Reads from Secondaries are eventually consistent

• Can be used to scale reads

• Automatic failover if a Primary fails

• Automatic recovery when a node joins the set

Page 46: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Sharding

http://www.flickr.com/photos/60218876@N08/6888405266

Page 47: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

What is Sharding?• Ad-hoc partitioning

• Consistent hashing• Amazon Dynamo

• Range based partitioning• Google BigTable• Yahoo! PNUTS• mongoDB

Page 48: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

mongoDB Sharding• Automatic partitioning and management

• Range based

• Convert to sharded system with no downtime

• Fully consistent

Page 49: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> db.runCommand({addshard: "shard1"});> db.runCommand({shardCollection: "mydb.users", key: {age: 1}})

How mongoDB Sharding works

Range keys from -∞ to +∞  Ranges are stored as "chunks"

-∞  +∞  

Page 50: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> db.users.save({age: 40})

How mongoDB Sharding works

Data in insertedRanges are split into more "chunks"

-∞  +∞  

-∞   40 41 +∞  

Page 51: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> db.users.save({age: 40})> db.users.save({age: 50})

How mongoDB Sharding works

More data insertedRanges are split into more "chunks"

-∞  +∞  

-∞   40 41 +∞  

41 50 51 +∞  

Page 52: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

> db.users.save({age: 40})> db.users.save({age: 50})> db.users.save({age: 60})

How mongoDB Sharding works

-∞  +∞  

-∞   40 41 +∞  

41 50 51 +∞  

61 +∞  51 60

Page 53: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

-∞  +∞  

41 +∞  

51 +∞  

-∞   40

41 50

61 +∞  51 60

> db.users.save({age: 40})> db.users.save({age: 50})> db.users.save({age: 60})

How mongo Sharding works

Page 54: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

-∞   40

41 50

61 +∞  

51 60

shard1

> db.users.save({age: 40})> db.users.save({age: 50})> db.users.save({age: 60})

How mongo Sharding works

Page 55: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

-∞   40

41 50

61 +∞  

51 60

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 56: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

-∞   40

41 50

61 +∞  

51 60

shard1

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 57: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

-∞   40

41 50

61 +∞  

51 60

shard1 shard2 shard3

> db.runCommand({addshard: "shard2"});> db.runCommand({addshard: "shard3"});

How mongoDB Sharding works

Page 58: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Sharding Features• Shard data without no downtime • Automatic balancing as data is written• Commands routed (switched) to correct node• Inserts - must have the Shard Key• Updates - must have the Shard Key• Queries• With Shard Key - routed to nodes• Without Shard Key - scatter gather

• Indexed Queries• With Shard Key - routed in order• Without Shard Key - distributed sort merge

Page 59: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Architecture

Page 60: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Scaling with mongoDB• Schema & Index design• Simplest way to scale

• Replication• Provides High Availabilty• Can be used to automatically scale reads

• Sharding • Automatically scale writes

Page 61: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

Any Questions?

Page 62: Scaling with mongoDB - NETWAYS€¦ · • O/S tuning • Hardware configuration • Vertical scaling ... Scaling with mongoDB • Schema & Index design • Simplest way to scale

@mongodb

conferences, appearances, and meetupshttp://www.10gen.com/events

http://bit.ly/mongofb

Facebook | Twitter | LinkedInhttp://linkd.in/joinmongo

download at mongodb.org

support, training, and this talk brought to you by