or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Large Table Migration
Hi, I am Steven Huang (@steventtud).
A backend developer working for Splashtop.
Splashtop
Splashtop 是一家遠端桌面公司。
(remote desktop access solution)
OK! 接下來回到今天的主題!
Large table migration
What is migration?
migration 中針對已經建立的的 table 做修改的動作,就會使用到 Mysql 的 alter table 指令。
a migration
輸入
rake db:migrate
後會產生這樣的 sql 指令從以上可以了解
alter table 就是修改已存在的 table
資料量小的時候 alter table 很簡單
但如果 Table 資料量達千萬級(rows > 10 millions)
而且有讀寫分離的情況下(has read replicas)
Alter Table 可能成為你的惡夢。
(altering table would be your nightmare)
這次的 Talk 將會分享如何克服讀寫分離的 slave lag,成功的alter table 大型資料庫。
通常我們不會透漏公司資料量的規模
但是我剛剛在逛官網的時候…
這樣大致了解目前敝公司處理的資料量
Outline
在 Table 資料量達千萬級並有持續寫入的情況下該如何做 migration
Solution:
lhm - Online MySQL schema migrations
How a normal migration works?
一般的 migration 會使用
alter table
來修改資料庫在 alter table 時,原本的 Table 是無法寫入的。
新 Table 修改完畢後,將原本的 Table 與新 Table 交換名稱,並將原始的 Table 刪除。

直接使用 alter table 會有什麼問題?
Lhm is a solution for this situation.
How lhm works?
Lhm 的作法是使用 MySQL Trigger,當原始 Table 寫入的時候,mysql trigger 會同步到新的 Database。
等到結束的時候兩個 Table 互換,write lock 持續的時間很短。
Example of lhm migration
當你的資料庫有讀寫分離,又使用 LHM 的話可能會發生什麼事情?
What is read replica?
在大型系統中,為了讓主要的 Database 記憶體和CPU的使用量更穩定,通常會使用讀寫分離來分擔壓力
如果你在讀寫分離的 Database 中使用 lhm,並且沒有實際去了解 lhm 的機制。很可能遇到的問題是:
如何在這種極端的情況下,安全的使用 Migration?
lhm 在新版中推出了一個 SlaveLag Throttler
SlaveLag Throttler 大致上的概念是:
Lhm::Throttler::SlaveLag 的運作原理
這邊可以看 Lhm::Throttler::SlaveLag 的原始碼
其中最關鍵的一段
步驟拆解
初始化:
每次執行的時候會偵測最大的 slave lag
第一次
第二次
第三次
第4次
第5次
95%以上都會如前面兩個方式執行,例外為:
most pictures in this slide are from a article Rails Migrations at Scale
Thanks for listening.