Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Data Migration with Confidence
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Juanito Fatas
June 22, 2017
Programming
960
3
Share
Data Migration with Confidence
At RedDotRubyConf 2017
Juanito Fatas
June 22, 2017
More Decks by Juanito Fatas
See All by Juanito Fatas
My Open Source Journey
juanitofatas
1
3.3k
NSDanger
juanitofatas
1
190
How to build deppbot
juanitofatas
3
630
Introducing Danger
juanitofatas
0
360
Twemoji 3.0 in the making and announcement beyond SG50
juanitofatas
0
810
Continuous Updates
juanitofatas
0
160
Ruby Asia and dat bacon cannon
juanitofatas
1
280
Update Early, Update Often
juanitofatas
1
1.1k
RSpec for Practical Rubyist
juanitofatas
11
870
Other Decks in Programming
See All in Programming
おれのAgentic Coding 2026/03
tsukasagr
1
140
まかせられるPM・まかせられないPM / DevTech GUILD Meetup
yusukemukoyama
0
110
AI時代のPhpStorm最新事情 #phpcon_odawara
yusuke
0
140
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
0
310
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
2
260
ハンズオンで学ぶクラウドネイティブ
tatsukiminami
0
110
Running Swift without an OS
kishikawakatsumi
0
690
生成 AI 時代のスナップショットテストってやつを見せてあげますよ(α版)
ojun9
0
340
仕様漏れ実装漏れをなくすトレーサビリティAI基盤のご紹介
orgachem
PRO
9
5.1k
forteeの改修から振り返るPHPerKaigi 2026
muno92
PRO
3
250
ドメインイベントでビジネスロジックを解きほぐす #phpcon_odawara
kajitack
2
120
飯MCP
yusukebe
0
490
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
The browser strikes back
jonoalderson
0
930
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Building an army of robots
kneath
306
46k
Embracing the Ebb and Flow
colly
88
5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
170
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
160
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
180
Transcript
Data Migration with Confidence Juanito Fatas RedDotRubyConf 2017
@JuanitoFatas Ramen Specialist
Spanish name From Taiwan Live in Tokyo
I became a salaryman #
Cookpad Global Cookpad Japan
Made in Japan $
Reserve for local jokes %
Data Migration?
Schema Migration Data Migration
Schema Migration Alter Schemas over time https://en.wikipedia.org/wiki/Schema_migration
Data Migration https://en.wikipedia.org/wiki/Data_migration Transfer data from to A System B
System
Existing Data Migration
None
Data Migration to Existing System
Data Migration with Confidence @JuanitoFatas Specialist of cookpad RedDotRubyConf 2017
Why Migration?
Rewrote for Clients
New Partner joins company
Data Migration
~ Get All Data to our system Simple Goal
~ Import data Modeling Migrate After migrate
~ Get the Data Provider API Data Dump
Provider API & Generic Migration Code ✅
Data Dump & Generic Migration Code
HOWTO Data Migration
~ Start with a rake task
~
~ lib/tasks/data_migration.rake
~ lib/data_migration.rb
~ Import dump to local
~ GBs-size file
monthly users 62 30M countries ~
~ Add delay to the SQL dump for production
~ Add sleep() before INSERT INTO
~ Editing huge file
~ +
~
~ Enumerable#lazy https://ruby-doc.org/core-2.4.1/Enumerable.html#method-i-lazy
~
~
~ set accordingly for staging & production
~ Modeling Database
~
~ With these 5 methods, you can model anything.
~ Map data to your current system
~ Sometimes as easy as
~ As HTML in the recipes table field ‘steps’ ,
Sometimes…
~
~
~ Setup Test Suite
~
~
~ - Why Tests? The migration code only used once
~ Better code through boring tests
~ TDD to Get Things Done
~ Modeling Tests Repeat
Migration
~ Use all methods that raises exception
~ Fail Fast to find all errors
~ Example Migrate Recipes
~
~
None
~
~ Add more migrators to migrate
~ Data Integrity
~ Transaction
~ Idempotent Operation
~
~ Run Migration many times
~ Produce the Same Result
~ f(x) = f(x)
~ Upsert Update or Insert
~ MySQL ON DUPLICATE KEY UPDATE PostgreSQL ON CONFLICT UPDATE
PostgreSQL 9.5+ seamusabshere/upsert
~
~ Data Accuracy
~ Manually Check
~ Automated Check
~ Example Check users with most Recipes
~
~
~
~
~
~ To check more things Add more Checker objects
~ Use many small objects to compose
~ Objects Everywhere
For better object design
For better object design
Background Jobs
Workers = CPU cores
Designated Queues
None
None
Log Every Unexpected Error
None
None
For Better handling of Errors
Run against all data to be migrated
Fix every error you can before real migration
~ Tools
Retry mechanism
Foreign Key Constraints Locks
MySQL deadlock Results in
Automatic Retry # Rails 4: ActiveRecord::StatementInvalid
Make sure what should be Retry
retry_on discard_on ActiveJob::Exceptions
Automatic Retry
Examine & Retry
In Resque
None
Status Reporting
None
None
Report every minute
Monitoring CPU Usage
None
~ Performance
Performance is a Rabbit hole
Preload associations
Minimize scope of transaction
Transaction Isolation Levels https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html https://www.postgresql.org/docs/current/static/transaction-iso.html
Avoid unnecessary callbacks
None
Example You can touch http://api.rubyonrails.org/classes/ActiveRecord/NoTouching/ClassMethods.html#method-i-no_touching after migration
Process multiple records in one job
None
Cache data in Memory
Cache data in Redis
Migrate Important things first
First 10000 users w/ most recipes
IO bound
Scale up the Database
Decrease the workers /0
Bulk Insert Bulk Upsert* * Only MySQL supports bulk upsert
zdennis/activerecord-import
Every change to make it fast
Run the WHOLE migration again
Keep CPU usage max at 75% all
~ Post Migration
Update all necessities
Redirects
Redirect tables Cookpad Redirect programs Server redirects Provider
Redirection Service cookpad/mirin
~ Stories
Cases of Email
Remove duplicate emails before migration
Remove invalid emails before migration
downcase all the emails
~ Get Site Dump
~ 100GB generated on EC2* EC2 has bandwidth limits
~ scp takes days ONLY if nothing failed within days
~ delivers encrypted disk
Migrate Millions of records
AR + transaction bulk in/upsert activerecord-import load data in file
Weeks Month-ish
Run low priority job to migrate them
When migrated User signed in
Migrate their data in high priority
None
Migrate 100K photos
How our image work
Design so it produces the same hash 4
Set the designated hash during migration instead of upload, generate
hash
Benchmark how long to finish all
X days?
Migrate them X days before in low priority
99% photos won’t change
Migrate users password to secure auth
Figure out what algorithm(s) was(ere) used
When migrated user signed in
System’s password auth will fail
Fallback to Legacy Auth
None
When password matched from legacy auth
Set his password through secure password scheme
None
Migration ~ The Future
Migration done in Ghost Table fashion
Data Dump & Generic Migration Code Only need to modeling
database
~ Takeaways
Rails provides sharp tools thanks to rails core team
Use Small objects to make your code more readable &
maintainable
Abstraction is the God of Programming! “ ” — Matthew
Mongeau @halogenandtoast
Schedule >> Fast
Schedule >> Fast Integrity >> schedule
Data Migration sounds hard
Keep it Simple Made it Easy
Do the Simplest Things “ ” — Winston Teo Yon
Wei @winstonyw
Enjoy ☕ Thank you!