Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
隣の API のデータを Rails らしく、しなやかに扱う / Handling next-...
Search
osyoyu
October 22, 2022
Programming
0
310
隣の API のデータを Rails らしく、しなやかに扱う / Handling next-door API data in Rails
osyoyu
October 22, 2022
Tweet
Share
More Decks by osyoyu
See All by osyoyu
都市伝説バスターズ「WebアプリのボトルネックはDBだから言語の性能は関係ない」 - Kaigi on Rails 2024
osyoyu
14
7.8k
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
1
640
RubyKaigi Decks
osyoyu
0
53
The depths of profiling Ruby - RubyKaigi 2024
osyoyu
3
3.7k
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
1.1k
令和最新版 Ruby プロファイラ "Pf2" のご紹介
osyoyu
0
420
ちょっといい感じの Ruby プロファイラがほしい (RubyKaigi 2023 follow up)
osyoyu
1
320
Hacking and Profiling Ruby for Performance - RubyKaigi 2023
osyoyu
0
110
Other Decks in Programming
See All in Programming
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
120
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
330
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
180
Piniaの現状と今後
waka292
5
1.4k
VR HMDとしてのVision Pro+ゲーム開発について
yasei_no_otoko
0
100
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
320
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
15
3.8k
hotwire_or_react
harunatsujita
6
3.5k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
0
220
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
Server Driven Compose With Firebase
skydoves
0
360
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.4k
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
5
140
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
Code Reviewing Like a Champion
maltzj
519
39k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Adopting Sorbet at Scale
ufuk
73
9k
Docker and Python
trallard
40
3.1k
Automating Front-end Workflow
addyosmani
1365
200k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
0
34
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Transcript
Daisuke Aritomo (@osyoyu) / Kaigi on Rails 2022 ྡͷ API
ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏
@osyoyu Ͱ͢ • ΫοΫύουͰຖ Rails Λॻ͍ͯ·͢ • ී௨ͷ Ruby ͕͖
• ISUCON Ͱ Ruby Λ༏উ͍ͤͨ͞
ྡͷ API ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏
API ΞΫηεͳ͠ͰαʔϏεΛ࡞Εͣ ୭͕ API αʔόʔͱΫϥΠΞϯτͷ྆ํΛ࡞Δ େ API ࣌
API αʔόʔΛ࡞Δͱ͖ ϓϥΫςΟεཱ͕֬͞Ε͍ͯΔ
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖
ҰํɺΫϥΠΞϯτΛ࡞Δͱ͖ API ΫϥΠΞϯτͷ࡞Γํ ඞͣ͠ཧ͞Ε͍ͯͳ͍
ྡͷ API ͷσʔλΛ Rails Β͘͠ɺ͠ͳ͔ʹѻ͏ ͍͍ײ͡ͷ API ΫϥΠΞϯτͷ࡞Γ͔ͨ
ʮ͍͍ײ͡ͷ API ΫϥΠΞϯτͷ࡞Γํʯ ΫοΫύουͰ͋Γ͕ͪͳࡐΛྫʹ έʔεελσΟͯ͠ΈΑ͏
ͲͷΑ͏ͳ Ruby ΠϯλʔϑΣʔεʹ͢Δ͔ʁ Ϩγϐίϯςετͷ microservice ͷྫ User (MySQL) Recipe
Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) N 1 N 1 N 1 1 1
• ҟͳΔιʔεछผͷ en ti ty ҟͳΔऔಘํࣜΛఏڙ͢Δ • MySQL
༝དྷ AR::Base Λ ܧঝ͢ΔΫϥεͰදݱ • ֎෦༝དྷผͷΫϥεͰදݱ (i.e. AR::Base Λܧঝ͠ͳ͍) (1) ιʔεͷछผʹண͢Δ User (MySQL) Recipe Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) N 1 N 1 N 1 1 1
None
has_one ૉʹදݱͰ͖ͦ͏
belongs_to Ͱ N+1 ʹͳΓ͕ͪ has_one ૉʹදݱͰ͖ͦ͏ has_manyͱ belongs_toͷෳ߹
(1) ιʔεͷछผʹண͢Δ ੑ࣭͕ҟͳΔ (DB/API) σʔλ͕֤ʑʹదͨ͠ߏΛͭ ϓϩάϥϜͷڍಈΛ૾͍͢͠ 👍 JOIN ؔ࿈
(associa ti on) Λදݱ͢Δͷʹແཧ͕ੜ͡Δ 🤔
• ಉҰͷυϝΠϯͷ en ti ty ಉҰͷऔಘํ๏Λఏڙ͢Δ • ΞϓϦͷυϝΠϯ্ͳΒ
ͯ͢ Ac ti veRecord-like ͳ औಘํࣜΛఏڙ͢Δ (2) υϝΠϯڥքʹண͢Δ Tweet (API) User (MySQL) Recipe Contest (MySQL) Recipe (API) Recipe ContestEntry (MySQL) 1 N 1 N 1 1
ਓྨͷྺ࢙: ActiveResource, Her, ... • Ac ti veResource • RESTful
API Λ͍͍ײ͡ʹ model ͬΆͯ͘͘͠ΕΔ • Rails 3.2 ·Ͱͷඪ४ػೳͩͬͨ • Her • ͜Εಉ͡Α͏ͳతͷ Gem • ΄͔ʹ Ac ti veRestClient, Spyke, Flexirest ͳͲ͕͋Δ
ਓྨͷྺ࢙: ActiveResource, Her, ... • ͜ΕΒ "Res tf ul API"
Λେલఏʹ͓͍͍ͯΔ • ಛఆͷ URL ߏΛٻΊΒΕΔ • ͬͺΓ JOIN ͦ͜·ͰಘҙͰͳ͍
(2) υϝΠϯڥքʹண͢Δ ؔ࿈͢ΔσʔλʹಉҰͷૢ࡞ମܥͰΞΫηεͰ͖Δ ʹ σʔλͷར༻͕ࣗવͳܗͰهड़Ͱ͖Δ 👍 ຊདྷҟͳΔૢ࡞ΛಉҰͷૢ࡞ʹݟ͔͚͍ͤͯΔͷͰ ࣮ʹແཧ͕ग़͖͕ͯͪ 🤔
(2) υϝΠϯڥքʹண͢Δ ؔ࿈͢ΔσʔλʹಉҰͷૢ࡞ମܥͰΞΫηεͰ͖Δ ʹ σʔλͷར༻͕ࣗવͳܗͰهड़Ͱ͖Δ 👍 ຊདྷҟͳΔૢ࡞ΛಉҰͷૢ࡞ʹݟ͔͚͍ͤͯΔͷͰ ࣮ʹແཧ͕ग़͖͕ͯͪ 🤔
👊👊 ࣮ͰΓӽ͑Εղܾ 👊👊
ͭͬͯ͘ΈΑ͏ʂ
AR ෩ API ΫϥΠΞϯτΛͭͬͯ͘ΈΑ͏ • ֎෦ API ͕ఏڙ͢Δ Recipe ʹ͍ͭͯ
• Recipe. fi nd(1) ͷΑ͏ͳܗͰΫΤϦͰ͖Δ • @recipe. ti tle ͷΑ͏ͳܗͰϑΟʔϧυΛಡΊΔ • Ϧιʔεαʔόʔʹಛఆͷߏ (RESTful) Λཁٻ͠ͳ͍ • JOIN (has_many, belongs_to, ...) ͏·͘͜ͳͤΔ • Α͏ͳʢجຊతͳʣͷΛͭͬͯ͘ΈΑ͏
ͨͱ͑ AR ෩ʹ͢Δ߹Ͱ͋ͬͯɺτϥϯεϙʔτ͖͢ ϦτϥΠॲཧͳͲ͍ͨ͠͠ɺgRPC ҠߦͳͲָʹͳΔ ·ͣૢ࡞తͳΫϥΠΞϯτΛ༻ҙ Level 1
ͦΕΛͬͯ Recipe.find ͰϦΫΤετ Level 1 ҰԠ has_one తͳϝιου ΫΤϦ༻ͷϝιου (
fi nd, where) recipe# ti tle ͳͲͰϑΟʔϧυΞΫηε
Controller Ͱผݸʹॲཧ͢ΕճආՄೳͰ͋Δ͕…… includes(:recipes) ͷΑ͏ʹهड़͍ͨ͠ ͕͢͞ʹ·࣮ͩ༻త͡Όͳ͍͔ User Recipe Contest Recipe
Recipe ContestEntry recipes (API) ΛԿϦΫΤετͯ͠͠·͏😔ɹɹɹɹ
• Recipe. fi nd ͷ෦ͰΩϡʔΠϯά͢Δ • Recipe. fi nd ͔Βͻͱ·ͣ
Promise తͳΦϒδΣΫτ͕ฦΔ • exAspArk/batch-loader Λར༻ • Recipe. fi nd ͷฦΓʹରͯ͠ # ti tle ͳͲΛݺͿͱɺอཹ͞Ε͍ͯͨϦΫΤετ͕·ͱΊͯඈͿ BatchLoader Ͱ eager loading ͢Δ Level 2
has_one :recipe Ͱهड़͍ͨ͠…… has_many :recipes Ͱهड़͍ͨ͠……
API ຊͷ ActiveRecord ʹͪ͠Ό͏ Level 3 • ຊͷ Ac ti
veRecord Ϋϥεʹͯ͠͠·͏ ͜ͱͰ has_many, belongs_to ͳͲ͕͑Δ • ͜ͷٕΛ͑Δ͔ෆม݅࣍ୈ • ͜͜Ͱ "recipe.user_id ͕ෆมͰ͋Δ" • (͔ͭ recipe.user_id ͕ FK Ͱ͋Δ͜ͱ) • ෆม݅ͷ෦͚ͩςʔϒϧʹೖΕΔ
• ݸਓతʹ AR ෩Ͱͳ͘ɺͬͱૢ࡞తͳܗ͕ྑ͍ͱࢥ͏ • APIClient.update(id: 1, {name: "nanika"}) •
τϥϯβΫγϣϯ͕ඞཁͳΒ1ͭͷDBͷ౷߹Λߟ͑Δ͖ • ΫϥΠΞϯτͷߏʹؔͳ͘ɺ͞ΜτϥϯβΫγϣϯࠔ ͱ͜ΖͰߋ৽ɾআʁ Others
མͪึͻΖ͍ɾࡉ͔͍ Others • AR ෩ͷΦϒδΣΫτΛ༻ҙ͢ΔʹͤΑɺescape hatch ༻ҙͨ͠΄͏͕͍͍ • Ac ti
veModel::Model ʹ͍ͭͯ • ͓ΈͰ include ͢ΔͱΑ͍Ͱ͠ΐ͏ • valida ti on callbacks, rou ti ng ͳͲʹؔ࿈͢Δαϙʔτ͕͍͖ͭͯ·͢ • cookpad/expeditor ͰߋͳΔߴԽΛૂ͏
·ͱΊ ಉҰυϝΠϯͷen ti tyΛಉҰͷૢ࡞ମܥͰѻ͑Δͱؾ͍͍࣋ͪ
We are hiring! cookpad.jobs