Professional Documents
Culture Documents
Alvin Richards
alvin@10gen.com
Agenda
• Vertical Scaling
• Horizontal Scaling with MongoDB
• Schema & Index design
• Auto Sharding
• Replication
Scaling
• Operations/sec go up
• Storage needs go up
• Capacity
• IOPs
• Complexity goes up
• Caching
How do you scale now?
throughput
MongoDB Scaling - Single Node
read
node_a1
write
Read scaling - add Replicas
read
node_b1
node_a1
write
Read scaling - add Replicas
read
node_c1
node_b1
node_a1
write
Write scaling - Sharding
read
shard1
node_c1
node_b1
node_a1
write
Write scaling - add Shards
read
shard1 shard2
node_c1 node_c2
node_b1 node_b2
node_a1 node_a2
write
Write scaling - add Shards
read
write
Scaling with MongoDB
Entire index
in ram
Right-Balanced Index Access
Small portion
in ram
What is Sharding
• Ad-hoc partitioning
• Consistent hashing
• Amazon Dynamo
• Range based partitioning
• Google BigTable
• Yahoo! PNUTS
• MongoDB
-∞ +∞
-∞ +∞
-∞ 40 41 +∞
•Data in inserted
•Ranges are split into more “chunks”
How MongoDB Sharding works
-∞ +∞
-∞ 40 41 +∞
41 50 51 +∞
•More Data in inserted
•Ranges are split into more“chunks”
How MongoDB Sharding works
-∞ +∞
-∞ 40 41 +∞
41 50 51 +∞
51 60 61 +∞
How MongoDB Sharding works
-∞ +∞
-∞ 40 41 +∞
41 50 51 +∞
51 60 61 +∞
How MongoDB Sharding works
shard1
-∞ 40
41 50
51 60
61 +∞
How MongoDB Sharding works
-∞ 40
41 50
51 60
61 +∞
How MongoDB Sharding works
shard1
-∞ 40
41 50
51 60
61 +∞
How MongoDB Sharding works
shard1 shard2
-∞ 40
41 50
51 60
61 +∞
How MongoDB Sharding works
•Good
:
{server:1}
• All data for one server is in a single chunk
• Chunk cannot be split any smaller
•Better
:
{server:1,time:1}
‣
Chunk
can
be
split
by
millisecond
Sharding Key Examples
{
server
:
"ny153.example.com"
,
application
:
"apache"
,
time
:
"2011-‐01-‐02T21:21:56.249Z"
,
level
:
"ERROR"
,
msg
:
"something
is
broken"
}
•Better
:
{server:1,application:1,time:1}
‣More key values to enable writes to all shards
Sharding Features
• Variations:
•Master / slave
•Replica Sets
Replica Set features
• A cluster of N servers
• Any (one) node can be primary
• Consensus election of primary
• Automatic failover
• Automatic recovery
• All writes to primary
• Reads can be to primary (default) or a secondary
How MongoDB Replication works
Member
1
Member
3
Member 2
Member
1
Member
3
Member
2
PRIMARY
Member
2
DOWN
Member
1
Member
3
PRIMARY
Member
2
DOWN
Member
1
Member
3
PRIMARY
Member
2
RECOVERING
•Automatic recovery
How MongoDB Replication works
Member
1
Member
3
PRIMARY
Member 2
slaveOk()
- driver will send read requests to Secondaries
- driver will always send writes to Primary
Java examples
-‐
DB.slaveOk()
-‐
Collection.slaveOk()
-‐
find(q).addOption(Bytes.QUERYOPTION_SLAVEOK);
Creating a Replica Set
>
cfg
=
{
...
_id
:
"acme_a",
...
members
:
[
...
{
_id
:
0,
host
:
"sf1.acme.com"
},
...
{
_id
:
1,
host
:
"sf2.acme.com"
},
...
{
_id
:
2,
host
:
"sf3.acme.com"
}
]
}
>
use
admin
>
db.runCommand({replSetInitiate:cfg})
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
Replication features
•Sharding
• Automatically scale writes
•Replication
•Automatically scale reads
download at mongodb.org