2018年5月16日 星期三
Rails deployment 如何避免 downtime?
有哪些造成 downtime 的可能性?
1. Code 有問題
2. Migration 跑完後 Web Server 還沒 restart
3. Deploy 完後需要重啟 Server
4. Request 沒有被 queue 起來
可能的解決方式:
## 1. Code 有問題
1. 勤勞寫 Test
2. Manually test
3. Code review
4. Staging -> Production
## 2. Migration 跑完後 Web Server 還沒 restart
有篇文章寫得很不錯, cover 了一些之前沒想到的技巧:
https://blog.codeship.com/rails-migrations-zero-downtime/
1. read only method
2. 先讓 AR ignore columns 後再刪除
```
class User
def self.columns
super.reject { |c| c.name == "notes" }
end
end
```
3. renaming column 時可以用 `super || attributes["new_name"]`
```
def first_name
super || attributes["fname"]
end
```
4. 加 index 時有時會卡住 migration 造成問題,可以考慮 create it concurrently
## 3. Deploy 完後需要重啟 Server
1. 可以先把新的 instances run 起來,然後把 request 切換到新的 instances 後再 shut down 舊的
2. 靠 Load balancer 把 request queue 起來等重啟完再執行
## 4. Request 沒有被 queue 起來
之前就遇過因為 ActionCable and AWS ELB 的各種坑
https://everyday1percent.blogspot.com/2018/04/actioncable-and-aws-elb.html
所以也是有這個可能性,解決方式是要讓 load balancer 確保有 queue request 等 server 恢復時才執行
剩下有想到再補充
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言