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

Tuning GraphQL on Rails

Tuning GraphQL on Rails

Kaigi on Rails 2024でお話しました。

Kazuhiko Yamashita

October 26, 2024
Tweet

More Decks by Kazuhiko Yamashita

Other Decks in Programming

Transcript

  1. ࠓ೔࿩͢͜ͱ 1. ݱ৔঺հ 2. ൃੜ͍ͯͨ͠՝୊ 3. ͲͷΑ͏ʹղܾ͔ͨ͠ 4. ࣮ફN+1ղফ 5.

    ೔ʑͷӡ༻ 6. զ͕Ոʹ͓͚Δચ୕पظͷ໰୊Λ͍͔ʹղܾ͔ͨ͠
  2. minneͷΞϓϦέʔγϣϯߏ੒֓ཁ browser mobile app Web UI Rails Next.js Rails API

    ϞϊϦεߏ੒ͳRails ※ਤ্͸Rails͕̎ͭ͋ΔΑ͏ʹݟ͑·͕͢ɺͦΕ͸࢒૾Ͱ͋Γɺ࣮ࡍ͸1ϦϙδτϦͰಉ͡ίʔυͰ͢ɻ WEB UIͷϨϯμϦϯάͱɺAPIͷෛՙɺτϥϑΟοΫ͕ҟͳΔͨΊɺϦιʔε෼཭͍ͯ͠·͢ɻ
  3. minneͷγεςϜߏ੒֓ཁ ΦϯϓϨϛεͱAWSͷϚϧνΫϥ΢υߏ੒ on premises AWS NKE(k8s) OpenStack Web Web API

    API EKS(k8s) EC2 Batch Direct
 Connect RDS Elastic xxx ѹ౗తͳ௿ίετ͕ͩɺ ϓϥοτϑΥʔϜͷ؅ཧίετ͕͋Δ ίετ͸ֻ͔Δ͕ɺσʔλετΞͷ ϚωʔδυɾαʔϏεͳͲӡ༻ෛՙܰݮ Web API
  4. ͲͷΑ͏ʹվળ͔ͨ͠ʁ query { orders { id customer { name }

    } } select * from orders; select * from customers where customers_id = 1; select * from customers where customers_id = 2; select * from customers where customers_id = 3; … select * from customers where customers_id = 5000; GraphQLͰΑ͋͘ΔN+1ͳΫΤϦ
  5. select * from orders; select * from customers where customers_id

    = 1; select * from customers where customers_id = 2; select * from customers where customers_id = 3; … select * from customers where customers_id = 5000; Ͳ͜Λ஗Ԇ࣮ߦͤ͞Δ͔ʁ ID͚ͩઌʹूΊͱ͍ͯɺ ΫΤϦ͸͋ͱ͔Β஗Ԇ࣮ߦͯ͠ ·ͱΊͯ౤͍͛ͨ
  6. Promise.sync=Promiseͷ࣮ߦ query { orders { id customer { name }

    } } ্͔Βॱ൪ʹઙ͍ॱʹPromise.sync͕ίʔϧ͞ΕΔ 1. Ordersͷϓϩϛε͕࣮ߦ͞ΕΔ 2. Customerͷϓϩϛε͕ ࣮ߦ͞ΕΔ
  7. in۟ʹ5000΋Ҿ਺౉͢ͳ͸ॾઆ͋Δ select * from orders; select * from … where

    customers_id in(1, 2…5000); ActiveRecord::Associations::PreloaderͰ in۟ʹ·ͱΊΒΕΔ
  8. Πϯελϯεϝιου query { orders { id customer { name isLoyal

    } } } select 1 As one from sales where customers_id = 1; select 1 As one from sales where customers_id = 2; select 1 As one from sales where customers_id = 3; … ഁ໓ϩυϦήεʂʂʂʂʂ
  9. ύϑΥʔϚϯεͷ໰୊ͷ୳͠ํ query { orders { id customer { name isLoyal

    } } } ΫΤϦͷਂ͕͞ઙ͍ॱʹΫΤϦ͕࣮ߦ͞ΕΔ
  10. SLIͱSLO SLI = Service Level Indicator αʔϏεϨϕϧࢦඪ SLO = Service

    Level Objective αʔϏεϨϕϧ໨ඪ GraphQLͷϨΠςϯγΛSLIʹઃఆ