[More]
MongoDB Performance
       Tips
           Colin Howe
           @colinhowe
   http://www.colinhowe.co.uk
MongoDB is a Sports Car
MongoDB is a Sports Car




Give it bad conditions and it crawls
3 Tricks for
Minimising
Disk Access
Group Documents Together (1)
Stats system. Data = 3x RAM.

Single document per day
   1.6s to read an entire year

Single document per month
   0.3s to read an entire year
Group Documents Together (2)
Fewer random seeks
  = Faster

Grouped documents
  = Less overhead
  = More in working set

See: http://bit.ly/foursquare-metrics-mongodb
Unusual Indices (1)
Index on metric type then date:
   Inserts started at 10k/sec
   Dropped to 2.5k/sec after 20m inserts

Index on date then metric type:
   Inserts stayed at 10k/sec

No hit on query performance
Unusual Indices (2)
Only inserting to one side of index

Rebalancing hits less of the index
  ⇒ Less to flush to disk
  ⇒ More will be in memory
Pre-Allocate for Locality (1)
Pre-allocate data in read order

Data written in key then date order
  6.6ms to query data for a year

Data written in date then key order
  62ms to query data for a year
Pre-Allocate for Locality (2)
Data exists on disk in the order it is written
  (ignoring resized documents)

Reading 12 random documents from disk
  = 12 seeks

Reading 12 documents written at same time
  = 1 seek + 11 sequential reads
MANDATORY
   NOTICE:

Always benchmark
  your use case
Questions?

[More] MongoDB Performance Tips

  • 1.
    [More] MongoDB Performance Tips Colin Howe @colinhowe http://www.colinhowe.co.uk
  • 2.
    MongoDB is aSports Car
  • 3.
    MongoDB is aSports Car Give it bad conditions and it crawls
  • 4.
  • 5.
    Group Documents Together(1) Stats system. Data = 3x RAM. Single document per day 1.6s to read an entire year Single document per month 0.3s to read an entire year
  • 6.
    Group Documents Together(2) Fewer random seeks = Faster Grouped documents = Less overhead = More in working set See: http://bit.ly/foursquare-metrics-mongodb
  • 7.
    Unusual Indices (1) Indexon metric type then date: Inserts started at 10k/sec Dropped to 2.5k/sec after 20m inserts Index on date then metric type: Inserts stayed at 10k/sec No hit on query performance
  • 8.
    Unusual Indices (2) Onlyinserting to one side of index Rebalancing hits less of the index ⇒ Less to flush to disk ⇒ More will be in memory
  • 9.
    Pre-Allocate for Locality(1) Pre-allocate data in read order Data written in key then date order 6.6ms to query data for a year Data written in date then key order 62ms to query data for a year
  • 10.
    Pre-Allocate for Locality(2) Data exists on disk in the order it is written (ignoring resized documents) Reading 12 random documents from disk = 12 seeks Reading 12 documents written at same time = 1 seek + 11 sequential reads
  • 11.
    MANDATORY NOTICE: Always benchmark your use case
  • 12.