SlideShare a Scribd company logo
DB (safe) MIGRATIONS
rails db:migrate:safeeeeeee...
Migrations
● Migration is a set of database instruction.
● They describe database changes.
Rails migrations
● Rails Migration allows you to use Ruby to define changes to
your database schema, making it possible to use a version
control system to keep things synchronized with the actual
code.
● Adding a column
● Backfilling data
● Removing a column
● Changing the type of a column
● Renaming a column
● Renaming a table
● Creating a table with the force option
● Adding a check constraint
● Setting NOT NULL on an existing column
● Executing SQL directly
Some of the unsafe migrations
● Adding an index non-concurrently
● Adding a reference
● Adding a foreign key
● Adding a json column
Postgres-specific checks
Adding a column
Not really!
Adding a column
Locks!
DB locks
● Locks are a mechanism for ensuring multiple operations
don’t update the same row at the same time.
● There are 8 different lock modes, ranging from ACCESS
SHARE (anyone can read and write data) to ACCESS
EXCLUSIVE (no one else is permitted to read data).
● Certain database migrations will obtain an ACCESS
EXCLUSIVE lock, and prevent the rest of your application
from reading data until the migration completes.
Users table
Table locked
Column added
Sets default value
Migrated
How can this be
avoided???
DON’T add columns with
a default value.
Because,
● Of the locking mode it uses and can and will cause
downtime if you have enough rows in your database and
enough traffic on the system.
● Though Postgres 11 actually addresses this problem in
certain circumstances. Adding a static default value no
longer requires obtaining a table level access exclusive
locks. But note the caveat, under certain circumstances.
● For example adding backfilling a new UUID column will
obtain that lock.
Adding a column
(Without a default value)
Now let’s try that again
Adding a column (without a default value)
DONE!
Actually no!
Transactions!
DB transactions
● Transactions combine multiple database operations into a
single, “all-or-nothing” operation.
● They provide four guarantees: atomicity, consistency,
isolation, and durability (“ACID”).
● Consistency and isolation are guaranteed by locks.
● When a a row is being updated, an exclusive lock is issued,
and no one else can update that same row until the first
update is complete.
DB transactions
● Locks are issued on a first-come, first-served basis, and live
for the duration of a transaction, even if the statement that
requested the lock has already executed.
● Migrations are automatically wrapped in a transaction.
● So for most of your database operations this might not be a
problem, as it usually happens in a the order of milliseconds.
● But when you have to perform millions of database
operations on a very large datasets.
Updating in a transaction
Updating in a transaction
Updated successfully
So, how this transactions affect migrations
● Our columns were added, with row 1 we are not actually
locking the entire table, but instead the first row is locked,
mark it true and move on. Even though it was successful, as
I mentioned, that lock doesn't get released until your
transaction get committed.
Adding a column (THE CORRECT WAY)
DON’T BACKFILL DATA
INSIDE A TRANSACTION.
Backfilling data (THE CORRECT WAY)
disable_ddl_transaction!
● It disables that global transaction.
● It is implicitly enabled but you can explicitly when you're
running a particular migration.
● So, we write a separate migration and run once the column
was added.
● Rather than marking every single user inside/outside a
transaction, we iterate users in batches and wrapping each
individual batch inside of a transaction.
● Batch size defaults to 1000 of course it's configurable based
on your individual needs.
What’s the difference??
● This transaction that is updating 1000 rows is gonna
complete and commit much faster than a transaction
updating 10 million rows.
● That changes your lag time from minutes to order of
seconds or even lesser where an individual subset of users
might receive a slightly delayed response.
● So, users most likely won't even notice that anything
happened.
● So our rule of thumb here is???
DON’T MIX SCHEMA AND
DATA CHANGES.
What now??
● We have successfully added users who are active.
● But how are we gonna look up active users?
● Any idea??
Adding an index
Not really!
For Postgres only
Adding an index
Indexing will
● Interfere with regular operation of a database.
● Locks the table to be indexed against writes and performs
the entire index build with a single scan of the table.
● Have a severe effect if the system is a live production
database.
● Very large tables can take many hours to be indexed, and
even for smaller tables, an index build can lock out writers
for periods that are unacceptably long for a production
system.
DO ADD POSTGRES INDICES
CONCURRENTLY.
Adding an index (THE CORRECT WAY)
algorithm: :concurrently
● Waits for all existing transactions that could potentially
modify or use the index to terminate.
● Requires more total work than a standard index build and
takes significantly longer to complete.
● Useful for adding new indexes in a production environment.
● Of course, the extra CPU and I/O load imposed by the index
creation might slow other operations.
Concurrency!
L = λ * W
Little’s law
Concurrency Throughput Response Time
4 = 100 * 40 ms
Concurrent requests Req’s / sec Response Time
Concurrency
● Every application has a theoretical maximum level of
concurrency it can support at any given time.
● Your database obeys the same principles. How fast your
queries are, and how large your connection pool is,
determines how many queries you can concurrently handle.
● Requests start queueing when they arrive faster than your
application, or its database, can respond to them.
● If a database operation blocks many requests for a long
time, your entire application will grind to a halt.
DO TEST DATABASE
PERFORMANCE.
DB Performance
● You don't have to understand the performance
characteristics of the application.
● But you have to understand how they change during before
and after your migration.
● You have to do this on a regular basis.
● If we had an understanding on the effects of the migration
even before we migrate them live, makes an advantage on
us to not drop on outages.
Tools and resources
Gems
● To help your database healthy and still can add schema
changes.
● Static analysis will warn in advance about certain unsafe
migrations.
● Catch problems at dev time, not deploy time.
● ankane/strong_migrations
● LendingHome/zero_downtime_migrations
● Not technically a gem, but: Gitlab migration helpers
Strong migrations
● Catch unsafe migrations in development
● Detects potentially dangerous operations
● Prevents them from running by default
● Provides instructions on safer ways to do what you want
● Supports for PostgreSQL, MySQL, and MariaDB
Strong migrations - Warning and Suggestions
Strong migrations - Warning and Suggestions
Application Performance Monitoring
● Understanding your application's baseline performance is
critical to understanding how migrations will change its
performance characteristics.
Takeaways
● DON’T add columns with a default value.
● DON’T backfill data inside a transaction.
● DON’T mix schema and data changes in the same migration.
● DO add Postgres indexes concurrently.
● DO monitor and test database performance before, during,
and after migrations.
Questions???
IF WE WRITE SAFE MIGRATIONS,
WE'LL RUN SAFE MIGRATIONS.
Thank you!
Ad

