concurrency control in mongodb 3.0

49
Concurrency Control in MongoDB 3.0 Kaloian Manassiev Senior Engineer at MongoDB [email protected] @kaloianm

Upload: mongodb

Post on 11-Aug-2015

285 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concurrency Control in MongoDB 3.0

Concurrency Control in MongoDB 3.0

Kaloian ManassievSenior Engineer at MongoDB

[email protected]@kaloianm

Page 2: Concurrency Control in MongoDB 3.0

2

Audience

• Operations engineers• Application developers• Third-party storage engine developers• Anybody who is curious

Page 3: Concurrency Control in MongoDB 3.0

3

What is concurrency control?

Page 4: Concurrency Control in MongoDB 3.0

4

What is concurrency control?

• Locking• Data consistency

$100 + $100 = $200 (not $100 )• MVCC

Page 5: Concurrency Control in MongoDB 3.0

5

Collection Collection

Page 6: Concurrency Control in MongoDB 3.0

6

Collection Collection

Storage Engine API

Page 7: Concurrency Control in MongoDB 3.0

7

What you will learn

• Top level concurrency control–Instance, database and collection

• How top level cooperates with the storage engine

Page 8: Concurrency Control in MongoDB 3.0

8

What you will NOT learn

• WiredTiger internals–Separate session on this

Page 9: Concurrency Control in MongoDB 3.0

9

Talk outline

• MongoDB concurrency control• Multiple-granularity locking• WiredTiger vs MMAP V1• Questions

Page 10: Concurrency Control in MongoDB 3.0

10

MongoDB 2.0 (and before)

THETOP LOCK

R WR OK NOW NO NO

Page 11: Concurrency Control in MongoDB 3.0

11

MongoDB 2.2

Lock per database

db1.coll.insert({a:1}) db4.coll.find({c:5})

Page 12: Concurrency Control in MongoDB 3.0

12

MongoDB 2.2

THETOP LOCK

+ intents

Page 13: Concurrency Control in MongoDB 3.0

13

TOP

db1.coll.insert({a:1}) db4.coll.find({c:5})

IX IS

X S

Page 14: Concurrency Control in MongoDB 3.0

14

TOP

db.coll.insert({a:1}) db.coll.find({c:5})

IX IS

X S

Page 15: Concurrency Control in MongoDB 3.0

15

Why intents?

• Get rid of the global lock?

• Use read (shared) lock?

Page 16: Concurrency Control in MongoDB 3.0

16

TOP

db1.coll.insert({a:1}) db4.coll.find({c:5})

S S

X S

Page 17: Concurrency Control in MongoDB 3.0

17

TOP

db1.coll.insert({a:1}) db.fsyncLock({lock:1})

S S

X

Page 18: Concurrency Control in MongoDB 3.0

18

TOP

db1.coll.insert({a:1}) db.fsyncLock({lock:1})

IX S

X

Page 19: Concurrency Control in MongoDB 3.0

19

TOP

db1.coll.insert({a:1}) db.fsyncLock({lock:1})

IX S

XS S S S

Page 20: Concurrency Control in MongoDB 3.0

20

TOP

db.fsyncLock({lock:1})

S

S S S S

db1.coll.find({a:1})

IS

S

Page 21: Concurrency Control in MongoDB 3.0

21

Intents

• “Intention” to access one or more children of an item–Compatible with other intents–Need to acquire lock further down–No overhead

Page 22: Concurrency Control in MongoDB 3.0

22

Locks

• “Lock” an item for particular access–Item being locked–All items below it

Page 23: Concurrency Control in MongoDB 3.0

23

Intents compatibility

IS IX S XIS OK OK OK NOIX OK OK NO NOS OK NO OK NOX NO NO NO NO

Page 24: Concurrency Control in MongoDB 3.0

24

MongoDB 3.0

TOP

Page 25: Concurrency Control in MongoDB 3.0

25

Multiple-granularity locking

TOP

Page 26: Concurrency Control in MongoDB 3.0

26

Lock Manager

• Ensures the locking protocol is obeyed

• Very low overhead

• Testable

Page 27: Concurrency Control in MongoDB 3.0

27

Lock Manager

• Improved fairness

• Support for locking policies

• Lock statistics gathering

Page 28: Concurrency Control in MongoDB 3.0

28

TOP

1

db.coll1.update({a:1}) db.coll1.update({a:4})

IX IX

IX IX

IX IX

X X

Page 29: Concurrency Control in MongoDB 3.0

29

Storage Engine API

TOP

Page 30: Concurrency Control in MongoDB 3.0

30

Storage Engine API

TOP

Storage Engine API

Page 31: Concurrency Control in MongoDB 3.0

31

TOP

db.coll1.update({a:1}) db.coll1.update({a:4})

IX IX

IX IX

IX IX

WiredTiger MVCC

Page 32: Concurrency Control in MongoDB 3.0

32

TOP

db.coll1.update({a:1}) db.coll1.drop()

IX IX

IX X

IX

WiredTiger MVCC

Page 33: Concurrency Control in MongoDB 3.0

33

How does MMAP V1 work?

• Supports collection concurrency

• Takes lock on the collection–Instead of intent

Page 34: Concurrency Control in MongoDB 3.0

34

TOP

db.coll1.update({a:1}) db.coll2.update({a:4})

IX IX

IX IX

X X

MMAP V1

Page 35: Concurrency Control in MongoDB 3.0

35

TOP

db.coll1.update({a:1}) db.coll1.find({a:4})

IX IS

IX IS

X S

MMAP V1

Page 36: Concurrency Control in MongoDB 3.0

36

WiredTiger

• Intents: Global, Database, Collection

• Document-level concurrency left to the storage engine

Page 37: Concurrency Control in MongoDB 3.0

37

MMAP V1

• Intents: Global, Database

• Locks: Collection

Page 38: Concurrency Control in MongoDB 3.0

38

Conclusion

• Storage engines have direct control over concurrency–Completely decoupled from top-level locking

–Enabled document-level control

Page 39: Concurrency Control in MongoDB 3.0

39

Conclusion

• Retrofitted MMAP V1 to use multi-granularity locks–Support for collection-level locking–Got rid of the global lock usage for journaling

Page 40: Concurrency Control in MongoDB 3.0

QUESTIONS?

Page 41: Concurrency Control in MongoDB 3.0

EXTRA SLIDES

Page 42: Concurrency Control in MongoDB 3.0

42

Page 43: Concurrency Control in MongoDB 3.0

43

Page 44: Concurrency Control in MongoDB 3.0

44

Page 45: Concurrency Control in MongoDB 3.0

45

Page 46: Concurrency Control in MongoDB 3.0

46

Page 47: Concurrency Control in MongoDB 3.0

47

Page 48: Concurrency Control in MongoDB 3.0

48

Page 49: Concurrency Control in MongoDB 3.0

49