34
Eytan Daniyalzade @daniyalzade http://bit.ly/cb_mongodb_meetup MongoDB & EC2: A Love Story?

Mongodb meetup

Embed Size (px)

DESCRIPTION

Presentation on how Chartbeat uses MongoDB on EC2

Citation preview

Page 1: Mongodb meetup

Eytan Daniyalzade@daniyalzade

http://bit.ly/cb_mongodb_meetup

MongoDB & EC2: A Love Story?

Page 2: Mongodb meetup

Contents

● Chartbeat● Architecture● MongoDB & EC2 Challenges● Happy Ending: (MongoDB ? EC2)● Takeaways

Page 3: Mongodb meetup

chartbeat

Page 4: Mongodb meetup

Chartbeat: real-time analytics service

● 18 person startup in New York● part of Betaworks● peaking at just under 5M concurrents daily

○ up from 1M in July/2010

Page 5: Mongodb meetup

What chartbeat Provides

● real-time view of site performance

○ top pages

○ new/returning visitors

○ traffic flow■ where are people coming from■ where are people going to

● historic replay for the last 30 days

Page 6: Mongodb meetup

the architecture

Page 7: Mongodb meetup

Architecture, Browser

Part 1:<head><script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>...

Part 2:...function loadChartbeat() { // insert script tag}window.onload = loadChartbeat;</body>(highly simplified)

Ping is standard beacon logic, i.e. loading a 1x1 image.

Page 8: Mongodb meetup

Architecture, Backend

● custom libevent-based C backend○ real-time collection and aggregation

● real-time system in-memory only

● background queue jobs snapshot every x minutes○ Gearman

● historical data○ mostly in MongoDB

Page 9: Mongodb meetup

Why Chartbeat uses MongoDB

● Pure JSON all along○ Live API○ Historical data○ No mapping back and forth

● Fast Inserts (fire and forget)

● Flexible Schema

Page 10: Mongodb meetup

Why Chartbeat uses EC2

● Elastic Capacity

● No trips to datacenter

● EBS snapshots

Page 11: Mongodb meetup

Chartbeat & MongoDB & EC2 (1)

● 3 Clusters○ 1 for each product○ 1 as a caching layer○ 2 - 4 instance/cluster

● m2-2xlarge

○ 34.2 GB merory○ Ubuntu 10.04○ RAID0 x 4 - 1 TB volumes

● Dedicated Snapshot Server○ Shared among clusters○ Serves as an arbiter as well

Page 12: Mongodb meetup

Chartbeat & MongoDB & EC2 (2)Cluster View

Page 13: Mongodb meetup

MongoDB & EC2 Challenges

● Instances disappear○ MongoDB can have long recovery operations○ MongoDB is (was) not ACID compliant. Unclean

shutdown could corrupt your data.

● Poor IO performance on EBS○ MongoDB has global read/write lock

● Variable IO performance on EBS○ Could cause replication issues

Page 14: Mongodb meetup

Question:

�???

Page 15: Mongodb meetup

Disappearing Instances

Page 16: Mongodb meetup

Instances Disappearing - Master/Slave

● Down-time :(

● Slave-promotion = headache○ New instance○ Copy oplog○ Code change○ Long/manual/error prone

Page 17: Mongodb meetup

Instances Disappearing - Replica Sets

● No down-time :) yay!

● Automatic failover on writes

● Eventual failover on reads

● No code change

Page 18: Mongodb meetup

Instances Disappearing - Replica Sets(caveats)

● pymongo driver reads/writes from primary○ pymongo 2.1 will fix this

● chartbeat pymongo driver○ based on MasterSlaveConnection○ writes to primary○ distribute reads among secondaries○ automatic failover○ eventual read re-distribution

Page 19: Mongodb meetup

Instances Disappearing - Fact of Life● Accept this fact of life

● Always snapshot○ Dedicated snapshot server○ Hidden, i.e. no reads

● Automate everything○ puppet

■ New instance from scratch within a minute○ python-boto

■ Script all EC2 interaction■ new_instance.py■ mount_volumes_from_snap.py -o iid -n iid■ snapshot_mongo.py

Page 20: Mongodb meetup

Instances Disappearing - Caveats

● New volumes - slow!!!○ EBS loads blocks lazily

● Warm up EBS & File Cache before use○ Options

■ Slowly direct the reads (app by app)■ Run cache warm-up scripts

○ Not automated currently

Page 21: Mongodb meetup

Poor IO Performance on EBS

Page 22: Mongodb meetup

Poor IO Performance on EBS

● XFS & RAIDing Helps

but,

● Disk IO varies over time

● MongoDB holds global lock on writes

● Query of death○ Grinding-halt if not careful

Page 23: Mongodb meetup

Case Study: Historical Data● For historical data, we store time series.

{key:<key>ts:<key>values: {metric1: int1, metric2: int2}meta:{}}

● High Insert Rate vs Fast Historical Read○ Optimize reads or writes?

● Fast inserts: ~1 MB/sec (through append only)○ No disk-seek

● Historical reads: painfully slow

Page 24: Mongodb meetup

Faster Reads Through Cache DB● Avoid reading from disk● Favor reads over writes● Aim for disk & memory locality

{day_tskey:<key>values: {metric1: list(int), metric2: list(int)}}

● �Data for historical reads resides together

● .append() to list could cause disk fragmentation

Page 25: Mongodb meetup

Avoid Fragmentation w/ Preallocation● Fragmentation causes:

○ Inefficient disk usage○ Slower writes (due to block allocation)

● Preallocate daily arrays instead○ Pros:

■ No fragmentation■ Write causes no change in data size

○ Cons:■ Wasteful (we don't know keys ahead of time)■ Requires heavy disk IO, ~7MB/sec (~60Mbis/sec on EBS)

● Conclusion: spread preallocation over 1 hour

Page 26: Mongodb meetup

EC2 Performance is Unpredictable

Page 27: Mongodb meetup

EC2 Unpredictability - Challenges

● Resource contention in virtualized environment

● EBS and Network IO performance varies drastically

● RAID0 over 4 disks = 4 x risk

Page 28: Mongodb meetup

Heavy Monitoring (1)● Track individual disk performance over time

● Create a new instance if disk not getting better

Page 29: Mongodb meetup

Heavy Monitoring (2)

● Monitor replication lag

● Remove from read mix if lag gets too high○ Incorrect data○ Strain on primary

Page 30: Mongodb meetup

Heavy Monitoring (3)● Track slow queries / opcounts / track page faults / IO

volume○ Tweak indexes accordingly○ Limit requested data size if you can

Page 31: Mongodb meetup

Open Issues

● More granular page-fault / memory usage information○ Difficult due to mmap

● Multi-datacenter usage

● Burn-in scripts

● Sharding○ Tipping point will be insert volume○ Or inefficient read memory usage

● Better understand replication failures

Page 32: Mongodb meetup

Take-aways (1)

● Automate everything○ Instance creation, snapshotting, mount/unmount

● Strive for high locality & low fragmentation● Repeatedly revise schema/index● Heavily monitor

○ Server: IO/mem/disk○ MongoDB: Opcounts/Index Hits/Slow queries○ Cluster: Replication lag○ Application: CRUD times

Page 33: Mongodb meetup

Take-aways (2)

Page 34: Mongodb meetup

Questions?

Slides: http://bit.ly/cb_mongodb_meetup