More Related Content

Similar to Rails DB migrate SAFE.pdf (20)

PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
Galera Cluster DDL and Schema Upgrades 220217
Galera Cluster DDL and Schema Upgrades 220217Galera Cluster DDL and Schema Upgrades 220217
Galera Cluster DDL and Schema Upgrades 220217
Codership Oy - Creators of Galera Cluster
 
NewSQL - The Future of Databases?
NewSQL - The Future of Databases?NewSQL - The Future of Databases?
NewSQL - The Future of Databases?
Elvis Saravia
 
M|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change MethodsM|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change Methods
MariaDB plc
 
What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3
Pavan Deolasee
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
Mohammed Ragab
 
Schema migration in agile environmnets
Schema migration in agile environmnetsSchema migration in agile environmnets
Schema migration in agile environmnets
Vivek Dhayalan
 
MySQL Overview
MySQL OverviewMySQL Overview
MySQL Overview
Andrey Sidelev
 
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Rajesh Kannan S
 
Transaction Managment in database management systems.pptx
Transaction Managment in database management systems.pptxTransaction Managment in database management systems.pptx
Transaction Managment in database management systems.pptx
ధావన్ కుమార్
 
Liquibase case study
Liquibase case studyLiquibase case study
Liquibase case study
Vivek Dhayalan
 
When NOT to use MongoDB
When NOT to use MongoDBWhen NOT to use MongoDB
When NOT to use MongoDB
Mike Michaud
 
Reliable Data Replication by Cameron Morgan
Reliable Data Replication by Cameron MorganReliable Data Replication by Cameron Morgan
Reliable Data Replication by Cameron Morgan
ScyllaDB
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
Antonios Chatzipavlis
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. Transactions
Alexey Furmanov
 
