Top Banner
Midas On-the-fly-Schema-Migration Tool For
46

Midas - on-the-fly schema migration tool for MongoDB.

May 12, 2015

Download

Technology

Dhaval Dalal

Midas is an on-the-fly schema migration for MongoDB.

Currently, applications have to hand-roll their own schema migration infrastructure or use some third-party tool.
It is difficult to migrate TBs of data without downtime (unacceptable from SLA stand-point!).
This is where Midas fills the gap.

It intercepts responses at MongoDB Protocol level and upgrades or downgrades document schema in-transit.
As Midas works at protocol level, it is agnostic of Language specific MongoDB drivers (Ruby, Python, C#
and Java drivers) and their versions within those languages.

Further, Midas is agnostic of the MongoDB configurations like Standalone, Replica Sets, Sharded environments.

Features Summary:
* Expansion operations - add, copy, merge, split, transform.
* Contraction operations - remove
* Schema migration for multiple applications simultaneously
* Support multi node configuration for application
* Without shutting down Midas, you can -
* Add or remove applications on-the-fly
* Add or remove nodes on-the-fly
* Add or remove deltas/changeset on-the-fly
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Midas - on-the-fly schema migration tool for MongoDB.

MidasOn-the-fly-Schema-Migration Tool

For

Page 2: Midas - on-the-fly schema migration tool for MongoDB.

Schema Migration ProblemsApplications have to hand-roll their own schema migration infrastructure or use some third-party tool

!

Difficult to migrate TBs of data without downtime

unacceptable from SLA stand-point!

!

How about: on-the-fly schema migration - a Midas Touch?

Page 3: Midas - on-the-fly schema migration tool for MongoDB.

Zero-downtime DeploymentExpansion Scripts

Apply changes to the documents safely that do not break backwards compatibility with existing version of the application.

e.g Adding, copying, merging, splitting fields in a document.

Contraction Scripts

Clean up any database schema that is not needed after the upgrade.

e.g. removing fields from a document.

Page 4: Midas - on-the-fly schema migration tool for MongoDB.

The Mechanics

Run Expansion scripts before upgrading the application

Upgrade the cluster, a node at a time

Run Contraction Scripts

Once the system has been completely upgraded and deemed stable.

Typically, contractions can be run, say days/weeks after complete validation.

Page 5: Midas - on-the-fly schema migration tool for MongoDB.

Do we need DB rollback?Short Answer

No

!

Long Answer

Reversing DB changes can lead to potential loss of data or leave it in an inconsistent state.

Its safer to rollback application without needing to rollback DB changes as expansions are backward compatible.

Page 6: Midas - on-the-fly schema migration tool for MongoDB.

How it works? An Architectural Overview

Application

Page 7: Midas - on-the-fly schema migration tool for MongoDB.

How it works? An Architectural Overview

Application Midas

Intercepts Responses at Protocol Level

Upgrades/Downgrades Document schema in-transit

Page 8: Midas - on-the-fly schema migration tool for MongoDB.

Protocol Level brings Transparency

From the App perspective Midas is Agnostic of Language specific drivers and versions within those languages

Works with versions of Ruby, Python, C# and Java drivers.

From MongoDB perspective Midas is Agnostic of the MongoDB configurations

Works with Standalone, Replica Sets, Sharded environments.

Page 9: Midas - on-the-fly schema migration tool for MongoDB.

Delta Files (written in a DSL)

Deep DiveMidas

Req

Res

Req

ResTransformer

Configurer

Parser

I’m just a decorating proxy!

Appl

icatio

n

Page 10: Midas - on-the-fly schema migration tool for MongoDB.

Delta scripts Group ChangeSet as Directories

Group deltas as Expansion and/or Contraction deltas as Directories within ChangeSet

Write delta scripts using the .delta extension

Midas relies on the order specified by you

Ordering info is embedded by you within the change set directory name and delta files

Page 11: Midas - on-the-fly schema migration tool for MongoDB.

Delta Scripts - ConventionMust begin change set directories with a number and subsequent change set directories are numbered in ascending order.

Example: use <changesetNumber>-<featureName> as convention for naming change set directory.

Within a change set, expansion and contraction scripts are grouped by folders - expansions and contractions.

Always begin delta scripts with a number and subsequent delta scripts are numbered in ascending order.

