database migrations without down time (v2:php:dpc)

95
DATABASE MIGRATIONS WITHOUT DOWN TIME MICHIEL ROOK @michieltcs

Upload: others

Post on 07-Jun-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database migrations without down time (V2:PHP:DPC)

DATABASE MIGRATIONSWITHOUT DOWN TIME

MICHIEL ROOK

@michieltcs

Page 2: Database migrations without down time (V2:PHP:DPC)

▸ Developer, consultant, trainer, speaker

▸ @michieltcs

Page 3: Database migrations without down time (V2:PHP:DPC)

ABOUT DATABASE MIGRATIONS

Page 4: Database migrations without down time (V2:PHP:DPC)

ABOUT SCHEMA MIGRATIONS

Page 5: Database migrations without down time (V2:PHP:DPC)

SQL

Page 6: Database migrations without down time (V2:PHP:DPC)

@michieltcs

UP

Page 7: Database migrations without down time (V2:PHP:DPC)

@michieltcs

UP DOWN

Page 8: Database migrations without down time (V2:PHP:DPC)

DOCTRINE

@michieltcs

class Version20170228150619 extends AbstractMigration { public function up(Schema $schema) { $this->addSql('CREATE TABLE addresses (id INT NOT NULL, street VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); } public function down(Schema $schema) { $this->addSql('DROP TABLE addresses'); } }

Page 9: Database migrations without down time (V2:PHP:DPC)

DOCTRINE

@michieltcs

mysql> select * from migration_versions; +----------------+ | version | +----------------+ | 20170228150619 | +----------------+ 1 row in set (0,00 sec)

Page 10: Database migrations without down time (V2:PHP:DPC)

FLYWAY

@michieltcs

V20170228150619-create_addresses_table.sqlCREATE TABLE addresses (id INT NOT NULL, street VARCHAR(255) NOT NULL, PRIMARY KEY(id))');

Page 11: Database migrations without down time (V2:PHP:DPC)

FLYWAY

@michieltcs

mysql> select * from schema_version; +----------------+----------------+------------------------+----------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+----------------+------------------------+----------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ | 1 | 1 | << Flyway Baseline >> | BASELINE | << Flyway Baseline >> | NULL | migrations_1 | 2017-02-28 16:27:21 | 0 | 1 | | 2 | 20170228150619 | create addresses table | SQL | V20170228150619__create-addresses-table.sql | 1789629568 | migrations_1 | 2017-02-28 16:27:21 | 16 | 1 | +----------------+----------------+------------------------+----------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ 2 rows in set (0,00 sec)

+------------+--------------+---------------------+----------------+---------+ | checksum | installed_by | installed_on | execution_time | success | +------------+--------------+---------------------+----------------+---------+ | NULL | migrations_1 | 2017-02-28 16:27:21 | 0 | 1 | | 1789629568 | migrations_1 | 2017-02-28 16:27:21 | 16 | 1 | +------------+--------------+---------------------+----------------+---------+

Page 12: Database migrations without down time (V2:PHP:DPC)

TRANSACTIONS?

Page 13: Database migrations without down time (V2:PHP:DPC)

ROLLBACKS?

Page 14: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

1

Page 15: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

1 LONG-LIVED LOCKS

Page 16: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

1 LONG-LIVED LOCKS

RENAMES

Page 17: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

1 LONG-LIVED LOCKS

RENAMES

DELETES

Page 18: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

IMPLICIT COMMITS

1 2

Page 19: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

IMPLICIT COMMITS

SHARDING

1 2 3

Page 20: Database migrations without down time (V2:PHP:DPC)

@michieltcs

DESTRUCTIVE CHANGES

IMPLICIT COMMITS

SHARDING MULTIPLE DATABASES

1 2 3 4

Page 21: Database migrations without down time (V2:PHP:DPC)

ROLLBACKS

Page 22: Database migrations without down time (V2:PHP:DPC)

DOWNTIME?

Page 23: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE PACKAGE

Page 24: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE PACKAGE

SHUT DOWN APPLICATION

Page 25: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE PACKAGE

SHUT DOWN APPLICATION

RUN MIGRATION

Page 26: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE PACKAGE

SHUT DOWN APPLICATION

RUN MIGRATION

DEPLOY PACKAGE

Page 27: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE PACKAGE

SHUT DOWN APPLICATION

RUN MIGRATION

DEPLOY PACKAGE

RUN APPLICATION

Page 28: Database migrations without down time (V2:PHP:DPC)

DOWNTIME?

Page 29: Database migrations without down time (V2:PHP:DPC)

DOWNTIME

Page 30: Database migrations without down time (V2:PHP:DPC)

NO MIGRATIONS!

Page 31: Database migrations without down time (V2:PHP:DPC)

DOWNTIME

Page 32: Database migrations without down time (V2:PHP:DPC)

ZERO DOWNTIME DEPLOYMENT

@michieltcs

LOAD BALANCER

APP V1

Page 33: Database migrations without down time (V2:PHP:DPC)

@michieltcs

LOAD BALANCER

APP V1 APP V2

ZERO DOWNTIME DEPLOYMENT

Page 34: Database migrations without down time (V2:PHP:DPC)

@michieltcs

LOAD BALANCER

APP V2

ZERO DOWNTIME DEPLOYMENT

Page 35: Database migrations without down time (V2:PHP:DPC)

DATABASE STATE?

Page 36: Database migrations without down time (V2:PHP:DPC)

DATABASE VERSION

@michieltcs

LOAD BALANCER

APP V1

DATABASE V1

expects v1

Page 37: Database migrations without down time (V2:PHP:DPC)

@michieltcs

LOAD BALANCER

APP V1

DATABASE V2

expects v1

DATABASE VERSION

Page 38: Database migrations without down time (V2:PHP:DPC)

@michieltcs

LOAD BALANCER

APP V1 APP V2

DATABASE V2

expects v1 expects v2

DATABASE VERSION

Page 39: Database migrations without down time (V2:PHP:DPC)

OLD APPSHOULD WORK WITH

NEW STATE

Page 40: Database migrations without down time (V2:PHP:DPC)

DECOUPLE

Page 41: Database migrations without down time (V2:PHP:DPC)

MIGRATION DECOUPLE

DEPLOYMENT

Page 42: Database migrations without down time (V2:PHP:DPC)

EXPAND - CONTRACT

Page 43: Database migrations without down time (V2:PHP:DPC)

BACKWARDS COMPATIBILITY

Page 44: Database migrations without down time (V2:PHP:DPC)

CLIENT

EXISTING API

@michieltcs

CLIENT

CLIENT

Page 45: Database migrations without down time (V2:PHP:DPC)

CLIENT

EXISTING API

@michieltcs

CLIENT

CLIENT

NEWAPI

Page 46: Database migrations without down time (V2:PHP:DPC)

DEPRECATION

Page 47: Database migrations without down time (V2:PHP:DPC)

CLIENT

EXISTING API

@michieltcs

CLIENT

CLIENT

NEWAPI

Page 48: Database migrations without down time (V2:PHP:DPC)

CLIENT

EXISTING API

@michieltcs

CLIENT

CLIENT

NEWAPI

Page 49: Database migrations without down time (V2:PHP:DPC)

CLIENT

EXISTING API

@michieltcs

CLIENT

CLIENT

NEWAPI

Page 50: Database migrations without down time (V2:PHP:DPC)

CLIENT

@michieltcs

CLIENT

CLIENT

NEWAPI

Page 51: Database migrations without down time (V2:PHP:DPC)

NON-DESTRUCTIVE CHANGES

Page 52: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ADDING TABLES

Page 53: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ADDING TABLES

ADDING COLUMNS

Page 54: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ADDING TABLES

ADDING COLUMNS

CREATING INDEXES

Page 55: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ADDING TABLES

ADDING COLUMNS

CREATING INDEXES

sometimes ...

Page 56: Database migrations without down time (V2:PHP:DPC)

ONE DIFF BACK

Page 57: Database migrations without down time (V2:PHP:DPC)

EXAMPLE: RENAMING A COLUMN

Page 58: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

1

Page 59: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

WRITE TO OLD & NEW

COLUMNS

1 2

Page 60: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

WRITE TO OLD & NEW

COLUMNS

MIGRATE OLD RECORDS

1 2 3

Page 61: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

WRITE TO OLD & NEW

COLUMNS

MIGRATE OLD RECORDS

READ FROM NEW COLUMN

1 2 3 4

Page 62: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

WRITE TO OLD & NEW

COLUMNS

MIGRATE OLD RECORDS

READ FROM NEW COLUMN

1 2 3 4

remove old column and

code

Page 63: Database migrations without down time (V2:PHP:DPC)
Page 64: Database migrations without down time (V2:PHP:DPC)

FEATURE TOGGLES

Page 65: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ENABLE/DISABLE FEATURES

Page 66: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ALTERNATIVE CODE PATHS

ENABLE/DISABLE FEATURES

Page 67: Database migrations without down time (V2:PHP:DPC)

@michieltcs

ALTERNATIVE CODE PATHS

ENABLE/DISABLE FEATURES

AT RUNTIME

Page 68: Database migrations without down time (V2:PHP:DPC)
Page 69: Database migrations without down time (V2:PHP:DPC)

@michieltcs

CREATE COLUMN WITH

NEW NAME

WRITE TO OLD & NEW

COLUMNS

MIGRATE OLD RECORDS

READ FROM NEW COLUMN

1 2 3 4

feature toggle feature toggle

Page 70: Database migrations without down time (V2:PHP:DPC)

NOSQL

Page 71: Database migrations without down time (V2:PHP:DPC)

NO SCHEMA

Page 72: Database migrations without down time (V2:PHP:DPC)

MIGRATE DURING READ

Page 73: Database migrations without down time (V2:PHP:DPC)

BACKGROUND MIGRATION

Page 74: Database migrations without down time (V2:PHP:DPC)

NO DOWNTIME

Page 75: Database migrations without down time (V2:PHP:DPC)

LARGE DATASETS

Page 76: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

Page 77: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

LONG RUNNING QUERIES

Page 78: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

MEMORY USAGELONG RUNNING QUERIES

Page 79: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

FAILURES

MEMORY USAGELONG RUNNING QUERIES

Page 80: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

FAILURES

MEMORY USAGE

RESTARTING, RESUMING

LONG RUNNING QUERIES

Page 81: Database migrations without down time (V2:PHP:DPC)

LONG MIGRATIONS

FAILURES

MEMORY USAGE

RESTARTING, RESUMING

LOGGING & PROGRESS

LONG RUNNING QUERIES

Page 82: Database migrations without down time (V2:PHP:DPC)

QUEUES

Page 83: Database migrations without down time (V2:PHP:DPC)

@michieltcs

UI

Data Layer

Database

messagesqueries DTOs

@michieltcs

Queue

Backend

messages

Page 84: Database migrations without down time (V2:PHP:DPC)

@michieltcs

UI

Data Layer

Database

messagesqueries DTOs

@michieltcs

Queue

Backend

messages

Page 85: Database migrations without down time (V2:PHP:DPC)

CQRS

Page 86: Database migrations without down time (V2:PHP:DPC)

COMMAND QUERY RESPONSIBILITY SEGREGATION

Page 87: Database migrations without down time (V2:PHP:DPC)

SEPARATEREADS FROM WRITES

Page 88: Database migrations without down time (V2:PHP:DPC)

@michieltcs

UI

Event Handlers

CommandsData Layer

Database Database

updates

events

queries DTOs

Repository

@michieltcs

Database

Page 89: Database migrations without down time (V2:PHP:DPC)

TOOLS

Page 90: Database migrations without down time (V2:PHP:DPC)

@michieltcs

Page 91: Database migrations without down time (V2:PHP:DPC)

RECAP

Page 92: Database migrations without down time (V2:PHP:DPC)

MULTIPLE STEPS

Page 93: Database migrations without down time (V2:PHP:DPC)

NO BREAKING CHANGES

Page 94: Database migrations without down time (V2:PHP:DPC)

LITERATURE

databaserefactoring.com

github.com/barsoom/devbook/

Page 95: Database migrations without down time (V2:PHP:DPC)

THANK YOU!

@michieltcs / [email protected]

www.michielrook.nl

https://joind.in/talk/ebd59