Migrating from PostgreSQL to MySQL Without Downtime

50
Migrating from PostgreSQL to MySQL ... without downtime Matthew Graham Percona Live NYC 2011

description

How Etsy migrates live data into MySQL shards without downtime.

Transcript of Migrating from PostgreSQL to MySQL Without Downtime

Page 1: Migrating from PostgreSQL to MySQL Without Downtime

Migrating from PostgreSQL to MySQL... without downtime

Matthew GrahamPercona Live NYC 2011

Page 2: Migrating from PostgreSQL to MySQL Without Downtime

• 8 million members

• 800k active shops

• 1 billion page views / month

• $38.7 million goods sold

• 2 million items sold

• 1.8 million new items listed

• 390k new members joined

April 2011Total

Global marketplace forbuying and selling handmade goods

Page 3: Migrating from PostgreSQL to MySQL Without Downtime

• 8 million members

• 800k active shops

• 1 billion page views / month

• $38.7 million = $900 / min

• 2 million items sold

• 1.8 million new items listed

• 390k new members joined

April 2011Total

Global marketplace forbuying and selling handmade goods

Page 4: Migrating from PostgreSQL to MySQL Without Downtime

Downtime = -$

Page 5: Migrating from PostgreSQL to MySQL Without Downtime
Page 6: Migrating from PostgreSQL to MySQL Without Downtime

$0m

$80m

$160m

$240m

$320m

2006 2007 2008 2009 2010

Gross Merchandise Sales

Page 7: Migrating from PostgreSQL to MySQL Without Downtime
Page 8: Migrating from PostgreSQL to MySQL Without Downtime

Reasons To Migrate

• Horizontal Scaling

• Reduce Types of Databases

• Licensing Costs

• Functional Partitioning

• Schema Refactor

Page 9: Migrating from PostgreSQL to MySQL Without Downtime

Deciding to Switchexpected quality

- transition-----------------

net quality > actual quality

Page 10: Migrating from PostgreSQL to MySQL Without Downtime
Page 11: Migrating from PostgreSQL to MySQL Without Downtime
Page 12: Migrating from PostgreSQL to MySQL Without Downtime

Source TargetMigration

Page 13: Migrating from PostgreSQL to MySQL Without Downtime

Why ?

Page 14: Migrating from PostgreSQL to MySQL Without Downtime
Page 15: Migrating from PostgreSQL to MySQL Without Downtime

• Migrate One Table at a Time

• Progressive Ramp Up

• Data Duplication During Transition

Foundational Principles

Page 16: Migrating from PostgreSQL to MySQL Without Downtime

1. Create Target Tables

2. Tee Writes

3. Backfill

4. Read from Target Tables

5. Wrap Up

5 Steps Per Table

Page 17: Migrating from PostgreSQL to MySQL Without Downtime

Create the Target

Page 18: Migrating from PostgreSQL to MySQL Without Downtime

Merge into the Target

Page 19: Migrating from PostgreSQL to MySQL Without Downtime

Separate the Target

Page 20: Migrating from PostgreSQL to MySQL Without Downtime

Source Only

Source

ApplicationWrites

Target

Page 21: Migrating from PostgreSQL to MySQL Without Downtime

Teed Writes

Source

ApplicationWrites

Target

Page 22: Migrating from PostgreSQL to MySQL Without Downtime

Generating IDsSourceSource TargetTarget

ID Name ID Name

1 Adam 1 Adam

2 Bill 2 Bill

3 Charlie 3 David

4 David 4 Charlie

Ticket Server: http://bit.ly/dbtickets

Page 23: Migrating from PostgreSQL to MySQL Without Downtime

Backfill

Page 24: Migrating from PostgreSQL to MySQL Without Downtime

Application Code Backfill

function backfill_user(user_id) { old_row = find_old_user(user_id); new_row = create_new_user(); new_row.id = old_row.id; ... store_new_user(new_row);}

Page 25: Migrating from PostgreSQL to MySQL Without Downtime

ETL Backfill

Extract

Transform

Load

Source Target

Page 26: Migrating from PostgreSQL to MySQL Without Downtime

Don’t Overload

• Script bulk inserts 100-2000 row batches

INSERT INTO account (id, firstname, lastname)VALUES(1, ‘Alan’, ‘Alda’),(2, ‘Barry’, ‘Bonds’),(3, ‘Charlie’, ‘Chaplin’);