Example: use <changeNumber>_<WhatTheChangeIs>.delta as convention for naming delta files

Page 12: Midas - on-the-fly schema migration tool for MongoDB.

Sample Delta ScriptEach Delta script is written using a DSL

Very close to MongoDB lingo, virtually no learning curve.

use users db.customers.remove(‘["address.line1"]') db.customers.merge([“lname”, “fname”], “ “, “name”) !!use transactions db.orders.add(‘dispatch : { status: ‘NOT DELIVERED’}’)

Page 13: Midas - on-the-fly schema migration tool for MongoDB.

Agile App Delivery & DevOpsInject Midas into Architecture

Start or Middle of project

Supports Development of Application in small-steps Add Application ChangeSets/Deltas on-the-fly

Copes with Load Add/Remove Application Nodes on-the-fly

Supports Multiple Applications Add/Remove Application(s) on-the-fly

Page 14: Midas - on-the-fly schema migration tool for MongoDB.

No Leaky AbstractionsDoes not expect the Domain Model to be aware of versioning.

Allows developers to focus on the domain while freeing them from versioning concerns.

If you wish to take charge, Midas will not come in your way.

Midas maintains versioning information within the document itself.

_expansionVersion and _contractionVersion fields are part of the documents “touched” by Midas.

Updates them during request and response.

Page 15: Midas - on-the-fly schema migration tool for MongoDB.

CaveatsNever ever change a delta that has been applied to production.

Always move forward in time.

Reverse a change by a counter-change.

Force-update migration on documents that are not expanded by App demand, and then proceed to contraction.

contracted by App demand, and then proceed to next App-upgrade cycle.

Currently Midas supports Zero Downtime Configuration - 1 (see later slides), others will be supported in subsequent releases.

Page 16: Midas - on-the-fly schema migration tool for MongoDB.

Midas Domain ModelConfiguration

Application

Transformer

<<Folder>> ChangeSet

<<File>> Delta

1

*

1 * 1 *

1

1RequestInterceptor

ResponseInterceptorNode

*1

<<Verb>> Transformation

Parsed and Injected in

Page 17: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 1 !

Injecting and Using Midas

Page 18: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v0

App v00 Offline

Online

Deployment config - 1

Offline does not mean node is physically taken

down or is off

Page 19: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

MidasSite B

App v0

App v01 Offline

Online

Deployment config - 11. Inject Midas in to the architecture with

app configured in Expansion Mode 2. Start with all Sites, having changeSet = 0

(no change) for all nodes

Offline does not mean node is physically taken

down or is off

Page 20: Midas - on-the-fly schema migration tool for MongoDB.

Site A

Deployment config - 1

App v0

Load Balancer

App v0

Site B

App v1

App v12

Midas

Offline

Online

4. For all nodes in Site B - v1, Apply new change set, say changeSet = 3

3. Upgrade Site B to App v1.

Offline does not mean node is physically taken

down or is off

Page 21: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v1

App v13

Deployment config - 1

Midas

Offline

OnlineOffline does not mean node is physically taken

down or is off

5. Flip to Site B

Page 22: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v1

App v14

Deployment config - 1

Midas

6. Confirm that App v1 is stable, in case it is not, bug fix and re-deploy it. 7. Once deemed stable, Make sure that expansion of all documents is complete. This can be currently achieved by writing a simple script, that does a find and save thru’ Midas.

Offline

Online

Page 23: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v1

App v15

Deployment config - 1

Midas

8. Change Application mode to Contraction.

Offline

Online

Page 24: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v1

App v16

Deployment config - 1

Midas

Offline

Online9. Make Sure that Contraction of all documents is complete. This can be achieved by writing a simple script that does find and save through Midas.

Page 25: Midas - on-the-fly schema migration tool for MongoDB.

Site A

App v0

Load Balancer

App v0

Site B

App v1

App v17

Deployment config - 1

Midas

Offline

OnlineOffline does not mean node is physically taken

down or is off

10. Prepare for next Release and Repeat steps from #1.

Page 26: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 1 !

Node Removal at Runtime

Page 27: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer

Site ANode0

Node1Midas

2. Remove Node0 from app.midas !demoAppV1 {! mode = contraction!// siteANode0 {!// ip = x.x.x.x!// changeSet = 3!// }! siteANode1 {! ip = y.y.y.y! changeSet = 3! }!}

Offline

Online

Say, due to some problems, you want to remove Node0 from service: !1. Remove Node0 from LB.

0

Page 28: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer

Site ANode0

Node1Midas

Offline

OnlineOffline does not mean node is physically taken

down or is off

2. Midas will sever all connections with Node0

1

Page 29: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 1 !

Node Injection at Runtime

Page 30: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer Midas

0

Site ANode0

Node1

Node2

Offline

OnlineOffline does not mean node is physically taken

down or is off

You want to inject new node for App in service 1. Add Node2 to app.midas. demoAppV1 {! mode = expansion! siteANode0 {! ip = x.x.x.x! changeSet = 3! }! siteANode1 {! ip = y.y.y.y! changeSet = 3! }! siteANode2 {! ip = z.z.z.z! changeSet = 3! }!}

2. Midas is ready to receive connections from Node2

Page 31: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer

1

3. Add Node2 to LB.

Offline

Online

Midas

Site ANode0

Node1

Node2

Page 32: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 1 !

App Removal at Runtime

Page 33: Midas - on-the-fly schema migration tool for MongoDB.

App1 Load

Balancer

Midas

0

Deployment config - 1

Offline

Online

Site A

Site B

Site A

Site B

App2 Load

Balancer

1. Say, you want to remove App2 from Midas.

2. Stop App2 LB so that it stops receiving requests

Page 34: Midas - on-the-fly schema migration tool for MongoDB.

App1 Load

Balancer

Midas

1

1. Remove App2 from midas.config !apps {! demoApp1!// demoApp2!}!!Note: You don’t have to remove demoApp2 dir or demoApp2.midas to remove the application from Midas (this allows you to re-inject App2 should the need be)

Offline

Online

Site A

Site B

Site A

Site B

App2 Load

Balancer

3. You can now take down App2

2. Midas will sever all connections from App2

Page 35: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 1 !

App Injection at Runtime

Page 36: Midas - on-the-fly schema migration tool for MongoDB.

App1 Load

Balancer

Midas

0 Offline

Online

Site A

Site B

Site A

Site B

App2 Load

Balancer

1. Say, you want to add App2 to Midas.

2. Setup the infrastructure for App2 (Sites and Nodes).

Page 37: Midas - on-the-fly schema migration tool for MongoDB.

App1 Load

Balancer

Midas

1

3. Create App2 dir within deltas dir 4. Set-up change sets with expansions and contractions in App2 dir.

Offline

Online

Site A

Site B

Site A

Site B

App2 Load

Balancer

Page 38: Midas - on-the-fly schema migration tool for MongoDB.

App2 Load

Balancer

Site A

Site B

2 Offline

Online

App1 Load

Balancer

Midas

5. Add demoApp2.midas (with mode and nodes configured) demoApp2Ver1_0 {! mode = expansion! siteANode1 {! ip = a.a.a.a! changeSet = 2! }! …! …! siteBNode1 {! ip = p.p.p.p! changeSet = 2! }!}

Site A

Site B

Page 39: Midas - on-the-fly schema migration tool for MongoDB.

App2 Load

Balancer

Site A

Site B

2 Offline

Online

9. Start the LB so that App2 is live

8. Start any Site that can go-live, say Site A here

App1 Load

Balancer

Midas

6. Add App2 to midas.config apps {! demoApp1! demoApp2!}!!7. Midas is ready to receive connections from App2

Site A

Site B

Page 40: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 2Not Supported in this Release

Page 41: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer

Node AApp v1

0 Offline

Online

Deployment config - 2

Node BApp v1

Node CApp v1

Page 42: Midas - on-the-fly schema migration tool for MongoDB.

Zero-Downtime Deployment Configuration - 3 AWSNot Supported in this Release

Page 43: Midas - on-the-fly schema migration tool for MongoDB.

Load Balancer

Node AApp v1

0 Offline

Online

Deployment config - 3

Node BApp v1

Node CApp v1

Page 45: Midas - on-the-fly schema migration tool for MongoDB.

References

Owen Rogers

http://exortech.com/blog/2009/02/01/weekly-release-blog-11-zero-downtime-database-deployment/

Page 46: Midas - on-the-fly schema migration tool for MongoDB.

Thank-You