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

    View full-size slide

  2. A B O U T M Y S E L F
    ©2018 Ginco, Inc.
    ࣗݾ঺հ
    Soichi Sumi
    @_yoyoyousei
    େࡕେֶେֶӃ$4ઐ߈मྃɻϓϩάϥϜࣗಈमਖ਼ͷߴ଎Խख๏ͷఏҊͱධ
    ՁΛߦ͏ɻͦͷޙɺϦΫϧʔτϗʔϧσΟϯάεʹ৽ଔೖࣾ͠ɺΞʔΩς
    ΫτνʔϜϝϯόʔͱͯ͠ɺೝূػߏͳͲͷઃܭɾ։ൃΛߦͬͨɻ
    ೥݄͔Β͸גࣜձࣾ(JODPͰΠϯϑϥߏஙɾӡ༻ɺ"1*αʔόͷ։
    ൃΛ୲౰ɻ

    View full-size slide

  3. ©2018 Ginco, Inc.
    ࠓճ࿩͢͜ͱ
    • $MPVEGVODUJPOTͱ͸
    • (JODPͰͷ$MPVEGVODUJPOTͷར༻ࣄྫ
    • ࢖͍࢝Ίͯ෼͔ͬͨ໰୊఺ͱͦͷରࡦ
    • ࠓޙऔΓ૊Έ͍ͨ͜ͱ

    View full-size slide

  4. ©2018 Ginco, Inc.
    $MPVEGVODUJPOTͱ͸
    • (PPHMF͕ఏڙ͢ΔϑϧϚωʔδυͳؔ਺࣮ߦ؀ڥ
    • τϦΨʔͱ࣮ߦ͢Δؔ਺Λهड़ͯ͠"1*Λ࣮૷͢Δ
    ར఺
    • ϦΫΤετʹର͢ΔॲཧΛॻ͚ͩ͘ɻ؆୯
    • σϓϩΠ͞Εͨݸʑͷؔ਺͸׬શʹҟͳΔ؀ڥͰ࣮ߦ͞ΕΔ
    • ͋Δؔ਺ͷमਖ਼͕ଞͷؔ਺΁ӨڹΛ༩͑ͳ͍
    • େن໛ʹͳͬͯ΋ػೳ௥Ճɾमਖ਼͕༰қ
    • ϦΫΤετྔʹԠͯ͡Φʔτεέʔϧ͢Δ
    • τϦΨʔͷछྨ͕๛෋

    View full-size slide

  5. ©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

    View full-size slide

  6. ©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࿈ܞ




    View full-size slide

  7. ©2018 Ginco, Inc.
    ࢖͍࢝Ίͯ෼͔ͬͨ໰୊఺
    • GVODUJPOͷ਺͕૿͑ΔʹͭΕɺϨεϙϯε͕஗͘ͳΔ
    • pSFTUPSFʹ઀ଓ͢ΔGVODUJPO͸ಛʹ஗͍৔߹͕͋Δ
    • ࠷΋஗͍৔߹Ͱ໿TFD

    View full-size slide

  8. ©2018 Ginco, Inc.
    Ϩεϙϯε͕஗͍ݪҼ
    • $MPVEGVODUJPOTͷ$PMETUBSU
    • 'VODUJPOͷϦʔδϣϯ͕64
    • $MPVEpSFTUPSFͷDMJFOUͷ$PMETUBSU

    View full-size slide

  9. ©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
    ۭ ͖ Π ϯ ε λ ϯ ε ͕ ͋ Δ ৔ ߹

    View full-size slide

  10. ©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

    View full-size slide

  11. ©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
    ௕ ͍ ؒ Ϧ Ϋ Τ ε τ Λ ॲ ཧ ͠ ͯ ͍ ͳ ͍ Π ϯ ε λ ϯ ε ͕ ͋ Δ ৔ ߹

    View full-size slide

  12. ©2018 Ginco, Inc.
    $MPVEGVODUJPOTͷ$PMETUBSUͷվળ
    • $PMETUBSU͸࢓૊Έ্ආ͚ΒΕͳ͍͕ɺܰݮ͢Δ͜ͱ
    ͸Ͱ͖Δ
    • ԼهରࡦΛߦͬͨ
    • άϩʔόϧείʔϓͰͷSFRVJSFͷআڈ
    • ґଘϥΠϒϥϦͷόʔδϣϯΞοϓ

    View full-size slide

  13. ©2018 Ginco, Inc.
    άϩʔόϧείʔϓͰͷSFRVJSFͷআڈ
    • $MPVEGVODUJPOT͸$PMETUBSU࣌ʹJOEFYKTΛϩʔυ͢Δ
    • ͭͷؔ਺Λ࣮ߦ͢Δ৔߹ʹ΋શͯͷؔ਺͕ಡΈࠐ·ΕΔ
    • ࣮ߦ͢Δؔ਺Ҏ֎ͷSFRVJSFΛߦΘͳ͍Α͏मਖ਼ͨ͠

    View full-size slide

  14. ©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
    Ͳ ͷ ؔ ਺ Λ ࣮ ߦ ͢ Δ ৔ ߹ Ͱ ΋ ɺ ͢ ΂ ͯ ͷ
    ؔ ਺ ͕ ϩ ʔ υ ͞ Ε Δ

    View full-size slide

  15. ©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 ͸ ۭ
    σ ϓ ϩ Π ࣌ ʹ ͸ ͢ ΂ ͯ ͷ ؔ ਺ ͕ ϩ ʔ υ ͞ Ε ɺ
    ࣮ ߦ ࣌ ʹ ͸ ࣮ ߦ ର ৅ ͷ ؔ ਺ ͷ Έ ͕ ϩ ʔ υ ͞ Ε Δ ʂ

    View full-size slide

  16. ©2018 Ginco, Inc.
    ґଘϞδϡʔϧͷόʔδϣϯΞοϓ
    • $MPVEGVODUJPOT͸ϞδϡʔϧΛΩϟογϡ͠ɺશͯͷؔ਺Ͱ
    ڞ༗͍ͯ͠Δ
    • Ϟδϡʔϧ͕Ωϟογϡʹ͋Ε͹ϥΠϒϥϦͷϩʔυ࣌ؒΛ୹
    ॖͰ͖Δ
    • ՄೳͳΒ৽͍͠όʔδϣϯͷϞδϡʔϧΛ࢖͏Α͏मਖ਼
    • ϞδϡʔϧΛ࠷৽ʹΞοϓσʔτͨ͠

    View full-size slide

  17. ©2018 Ginco, Inc.
    ࣮ݧ
    • ରࡦͷ࣮ࢪલޙͰϨεϙϯε࣌ؒΛܭଌ
    • ࣮ݧର৅͸#JUDPJOͷ5YΛϒϩʔυΩϟετ͢Δ"1*
    • ϩʔΧϧ1$͔ΒճϦΫΤετΛૹΓɺͦͷฏۉ஋Λൺֱ͢Δ
    • σϓϩΠ௚ޙͷϨεϙϯεΛ$PMETUBSU࣌ͷϨεϙϯεɺͦΕ
    Ҏ߱ͷϨεϙϯεΛ௨ৗ࣌ͷϨεϙϯεͱͨ͠

    View full-size slide

  18. ©2018 Ginco, Inc.
    ݁Ռ
    • $PMETUBSU͕վળͨ͠ɻ௨ৗ࣌ͷϨεϙϯε͸΄΅มΘΒͣ
    • $PMETUBSUTFDˠTFD
    • ௨ৗ࣌NTFDˠNTFD
    • վળͷେ෦෼͸ඞཁʹͳͬͨஈ֊ͰͷSFRVJSFʹΑΔ΋ͷͩͬͨ
    • ͔͠͠ɺ·ͩ஗͍





    $PMETUBSU /PSNBM
    վળલ վળޙ

    View full-size slide

  19. ©2018 Ginco, Inc.
    Ϧʔδϣϯͷมߋ
    • ݄Լ०ʹ$MPVEGVODUJPOTͷ౦ژϦʔδϣϯ͕
    ௥Ճ͞Εͨ
    • ಋೖ౰࣌͸ถࠃϦʔδϣϯͷΈ
    • ౦ژϦʔδϣϯͰ΋࣮ݧ

    View full-size slide

  20. ©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
    ͜ ͜ Λ ௥ Ճ ͢ Δ ͩ ͚ ʂ

    View full-size slide

  21. ©2018 Ginco, Inc.
    ݁Ռ
    • $PMETUBSU΋௨ৗ࣌΋଎͘ͳͬͨ
    • $PMETUBSUTFDˠTFD
    • ௨ৗ࣌NTFDˠNTFD
    • ௨ৗ࣌ͷϨεϙϯε͸͔ͳΓྑ͘ͳ͕ͬͨɺ$PMETUBSU͸·ͩ஗͍





    $PMETUBSU /PSNBM
    վળલ DPMETUBSUରࡦ Ϧʔδϣϯมߋ

    View full-size slide

  22. ©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

    View full-size slide

  23. ©2018 Ginco, Inc.
    $MPVEpSFTUPSFͷ$PMETUBSUͷճආ
    • શͯͷ'VODUJPOͰɺॲཧͷ࠷ॳʹ'JSFTUPSF΁ͷ໰͍߹ΘͤΛߦͬ
    ͍ͯͨ
    • (JODPϢʔβ͔ΒͷϦΫΤετ͔ݕূ͢ΔͨΊ
    • PO$BMMτϦΨʔΛ࢖͍ɺϢʔβೝূΛ'JSFCBTF"VUIFOUJDBUJPO
    ʹ೚ͤΔ͜ͱͰ'JSFTUPSFͷ໰͍߹ΘͤΛऔΓআ͍ͨ

    View full-size slide

  24. ©2018 Ginco, Inc.
    PO$BMMτϦΨʔ
    • ΞϓϦͳͲ͔Β'VODUJPOΛ௚઀ݺͼग़ͨ͢ΊͷτϦΨʔ
    • 'JSFCBTF"VUIFOUJDBUJPOͰೝূঢ়ଶͷ৔߹ͷΈ'VODUJPO͕
    ୟ͚ΔΑ͏ೝূνΣοΫ͕ೖΔ
    • 'VODUJPOͷࢦఆ͸ؔ਺໊ͱϦʔδϣϯͰߦ͏

    View full-size slide

  25. ©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 Π ϯ ε λ ϯ ε
    Λ ࡞ ੒ ͠ ɺ ؔ ਺ ໊ Λ ࢦ ఆ

    View full-size slide

  26. ©2018 Ginco, Inc.
    ݁Ռ
    • $PMETUBSU΋௨ৗ࣌΋े෼଎͘ͳͬͨ
    • $PMETUBSUTFDˠTFD
    • ௨ৗ࣌NTFDˠNTFD
    • վળલͱൺ΂Δͱ
    • $PMETUBSUTFD࡟ݮ
    • ௨ৗ࣌NTFD࡟ݮ





    $PMETUBSU OPSNBM
    վળલ $PMETUBSUରࡦ Ϧʔδϣϯมߋ PO$BMMτϦΨʔ

    View full-size slide

  27. ©2018 Ginco, Inc.
    ࠓޙऔΓ૊Έ͍ͨ͜ͱ
    • 'VODUJPOTͷϥϯλΠϜΛ(Pʹมߋ
    • ϥϯλΠϜىಈ΋ϥΠϒϥϦͷϩʔυ΋ૣ͘ͳΔ
    • 'JSFTUPSFΛෳ਺ϦʔδϣϯͰ΋ར༻
    • ݱࡏ͸ถࠃͷϚϧνϦʔδϣϯ
    • ϚϧνϦʔδϣϯͷϩέʔγϣϯʹΞδΞ͕௥Ճ͞ΕΔ͜ͱʹظ଴
    • ΠϯελϯεؒͰσʔλͷҰ؏ੑΛऔΔඞཁ͕ग़ͯ͘Δ͔΋

    View full-size slide

  28. ©2018 Ginco, Inc.
    ·ͱΊ
    • (JODPͰͷ$MPVEGVODUJPOͷར༻ࣄྫΛ঺հͨ͠
    • $MPVEGVODUJPOTͷ࢓૊ΈΛղઆͨ͠
    • ࢖͍࢝Ίͯ෼͔ͬͨ໰୊఺ͱͦͷରࡦΛ঺հͨ͠
    • $PMETUBSUTFD࡟ݮ
    • ௨ৗ࣌NTFD࡟ݮ
    • ࠓޙऔΓ૊Έ͍ͨ͜ͱΛ঺հͨ͠
    • 'VODUJPOTͷϥϯλΠϜΛ(P΁มߋ
    • 'JSFTUPSFΛෳ਺ϦʔδϣϯͰར༻

    View full-size slide