Vous êtes sur la page 1sur 79

Testing 1...

2...
3...
Congratulations!
1. Literally Scale
1. Literally Scale

$3,200 $4,000,000
2. Master-Slave Slave

Read

Master

Writes

Slave
Read
3. Shard
April - July 2009

January - March 2009 August - December 2009


3. Shard

Comments from Bob?


Those are stored here.
3. Shard
Server #3, you're
getting overloaded.

Configuration
3. Shard
Server #3, lockdown.
Split the data,
migrate half to the
new shard.

Router, server #3 has


A-M, server #5 has N-Z.
3. Shard
3. Shard
Shards
3. Shard

Configuration

Routers
Master-Slave
master

slave slave slave


Master-Slave
writes

Appserver Appserver Appserver


Replica Pairs
master

slave
Replica Pairs

slave
Replica Pairs

master
Replica Pairs
slave

master
"Yeah, the RDBMS went down
at 2am. There wasn't any need
to wake the DBA. She can fix it
later today."

"NoSQL is a Horseless Carriage" - Steve Yen


When the worst happens
A Transaction

Insert this.

Okay, got it.

Phew, my data's safe.


A Transaction
A Transaction
Get Paranoid

Insert this.

Okay, got it.

Phew, my data's safe.


Get Paranoid
Get Paranoid

? I have no idea what


you're talking about.
Get Real Paranoid
Write this to disk

I know better than


he does, I'll just
let this sit in a
buffer for a while.
All over it!
Trust No One!

...trust a bunch of ones. Mostly.


Consistency

Hey Twitter, I'm eating


a donut.

Okay
Fred
Consistency

Hey Twitter, I'm eating


a donut.

x 1,000,000

Fred

Fail Whale
Consistency

Hey Twitter, I'm eating


a donut.

Okay
Fred
Consistency

Hey Twitter, I'm eating Nothing new


a donut. from Fred

Okay
Fred

Bob
Consistency

Hey Twitter, I'm eating


a donut.

One sec
Fred
Consistency
Wait a sec, Bob

Bob
Consistency

I'd rather have Twitter up and know


what Fred is doing later.

Bob

...eventual consistency works for your customers


Consistency

I MUST KNOW WHAT FRED IS


DOING RIGHT THIS SECOND!

Bob

...you need "real" consistency


Programming with MongoDB
sudo cpan MongoDB
Connecting
my $conn = MongoDB::Connection->new;
Database
my $conn = MongoDB::Connection->new;
my $db = $conn->selectDB('foo');
Collection
my $conn = MongoDB::Connection->new;
my $db = $conn->get_database('foo');
my $collection =
$db->get_collection('bar');

...I'll be using $c, not $collection, for collections in


code examples because it's shorter.
Documents
$c->insert({name => "Joe", age => 34});

print Dumper($c->find_one({name => "Joe"}));

$VAR1 = {
'_id' => bless ( {
'value' => '4b55f086f8fb65f15e219195'
}, 'MongoDB::OID' ),
'name' => 'Joe',
'age' => 34
};
MongoId
$x = {"foo" => 1, "bar" => 2};
my $id = $collection->insert($x);

print "$id\n";

Result:
2fe3e4d892aa73234c910bed
MongoId
Q: How do we make a unique ID quickly that is guaranteed to
be unique across multiple servers?

A:
2fe3e4d892aa73234c910bed
|------||----||--||----|
ts mac pid inc

Built-in document creation timestamp: $id->get_time


Kingdom of Loathing
{
"description" => "You munch the sausage.
Sausage muncher.",
"type" => "food",
"effect" => {
"adventures" => [3, 10]
}
}
Kingdom of Loathing
{
"title" => "Ninja Snowman Weaponmaster",
"type" => "fight",
"stats" => {
"hit points" => [200, 230],
"offense" => [20, 30]
}
"attacks" => [
{"desc" => "He strangles you mercilessly with a length of
chain...", damage => [30, 40]},
{"desc" => "He backflips over your head clocking you
with...", damage => [40, 50]}
]
"areas" => ["Mt. McLargeHuge"]
}
Kingdom of Loathing - User

{
"username" => "kristina",
"level" => 9,
}
I acquire a pet
$db->users->update(
{'username' => 'kristina'},
{'$set' => {'pet' => {
'name' => 'Trot',
'species' => 'Mosquito',
'weight' => 20}}});
Kingdom of Loathing
{
"username" => "kristina",
"level" => 9,
"pet" => {
"name" => "Trot",
"species" => "Mosquito",
"weight" => 20
}
}
Kingdom of Loathing
$users->find({"pet.weight" => 20});

$users->find({"pet.weight" => {'$gt' => 20}});

$users->find({"pet.weight" => {'$gte' => 20}});

$users->find({"pet.weight" => {'$ne' => 20}});


SQL Mongo
INSERT INTO foo (x) VALUES ("y") $foo->insert({x => "y"})
SELECT * FROM foo $foo->query
UPDATE foo SET x="z" WHERE x="y" $foo->update({x => "y"},
{'$set' => {x => "z"}})
DELETE FROM foo WHERE x="y" $foo->remove({x => "y"})
Analytics
{
"url" => "www.example.com",
"pageviews" => 0
}
Analytics - Increment Pageviews
my $page = $analytics->find_one(
{url => "www.example.com"});

if ($page) {
$page->{'pageviews'}++;
$analytics->save($page);
}
else {
$analytics->insert({
"url" => "www.example.com",
"pageviews" => 1});
}
...that's 1 round trip + 1 update or insert.
Analytics - Upsert

$analytics->update(
{url => "www.example.com"},
{'$inc' => {"pageviews" => 1}},
{upsert => true});

...all in one update!


Logging & Capped Collections
4 GB, 50 MB, 97 documents, etc.
Storing Files - GridFS
Max: 4 MB
Storing Files - GridFS

(More than 4 MB)


Storing Files - GridFS

J J J

chunks
J J J

J J J
_id : J files
Storing Files - GridFS
my $grid = $db->get_gridfs;

$grid->insert($fh,
{permissions => 644,
comment => "vacation pics"});

But wait, there's more!


Also includes MD5 hash and upload date.
Storing Files - GridFS
$file = $grid->find_one($query);

$file->print($fh);
$where
$c->find_one({'$where' =>
'this.y == (this.x + this.z)'});

Will work:
{"x" => 1, "y" => 4, "z" => 3}
{"x" => "hi", "y" => "hibye", "z" => "bye"}

Won’t work:
{"x" => 1, "y" => 1}
JavaScript Functions
$db->eval("function() { return 'hello'; }")

hello

my $func = 'function(x) { return "hello "+ x + "!"; }';

$db->eval($func, ["joe"])

hello, joe!
"Stored Procedures"
$db->system->js->insert({
"_id" => "x",
"value" => 3});

$db->system->js->insert({
"_id" => "y",
"value" => 4});

$db->eval("return x+y");
JS Shell
$ ./mongo
MongoDB shell version 1.3.1-
url: test
connecting to: test
type "help" for help
> print("hello, world")
hello, world
>
JS Shell
$ ./mongo --quiet report.js > "`date`.log"
Thank You!
kristina@mongodb.org

@mongodb, @kchodorow

irc.freenode.net#mongodb
http://groups.google.com/group/mongodb-user/

www.mongodb.org

Vous aimerez peut-être aussi