Save 37% off PRO during our Black Friday Sale! »

Ruby on Rails & Vitessによる
DBスケールアウト

D4715d085532a801bb2040ce5d9d55ec?s=47 junsumida
February 10, 2016

Ruby on Rails & Vitessによる
DBスケールアウト

ソーシャルゲームのような高負荷がかかるWebアプリケーションにおいては、何十, 何百台ものサーバーでスケールアウトするのが当然の時代になりました。特にDBサーバーでは垂直分割、水平分割による複数のDBへの負荷の分散が行われ、アプリケーション側でハンドリングすべきことが増してきています。

今回は、Google社のyoutubeの基盤として大量のクエリを捌いているVitessを、当社のゲームにおいて実際にDBサーバーのマネージ&スケールアウトに使用した話をします。主にVitess, gRPC, MySQL/KVSとの比較, Rails/ActiveRecordにおけるデータベース分割, VitessとRails, Live Resharding周りの話です。

D4715d085532a801bb2040ce5d9d55ec?s=128

junsumida

February 10, 2016
Tweet

Transcript

  1. > next Ruby on Rails & VitessʹΑΔ
 DBεέʔϧΞ΢τ Jun Sumida

    / mixi,inc. XFLAG™ ελδΦ Meetup
  2. > < next previous Jun Sumida ΤοΫεϑϥοάελδΦ ւ֎൛Ϟϯετ & ৽نλΠτϧ

    2013೥৽ଔೖࣾ
  3. > < next previous RailsΛ࢖ͬͨ͜ͱͷ͋Δํ?

  4. > < next previous RailsͰԿ͔͠ΒͷDBͷਫฏ/ਨ௚ ෼ׂΛߦͬͨ͜ͱͷ͋Δํʁ

  5. > < next previous Railsʹ͓͚ΔDB෼ׂ ‣ Octopus ‣ ਫฏਨ௚෼ׂʹରԠ ‣

    SwitchPoint ‣ Cookpad͞Μ ‣ ਨ௚෼ׂʹରԠ ‣ ActiveRecord::Turntable ‣ Drecom͞Μ ‣ ਫฏਨ௚෼ׂʹରԠ ‣ ฐࣾͰ΋࢖͍ͬͯ·͢ * https://github.com/thiagopradi/octopus * http://techlife.cookpad.com/entry/2014/08/28/194147 * https://github.com/drecom/activerecord-turntable
  6. > < next previous DB෼ׂ / shardingपΓͰۤ ࿑ͨ͜͠ͱ͋Γ·ͤΜ͔?

  7. > < next previous MySQLͷindex΍σʔλू໿΋ࣺͯ೉͍
 NoSQLͷεέʔϧੑ΋ࣺͯ೉͍

  8. > < next previous ͦ͜Ͱग़ձͬͨͷ͕

  9. > < next previous Vitess by youtube * http://vitess.io

  10. > < next previous Vitessʹ͍ͭͯ؆୯ʹ ‣ ݩʑ͸Youtube༻ͷconnection-pooling proxy ‣ ঃʑʹ͍Ζ͍Ζͳػೳ͕௥Ճ͞Ε͍ͯͬͨ

    ‣ Go੡ ‣ gRPC, BSON RPC ͰΫϥΠΞϯτͱ௨৴ ‣ PHP, Go, Python, JavaͷެࣜΫϥΠΞϯτ ‣ 2011೥͔Βyoutubeͷຊ൪؀ڥͰ࢖༻ * Youtube Vitess (Cloud Native Platform) * https://github.com/youtube/vitess
  11. > < next previous Կ͕Ͱ͖Δͷʁ

  12. > < next previous ৭ʑͰ͖·͢

  13. > < next previous Vitess͕Ͱ͖Δ͜ͱ ‣ Connection pooling ‣ QueryͷΩϟογϡ

    ‣ Queryղੳ, ةݥͳΫΤϦͷॻ͖׵͑ ‣ ߏ੒֬ೝ / εΩʔϚมߋ༻ͷ web console ‣ DBΠϯελϯεͷϞχλϦϯά ‣ Live Resharding, master failover, etc… * Youtube Vitess (Scaling MySQL with Vitess Kubernetes)
  14. > < next previous Live Resharding * http://vitess.io/resources/openworld-2015-vitess.pdf

  15. > < next previous Vitess vs MySQL ‣ ܰྔͳconnection (BSON

    vs MySQL) ‣ SQL Parser͕ةݥͳΫΤϦΛ๷ࢭ ‣ ਫฏɺਨ௚෼ׂΛαϙʔτ ‣ Rangeϕʔεͷsharding ‣ master failover΍data backupʹ΋ରԠ * http://vitess.io/overview/
  16. > < next previous Vitess vs NoSQL ‣ ୯७ͳKVSͱ͸ҟͳΓɺwhere, join,

    count, group byͳ Ͳ͕Ͱ͖Δ ‣ VTGate V3Ͱ͸·ͩcount, group byͳͲͷू໿ܥ͕ಈ͔ͳ͍ ‣ Transaction͕࢖͑Δ ‣ cross shardͳTransaction͸·ͩͰ͖ͳ͍ ‣ MySQLΛ࢖͑ΔϢʔβʔͰ͋Ε͹ֶशίετ͕௿͍ ‣ MySQLͰ࢖͑Δindexػೳ͕͢΂ͯ࢖͑Δ * http://vitess.io/overview/
  17. > < next previous Vitessͷߏ੒

  18. > < next previous app mysql vttablet mysql vttablet mysql

    vttablet * Youtube Vitess (Anatomy of a Distributed Database on Kubernetes) app app vtctl schema؅ཧ / ߏ੒֬ೝ gRPC / BSON RPC gRPC / BSON RPC vtgate ܰྔproxy / routing
  19. > < next previous ߏ੒ʹ͍ͭͯ΋͏ͪΐͬͱ͚ͩ ‣ MySQL 5.6 / MariaDB

    10.0 or higher ʹରԠ ‣ ੲ͸ΦϦδφϧͷMySQLͩͬͨ ‣ etcd or Zookeeper Λ࢖ͬͯɺSchema΍DBߏ੒ͳͲΛ ෼ࢄͯ͠ه࿥͍ͯ͠Δ ‣ Google಺෦Ͱ͸Goolgle ChubbyΛ࢖͍ͬͯΔΒ͍͠ ‣ vttablet, vtctldʹ͸web console͕෇͍͍ͯͯschemaΛ ૢ࡞ͨ͠ΓɺmysqldͷϞχλϦϯά͕Ͱ͖Δ * http://vitess.io/overview/ * https://www.youtube.com/watch?v=midJ6b1LkA0
  20. > < next previous Ͳ͏Railsͱܨ͛Δͷ͔

  21. > < next previous app mysql vttablet mysql vttablet mysql

    vttablet * Youtube Vitess (Anatomy of a Distributed Database on Kubernetes) app app gRPC / BSON RPC vtgate ܰྔproxy / routing
  22. > < next previous A high performance, open source, general

    RPC framework that puts mobile and HTTP/2 first. * http://www.grpc.io
  23. > < next previous gRPCͱ͸ ‣ ͍ΘΏΔRPCϑϨʔϜϫʔΫ ‣ HTTP/2 ඪ४ʹଇ͍ͬͯΔ

    ‣ ίΞ෦෼͸ڞ௨ɺCͰ࣮૷ ‣ 10ݴޠʹରԠ ‣ C, C++, Ruby, NodeJS, Python, PHP, Objective-C, C#, Go, Java ‣ σϑΥϧτͷσʔλߏ଄ͱͯ͠Protocol Buffers * http://www.grpc.io * https://github.com/grpc/grpc
  24. > < next previous * What is gRPC? (www.grpc.io/docs/#what-is-grpc) gRPCͷྫ

  25. > < next previous Protocol Buffers?

  26. > < next previous Protocol Buffersͱ͸ ‣ ݴޠʹ΋ɺϓϥοτϑΥʔϜʹ΋தཱͳɺߏ଄Խ͞ ΕͨσʔλΛγϦΞϥΠζ͢ΔͨΊͷ֦ுՄೳͳػ ߏ

    (௚༁) ‣ ΑΓγϯϓϧͰߴ଎Ͱখ͍͞XML (௚༁) ‣ ΋ͷ͘͢͝ࡶʹݴ͏ͱɺ࠷ۙ͸΍ΓͷJSON Schema Έ͍ͨͳ΍ͭɺͨͩ͠όΠφϦ ‣ MessagePackͱൺֱ͞ΕΔ͜ͱ͕ଟ͍Ͱ͢ * https://developers.google.com/protocol-buffers/
  27. > < next previous * Service Definition (www.grpc.io/docs/guides/concepts.html#service-definition) Protocol Buffersͷྫ

  28. > < next previous * What is gRPC? (www.grpc.io/docs/#what-is-grpc) Protocol

    Buffersͷྫ (࣮ࡍͷVitessͷ.proto file) Rubyͩͱ͜Μͳײ͡ʹͳΓ·͢
  29. > < next previous gRPC w/ Protocol Buffers ‣ ଞݴޠରԠͯ͠ΔͨΊɺClient΍Mock

    Server·ͰΛ ࣗಈੜ੒͢Δ͜ͱ͕Մೳ ‣ ௨৴ର৅ͷΞϓϦέʔγϣϯͷ۱ʑ·ͰίʔυΛಡ ·ͳͯ͘΋ClientΛ࡞Δ/͔ͭ͏͜ͱ͕Ͱ͖Δ * https://developers.google.com/protocol-buffers/
  30. > < next previous gRPCͰΫϥΠΞϯτ͕ॻ͚Δ protocol bufferͰΫϥΠΞϯτ͸ࣗಈੜ੒Ͱ͖Δ ΫϥΠΞϯτͷwrapper͚ͩRubyͰॻ͚͹͍͍ͷͰ͸? ͋ͱ͸ActiveRecordͷAdapterͱͭͳ͛Δ͚ͩͰOKͩͶ! ͜͜Ͱࢥ͍͍ͭͯ͠·ͬͨ…

  31. > < next previous ͦ͏ࢥ͍ͬͯͨ࣌ظ͕ ࢲʹ΋͋Γ·ͨ͠

  32. > < next previous VitessͷRuby ClientΛ࡞Δ্ͰͰਏ͔ͬͨ͜ͱ ‣ ҰԠ·ͩgRPC͕beta൛ɺVitess Client΋৭ʑ଍Γͳ͍ ‣

    Protocol Buffer͔ΒRubyͷίʔυ͕ࣗಈੜ੒Ͱ͖Δ͕ɺੜ੒͞ ΕΔίʔυͷ໊લۭ͕ؒRubyͱ૬ੑ͕ѱ͘ɺࣗಈੜ੒͞Εͨ ίʔυΛ͍͡Γճ͢Ӌ໨ʹ ‣ Mock serverͷΑ͏ͳ΋ͷ΋࡞ΕΔ͕ɺbinaryͷarrayΛ͏·͘ σ/γϦΞϥΠζͰ͖ͳ͍ͷ͔ΤϥʔΛى͜͢ ‣ ͨ·ʹMock server͕segvͰࢮ͵ ‣ ͜ͷลͷ໰୊ͷ੾Γ෼͚͕Ұ൪ਏ͔ͬͨ ‣ ͦ΋ͦ΋RubyͷgRPC gem͕versionʹΑͬͯίϯύΠϧͰ͖ͳ͔ͬͨΓ͢Δ ‣ Railsͷίʔυ(ಛʹActiveRecord)ΛԆʑͱ௥͏Ӌ໨ʹ ‣ ͓͔͛ͰActiveRecordपΓͷίʔυʹ͸ৄ͘͠ͳΕ·ͨ͠^^
  33. > < next previous Rails mysql vttablet mysql vttablet mysql

    vttablet vtctl gRPC / BSON RPC gRPC / BSON RPC vtgate ruby
 client schema༻ mysql Rails w/ Vitess
  34. > < next previous ͜Ε͔Βͷ՝୊ ‣ ·ͩ৭ʑͱݕূத (2016 2݄ݱࡏ) ‣

    ήʔϜͰ࢖ͬͨ৔߹ͷύϑΥʔϚϯεͳͲ ‣ εΩʔϚ؅ཧ͋ͨΓ΋ ‣ ڊେͳΞϓϦέʔγϣϯͳͷͰɺ·ͩ·ͩClientʹ଍Γͳ͍ػ ೳ͕͋Δ ‣ GoogleͷBorgʹ࠷దԽ͞Ε͍ͯΔͷͰɺAWS EC2ɺmonitoring πʔϧ, ߏ੒؅ཧπʔϧͳͲͱͲ͏ંΓ߹͍Λ͚ͭΔ͔ ‣ ӡ༻ϑϩʔ͕ͳΔ΂͘มΘΒͳ͍Α͏ʹ͍ͨ͠
  35. > < next previous ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  36. > < next previous ࣭໰͕͋Ε͹͓ؾܰʹͲ͏ͧʂ