Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Abril Pro Ruby

Abril Pro Ruby

Sharding presentation given at abrilproruby.com/en

Arthur Nogueira Neves

April 26, 2014
Tweet

More Decks by Arthur Nogueira Neves

Other Decks in Programming

Transcript

  1. 81 2011 2012 • 4 x Intel E5-4650 2.7 GHz

    •256GB of RAM •2 x 600GB OCZ Z-Drive •2xIntel E5640 2.67GHz •192GB of RAM •2 x 300GB OCZ Z- Drive R4 PCIe SSDs
  2. Within the block sharded models use the current shard connection

    Sharded models include Sharding::Concern, have shop_id column.
  3. around_action :with_shop ! def with_shop ! @shop = ShopManager.shop_for(request.host) !

    Sharding.with_shop(@shop) yield # Actual request happens here. end end
  4. things = [] rel = Model.preload(:checkouts, :order) .where(id: params[:id].split(+)) !

    Sharding.query_on_each_shard(rel, :on_lock => :raise) do |recs| things.concat(recs) end
  5. r = model.unscoped ! Sharding.find_in_batches_on_each_shard(r, on_lock: :ignore) do |recs| recs.each

    do |record| reserialize_record(record, params[:fields]) progress.tick end end
  6. Per shop shared lock Taken by the query across shard

    methods as they iterate, important for things that go across shards
  7. T0 T1 (+100secs) T2 T3 T4 T5 Take global lock

    or bail, shop.is_locked = true
  8. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Take

    global lock or bail, shop.is_locked = true
  9. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Jobs

    have released shared locks Take global lock or bail, shop.is_locked = true
  10. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Fork,

    copy, verify Jobs have released shared locks Take global lock or bail, shop.is_locked = true
  11. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Fork,

    copy, verify Change shop.shard_id Jobs have released shared locks Take global lock or bail, shop.is_locked = true
  12. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Fork,

    copy, verify Change shop.shard_id Jobs have released shared locks shop.is_locked = false Take global lock or bail, shop.is_locked = true
  13. T0 T1 (+100secs) T2 T3 T4 T5 Unicorns dead Fork,

    copy, verify Change shop.shard_id Jobs have released shared locks shop.is_locked = false Take global lock or bail, shop.is_locked = true
  14. Sharding checklist • How to slice data • Primary key

    generation • Connection switching • Query across shards • Rebalancing 107