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

FintechとRailsとgRPCと

 FintechとRailsとgRPCと

at Rails developer meetup 2018 Day 2

Sample code: https://github.com/cnosuke/bank_sample

cnosuke

March 25, 2018
Tweet

More Decks by cnosuke

Other Decks in Technology

Transcript

  1. 'JOUFDIͱ 3BJMTͱ H31$ͱ Shinnosuke Takeda a.k.a. @cnosuke Rails developer meetup

    2018 day2 ຊߨԋٴͼຊࢿྉʹ͓͍ͯɺࢲ͕ࣾһͱͯؔ͠༩͢Δגࣜձ͓ࣾۚͷσβΠϯͷ ౤ࢿҰ೚ӡ༻αʔϏεʮ5)&0ʯʹ͍ͭͯݴٴ͢Δ৔߹͕͋Γ·͢ɻ ͜Ε͸ɺࢲͷઆ໌಺༰ΛΑΓਂ͘͝ཧղ͍ͨͩ͘໨తͰݴٴ͍ͯ͠Δ΋ͷͰ͋Γɺ ಉ౤ࢿҰ೚ӡ༻αʔϏεʮ5)&0ʯͷਪ঑΍ק༠Λߦ͏΋ͷͰ͸͋Γ·ͤΜɻ ·ͨɺಉ౤ࢿҰ೚ӡ༻αʔϏεʮ5)&0ʯʹؔ͢Δࢲͷݟղ͸ɺ גࣜձ͓ࣾۚͷσβΠϯͱ͸શؔ͘܎ͳ͘ɺࢲݸਓͷ΋ͷͰ͢ɻ
  2. @cnosuke ͠Μͷ͚͢/͠ʔͷ͚͢ גࣜձ͓ࣾۚͷσβΠϯ SRE Team Leader / Software Engineer •

    2011 WebडୗܥͷελʔτΞοϓ΍ΔɻRoR͸͜ͷࠒ͔Βɻ • 2013 Cookpad (ձһࣄۀ෦, ৽نࣄۀ, ޿ࠂ഑৴ج൫౳) • 2015 ஜ೾େӃCS (म࢜) • 2016 גࣜձ͓ࣾۚͷσβΠϯ (SRE/SWE)
  3. ։ൃνʔϜ͸Ͳ͏ͳΔͱΑ͍͔ w ҆৺҆શͳྖҬΛ୲౰͢ΔΤϯδχΞνʔϜ w ೖग़ۚ؅ཧ ূ݊ޱ࠲؅ཧ w 6*69ͱ͍ͬͨɺϢʔβʹਖ਼໘͔Β޲͖߹͏νʔϜ w ϑϩϯτΤϯυ

    ΞϓϦ޲͚"1* ৽نొ࿥ɾޱ࠲։ઃϑϩʔ w ۚ༥γεςϜؔ࿈ͷνʔϜ w ʮϩϘΞυόΠβʔʯͷֵ৽෦෼ w ࣮ࡍʹࢢ৔Ͱূ݊ങ͍෇͚Λ͢ΔγεςϜ౳ʑ
  4. Conway’s law > Organizations which design systems ... are constrained

    to produce designs which are copies of the communication structures of these organizations. “γεςϜΛઃܭ͢Δ૊৫͸ɺͦͷߏ଄Λͦͬ͘Γ·Ͷͨߏ଄ͷ ઃܭΛੜΈग़ͯ͠͠·͏” Conway, Melvin E. (April 1968), "How do Committees Invent?"
  5. CloudFront 8FCGSPOU DC(ΦϯϓϨ) ূ݊؅ཧ αʔϏε ޱ࠲؅ཧ αʔϏε %# 41"QQ"1* ϢʔβೝূαʔϏε

    ܖ໿ॻ؅ཧαʔϏε ೖۚɾੵཱॲཧαʔϏε ఏܞۜߦ޲͚"1* ೖग़ۚ؅ཧDB ܖ໿ॻ؅ཧDB ౳ʑ… ܖ໿ॻS3 ఏܞઌாථ ౳ʑ… Firebase AuthN ূ݊όοΫΦϑΟεγεςϜ(֎෦) ূ݊ϒϩʔΧʔ(ূ݊ങ͍෇͚౳) ౳ʑ…
  6. SREͷ࢓ࣄΛݮΒ͢ w ग़དྷΔݶΓ*OGSBBT$PEF͢Δ w 3PVUF $MPVE'SPOU *". %#4DIFNBʜ w ΄ͱΜͲDPEFOJ[FUPPMTͷ͓͔͛ɻ࠷ߴɻ

    w มߋ͢Δ࣌͸ɺجຊతʹϓϧϦΫΤετΛ΋Β͏ɻ w ྫ͑͹*".Ͱݖݶ෇͚ͯཉ͍࣌͠ͱ͔͸ͦͷਓʹ ௚઀ϓϧϦΫΤετ΋Β͏
  7. ͜͜·Ͱͷ·ͱΊ • THEO ͸ Microservice ԽΛਐΊ͍ͯ·͢ • αʔϏε͝ͱͷ҆ఆੑͷج४΍ɺϦϦʔεαΠΫϧ • Microservice

    ͸ Kubernetes Ͱࢧ͑Δ • αʔϏε͕ଟ͍ͱSREࢮʹͦ͏ʹͳΔͷͰ͍͔ʹ ͯ͠࢓ࣄΛݮΒ͔͕͢ॏཁ
  8. RPC > Remote procedure call is the synchronous language-level transfer

    of control between programs in disjoint address spaces whose primary communication medium is a narrow channel. ଞͷΞυϨεۭؒʹ͋ΔॲཧΛ࣮ߦ͢Δ͜ͱ #SVDF+BZ/FMTPO3FNPUF1SPDFEVSF$BMM IUUQTEMBDNPSHDJUBUJPODGN JE
  9. RPC as HTTP API APIͷઃܭํ਑ σʔλͷදݱܗࣜ JSON, XML, Protocol Buffers,

    … REST, RPC, … ,.BUTVNPUP lH31$ͱ1SPUPDPM#V⒎FSTͰҰຯҧ͏"OESPJE௨৴z %SPJE,BJHJIUUQTTQFBLFSEFDLDPNLNBUTQSPUPCVG
  10. RPC as HTTP API APIͷઃܭํ਑ σʔλͷදݱܗࣜ JSON, XML, Protocol Buffers,

    … REST, RPC, … ,.BUTVNPUP lH31$ͱ1SPUPDPM#V⒎FSTͰҰຯҧ͏"OESPJE௨৴z %SPJE,BJHJIUUQTTQFBLFSEFDLDPNLNBUTQSPUPCVG
  11. REST vs RPC as HTTP API • ҧ͍ͷ1ͭ:ʮAPIઃܭ࣌ʹͲ͜ʹண໨͢Δ͔ʯ • Ϧιʔεʢૢ࡞ର৅ʣͱΞΫγϣϯʢૢ࡞ํ

    ๏ʣΛͲ͏΍ͬͯදݱ͢Δ͔ • REST: ϦιʔεΛத৺ʹߟ͑Δ • RPC: ΞΫγϣϯΛத৺ʹߟ͑Δ ,.BUTVNPUP lH31$ͱ1SPUPDPM#V⒎FSTͰҰຯҧ͏"OESPJE௨৴z %SPJE,BJHJIUUQTTQFBLFSEFDLDPNLNBUTQSPUPCVG
  12. RESTͷํ͕޲͍͍ͯΔέʔε • e.g. ϒϩάαʔϏεͷAPI • APIͷओͳ໾ׂ͸ϒϩάهࣄʹରͯ͠CRUD ૢ࡞Λ͢Δ͜ͱ • هࣄͱ͍͏ϦιʔεΛத৺ʹߟ͑ͨํ͕ྑ͍ •

    ΞΫγϣϯ͸HTTPϝιουͰදݱ https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  13. ϒϩάAPI: RESTͷ৔߹ • Create: • POST https://example.com/api/articles • body: {“title”:

    “New post”, “author”: “John”, “description”: “…”} • Read: • GET https://example.com/api/articles/123 • Update: • PATCH https://example.com/api/articles/123 • body: {“title”: “Revised”} • Delete: • DELETE https://example.com/api/articles/123 https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  14. ϒϩάAPI: RPCͷ৔߹ • Create: • https://example.com/api/createArticle • body: {“title”: “New

    post”, “author”: “John”, “description”: “…”} • Read: • https://example.com/api/getArticle • Update: • https://example.com/api/updateArticle • body: {“article_id”: 123, “title”: “Revised”} • Delete: • https://example.com/api/deleteArticle https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  15. RPCͷํ͕޲͍͍ͯΔέʔε • e.g. SlackͷΑ͏ͳνϟοταʔϏεͷAPI • APIΛ௨ͯ͡ҎԼͷػೳΛఏڙ͍ͨ͠ͱ͢Δ • ͋Δνϟϯωϧʹ͍ΔϢʔβ͕νϟϯωϧ͔Βୀग़ ͢Δ (leave)

    • ͋Δνϟϯωϧʹ͍ΔϢʔβΛνϟϯωϧ͔Β௥͍ ग़͠ɺ࠶ࢀՃΛې͡Δʢkickʣ https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  16. νϟοτAPI: RESTͷ৔߹ • leaveͱkickͷҧ͍Λ៉ྷͳRESTͰදݱ͢Δͷ͕೉͍͠ • e.g. bodyʹԿ͔͠Βͷ৘ใΛ௥Ճ͢Δ౳ͷ޻෉͕ඞཁ • leave: •

    PATCH https://example.com/api/channels/123/users/456 • body: {“status”: “left”} • kick: • PATCH https://example.com/api/channels/123/users/456 • body: {“status”: “kicked”} https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  17. νϟοτAPI: RESTͷ৔߹ • ҎԼͷΑ͏ʹ͢ΔͱRESTͱRPCͷதؒͷΑ͏ͳ΋ͷʹͳͬͯ͠·͏ • leave: • POST https://example.com/api/channels/123/users/456/leave •

    kick: • POST https://example.com/api/channels/123/users/456/kick https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  18. νϟοτAPI: RPCͷ৔߹ • leave: • https://example.com/api/leaveFromChannel • body: {“channel_id”: 123,

    “user_id”: 456} • kick: • https://example.com/api/kickFromChannel • body: {“channel_id”: 123, “user_id”: 456} https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
  19. Slackͷweb API͸RPC > The Web API is a collection of

    HTTP RPC-style methods, all with URLs in the form `https://slack.com/api/METHOD_FAMILY.method`. https://api.slack.com/web Web API͸HTTP RPCελΠϧͷϝιουͷू·ΓͰ͋ΓɺશͯҎ ԼͷϑΥʔϜͰද͞Ε·͢ɻ `https://slack.com/api/METHOD_FAMILY.method`.
  20. Protocol Buffers (Protobuf) • XML౳ͷΑ͏ʹߏ଄Խ͞Εͨσʔλදݱܗࣜɺ ͳ͍͠͸IDL (Interface Description Language) •

    ݱࡏͷ࠷৽͸ver.3 (proto3) > think XML, but smaller, faster, and simpler. https://developers.google.com/protocol-buffers/docs/overview
  21. .protoϑΝΠϧ syntax = "proto3"; message Bank { int32 code =

    1; string name = 2; string name_kana = 3; } https://developers.google.com/protocol-buffers/docs/overview
  22. Protobufͱ͸ • .protoϑΝΠϧΛαʔόɾΫϥΠΞϯτؒͰڞ༗ • Protobuf compilerʹΑͬͯ.protoϑΝΠϧ͔ΒγϦΞϥΠζɾ σγϦΞϥΠζ༻ͷΫϥε͕ੜ੒͞ΕΔ • όΠφϦʹΤϯίʔυͯ͠ૹ৴ •

    JSON΁Τϯίʔυ͢Δ͜ͱ΋Մ(ͨͩ͠gRPCͰ͸ະαϙʔτ) • ެࣜɾαʔυύʔςΟ߹Θͤͯ10छྨҎ্ͷݴޠʹରԠ https://developers.google.com/protocol-buffers/docs/overview
  23. ഑ྻͱmessageͷೖΕࢠ message GetBanksResponse { repeated Bank bank = 1; }

    message Bank { int32 code = 1; string name = 2; string name_kana = 3; } https://developers.google.com/protocol-buffers/docs/overview
  24. Enum enum BankAccountType { SAVING = 0; // ී௨ CHECKING

    = 1; // ౰࠲ } https://developers.google.com/protocol-buffers/docs/overview
  25. Enum enum BankAccountType { option allow_alias = true; SAVING =

    0; // ී௨ CHECKING = 1; // ౰࠲ CURRENT = 1; // ౰࠲ʢผ໊ʣ } https://developers.google.com/protocol-buffers/docs/overview
  26. @Deprecated message Bank { int32 code = 1; string name

    = 2; string name_kana = 3 [deprecated=true]; string name_katakana = 4; } https://developers.google.com/protocol-buffers/docs/overview
  27. RPC service BankListService { rpc GetBanks (GetBanksRequest) returns (GetBanksResponse); }

    message GetBanksRequest {} message GetBanksResponse { repeated Bank bank = 1; } https://developers.google.com/protocol-buffers/docs/overview
  28. THEO ὑ gRPC • εϚϗΞϓϦͱAPIαʔόؒ͸gRPC • ࣾ಺ͷ֤αʔϏεؒͷ௨৴͸ݪଇgRPCͰߏஙத • (ͪͳΈʹ࠷ۙ͸ࣾ಺Ͱ͸Go+gRPCͰ࡞Δ͜ͱ΋) •

    ࣾ಺ͷطଘαʔϏε΋ग़དྷΕ͹gRPCʹ͍͖͍ͯͨ͠ • Rails੡ͷαʔϏε΋͋ΔͷͰɺ͜Ε΋gRPCʹ͍ͨ͠
  29. ࠓճ͸ “͓͍͞;αʔϏε” Λ ྫʹߟ͑ͯΈ·͠ΐ͏ 6TFS ✦ OBNF<TUSJOH> 8BMMFU ✦ VTFS@JE<JOUFHFS>

    1BZNFOU ✦ XBMMFU@JE<JOUFHFS> ✦ BNPVOU<JOUFHFS> ✦ UBSHFU<TUSJOH> ✦ SFBTPO<TUSJOH> 1 1 1 n
  30. ͍͍ͩͨಈ͍ͨ • ·ͩ Proof of Concept ͱ͍͏ײ͡ • Ϛϧνϓϩηε۩߹ʹෆ҆ •

    ཁݕূɻGVLͱ͔͋ΔͷͰͲ͏ͳΔ͔Θ͔ΒΜɻ • treasure-data/serverengine ࢖͏ͱྑ͍ؾ͕ͯ͠ ͍Δ
  31. ·ͱΊ • THEO ͸ Microservice ԽΛؤுͬͯ·͢ɻ • ෼ׂͭͭ͠ɺ΋ͱ΋ͱ෼͔Ε͍ͯΔαʔϏεΛ gRPC ʹ͠

    ͍͖͍ͯͨɻ • Rails ͷطଘαʔϏε΋౰વ gRPC ʹ͍ͨ͠ɻ • ࠓճ͸ PoC Λ࡞ͬͨɻ • ࠓޙݕূͭͭ͠ຊ൪౤ೖ͢Δͭ΋ΓͰ͢ɻ