migration from sql to mongodb - a case study at theknot.com

59
| 1 © 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Upload: mongodb

Post on 13-Aug-2015

1.377 views

Category:

Technology


11 download

TRANSCRIPT

Page 1: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 1

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Page 2: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 2

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

XO Group Inc.

Membership and Community Team

Alexander Copquin - Senior Software Engineer

Vladimir Carballo - Senior Software Engineer

Page 3: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 3

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites API Re-platforming

…a case study

Page 4: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 4

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites API Re-platforming

• Architectures SQL .NET / Ruby Mongo• Reasons for migration• Schema design• RoR model design and implementation• Migration strategies and systems• Lessons learned

Page 5: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 5

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Our Favorites Feature

Page 6: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 6

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites API

• Add / Edit / Delete Object.• Manage Boards• Get counts & stats• RESTful API• Rails • JavaScript • Ios• Android

Features

Page 7: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 7

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• + 100,000,000 “favorited” objects• + 760,000 boards• Avg. 55,000 new objects per day

Stats

Page 8: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 8

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Legacy Architecture

Page 9: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 9

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Database 55 GB and growing…

• Avg 45 rpm on peak times

• Avg 80 msec response POST

• Avg 460 msec response GET

Legacy Benchmarks

Page 10: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 10

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Db Reaching max. capacity for setup• Scalability problems• Hard to modify schema• Bad response times• Very complex caching layer• Out of line with company’s strategy

Maxed

Page 11: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 11

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

New Architecture

Page 12: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 12

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Easy to scale• Flexible schema• Fast Response• No Cache Layer• Fast Iteration / Deploy• TDD first and foremost• At a glance monitoring of all layers

Scalable

Page 13: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 13

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Implementation

Page 14: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 14

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

What we persisted in the legacy schema

• UserId (primary key)• UniqueId• Url (unique per user)• ImageUrl• Name• Description• ObjectId (unique per application adding favorites)

• Category• Timestamps• Other

Page 15: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 15

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites DB Legacy Schema

Page 16: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 16

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

select top 10 UserFavoriteId, Name, Description, Url, ImageUrl from userFavorites where userId = '5174181997807393'

Sample queries

Page 17: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 17

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

select top 5 grp.groupId, grp.Name as GroupName, fav.userFavoriteId, fav.name, fav.Description, fav.Url, fav.ImageUrlfrom userFavoritesGroups grpinner join userFavoritesGroupsItems grpItm on grp.GroupId = grpItm.GroupIdinner join userFavorites fav on grpItm.userFavoriteId = fav.userFavoriteIdwhere grp.userId = '5174181997807393'

Sample queries

Page 18: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 18

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Towards a new Schema and Persistance Layer

• Start with a clean slate• Break with the past• Persist only relevant minimum data points• Think and rethink relationships• High Performance• Flexible• Prototype different scenarios

Page 19: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 19

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

First attempt

Page 20: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 20

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

UserFavorites

Page 21: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 21

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Document contains embedded documents which are required to be accessed on its own

• Documents would grow without bound• Most queries would be slow• Indexes would be very expensive• Tries too hard to imitate legacy

Cons

Page 22: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 22

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Second attempt

Page 23: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 23

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Page 24: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 24

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Board document with one recent favorite

Page 25: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 25

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Board document with more recent favorites

Page 26: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 26

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Page 27: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 27

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorite document located on different boards

Page 28: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 28

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Document structure matches the data required on the view• A Board document includes the 4 most recent favorites.• A Favorite document includes the list of boards it was

added to• Faster queries.• More control on the size of each document• Better implementation of UX intent

Pros

Page 29: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 29

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Sample queriesdb.favorites.find({'member': 'e1606ed5-4ac8-48b4-aee6-bc4203937903'})

.limit(1)

db.favorites.find({'boards': '7557acf8-b7b1-4eab-a64d-57449034cfc6'})

.limit(1)

db.favorites.find({'application': 'marketplace'}) .limit(1)

db.boards.find({'member': 'e1606ed5-4ac8-48b4-aee6-bc4203937903'}) .limit(1)

db.boards.find({'member': 'e1606ed5-4ac8-48b4-aee6-bc4203937903', 'default_board': true})

db.boards.find({'name' : 'Simple Reception Decor'}).limit(1)

Page 30: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 30

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

● Rails web application framework● We speak RoR and JS● mongoDB as a data repository (we love NoSQL)● Two collections, one for Boards and one for

