ø downtime migrations - ruby conf 2015

57
ø downtime Jônatas Davi Paganini jonatas jonatasdp

Upload: jonatas-paganini

Post on 14-Apr-2017

434 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: ø Downtime migrations  - Ruby Conf 2015

ø downtimeJônatas Davi Paganini

jonatasjonatasdp

Page 2: ø Downtime migrations  - Ruby Conf 2015

8 dev teams

Page 3: ø Downtime migrations  - Ruby Conf 2015

10 deploys / day

Page 4: ø Downtime migrations  - Ruby Conf 2015

RD Station

Page 5: ø Downtime migrations  - Ruby Conf 2015

+2m mail / day

Page 6: ø Downtime migrations  - Ruby Conf 2015

+700 external services

integrations

Page 7: ø Downtime migrations  - Ruby Conf 2015

+N callbacks

Page 8: ø Downtime migrations  - Ruby Conf 2015

Availability is

REQUIRED

Page 9: ø Downtime migrations  - Ruby Conf 2015

99,9%

99,5%

99,0%

98,6%

98,0%

97,0%

99,99%

00:43:00

03:36:00

07:12:00

10:00:00

14:24:00

21:36:00

00:04:32

Availability

Page 10: ø Downtime migrations  - Ruby Conf 2015

99.999???

Page 11: ø Downtime migrations  - Ruby Conf 2015

availability =( total_time - timeout / total_time ) * 100

Page 12: ø Downtime migrations  - Ruby Conf 2015

ø downtime

Page 13: ø Downtime migrations  - Ruby Conf 2015

mindset

Page 14: ø Downtime migrations  - Ruby Conf 2015

NO schedulemaintenance

Page 15: ø Downtime migrations  - Ruby Conf 2015

avoid: all || nothing

Page 16: ø Downtime migrations  - Ruby Conf 2015

build reversible things

Page 17: ø Downtime migrations  - Ruby Conf 2015

build incremental migrations

Page 18: ø Downtime migrations  - Ruby Conf 2015

RubyConf 2012

Page 19: ø Downtime migrations  - Ruby Conf 2015

compatible versions

Page 20: ø Downtime migrations  - Ruby Conf 2015

http://shipit.resultadosdigitais.com.br/blog/migrando-com-zero-downtime

talk is <cheap>!

Page 21: ø Downtime migrations  - Ruby Conf 2015

show me the code!!!

Page 22: ø Downtime migrations  - Ruby Conf 2015

Migration

add_column :people, :full_name, :string

Page 23: ø Downtime migrations  - Ruby Conf 2015

Update data

Person.all.each do |person| person.full_name = "#{person.first_name} #{person.last_name}" person.saveend

Page 24: ø Downtime migrations  - Ruby Conf 2015

350 million updates!!!

Page 25: ø Downtime migrations  - Ruby Conf 2015

WTF time?!

Page 26: ø Downtime migrations  - Ruby Conf 2015

Let’s improve it!

Person.all.each do |person| person.full_name = "#{person.first_name} #{person.last_name}" person.saveend

Page 27: ø Downtime migrations  - Ruby Conf 2015

Select RIGHT attributes!

Page 28: ø Downtime migrations  - Ruby Conf 2015

Update 0.1

Person.select("id,first_name,last_name").each do |person| person.full_name = "#{person.first_name} #{person.last_name}" person.saveend

Page 29: ø Downtime migrations  - Ruby Conf 2015

update_attribute instead of save

Page 30: ø Downtime migrations  - Ruby Conf 2015

Update 0.2

Person.select("id,first_name,last_name").each do |person| person.update_attribute "full_name", "#{person.first_name} #{person.last_name}"end

Page 31: ø Downtime migrations  - Ruby Conf 2015

find in batches

Page 32: ø Downtime migrations  - Ruby Conf 2015

avoid transaction

overhead

Page 33: ø Downtime migrations  - Ruby Conf 2015

Update 0.3

Person.select("id,first_name,last_name").find_in_batches do |people| People.transaction do people.each do |person| person.update_attribute "full_name", "#{person.first_name} #{person.last_name}" end endend

Page 34: ø Downtime migrations  - Ruby Conf 2015

FAILURE resilient

Page 35: ø Downtime migrations  - Ruby Conf 2015

Update 0.4

Person.where(full_name: nil).select("id,first_name,last_name").find_in_batches do |people| People.transaction do people.each do |person| person.update_attribute "full_name", "#{person.first_name} #{person.last_name}" end endend

Page 36: ø Downtime migrations  - Ruby Conf 2015

explore connection pool

Page 37: ø Downtime migrations  - Ruby Conf 2015

Update 0.5

update_sql = "UPDATE people set full_name ="

Person.where(full_name: nil).select("id,first_name,last_name").find_in_batches do |people| People.transaction do people.each(ActiveRecord::Base.connection_config[:pool]) do |person| ActiveRecord::Base.connection_pool.with_connection do |conn| set_full_name = conn.quote("#{person.first_name} #{person.last_name}") conn.execute("#{update_sql} #{set_full_name} where id = #{person.id} ") end end endend

Page 38: ø Downtime migrations  - Ruby Conf 2015

model hook

Page 39: ø Downtime migrations  - Ruby Conf 2015

Model

class Person < ActiveRecord::Base before_save :update_full_name

def update_full_name self.full_name = "#{first_name} #{last_name}" endend

Page 40: ø Downtime migrations  - Ruby Conf 2015

rollout control

Page 41: ø Downtime migrations  - Ruby Conf 2015

Rollout

class Rollout def enabled?(feature, context) redis.sismember(feature, context) rescue Redis::BaseError false end

def enable(feature, context) redis.sadd(feature) end def disable(feature, context) redis.srem(feature) end

private def redis Redis::Namespace.new("rollout", redis: $redis) endend

Page 42: ø Downtime migrations  - Ruby Conf 2015

rollout use

Page 43: ø Downtime migrations  - Ruby Conf 2015

if:

before_save :update_full_name, if: -> { Rollout.enabled? "started_migration", "global" }

Page 44: ø Downtime migrations  - Ruby Conf 2015

deploy steps

Page 45: ø Downtime migrations  - Ruby Conf 2015

smoothlysteps

Page 46: ø Downtime migrations  - Ruby Conf 2015

PR #1

add new column

Page 47: ø Downtime migrations  - Ruby Conf 2015

PR #2

write old & new columns

Page 48: ø Downtime migrations  - Ruby Conf 2015

PR #3

migrate data

Page 49: ø Downtime migrations  - Ruby Conf 2015

PR #4

read from new column

Page 50: ø Downtime migrations  - Ruby Conf 2015

PR #5

remove old column

Page 51: ø Downtime migrations  - Ruby Conf 2015

PR #6

remove rollout code, cache

Page 52: ø Downtime migrations  - Ruby Conf 2015

Conclusion

Page 53: ø Downtime migrations  - Ruby Conf 2015

Embrace migrations ● smoothly● incremental● safe● resilient

Page 54: ø Downtime migrations  - Ruby Conf 2015

Avoid migrations

● all || nothing● ! roll outable features

Page 55: ø Downtime migrations  - Ruby Conf 2015

REFERENCES

blog.codeship.com/rails-migrations-zero-downtime/

shipit.resultadosdigitais.com.br/blog/migrando-com-zero-downtime/

confreaks.tv/videos/railsconf2012-zero-downtime-deploys-for-rails-apps

Page 56: ø Downtime migrations  - Ruby Conf 2015

?

Page 57: ø Downtime migrations  - Ruby Conf 2015

Thanks!jonatasjonatasdp

ideia.meshipit.resultadosdigitais.com.br