Database reliability - zero downtime schema migrations with MySQL
Chinmay Naik
Founder and CEO at One2N | Building Cloud Native Solutions | Is your business scaling faster than tech can handle? DM me.
(Database reliability story 1)
You join a team as lead SRE, and the CTO asks you to manage and scale a self-managed 6-node MySQL cluster on production.
Here's a story of how you turn this around - from many database failures per month to three nines of uptime for the database.
Context
To get the context, you ask some questions:
You have many questions, but you start with these.
The CTO candidly replies.
Your plan
With this, you suggest these improvements:
Finally, migrate to SaaS offering e.g., AWS RDS.
[Each of these improvements deserves a separate post (and a conference talk). But for now, I'll write about one specific outcome.]
"Zero downtime schema migration"
Before the solution, let me talk about why schema migration on large tables is a challenge in MySQL.
Challenges in MySQL schema migration
MySQL Schema migration with a simple alter table causes many problems:
You find out that at least 7 product features are blocked due to the inability to perform schema migrations on large tables on production.
The engineering team has created PRs for these features, but these can't be merged as we currently don't have zero downtime schema migrations.
You know that you're not the first to face this problem (MySQL has been around for long, so there must be a way).
Your approach
You do the research and find two approaches and tools
You research more and prepare a document comparing the two approaches.
After much consideration, you go ahead with GitHub's gh-ost, as it gives you an asynchronous data copy mechanism, controllable switchover, and throttling support.
But what do these terms mean?
领英推荐
See, zero downtime MySQL schema migration works this way:
Both gh-ost and pt-osc work this way. But, they differ in step 3 (how data is copied from the source table to the shadow table).
pt-osc uses triggers, gh-ost uses MySQL binlogs.
Both of these mechanisms have their own pros and cons. For our use case, we went ahead with gh-ost.
Prod setup
You set up a replica of the production environment where you'd try out gh-ost and its various configuration options.
The two most imp config flags are:
You go through gh-ost documentation and even the source code to find out how it works. You read through most use cases and lurk in forums to understand production challenges. You simulate prod-like scale (400M rows) and run multiple migrations on such tables with various flags.
Fast forwarding the story:
Once you have the confidence, you perform your first migration on a 10M row table. It works well, with zero downtime. Gradually, you move on to migrating 400M row table. It takes 12+ hours, but things are mainly smooth. You document the runbook.
Outcome
Thanks to your efforts,
So far, the team has run 100s of schema migrations without any problems.
Learnings
Scaling stateless systems is easy. Stateful, less so.
Lessons:
I write such stories on software engineering.
There's no specific frequency, as I don't make up these.
If you liked this one, you might love - https://www.dhirubhai.net/pulse/building-pull-request-based-ephemeral-preview-kubernetes-chinmay-naik/
Follow me - Chinmay Naik , for more such stuff, straight from the production oven!
Oh, by the way, if you need help with database reliability and scaling, my DMs are open. We have worked at Terabyte scale when it comes to relational and non-relational databases.
This was one of the reasons I started One2N - to help growing orgs scale sustainably.
Lead Engineer @ShopUp
7 个月How you handle rename table operation, when there is active transactions? Especially for mysql 5.7
Tech Lead | Team Lead - Huiospay Limited
1 年Awesome guide?
Fighting shelfware | I will pilot your IT to save $100k
1 年This is very well written!!
MS @ CMU | SRE @Cisco | CKA | Kubernetes release team | Mongo DBA | Devops | Open Source Contributor
1 年Awesome solution