MongoDB Memory Management Demystified

38
MongoDB Memory Management Demystified Alon Horev MongoDB World 2014

description

 

Transcript of MongoDB Memory Management Demystified

Page 1: MongoDB Memory Management Demystified

MongoDB Memory

ManagementDemystified

Alon Horev

MongoDB World 2014

Page 2: MongoDB Memory Management Demystified

Meta

Alon Horev

@alonhorev

Page 3: MongoDB Memory Management Demystified
Page 4: MongoDB Memory Management Demystified

Why should you care?

Page 5: MongoDB Memory Management Demystified

MongoDB

Memory Mapped Files

Page Cache

Storage

Page 6: MongoDB Memory Management Demystified

MongoDB

Memory Mapped Files

Page Cache

Storage

Page 7: MongoDB Memory Management Demystified

Storage

RAM SSD

HDD

volatil

epersiste

nt

Page 8: MongoDB Memory Management Demystified

Throughput in MB

Price per GB5.5$ 0.5$ 0.05$

6400 650 1-160

Page 9: MongoDB Memory Management Demystified

Hardware Configuration

Page 10: MongoDB Memory Management Demystified

MongoDB

Memory Mapped Files

Page Cache

Storage

Page 11: MongoDB Memory Management Demystified

Page Cache

Process

Page 12: MongoDB Memory Management Demystified

User Space Kernel Space

Process

read(fd, *buffer, count)

Page CacheSystem

call

Page Cache – Read

Example File

Page 1

Page 2

Page 3

File descriptorAt 2,000

End at 10,000

Page in

cache?

offset+count pages

Read from disk and store in cache

Read from cache and copy to

*buffer

No

Yes

Page 13: MongoDB Memory Management Demystified

Disk

Page Cache

Process

write(fd, *buffer, count)

Systemcall

Page Cache – Write

Update pageAnd mark as

dirty

After X secondsflush to disk

Page 14: MongoDB Memory Management Demystified

Page Reclamation

LRU – Least Recently Used

Page 15: MongoDB Memory Management Demystified

$ free -g total used free cachedMem: 64 61 3 55-/+ buffers/cache: 5 58Swap: 16 0 16

Free

Page 16: MongoDB Memory Management Demystified

MongoDB

Memory Mapped Files

Page Cache

Storage

Page 17: MongoDB Memory Management Demystified

Memory Mapped Files

Process File

2000

1000

4000

5000

Page 18: MongoDB Memory Management Demystified

mmapProcess

B File

Process A

Page 19: MongoDB Memory Management Demystified

MongoDB

Memory Mapped Files

Page Cache

Storage

Page 20: MongoDB Memory Management Demystified

MongoDBMaps everything: documents, indexes, journal

Running top:

Page 21: MongoDB Memory Management Demystified

ChallengesNo control over what is saved in memory

Warm-up

Expensive queries

Page 22: MongoDB Memory Management Demystified

Mitigation PlanProtect MongoDB with an API

Enforce index usage

Pass a query timeout (from 2.6)

Example of a simple API

def find_samples(start_time, end_time):return samples.find({‘time’: {‘$gte’:

start_time, ‘$lt’: end_time}})

Page 23: MongoDB Memory Management Demystified

ChallengesLack of Inter-process prioritization

Mitigation: isolate mongo

Estimate required memory

How big is the working set?

Page 24: MongoDB Memory Management Demystified

Working SetContains:

Documents

Indexes

Padding (!)

Doc 1 Doc 2 Doc 3

0 4k

Padding

Page 25: MongoDB Memory Management Demystified

Working Set AnalysisPlanning

Monitoring

Page 26: MongoDB Memory Management Demystified

Planning

db.samples.stats()

dataSize

indexSizes

ColdWarmHot

Month

Last 2 weeks

1 week

1 week

Page 27: MongoDB Memory Management Demystified

MonitoringOnline

top, iostat

db.currentOp(), mongostat, mongomem

Offline

Profiling collection

MMS/Graphite

Page 28: MongoDB Memory Management Demystified

MongomemTop collections:

local.oplog.rs 11218 / 49865 MB (22.496883%) [25 extents]

samples.quarter 3661 / 219714 MB (1.666450%) [128 extents]

samples.hour 1629 / 10921 MB (14.924107%) [26 extents]

Total resident pages: 16508 / 280500 MB (5.885%)

Page 29: MongoDB Memory Management Demystified

MongomemProcedure:

Stop the database

Clear the page cache:

echo 1 > /proc/sys/vm/drop_caches

Start the database

Run queries that should return fast

Run mongomem!

Page 30: MongoDB Memory Management Demystified

What to monitor?Thrashing

Page faults

Disk utilization

Symptoms

Queued queries

High locking ratios

Page 31: MongoDB Memory Management Demystified

iostat

$ iostat –xm 1 /dev/sdaDevice: r/s w/s rMB/s wMB/s %utilsda 570.00 0.00 31.28 0.00 100.00

Page 32: MongoDB Memory Management Demystified

mongostatUses db.serverStatus()

Metrics per second:

Page faults

Queued reads (qr)

Page 33: MongoDB Memory Management Demystified

Offline monitoringMMS/Graphite

Mandatory!

Page 34: MongoDB Memory Management Demystified
Page 35: MongoDB Memory Management Demystified
Page 36: MongoDB Memory Management Demystified

OptimizationSmaller = faster!

Less memory

Higher disk throughput

Schema

Shorten keys

firstName -> first -> f

Size vs. count

Page 37: MongoDB Memory Management Demystified

Optimizing indicesUnused indices

Sparse

Indices should fit in memory

A

Index on name:

Older Newer

Index on creation_time:

Z

Page 38: MongoDB Memory Management Demystified

SummaryHow it works

Challenges

Monitor

Optimize