Page 27: Migrating from PostgreSQL to MySQL Without Downtime

Backfill Speed

• Easier to Write • Faster Run Time

Application Code ETL

Page 28: Migrating from PostgreSQL to MySQL Without Downtime

• Easier to Write

• Less Likely to Get Out Of Sync

• Faster Run Time

• Needs an Extract Source

Application Code ETL

Backfill Extract

Page 29: Migrating from PostgreSQL to MySQL Without Downtime

• Easier to Write

• Less Likely to Get Out Of Sync

• Handles Duplicates from Multiple Executions

• Faster Run Time

• Needs an Extract Source

• REPLACE and INSERT ON DUPLICATE KEY UPDATE

Application Code ETL

Backfill Reruns

Page 30: Migrating from PostgreSQL to MySQL Without Downtime

Teed Writes then Backfillor

Backfill then Teed Writes

Page 31: Migrating from PostgreSQL to MySQL Without Downtime

Verification by Diff

$ diff_user 111222333target user row is missing

$ diff_user 123456789- source.user.address_state = ‘CA’+ target.user.address_state = ‘NY’

Page 32: Migrating from PostgreSQL to MySQL Without Downtime

Verification by DiffCOMPARING 200 ROWS From: 111222197

User Id: 111222333target user row is missing

User Id: 111222345- source.user.address_state = ‘CA’+ target.user.address_state = ‘NY’

SUMMARY: total rows with errors: 2/200

Page 33: Migrating from PostgreSQL to MySQL Without Downtime

Read from Target

Page 34: Migrating from PostgreSQL to MySQL Without Downtime

Progressive Ramp Up

Application Reads

100% 0%Source Target

Page 35: Migrating from PostgreSQL to MySQL Without Downtime

Progressive Ramp Up

Application Reads

99% 1%Source Target

Page 36: Migrating from PostgreSQL to MySQL Without Downtime

Progressive Ramp Up

Application Reads

95% 5%Source Target

Page 37: Migrating from PostgreSQL to MySQL Without Downtime

Progressive Ramp Up

Application Reads

75% 25%Source Target

Page 38: Migrating from PostgreSQL to MySQL Without Downtime

Progressive Ramp Up

Application Reads

0% 100%Source Target

Page 39: Migrating from PostgreSQL to MySQL Without Downtime

Ramp Up Example# global configurationuse_new_tables: employees: true percent: 1

# application codeif (enabled(‘use_new_tables’)) { $result = read_from_target();} else { $result = read_from_source();}

Page 40: Migrating from PostgreSQL to MySQL Without Downtime

Enabled or Disabled?

• Check cookies if user already assigned

• $enabled = configured threshold > random %

• Store $enabled in a cookie for future requests

Page 41: Migrating from PostgreSQL to MySQL Without Downtime

Continuous Deployment

• Ramp Up / Ramp Down

• Backfill Fixes

• Need Code Running on Prod to Proceed

• Makes it Easier

Page 42: Migrating from PostgreSQL to MySQL Without Downtime

No Foreign Keys

Page 43: Migrating from PostgreSQL to MySQL Without Downtime

Wrapping Up

Page 44: Migrating from PostgreSQL to MySQL Without Downtime

Analytical Data

Page 45: Migrating from PostgreSQL to MySQL Without Downtime

Teed Writes

Source

ApplicationWrites

Target

Page 46: Migrating from PostgreSQL to MySQL Without Downtime

Target Only

Source

ApplicationWrites

Target

Page 47: Migrating from PostgreSQL to MySQL Without Downtime

Things to Remove

• Code to use old tables

• Code to switch on configuration

• Configuration

• Drop the old tables... eventually

Page 48: Migrating from PostgreSQL to MySQL Without Downtime

1. Create Target Tables

2. Tee Writes

3. Backfill

4. Read from Target Tables

5. Wrap Up

5 Steps Per Table

Page 49: Migrating from PostgreSQL to MySQL Without Downtime

• Migrate One Table at a Time

• Progressive Ramp Up

• Data Duplication During Transition

Foundational Principles

Page 50: Migrating from PostgreSQL to MySQL Without Downtime

• Yes, we’re hiring! Specifically MySQL Opshttp://bit.ly/etsywantsawesome

• http://codeascraft.etsy.com

• http://twitter.com/lapsu

Questions?

Matthew GrahamPercona Live NYC 2011