Slide 1

Slide 1 text

a year with a talk by Armin '@mitsuhiko' Ronacher for PyGrunn 2013 mongoDB

Slide 2

Slide 2 text

That's me. I do Computers. Currently at Fireteam / Splash Damage. We do Internet for Pointy Shooty Games.

Slide 3

Slide 3 text

I don't like it let's not beat around the bush :(

Slide 4

Slide 4 text

but we're not all so negative

Slide 5

Slide 5 text

“MongoDB is a pretty okay data store” Jared Hefty (@bridwag)

Slide 6

Slide 6 text

this is not a rant it's our experience in a nutshell we find corner cases draw your own conclusions

Slide 7

Slide 7 text

“MongoDB is like a nuclear reactor: ensure proper working conditions and it's perfectly safe and powerful.” myself on 13th of October 2012

Slide 8

Slide 8 text

What changed?

Slide 9

Slide 9 text

RAD Soldiers Copyright © 2013 WarChest Limited. All Rights Reserved

Slide 10

Slide 10 text

RAD Soldiers

Slide 11

Slide 11 text

RAD Soldiers API calls 21st 24th oh

Slide 12

Slide 12 text

?

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

MongoDB Overview { }

Slide 15

Slide 15 text

WHY? We recently asked the question

Slide 16

Slide 16 text

Why the fuck did we pick MongoDB?

Slide 17

Slide 17 text

Why the fuck did we pick MongoDB? schemaless

Slide 18

Slide 18 text

Why the fuck did we pick MongoDB? schemaless scalable

Slide 19

Slide 19 text

Why the fuck did we pick MongoDB? schemaless scalable simple

Slide 20

Slide 20 text

schemaless scalable simple json records

Slide 21

Slide 21 text

schemaless scalable simple json records auto sharding

Slide 22

Slide 22 text

schemaless scalable simple json records auto sharding think in records

Slide 23

Slide 23 text

schemaless is wrong mongodb's sharding is annoying thinking in records is hard trololol: two-phase commit

Slide 24

Slide 24 text

mongod mongoc mongos

Slide 25

Slide 25 text

mongod mongoc mongos mongods

Slide 26

Slide 26 text

mongod mongoc mongos mongods mongocs

Slide 27

Slide 27 text

mongod mongoc mongos mongods mongocs mongoses

Slide 28

Slide 28 text

mongod mongoc mongos mongods mongocs mongoses stores data

Slide 29

Slide 29 text

mongod mongoc mongos mongods mongocs mongoses stores data says what's where

Slide 30

Slide 30 text

mongod mongoc mongos mongods mongocs mongoses stores data says what's where routes and merges

Slide 31

Slide 31 text

Many Moving Parts mongod mongoc mongos

Slide 32

Slide 32 text

We Fail { }

Slide 33

Slide 33 text

workers on m1.small most of the time in IO wait no need for more CPU

Slide 34

Slide 34 text

oh really?

Slide 35

Slide 35 text

worker setup nginx uwsgi mongos mongod

Slide 36

Slide 36 text

worker setup nginx uwsgi mongos mongod uwsgi uwsgi

Slide 37

Slide 37 text

worker setup nginx uwsgi mongos mongod This

Slide 38

Slide 38 text

T1 waits for IO T2 uses CPU

Slide 39

Slide 39 text

worker: mongos, give me data mongos: mongod, give me data … mongos: worker, here is your data worker: finally! mongos, now give me more data context

Slide 40

Slide 40 text

m1.medium: machines with 2 CPUs* worker and mongos active at the same time what a novel idea *

Slide 41

Slide 41 text

MOAR

Slide 42

Slide 42 text

CPU Changes mean

Slide 43

Slide 43 text

EBS it's pretty bad

Slide 44

Slide 44 text

Breaking your Instance 101 $ dd if=/dev/random of=/var/cache/hah bs=4096 count=1024

Slide 45

Slide 45 text

MongoDB's Execution Fails { }

Slide 46

Slide 46 text

No transactions Document-level Operations No state transparent

Slide 47

Slide 47 text

NO!

Slide 48

Slide 48 text

Expectation • mongos fans out and proxies • if mongos loses connection worker is good • voluntary primary election is transparent for worker

Slide 49

Slide 49 text

Actual Result • mongos fans out • if mongos loses connection it terminates both sides • voluntary primary election kills all connections well;

Slide 50

Slide 50 text

Tail-able Cursors getLastError() MongoDB is Stateful

Slide 51

Slide 51 text

SIGSEGV

Slide 52

Slide 52 text

Replica Set Annoyances 1. Add Hidden Secondary 2. Witness it synchronizing 3. Take an existing secondary out 4. Actually unregister the secondary 5. Watch the whole cluster re-elect the same primary and kill all active connections

Slide 53

Slide 53 text

Breaking your Cluster 101 • add new primary • remove old primary • don't shutdown old primary • network partitions and one of them overrides the config of the other in the mongoc

Slide 54

Slide 54 text

MongoDB's Design Fails { }

Slide 55

Slide 55 text

Schemaless

Slide 56

Slide 56 text

Schema vs Schema-less is just a different version of dynamic typing vs. static typing

Slide 57

Slide 57 text

static typing with an escape hatch to dynamic typing wins Ever since C# and TypeScript:

Slide 58

Slide 58 text

we built an ADT based type system anyways from fireline.schema import types username = types.String() profile = types.Dynamic() x = username.convert('mitsuhiko') y = profile.convert({'__binary': 'deadbeaf'})

Slide 59

Slide 59 text

GetLastError()

Slide 60

Slide 60 text

write oddity write request mongodb GetLastError() mongodb why do I need an extra network roundtrip?

Slide 61

Slide 61 text

performance fun import os from pymongo import Connection safe = os.environ.get('MONGO_SAFE') == '1' con = Connection() db = con['wtfmongo'] coll = db['test'] coll.remove() for x in xrange(50000): coll.insert({'foo': 'bar'}, safe=safe)

Slide 62

Slide 62 text

Disappointing $ MONGO_SAFE=0 time python test.py 1.92 real 1.37 user 0.27 sys $ MONGO_SAFE=1 time python test.py 5.57 real 2.50 user 0.62 sys

Slide 63

Slide 63 text

Disappointing $ MONGO_SAFE=0 time python test.py 1.92 real 1.37 user 0.27 sys $ MONGO_SAFE=1 time python test.py 5.57 real 2.50 user 0.62 sys And

Slide 64

Slide 64 text

that would not be a problem if safe mode was fast. As it stands currently safe mode is slower than Postgres

Slide 65

Slide 65 text

Lack of Joins (the

Slide 66

Slide 66 text

They will happen 1. Before we had joins, we did not have joins 2. not having joins is not a feature 3. I see people joining in their code by hand. Inefficient

Slide 67

Slide 67 text

RethinkDB has Distributed Joins :-) r \ .table('marvel') \ .inner_join(r.table('dc'), lambda m, dc: m['strength'] < dc['strength']) \ .run(conn)

Slide 68

Slide 68 text

MongoDB does not have Map-Reduce (that shitty JavaScript map-reduce thing does not count)

Slide 69

Slide 69 text

Inconsistent Queries (and

Slide 70

Slide 70 text

Oh got why!? db.bios.find({ "awards": {"$elemMatch": { "award": "Turing Award", "year": { "$gt": 1980 } }} }) db.users.find({"username": "mitsuhiko"})

Slide 71

Slide 71 text

Repeat after me: in-band signalling is wrong!

Slide 72

Slide 72 text

Aggregation Framework comes with SQL Injection db.zipcodes.aggregate({ "$group": {"_id": "$state", "total_pop": {"$sum": "$pop"}} }, { "$match": {"total_pop": {"$gte": 10 * 1000 * 1000}} })

Slide 73

Slide 73 text

Aggregation Framework comes with SQL Injection db.zipcodes.aggregate({ "$group": {"_id": "$state", "total_pop": {"$sum": "$pop"}} }, { "$match": {"total_pop": {"$gte": 10 * 1000 * 1000}} }) spot

Slide 74

Slide 74 text

No Transactions

Slide 75

Slide 75 text

They are important! 1. You will need them or you have inconsistent data 2. Everybody builds a two-phase commit system 3. You need a process to clean up stale transactions

Slide 76

Slide 76 text

Locks Everywhere

Slide 77

Slide 77 text

MVCC is good for you RethinkDB, Postgres and even MySQL support MVCC

Slide 78

Slide 78 text

Shitty Index Selection 1. MongoDB picks secondary indexes automatically 2. It will also start using sparse indexes 3. It might not give you results back 4. Sometimes forcing ordering makes MongoDB use a compound index

Slide 79

Slide 79 text

Limited Indexes 1. Given a compound index on [a, b] 2. {a: 1, b: 2} and {$and: [{a: 1}, {b: 2}]} are equivalent 3. Only the former picks up the compound index 4. Negations never use indexes 5. {$or: […]} is implemented as two parallel queries, both clauses might need separate indexes.

Slide 80

Slide 80 text

We

Slide 81

Slide 81 text

Other Things of Note { }

Slide 82

Slide 82 text

Making Mongo not Suck (as much) on OS X $ mongod --noprealloc --smallfiles --nojournal run what

Slide 83

Slide 83 text

Windows 1. don't

Slide 84

Slide 84 text

Keys are huge. In our case ⅓ of the Data. Shorten them. (if only MongoDB had something like a … schema?)

Slide 85

Slide 85 text

A MongoDB Cluster needs to boot in a certain Order (Great fun if you have a suspended test infrastructure on Amazon)

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

MongoDB is a pretty good data dump thing

Slide 89

Slide 89 text

MongoDB is a pretty good data dump thing it's not a SQL database

Slide 90

Slide 90 text

MongoDB is a pretty good data dump thing it's not a SQL database but you probably want a SQL database

Slide 91

Slide 91 text

MongoDB is a pretty good data dump thing it's not a SQL database but you probably want a SQL database at least until RethinkDB is ready

Slide 92

Slide 92 text

That's it. Now ask questions. And add me on twitter: @mitsuhiko Slides at lucumr.pocoo.org/talks ?

Slide 93

Slide 93 text

Legal Shenanigans Creative Common Sources for Images: CPU by EssjazNZ: http:/ /www.flickr.com/photos/essjay/4972875711/ Locks by katiejean97: http:/ /www.flickr.com/photos/katiejean97/7036715845/ Money Money Money by Images_of_Money: http:/ /www.flickr.com/photos/59937401@N07/5474168441/ Through any Window by Josep Ma. Rosell: http:/ /www.flickr.com/photos/batega/1354354592/in/photostream/ RAD Soldiers is a Trademark of WarChest Limited. RAD Soldiers Artwork and Logo Copyright © 2013 by WarChest Limited. All Rights Reserved.