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
24
13k
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
2
680
RubyKaigi Decks
osyoyu
0
62
The depths of profiling Ruby - RubyKaigi 2024
osyoyu
3
4.4k
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
1.1k
令和最新版 Ruby プロファイラ "Pf2" のご紹介
osyoyu
0
440
ちょっといい感じの Ruby プロファイラがほしい (RubyKaigi 2023 follow up)
osyoyu
1
350
Hacking and Profiling Ruby for Performance - RubyKaigi 2023
osyoyu
0
120
Other Decks in Programming
See All in Programming
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Jakarta EE meets AI
ivargrimstad
0
650
イベント駆動で成長して委員会
happymana
1
320
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Contemporary Test Cases
maaretp
0
140
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
最新TCAキャッチアップ
0si43
0
190
ヤプリ新卒SREの オンボーディング
masaki12
0
130
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
What's in a price? How to price your products and services
michaelherold
243
12k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Faster Mobile Websites
deanohume
305
30k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Statistics for Hackers
jakevdp
796
220k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
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