keeping mongodb data safe

29
Keeping your MongoDB Data Safe Tony Tam @fehguy

Upload: tony-tam

Post on 11-May-2015

6.117 views

Category:

Technology


2 download

DESCRIPTION

Deck presented at 2012 MongoSF on how you can keep your MongoDB data safe

TRANSCRIPT

Page 1: Keeping MongoDB Data Safe

Keeping your MongoDB Data Safe

Tony Tam@fehguy

Page 2: Keeping MongoDB Data Safe

Backups

Page 3: Keeping MongoDB Data Safe

You care because…

•Your data matters

•You run experiments on prod data

•Your devs have sudo on production

•You've seen this before

Page 4: Keeping MongoDB Data Safe

Background

Who am I?

•MongoDB user

• Migrated Wordnik to MongoDB in 2009

•MongoDB admin

• Had to keep it running

Who is Wordnik?

•Data-driven technology company

•MongoDB is our primary data store

Page 5: Keeping MongoDB Data Safe

Strategies

•It's a function of your data size, state of your business

Page 6: Keeping MongoDB Data Safe

What's in the Standard Toolbox

•Dump files via mongodump

•Exports via mongoexport

•Binary data files

•Redundancy

•Oplog

•3rd party or community-developed OSS

•Hosted MongoDB

Page 7: Keeping MongoDB Data Safe

The Lazy Developer

•One server (You've been there)

•Small data, small usage, small problems

•Mongodump is great!

• Small(ish) files (gzip will help you)

• FAST to create

• (typically) FAST to restore via mongorestore

Page 8: Keeping MongoDB Data Safe

Tradeoffs with dump/restore

•Can be done with no downtime. But…

•Potentially inconsistent snapshot

• Why? One collection at a time

• Non-blocking (will yield to writes)

•All or nothing

•Remove then restore

•Restore *might* take time

• Indexes!

Page 9: Keeping MongoDB Data Safe

Tradeoffs with dump/restore

•Can be done with no downtime. But…

•Potentially inconsistent snapshot

• Why? One collection at a time

• Non-blocking (will yield to writes)

•All or nothing

•Remove then restore

•Restore *might* take time

• Indexes!

This can take DAYS

Cost of being lazy

Page 10: Keeping MongoDB Data Safe

Replication

•Right! Replica sets

•HA by redundancy

•Auto fail-over

•Maintenance without downtime

•You can STILL use mongodump

There is NO

excuse

Page 11: Keeping MongoDB Data Safe

Replica Sets

•Lost a server? Add a new one

• Sync from nearby master

• Announce to clients when ready

•Time depends on data size

• And… oh yea, index size

•Gah! WTF!?

Page 12: Keeping MongoDB Data Safe

Replica Sets

•Lost a server? Add a new one

• Sync from nearby master

• Announce to clients when ready

•Time depends on data size

• And… oh yea, index size

•Gah! WTF!? You need

MORE RAM to rebuild

indexes!

Page 13: Keeping MongoDB Data Safe

Replica Sets

•They are Awesome! Really! But…

Test the process

before you need it!

Page 14: Keeping MongoDB Data Safe

Tradeoffs with Replica Sets

•Need multiple servers

•Fat finger?

•Malicious access?

•Software bug?

•You still need backups

Page 15: Keeping MongoDB Data Safe

Options with Replica Sets

•Slave Delay

• Keep one slave behind by X seconds

• *Read* is delayed, not *write*

Page 16: Keeping MongoDB Data Safe

Options with Replica Sets

•Slave Delay

• Keep one slave behind by X seconds

• *Read* is delayed, not *write*Fat finger problem solved?

No! Shut them all

down! Hurry!

Page 17: Keeping MongoDB Data Safe

Alternative to Mongodump?

•Snapshot the data files

• Stop server, back them up

• It's consistent! Snapshot time is well known

•Restoring is easy

• Copy the files, start a server, add to replica set

• NO index rebuilding delays

Page 18: Keeping MongoDB Data Safe

In action

•Stop server

•Snapshot data

•Archive

•Restart

•RepeatDaily?

Hourly?

Page 19: Keeping MongoDB Data Safe

Repeat often or lose data!

•Data copy time (EC2 => 20mbps if lucky)

• 1GB => 1 min

• 100GB => 1.5 hours

• 1TB => 14 hours

•Can't write to data files while copying!

Page 20: Keeping MongoDB Data Safe

Repeat often or lose data!

•Data copy time (EC2 => 20mbps if lucky)

• 1GB => 1 min

• 100GB => 1.5 hours

• 1TB => 14 hours

•Can't write to data files while copying!

Multiple backup servers?

Fancy storage device?

Page 21: Keeping MongoDB Data Safe

Plain-old copying might not cut it

•Many alternatives

• EBS Snapshots

• Logical Volume Manager (LVM)

• RYOR (Roll your own RAID)

• Other IT Black Magic

Page 22: Keeping MongoDB Data Safe

But what about Snapshot Gaps?

•The gaps can be real (and painful)

•Your DRP might need more

• OH, and we still have the fat finger issue

• Retention?

• "Rollback everything but one operation"?

•You can do incremental backups

• (with a little help)

•Easy to add to your automated snapshots

Page 23: Keeping MongoDB Data Safe

More about the OpLog

•All participating members have one

•Capped collection of all write ops

primary replica replicat0

time

t1

t3

t2

time

Page 24: Keeping MongoDB Data Safe

OpLog for incremental BU

•SAME mechanism used by slaves (it's rock solid)

• Just write operations to disk! It's just BSON

•How? (write some code)

cursor = oplog.find();cursor.addOption(Bytes.QUERYOPTION_TAILABLE);cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);while(cursor.hasNext) { DBObject x = cursor.next(); outputStream.write(BSON.encode(x)); ...}

Page 25: Keeping MongoDB Data Safe

OpLog for incremental BU

•Already done for youhttps://github.com/wordnik/wordnik-oss

• For the lazy:

• Get com.wordnik.mongo-admin-utils-distribution from sonatype/maven central

./bin/run.sh com.wordnik.system.mongodb.IncrementalBackupUtil -?

Page 26: Keeping MongoDB Data Safe

Using Wordnik Admin Tools

•Start the IncrementalBackupUtil

•Write to rotating files, last timestamp

•Kill at will

•Restart, picks up from last query

•Restore using RestoreUtil, mongorestore

Page 27: Keeping MongoDB Data Safe

How does it work?

•Easy, of course

Page 28: Keeping MongoDB Data Safe

In Summary

•Technique depends on your deployment

•Lots of tools available

•Fine grained control is available

Test before

you need it!

Page 29: Keeping MongoDB Data Safe

Questions?