8. 8
MongoDB Architecture
Content
Repo
IoT Sensor
Backend
Ad Service
Customer
Analytics
Archive
MongoDB Query Language (MQL) + Native Drivers
MongoDB Document Data Model
MMAP V1 WT In-Memory ? ?
Supported in MongoDB 3.0 Future Possible Storage Engines
Management
Security
Example Future State
Beta
9. 9
Storage Engine API
• Allows to "plug-in" different storage engines
– Different use cases require different performance characteristics
– mmapv1 is not ideal for all workloads
– More flexibility
• Can mix storage engines on same replica set/sharded cluster
• Opportunity to integrate further ( HDFS, native encrypted, hardware
optimized …)
11. 2 Storage Engines Available
… and more in the making!
MMAPv1 WiredTiger
12. 12
MongoDB Architecture
Content
Repo
IoT Sensor
Backend
Ad Service
Customer
Analytics
Archive
MongoDB Query Language (MQL) + Native Drivers
MongoDB Document Data Model
MMAP V1 WT In-Memory ? ?
Supported in MongoDB 3.0 Future Possible Storage Engines
Management
Security
Example Future State
Beta
15. 15
MMAPv1
• Improved concurrency control
• Great performance on read-heavy workloads
• Data & Indexes memory mapped into virtual address space
• Data access is paged into RAM
• OS evicts using LRU
• More frequently used pages stay in RAM
3.0 Default
18. 18
What is WiredTiger?
• Storage engine company founded by BerkeleyDB alums
• Recently acquired by MongoDB
• Available as a storage engine option in MongoDB 3.0
19. 19
Motivation for WiredTiger
• Take advantage of modern hardware:
– many CPU cores
– lots of RAM
• Minimize contention between threads
– lock-free algorithms, e.g., hazard pointers
– eliminate blocking due to concurrency control
• Hotter cache and more work per I/O
– compact file formats
– compression
35. Cache
Page in Memory
root page
Internal Page
Internal Page
Internal Page
Internal Page
Leaf Page Leaf Page Leaf Page Leaf Page
Doc1Doc0 Doc5Doc2 Doc3 Doc4
Writer
Reader
Standard
pointers
between
tree levels
instead of
traditional
files system
offset
pointers
36. Page in Cache
Disk
Page images
Page images
Page images
on-disk Page
Image
index
Update
on-disk Page
Image
index
Updates
db.collection.insert( {"_id": 123})
Clean
Page
Dirty
Page
Eviction
Thread
37. Page in Cache
Disk
Page images
Page images
Page images
on-disk Page
Image
index
Update
on-disk Page
Image
index
Updates
db.collection.insert( {"_id": 123})
Clean
Page
Dirty
Page
Index is build
during read
38. Page in Cache
Disk
Page images
Page images
Page images
on-disk Page
Image
index
Updates'
db.collection.insert( {"_id": 123})
Dirty
Page
Updates are
held in a skip
list Updates''
db.collection.insert( {"_id": 125})
Updates'''
db.collection.insert( {"_id": 124})
Time
39. Reconciliation
Disk
Page images
Page images
Page images
on-disk Page
Image
index
Updates'
Dirty
Page
Eviction
Thread
Updates''
Updates'''
Reconciliation
Cache full
After X operations
On a checkpoint
40. 40
In-memory performance
• Trees in cache are optimized for in-memory access
• Follow pointers to traverse a tree
– no locking to access pages in cache
• Keep updates separate from clean data
• Do structural changes (eviction, splits) in background threads
41. 41
Takeaways
• Page and tree access is optimized
• Allows concurrent operations – updates skip lists
• Tuning options that you should be aware of:
– storage.wiredTiger.engineConfig.cacheSizeGB
• You can determine the space allocated for your cache
• Makes your deployment and sizing more predictable
https://docs.mongodb.org/manual/reference/configuration-options/#storage.wiredTiger.engineConfig.cacheSizeGB
Wiredtiger cache! MongoDB
uses more memory than just
the Storage Engine needs!
43. 43
What is Concurrency Control?
• Computers have
– multiple CPU cores
– multiple I/O paths
• To make the most of the hardware, software has to execute
multiple operations in parallel
• Concurrency control has to keep data consistent
• Common approaches:
– locking
– keeping multiple versions of data (MVCC)
45. 45
Multiversion Concurrency Control (MVCC)
• Multiple versions of records kept in cache
• Readers see the committed version before the transaction started
– MongoDB “yields” turn large operations into small transactions
• Writers can create new versions concurrent with readers
• Concurrent updates to a single record cause write conflicts
– MongoDB retries with back-off
48. 48
WiredTiger Page IO
Document Data Model
WiredTiger Engine
Transactions
Snapshosts
Page
read/write
WAL
Cache
Schema &
Cursors
Row
Storage
Block
Management
DB Files Journal
on-disk Page
Image
index
Updates'''
Disk
Page
Reconciliation
Page Allocation
Splitting
49. 49
WiredTiger Page IO
Document Data Model
WiredTiger Engine
Transactions
Snapshosts
Page
read/write
WAL
Cache
Schema &
Cursors
Row
Storage
Block
Management
DB Files Journal
on-disk Page
Image
index
Updates'''
Disk
Page'
Reconciliation
Page Allocation
Splitting
Page''
50. 50
WiredTiger Page IO
Document Data Model
WiredTiger Engine
Transactions
Snapshosts
Page
read/write
WAL
Cache
Schema &
Cursors
Row
Storage
Block
Management
DB Files Journal
on-disk Page
Image
index
Updates'''
Disk
Reconciliation
Page Allocation
Splitting
Page
Compression
- snappy (default)
- zlib
- none
51. 51
Compression
• WiredTiger uses snappy compression by default in MongoDB
• Supported compression algorithms:
– snappy [default]: good compression, low overhead
– zlib: better compression, more CPU
– none
• Indexes also use prefix compression
– stays compressed in memory
52. 52
Checksums
• A checksum is stored with every uncompressed page
• Checksums are validated during page read
– detects filesystem corruption, random bitflips
• WiredTiger stores the checksum with the page address (typically
in a parent page)
– extra safety against reading a stale page image
54. 54
Takeaway
• Main feature that impacts the vast majority of MongoDB projects / use cases
• CPU bound
• Different algorithms for different workloads
– Collection level compression tuning
• Smaller Data Faster IO
– Not only improves the disk space footprint
– also IO
– and index traversing
56. 56
Index per Directory
• Use different drives for collections and indexes
– Parallelization of write operations
– MMAPv1 only allows directoryPerDatabase
mongod
Collection A
Index
"name
"
Collection B
Index
"_id"
57. 57
Consistency without Journaling
• MMAPv1 uses write-ahead log (journal) to guarantee consistency
• WT doesn't have this need: no in-place updates
– Write-ahead log committed at checkpoints and with j:true
– Better for insert-heavy workloads
– By default journaling is enabled!
• Replication guarantees the durability
60. 60
What’s next for WiredTiger?
• Tune for (many) more workloads
– avoid stalls during checkpoints with 100GB+ caches
– make capped collections (including oplog) more efficient
• Adding encryption
• More advanced transactional semantics in the storage engine API
61. 61
Updates and Upgrades
• Can not
– Can't copy database files
– Can't just restart w/ same dbpath
• Yes we can!
– Initial sync from replica set works perfectly!
– mongodump/restore
• Rolling upgrade of replica set to WT:
– Shutdown secondary
– Delete dbpath
– Relaunch w/ --storageEngine=wiredTiger
– Wait for resync
– Rollover