Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[SIC '16] Zero-Downtime Database Schema Evolution

[SIC '16] Zero-Downtime Database Schema Evolution

Talk given at the Dutch Software Industry Conference (SIC) 2016, describing the challenges and potential solutions for dealing with schema changes in an ever changing production environment without affecting dependent services.

Michael de Jong

March 17, 2016
Tweet

More Decks by Michael de Jong

Other Decks in Programming

Transcript

  1. • Fast release cycles • Quick bug-fixes • A/B testing

    • Rigid schema • ACID transactions • Constraints Continuous Deployment SQL Databases
  2. • Make only small changes to the database schema (pref.

    just one). • Ensure that both versions of the database schema are compatible with both the current and the next version of the application. How do you deal with this?
  3. • Make only small changes to the database schema (pref.

    just one). • Ensure that both versions of the database schema are compatible with both the current and the next version of the application. That’s not always possible!
  4. • Make only small changes to the database schema (pref.

    just one). • Ensure that both versions of the database schema are compatible with both the current and the next version of the application. This is error-prone, and 
 can require multiple deploys
  5. • Depending on the database you’re using, various schema operations

    may block read/write operations on a table while the schema operation is being executed. An additional challenge
  6. MySQL 5.5 MySQL 5.6 PostgreSQL 9.3 PostgreSQL 9.4 S1 Read-only

    Non-Blocking Blocking Blocking S2 Read-only Non-Blocking Non-Blocking Non-Blocking S3 Read-only Non-Blocking Non-Blocking Non-Blocking S4 Read-only Non-Blocking Non-Blocking Non-Blocking S5 Read-only Non-Blocking Non-Blocking Non-Blocking S6 Read-only Non-Blocking Non-Blocking Non-Blocking S7 N/A N/A Non-Blocking Non-Blocking S8 N/A N/A Non-Blocking Non-Blocking S9 N/A N/A Blocking Blocking S10 Read-only Read-only Non-Blocking Non-Blocking S11 Read-only Read-only Blocking Blocking S12 Non-Blocking Non-Blocking Non-Blocking Non-Blocking S13 Non-Blocking Non-Blocking Non-Blocking Non-Blocking S14 Read-only Read-only Blocking Blocking S15 Read-only Read-only Blocking Blocking S16 Read-only Non-Blocking Non-Blocking Non-Blocking S17 N/A N/A Non-Blocking Blocking S18 Blocking Non-Blocking Non-Blocking Non-Blocking S19 Non-Blocking Non-Blocking Non-Blocking Non-Blocking
  7. • Large Hadron Migrator • Online Schema Change But what

    about these tools? • TableMigrator • Percona/OAK toolkit
  8. • Support zero-downtime changes. • No support for FK constraints.

    But what about these tools? • Can only do one operation, on one table at a time.
  9. • Complex schema changes • Avoid blocking reads/writes What do

    we need? • Expose two versions of the schema • Support FK constraints
  10. changelog.addChangeSet(“Add referral column to customers table”, addColumn(“customers”, “referred_by”, integer()), addForeignKey(“customers”,

    “referred_by”) .named(“customer_referred_by_fk”) .onDelete(NO_ACTION) .referencing(“customers”, “id”)); Defining changes
  11. Connection connection = DriverManager.getConnection( “jdbc:postgresql://localhost:5432/database”, “username”, “password”); Connection connection =

    DriverManager.getConnection( “jdbc:quantumdb:postgresql://localhost:5432/database? version=80bfa11”,“username”, “password”); Accessing the database Intercepting database interactions
  12. SELECT * FROM rentals WHERE customer_id = 2372 AND return_date

    < NOW() AND returned = false; SELECT * FROM rentals* WHERE customer_id = 2372 AND return_date < NOW() AND returned = false; Accessing the database Rewriting intercepted queries
  13. Complex schema changes? • With QuantumDB you can apply multiple

    operations to multiple tables at once. QuantumDB automatically determines the “future state” of the schema, and how to get there without changing the existing tables.
  14. Two versions of the schema? • QuantumDB creates and exposes

    two or more versions of the database schema through a custom JDBC driver. • Allows you to run two or more versions of your service in parallel, each version connected its own schema.
  15. Blocking read/writes? • QuantumDB only applies schema operations to new

    tables. Thus the client is never blocked by operations executed by QuantumDB.
  16. SUPPORTED ADD COLUMN ALTER COLUMN DROP COLUMN CREATE TABLE COPY

    TABLE RENAME TABLE DROP TABLE ADD FOREIGN KEY DROP FOREIGN KEY CREATE INDEX DROP INDEX NOT (YET) SUPPORTED DML STATEMENTS ADD CHECK CONSTRAINT DROP CHECK CONSTRAINT CREATE VIEW DROP VIEW DECOMPOSE TABLE * JOIN TABLE * MERGE TABLE * PARTITION TABLE * * Update Rewriting and Integrity Constraint Maintenance in a Schema Evolution Support System: PRISM++ Supported operations