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

MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

  • Upload
    mongodb

  • View
    580

  • Download
    3

Embed Size (px)

Citation preview

Page 1: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

Concurrency Control in MongoDB

Kaloian ManassievSenior Engineer at MongoDB

[email protected]@kaloianm

Page 2: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

2

Audience

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

Page 3: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

3

What is concurrency control?

Page 4: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

4

Locking

Page 5: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

5

Data consistency

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

Page 6: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

6

Performance

Page 7: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

7

Motivation

• Understand MongoDB’s concurrency control responsibilities

• Learn how MongoDB achieves document-level locking

Page 8: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

8

Talk outline

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

Page 9: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

9

Collection Collection

Storage Engine API

Page 10: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

10

MongoDB 2.0 (and before)

THETOP LOCK

R WR OK NOW NO NO

Page 11: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

12

MongoDB 2.2

THETOP LOCK + intents

Page 13: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

13

TOP

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

IX IS

X S

Page 14: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

14

TOP

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

IX IS

X S

Page 15: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

15

Why intents?

• Get rid of the global lock?

• Use read (shared) lock?

Page 16: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

16

TOP

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

S S

X S

Page 17: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

17

TOP

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

S S

X

Page 18: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

18

TOP

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

IX S

X

Page 19: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

22

Locks

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

Page 23: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

24

MongoDB 3.0

TOP

Page 25: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

25

Multiple-granularity locking

TOP

Page 26: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

26

Lock Manager

• Ensures the locking protocol is obeyed

• Very low overhead

• Testable

Page 27: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

27

Lock Manager

• Improved fairness

• Support for locking policies

• Lock statistics gathering

Page 28: MongoDB Days Silicon Valley: 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: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

29

Storage Engine API

TOP

Page 30: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

30

Storage Engine API

TOP

Storage Engine API

Page 31: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

31

How does WiredTiger work?

• Supports document concurrency

• Takes intents for the entire hierarchy

Page 32: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

32

TOP

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

IX IX

IX IX

IX IX

WiredTiger MVCC

Page 33: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

33

TOP

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

IX IX

IX X

IX

WiredTiger MVCC

Page 34: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

34

How does MMAP V1 work?

• Supports collection concurrency

• Takes lock on the collection–Instead of intent

Page 35: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

35

TOP

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

IX IX

IX IX

X X

MMAP V1

Page 36: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

36

TOP

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

IX IS

IX IS

X S

MMAP V1

Page 37: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

37

WiredTiger

• Intents: Global, Database, Collection

• Document-level concurrency left to the storage engine

Page 38: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

38

MMAP V1

• Intents: Global, Database

• Locks: Collection

Page 39: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

39

Conclusion

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

–Enabled document-level control

Page 40: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

40

Conclusion

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

Page 41: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

QUESTIONS?

Page 42: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

#MDBDaysmongodb.com

Get your technical questions answered

In the foyer, 10:00 - 5:00By appointment only – register in person

Page 43: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

Tell me how I did today on Guidebook and enter for a chance to win one of these

How to do it: Download the Guidebook App

Search for MongoDB Silicon Valley Submit session feedback

Page 44: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

EXTRA SLIDES

Page 45: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

45

Page 46: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

46

Page 47: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

47

Page 48: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

48

Page 49: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

49

Page 50: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

50

Page 51: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

51

Page 52: MongoDB Days Silicon Valley: Concurrency Control in MongoDB 3.0+

52