Stumbling stones when migrating from Oracle
 Stumbling stones when migrating from Oracle Stumbling stones when migrating from Oracle
Stumbling stones when migrating from Oracle
EDB
 
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQLPL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
Reactive.IO
 
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera ClusterWebinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Continuent
 
GLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New FeaturesGLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New Features
Biju Thomas
 
Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”
EPAM Systems
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
NewSQL - The Future of Databases?
NewSQL - The Future of Databases?NewSQL - The Future of Databases?
NewSQL - The Future of Databases?
Elvis Saravia
 
M|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change MethodsM|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change Methods
MariaDB plc
 
What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3
Pavan Deolasee
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
Mohammed Ragab
 
Schema migration in agile environmnets
Schema migration in agile environmnetsSchema migration in agile environmnets
Schema migration in agile environmnets
Vivek Dhayalan
 
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Slashn Talk OLTP in Supply Chain - Handling Super-scale and Change Propagatio...
Rajesh Kannan S
 
When NOT to use MongoDB
When NOT to use MongoDBWhen NOT to use MongoDB
When NOT to use MongoDB
Mike Michaud
 
Reliable Data Replication by Cameron Morgan
Reliable Data Replication by Cameron MorganReliable Data Replication by Cameron Morgan
Reliable Data Replication by Cameron Morgan
ScyllaDB
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
Antonios Chatzipavlis
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. Transactions
Alexey Furmanov
 
Stumbling stones when migrating from Oracle
 Stumbling stones when migrating from Oracle Stumbling stones when migrating from Oracle
Stumbling stones when migrating from Oracle
EDB
 
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQLPL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
Reactive.IO
 
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera ClusterWebinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Webinar Slides: MySQL HA/DR/Geo-Scale - High Noon #2: Galera Cluster
Continuent
 
GLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New FeaturesGLOC 2014 NEOOUG - Oracle Database 12c New Features
GLOC 2014 NEOOUG - Oracle Database 12c New Features
Biju Thomas
 
Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”
EPAM Systems
 

Recently uploaded (20)

Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Ad

