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

How we reduced cold start time of Cloud Functions at Ginco Inc.

Soichi Sumi
October 04, 2018

How we reduced cold start time of Cloud Functions at Ginco Inc.

In this presentation, we introduce an use case of cloud functions at Ginco Inc and how we speed up functions on Cloud functions.
GincoにおけるCloud functionsの利用事例紹介と、productionで利用する際に行った高速化について解説しています

2018/10/04 (木)
Ginco Engineer Meetup 登壇資料
https://career.levtech.jp/hikalab/event/detail/187/

Ginco Inc.
https://ginco.io

Soichi Sumi

October 04, 2018
Tweet

More Decks by Soichi Sumi

Other Decks in Technology

Transcript

  1. (JODPʹ͓͚Δ$MPVEGVODUJPOT ͷར༻ͱͦͷߴ଎Խ G I N C O E N G

    I N E E R M E E T U P S O I C H I S U M I 2 0 1 8 / 1 0 / 4 / T H U
  2. A B O U T M Y S E L

    F ©2018 Ginco, Inc. ࣗݾ঺հ Soichi Sumi @_yoyoyousei େࡕେֶେֶӃ$4ઐ߈मྃɻϓϩάϥϜࣗಈमਖ਼ͷߴ଎Խख๏ͷఏҊͱධ ՁΛߦ͏ɻͦͷޙɺϦΫϧʔτϗʔϧσΟϯάεʹ৽ଔೖࣾ͠ɺΞʔΩς ΫτνʔϜϝϯόʔͱͯ͠ɺೝূػߏͳͲͷઃܭɾ։ൃΛߦͬͨɻ ೥݄͔Β͸גࣜձࣾ(JODPͰΠϯϑϥߏஙɾӡ༻ɺ"1*αʔόͷ։ ൃΛ୲౰ɻ
  3. ©2018 Ginco, Inc. $MPVEGVODUJPOTͱ͸ • (PPHMF͕ఏڙ͢ΔϑϧϚωʔδυͳؔ਺࣮ߦ؀ڥ • τϦΨʔͱ࣮ߦ͢Δؔ਺Λهड़ͯ͠"1*Λ࣮૷͢Δ ར఺ •

    ϦΫΤετʹର͢ΔॲཧΛॻ͚ͩ͘ɻ؆୯ • σϓϩΠ͞Εͨݸʑͷؔ਺͸׬શʹҟͳΔ؀ڥͰ࣮ߦ͞ΕΔ • ͋Δؔ਺ͷमਖ਼͕ଞͷؔ਺΁ӨڹΛ༩͑ͳ͍ • େن໛ʹͳͬͯ΋ػೳ௥Ճɾमਖ਼͕༰қ • ϦΫΤετྔʹԠͯ͡Φʔτεέʔϧ͢Δ • τϦΨʔͷछྨ͕๛෋
  4. ©2018 Ginco, Inc. ࣮૷ྫ)551τϦΨʔ const functions = require(‘firebase-functions'); exports.httpFunc =

    functions.https.onRequest((req, res) => { return res.status(200).json({ result: “result” }); }); 2. index.js ࣮૷ 3. σϓϩΠ test-MacBookPro% firebase deploy 1. ॳظԽ test-MacBookPro% firebase init
  5. ©2018 Ginco, Inc. (JODPͰͷ$MPVEGVODUJPOTͷར༻ K u b e r n

    e t e s B l o c k c h a i n N o d e s C l o u d f i r e s t o r e C l o u d f u n c t i o n s G i n c o B i g Q u e r y C l o u d S t o r a g e C l o u d m e s s a g i n g 1.֤ϒϩοΫνΣʔϯϊʔυͷ31$ݺͼग़͠ 2.Ϛʔέοτ৘ใऔಘ 3.ϓογϡ௨஌ 4.'JSFTUPSFͷఆظόοΫΞοϓ#JH2VFSZ࿈ܞ ᶃ ᶄ ᶅ ᶆ
  6. ©2018 Ginco, Inc. $MPVEGVODUJPOTͷ࢓૊Έ Routing ࢀ ߟ   L

    i f e o f a S e r v e r l e s s E v e n t : U n d e r t h e H o o d o f S e r v e r l e s s o n G C P h t t p s : / / y o u t u . b e / M B B Q 6 P 3 G a u Y ۭ ͖ Π ϯ ε λ ϯ ε ͕ ͋ Δ ৔ ߹
  7. ©2018 Ginco, Inc. $MPVEGVODUJPOTͷ࢓૊Έ Routing ࢀ ߟ   L

    i f e o f a S e r v e r l e s s E v e n t : U n d e r t h e H o o d o f S e r v e r l e s s o n G C P h t t p s : / / y o u t u . b e / M B B Q 6 P 3 G a u Y ʴ ۭ ͖ Π ϯ ε λ ϯ ε ͕ ແ ͍ ৔ ߹ $ P M E  T U B S U
  8. ©2018 Ginco, Inc. $MPVEGVODUJPOTͷ࢓૊Έ Routing ࢀ ߟ   L

    i f e o f a S e r v e r l e s s E v e n t : U n d e r t h e H o o d o f S e r v e r l e s s o n G C P h t t p s : / / y o u t u . b e / M B B Q 6 P 3 G a u Y ௕ ͍ ؒ Ϧ Ϋ Τ ε τ Λ ॲ ཧ ͠ ͯ ͍ ͳ ͍ Π ϯ ε λ ϯ ε ͕ ͋ Δ ৔ ߹
  9. ©2018 Ginco, Inc. άϩʔόϧείʔϓͷSFRVJSFͷআڈ exports.Func1 = require('./funcs/func1'); exports.Func2 = require('./funcs/func2');

    exports.Func3 = require('./funcs/func3'); exports.Func4 = require('./funcs/func4'); ... ࣮ ૷ ྫ - म ਖ਼ લ i n d e x . j s const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); const db = admin.firestore(); module.exports = functions.https.onRequest((req, res)=>{ // ...... }); f u n c s / f u n c 1 . j s const functions = require('firebase-functions') const google = require('googleapis'); const rp = require('request-promise'); const projectId = process.env.GCLOUD_PROJECT; module.exports = functions.storage
 .bucket(`${projectId}-bucket`) .object() .onFinalize((obj)=>{ // ...... }); f u n c s / f u n c 2 . j s Ͳ ͷ ؔ ਺ Λ ࣮ ߦ ͢ Δ ৔ ߹ Ͱ ΋ ɺ ͢ ΂ ͯ ͷ ؔ ਺ ͕ ϩ ʔ υ ͞ Ε Δ
  10. ©2018 Ginco, Inc. άϩʔόϧείʔϓͷSFRVJSFͷআڈ if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME === 'Func1')

    { exports.Func1 = require('./funcs/func1'); } if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME === 'Func2') { exports.Func2 = require('./funcs/func2'); } ... ࣮ ૷ ྫ - म ਖ਼ ޙ i n d e x . j s ؔ ਺ ࣮ ߦ ࣌ ʹ ͸ ؀ ڥ ม ਺ ʹ ؔ ਺ ໊ ͕ ೖ Δ ͦ Ε Ҏ ֎ ͷ ৔ ߹ ɺ F U N C T I O N _ N A M E ͸ ۭ σ ϓ ϩ Π ࣌ ʹ ͸ ͢ ΂ ͯ ͷ ؔ ਺ ͕ ϩ ʔ υ ͞ Ε ɺ ࣮ ߦ ࣌ ʹ ͸ ࣮ ߦ ର ৅ ͷ ؔ ਺ ͷ Έ ͕ ϩ ʔ υ ͞ Ε Δ ʂ
  11. ©2018 Ginco, Inc. ݁Ռ • $PMETUBSU͕վળͨ͠ɻ௨ৗ࣌ͷϨεϙϯε͸΄΅มΘΒͣ • $PMETUBSUTFDˠTFD • ௨ৗ࣌NTFDˠNTFD

    • վળͷେ෦෼͸ඞཁʹͳͬͨஈ֊ͰͷSFRVJSFʹΑΔ΋ͷͩͬͨ • ͔͠͠ɺ·ͩ஗͍      $PMETUBSU /PSNBM վળલ վળޙ
  12. ©2018 Ginco, Inc. ࣮૷ྫϦʔδϣϯͷมߋ const functions = require('firebase-functions'); module.exports =

    functions.region(‘asia-northeast1') .https .onRequest((req, res) => { // …… }); i n d e x . j s ͜ ͜ Λ ௥ Ճ ͢ Δ ͩ ͚ ʂ
  13. ©2018 Ginco, Inc. ݁Ռ • $PMETUBSU΋௨ৗ࣌΋଎͘ͳͬͨ • $PMETUBSUTFDˠTFD • ௨ৗ࣌NTFDˠNTFD

    • ௨ৗ࣌ͷϨεϙϯε͸͔ͳΓྑ͘ͳ͕ͬͨɺ$PMETUBSU͸·ͩ஗͍      $PMETUBSU /PSNBM վળલ DPMETUBSUରࡦ Ϧʔδϣϯมߋ
  14. ©2018 Ginco, Inc. ࢒ΔϘτϧωοΫ • $PMETUBSUͷϘτϧωοΫΛௐࠪͨ͠ • pSFTUPSFͷճ໨ͷHFU ͰTFD͔͔͍ͬͯͨ ʂ

     • JTTVFΛௐࠪ͢ΔͱɺpSFTUPSFͷDMJFOUʹ΋DPMETUBSU͕͋Δ͜ ͱ͕෼͔ͬͨ • ղܾʹ͸࣌ؒΛཁ͢ΔΑ͏ͳͷͰɺճආ͢Δ h t t p s : / / g i t h u b . c o m / f i r e b a s e / f i r e b a s e - f u n c t i o n s / i s s u e s / 2 6 3
  15. ©2018 Ginco, Inc. ࣮૷ྫPO$BMMτϦΨʔ const functions = require('firebase-functions') exports.Func1 =

    functions.region('asia-northeast1') .https.onCall((data, ctx) => { // ...... }) i n d e x . j s C a l l F u n c 1 . s w i f t o n R e q u e s t → o n C a l l ΁ ม ߋ let functions = Functions.functions(region: "asia-northeast1") functions.httpsCallable(“Func1”).call([“data": “hoge”]) {(result, error) in } Ϧ ʔ δ ϣ ϯ Λ ࢦ ఆ ͠ ͯ F u n c t i o n s Π ϯ ε λ ϯ ε Λ ࡞ ੒ ͠ ɺ ؔ ਺ ໊ Λ ࢦ ఆ
  16. ©2018 Ginco, Inc. ݁Ռ • $PMETUBSU΋௨ৗ࣌΋े෼଎͘ͳͬͨ • $PMETUBSUTFDˠTFD • ௨ৗ࣌NTFDˠNTFD

    • վળલͱൺ΂Δͱ • $PMETUBSUTFD࡟ݮ • ௨ৗ࣌NTFD࡟ݮ      $PMETUBSU OPSNBM վળલ $PMETUBSUରࡦ Ϧʔδϣϯมߋ PO$BMMτϦΨʔ
  17. ©2018 Ginco, Inc. ࠓޙऔΓ૊Έ͍ͨ͜ͱ • 'VODUJPOTͷϥϯλΠϜΛ(Pʹมߋ • ϥϯλΠϜىಈ΋ϥΠϒϥϦͷϩʔυ΋ૣ͘ͳΔ • 'JSFTUPSFΛෳ਺ϦʔδϣϯͰ΋ར༻

    • ݱࡏ͸ถࠃͷϚϧνϦʔδϣϯ • ϚϧνϦʔδϣϯͷϩέʔγϣϯʹΞδΞ͕௥Ճ͞ΕΔ͜ͱʹظ଴ • ΠϯελϯεؒͰσʔλͷҰ؏ੑΛऔΔඞཁ͕ग़ͯ͘Δ͔΋
  18. ©2018 Ginco, Inc. ·ͱΊ • (JODPͰͷ$MPVEGVODUJPOͷར༻ࣄྫΛ঺հͨ͠ • $MPVEGVODUJPOTͷ࢓૊ΈΛղઆͨ͠ • ࢖͍࢝Ίͯ෼͔ͬͨ໰୊఺ͱͦͷରࡦΛ঺հͨ͠

    • $PMETUBSUTFD࡟ݮ • ௨ৗ࣌NTFD࡟ݮ • ࠓޙऔΓ૊Έ͍ͨ͜ͱΛ঺հͨ͠ • 'VODUJPOTͷϥϯλΠϜΛ(P΁มߋ • 'JSFTUPSFΛෳ਺ϦʔδϣϯͰར༻