Favorites● No joins, no foreign keys● Referential integrity is handled in a different fashion.● MongoId Gem (Pros & Cons)

Some implementation details

Page 31: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 31

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites re-platform

Page 32: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 32

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Board class

Page 33: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 33

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Page 34: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 34

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorite class

Page 35: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 35

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Page 36: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 36

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Scaling reads with replica sets

Page 37: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 37

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Scaling reads with sharding

Page 38: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 38

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Migration

Page 39: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 39

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Clients switchover

NewAPI

Legacy API

Client

Client

Client

Client

ONE WAY

Page 40: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 40

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Migration Timeline

new API.Continuous

Migr.Implement Monitors

Turn on Continuous

Data Catch-up

Plug ClientsBulk Migr.

Development

Bulk Migr.

Migration

Page 41: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 41

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Bulk Migration

ETL

Page 42: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 42

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Bulk Migration

FavoritesUserFavorites

SQL Tables Mongo Collections

BoardsUserFavoritesGroups

UserFavoritesGroupsItems

Page 43: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 43

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Favorites Job

Page 44: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 44

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Pentaho Steps

Page 45: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 45

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Auto-increment Id vs. UUID

UserFavoriteId

GroupId

Favorites UUID

Groups UUID

Continuous Migration

Page 46: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 46

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Get UUID from the Get Go

• Add a column to legacy Db (+ 100M recs!!) with new Mongo UUID

• Then migrations will take care of inserting into new documents

SQL has all new

idsxxxxx-xxxx-xxxxxxxxx-xxxx-xxxxxxxxx-xxxx-xxxx

Mongo

Ids are inserted

Migration Systems

Page 47: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 47

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Add UUID Columns in SQL

100 M recs!!!Alter table add UUID uniqueidentifier

New Favs TempTablewith UUID

SELECT *, uuid = NEWID() INTO NewUserFavorites

FROM UserFavorites

Add Indexes

Rename & drop

Page 48: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 48

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• SQL needed some sanitation• SQL prep scripts approx. 4 hs• Pentaho ETL on local Workstation: 8hs• Restore into production Mongo Cluster: 4hs

Facts

Page 49: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 49

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

We’ve got data!!

Page 50: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 50

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Continuous Migration Architecture

Clients

Legacy API New API

SQS Queue Messenger

ONE WAY SYNC…

Page 51: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 51

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Continuous Migration Favorites Legacy Messenger

• Ruby• Consumer of an SQS queue coming from Legacy

that generates 1 message per operation• Issues API call to new app per each operation• Runs as a worker in the background

Legacy API

SQSLegacy

Messenger

NewAPI Mongo

Page 52: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 52

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

SQS Queue is not a FIFO Friend

Sent by Legacy

1

2

3

4

5

6

7

5

3

1

2

4

6

7

Consumed by Messenger

Page 53: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 53

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Queue is not FIFO• Objects don’t exist• Queue bloats fast• Can get like not-real-time• Data is different

Challenges

Page 54: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 54

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Verify if entity exists (API call),

otherwise, throw back in queue• Set message expiration• Sanitize data• Get multiple workers to achieve

near real-time syncing.

Solutions

Page 55: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 55

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Favor a simple document structure• Try different schema paradigms• Bypass native objectId generation in favor of UUID• Break with the past• Queues can be deceiving• Gems can simplify application layer impl.• Manage ref. integrity in app. layer• No cache required Take away

Page 56: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 56

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

• Avg 85 rpm on peak times

• Avg 58 msec response POST

• Avg 18 msec response GET

New Benchmarks

Page 57: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 57

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

New vs. Legacy

• Overall Performance Increase• 18 ms vs. 460 ms for GET• 58 ms vs. 80 ms for POST • Easy Schema Changes• Scalable• Simpler architecture• No Cache layer• Fast Code iteration, testing and deployment• In-line with company’s technology strategy

Good

Page 58: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 58

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

Acknowledgments

• Dmitri Nesterenko• Jason Sherman• Nelly Santoso• Phillip Chiu• Sean Lipkin• George Taveras• Alison Fay• Diana Taykhman• Rajendra Prashad• Josh Keys• Lewis DiFelice

Page 59: Migration from SQL to MongoDB - A Case Study at TheKnot.com

| 59

© 2014 XO GROUP INC. ALL RIGHTS RESERVED.

contact, questions, inquiries?

[email protected]