Rails DB migrate SAFE.pdf

  • 1. DB (safe) MIGRATIONS rails db:migrate:safeeeeeee...
  • 2. Migrations ● Migration is a set of database instruction. ● They describe database changes. Rails migrations ● Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.
  • 3. ● Adding a column ● Backfilling data ● Removing a column ● Changing the type of a column ● Renaming a column ● Renaming a table ● Creating a table with the force option ● Adding a check constraint ● Setting NOT NULL on an existing column ● Executing SQL directly Some of the unsafe migrations
  • 4. ● Adding an index non-concurrently ● Adding a reference ● Adding a foreign key ● Adding a json column Postgres-specific checks
  • 8. DB locks ● Locks are a mechanism for ensuring multiple operations don’t update the same row at the same time. ● There are 8 different lock modes, ranging from ACCESS SHARE (anyone can read and write data) to ACCESS EXCLUSIVE (no one else is permitted to read data). ● Certain database migrations will obtain an ACCESS EXCLUSIVE lock, and prevent the rest of your application from reading data until the migration completes.
  • 14. How can this be avoided???
  • 15. DON’T add columns with a default value.
  • 16. Because, ● Of the locking mode it uses and can and will cause downtime if you have enough rows in your database and enough traffic on the system. ● Though Postgres 11 actually addresses this problem in certain circumstances. Adding a static default value no longer requires obtaining a table level access exclusive locks. But note the caveat, under certain circumstances. ● For example adding backfilling a new UUID column will obtain that lock.
  • 17. Adding a column (Without a default value) Now let’s try that again
  • 18. Adding a column (without a default value)
  • 21. DB transactions ● Transactions combine multiple database operations into a single, “all-or-nothing” operation. ● They provide four guarantees: atomicity, consistency, isolation, and durability (“ACID”). ● Consistency and isolation are guaranteed by locks. ● When a a row is being updated, an exclusive lock is issued, and no one else can update that same row until the first update is complete.
  • 22. DB transactions ● Locks are issued on a first-come, first-served basis, and live for the duration of a transaction, even if the statement that requested the lock has already executed. ● Migrations are automatically wrapped in a transaction. ● So for most of your database operations this might not be a problem, as it usually happens in a the order of milliseconds. ● But when you have to perform millions of database operations on a very large datasets.
  • 23. Updating in a transaction
  • 24. Updating in a transaction
  • 26. So, how this transactions affect migrations ● Our columns were added, with row 1 we are not actually locking the entire table, but instead the first row is locked, mark it true and move on. Even though it was successful, as I mentioned, that lock doesn't get released until your transaction get committed.
  • 27. Adding a column (THE CORRECT WAY)
  • 29. Backfilling data (THE CORRECT WAY)
  • 30. disable_ddl_transaction! ● It disables that global transaction. ● It is implicitly enabled but you can explicitly when you're running a particular migration. ● So, we write a separate migration and run once the column was added. ● Rather than marking every single user inside/outside a transaction, we iterate users in batches and wrapping each individual batch inside of a transaction. ● Batch size defaults to 1000 of course it's configurable based on your individual needs.
  • 31. What’s the difference?? ● This transaction that is updating 1000 rows is gonna complete and commit much faster than a transaction updating 10 million rows. ● That changes your lag time from minutes to order of seconds or even lesser where an individual subset of users might receive a slightly delayed response. ● So, users most likely won't even notice that anything happened. ● So our rule of thumb here is???
  • 32. DON’T MIX SCHEMA AND DATA CHANGES.
  • 33. What now?? ● We have successfully added users who are active. ● But how are we gonna look up active users? ● Any idea??
  • 34. Adding an index Not really! For Postgres only
  • 36. Indexing will ● Interfere with regular operation of a database. ● Locks the table to be indexed against writes and performs the entire index build with a single scan of the table. ● Have a severe effect if the system is a live production database. ● Very large tables can take many hours to be indexed, and even for smaller tables, an index build can lock out writers for periods that are unacceptably long for a production system.
  • 37. DO ADD POSTGRES INDICES CONCURRENTLY.
  • 38. Adding an index (THE CORRECT WAY)
  • 39. algorithm: :concurrently ● Waits for all existing transactions that could potentially modify or use the index to terminate. ● Requires more total work than a standard index build and takes significantly longer to complete. ● Useful for adding new indexes in a production environment. ● Of course, the extra CPU and I/O load imposed by the index creation might slow other operations.
  • 41. L = λ * W Little’s law Concurrency Throughput Response Time 4 = 100 * 40 ms Concurrent requests Req’s / sec Response Time
  • 42. Concurrency ● Every application has a theoretical maximum level of concurrency it can support at any given time. ● Your database obeys the same principles. How fast your queries are, and how large your connection pool is, determines how many queries you can concurrently handle. ● Requests start queueing when they arrive faster than your application, or its database, can respond to them. ● If a database operation blocks many requests for a long time, your entire application will grind to a halt.
  • 44. DB Performance ● You don't have to understand the performance characteristics of the application. ● But you have to understand how they change during before and after your migration. ● You have to do this on a regular basis. ● If we had an understanding on the effects of the migration even before we migrate them live, makes an advantage on us to not drop on outages.
  • 46. Gems ● To help your database healthy and still can add schema changes. ● Static analysis will warn in advance about certain unsafe migrations. ● Catch problems at dev time, not deploy time. ● ankane/strong_migrations ● LendingHome/zero_downtime_migrations ● Not technically a gem, but: Gitlab migration helpers
  • 47. Strong migrations ● Catch unsafe migrations in development ● Detects potentially dangerous operations ● Prevents them from running by default ● Provides instructions on safer ways to do what you want ● Supports for PostgreSQL, MySQL, and MariaDB
  • 48. Strong migrations - Warning and Suggestions
  • 49. Strong migrations - Warning and Suggestions
  • 50. Application Performance Monitoring ● Understanding your application's baseline performance is critical to understanding how migrations will change its performance characteristics.
  • 51. Takeaways ● DON’T add columns with a default value. ● DON’T backfill data inside a transaction. ● DON’T mix schema and data changes in the same migration. ● DO add Postgres indexes concurrently. ● DO monitor and test database performance before, during, and after migrations.
  • 53. IF WE WRITE SAFE MIGRATIONS, WE'LL RUN SAFE MIGRATIONS. Thank you!