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
330
隣の 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
26
15k
プロファイラ開発者と見る「推測するな、計測せよ」 - YAPC::Hakodate 2024
osyoyu
6
690
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
2
790
RubyKaigi Decks
osyoyu
0
89
The depths of profiling Ruby - RubyKaigi 2024
osyoyu
3
5.3k
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
1.2k
令和最新版 Ruby プロファイラ "Pf2" のご紹介
osyoyu
0
510
ちょっといい感じの Ruby プロファイラがほしい (RubyKaigi 2023 follow up)
osyoyu
1
380
Hacking and Profiling Ruby for Performance - RubyKaigi 2023
osyoyu
0
150
Other Decks in Programming
See All in Programming
ASP.NET Core の OpenAPIサポート
h455h1
0
120
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
テストコード書いてみませんか?
onopon
2
340
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
1.9k
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
130
Flatt Security XSS Challenge 解答・解説
flatt_security
0
740
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
7
1.4k
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
940
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1.1k
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1.1k
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Designing Experiences People Love
moore
139
23k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Typedesign – Prime Four
hannesfritz
40
2.5k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Building Your Own Lightsaber
phodgson
104
6.2k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
What's in a price? How to price your products and services
michaelherold
244
12k
Making the Leap to Tech Lead
cromwellryan
133
9k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
210
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