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

Rubyの会社でRustを書くということ

KOBA789
February 10, 2018

 Rubyの会社でRustを書くということ

Cookpad TechConf 2018 ( https://techconf.cookpad.com/2018/ ) で発表した資料です。

KOBA789

February 10, 2018
Tweet

More Decks by KOBA789

Other Decks in Technology

Transcript

  1. KOBA789ʢখྛल࿨ʣ
    ΠϯϑϥετϥΫνϟʔ෦
    RubyͷձࣾͰ

    RustΛॻ͘ͱ͍͏͜ͱ
    Cookpad Inc.
    Feb 10th, 2018

    View full-size slide

  2. ࣗݾ঺հ
    2
    ɾ2017೥৽ଔ
    ɾΠϯϑϥετϥΫνϟʔ෦

    σʔλج൫άϧʔϓ
    KOBA789ʢখྛल࿨ʣ

    View full-size slide

  3. RubyͷձࣾͰ

    RustΛॻ͘ͱ͍͏͜ͱ

    View full-size slide

  4. 5
    Go Java
    Ruby

    View full-size slide

  5. 6
    Go Java
    Hako
    (DockerσϓϩΠج൫)
    Ruby

    View full-size slide

  6. 7
    Go Java
    Hako
    (DockerσϓϩΠج൫)
    Ruby

    View full-size slide

  7. 8
    Go Java
    Ruby Rust
    Hello !

    View full-size slide

  8. 9
    Ruby͚ͩͷձࣾ

    Ͱ͸ͳ͍

    View full-size slide

  9. 10
    Push௨஌഑৴ج൫

    View full-size slide

  10. ɾΠϕϯτൃੜ͝ͱʹૹ৴
    ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ
    ɾ഑৴਺͸গͳ͍
    ɾಛఆͷ૚ʹҰ੪഑৴
    ɾٳ຾Ϣʔβʔ΁ͷૌٻ
    ɾ഑৴਺͸ଟ͍
    ɾ਺ඦສ௨ن໛
    ɾݟੵ΋Γ͕େ੾
    ϓογϡ௨஌
    11
    ౎౓഑৴ Ұ੪഑৴

    View full-size slide

  11. 12
    App
    S3
    ج൫
    SNS
    1. ड৴ઃఆΛΫΤϦ
    2. ARNΛΫΤϦ
    3. S3ʹॻ͖ࠐΈ
    4. S3͔ΒಡΈग़͢
    5. SNS΁ૹ৴





    MySQL

    View full-size slide

  12. 13
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNS΁ૹ৴
    S3ʹॻ͖ࠐΉ
    S3͔ΒಡΈग़͢

    View full-size slide

  13. 14
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNSʹૹ৴

    View full-size slide

  14. 15
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNSʹૹ৴
    {
    چج൫
    {
    App

    View full-size slide

  15. 16
    World's Largest

    Rails Monolith

    View full-size slide

  16. S3
    ج൫
    SNS
    17
    App
    New!
    MySQL

    View full-size slide

  17. S3
    ج൫
    SNS
    New!
    DBڞ༗
    18
    ίϐϖϩδοΫ
    App
    MySQL

    View full-size slide

  18. 19
    ཧ૝ͷPush௨஌഑৴ج൫

    View full-size slide

  19. 20
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNSʹૹ৴
    {
    }
    چج൫
    ৽ج൫

    View full-size slide

  20. 21
    ϢʔβʔIDࢦఆ
    ARNࢦఆ
    λʔήοτ

    View full-size slide

  21. 22
    ड৴ઃఆͰϑΟϧλ

    View full-size slide

  22. 23
    S3
    ج൫
    SNS
    MySQL
    େྔ഑৴
    ౎౓഑৴ HTTP API

    View full-size slide

  23. 24
    S3
    ج൫
    SNS
    MySQL
    େྔ഑৴
    ౎౓഑৴ HTTP API
    ϢʔβʔIDͱϝοηʔδ͑͞

    ์ΓࠐΊ͹഑৴Ͱ͖Δ

    View full-size slide

  24. ɾΠϕϯτൃੜ͝ͱʹૹ৴
    ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ
    ɾ഑৴਺͸গͳ͍
    ɾಛఆͷ૚ʹҰ੪഑৴
    ɾٳ຾Ϣʔβʔ΁ͷૌٻ
    ɾ഑৴਺͸ଟ͍
    ɾ਺ඦສ௨ن໛
    ɾݟੵ΋Γ͕େ੾
    ϓογϡ௨஌
    25
    ౎౓഑৴ Ұ੪഑৴

    View full-size slide

  25. ɾΠϕϯτൃੜ͝ͱʹૹ৴
    ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ
    ɾ഑৴਺͸গͳ͍
    ɾಛఆͷ૚ʹҰ੪ૹ৴
    ɾٳ຾Ϣʔβʔ΁ͷૌٻ
    ɾ഑৴਺͸ଟ͍
    ɾ਺ඦສ௨ن໛
    ɾݟੵ΋Γ͕େ੾
    ϓογϡ௨஌
    26
    ౎౓഑৴ ΞϓϦ಺ϚʔέςΟϯά

    View full-size slide

  26. ɾΠϕϯτൃੜ͝ͱʹૹ৴
    ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ
    ɾ഑৴਺͸গͳ͍
    ɾಛఆͷ૚ʹҰ੪ૹ৴
    ɾٳ຾Ϣʔβʔ΁ͷૌٻ
    ɾ഑৴਺͸ଟ͍
    ɾ਺ඦສ௨ن໛
    ɾݟੵ΋Γ͕େ੾
    ϓογϡ௨஌
    27
    ౎౓഑৴ ΞϓϦ಺ϚʔέςΟϯά
    ߴ଎ͳdry-run
    ਺ඦສ௨Λ਺෼Ͱ

    View full-size slide

  27. 28
    RubyͰ࡞Δͷ͸େม

    View full-size slide

  28. Ruby or else
    29
    ɾ଎౓ɾ҆શੑɾฒߦੑʹϑΥʔΧε
    ɾίϯύΠϥ͕σʔλڝ߹Λݕࠪ
    ɾτϨΠτʹΑΔδΣωϦΫε
    Rust

    View full-size slide

  29. 30
    Rust is not magic

    View full-size slide

  30. 31
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNSʹૹ৴

    View full-size slide

  31. 32
    ड৴ઃఆΛΫΤϦ
    ARNΛΫΤϦ
    SNSʹૹ৴
    6ms
    6ms
    }✕ 100ສ
    = 3࣌ؒ

    View full-size slide

  32. 33
    Inspired by facebook/dataloader
    https://github.com/facebook/dataloader

    View full-size slide

  33. 34
    ΫΤϦΛ·ͱΊΔ

    View full-size slide

  34. 35
    ... WHERE user_id = 1;
    ... WHERE user_id = 2;
    ... WHERE user_id = 3;
    ... WHERE user_id IN (1, 2, 3);

    View full-size slide

  35. ΫΤϦΛ·ͱΊΔ
    36
    WHERE user_id = ? →6ms (only 1 id)
    WHERE user_id IN (?, ?, ... ?) →11ms (1000 ids)
    ϕϯνϚʔΫͷ݁Ռɺ਺ઍ͘Β͍͕Α͍
    ࠷దͳόοναΠζ

    View full-size slide

  36. ΫΤϦΛ·ͱΊΔ
    37
    ɾΩϡʔʹuser_idΛೖΕΔ
    ɾΩϡʔʹཷ·͍ͬͯΔuser_idΛҰؾʹऔΓग़͢
    ɾIN (...) ͷதʹల։ͯ͠DB΁ΫΤϦ
    ΞϧΰϦζϜ

    View full-size slide

  37. 38
    KOBA789/batch-recv-rs
    https://github.com/KOBA789/batch-recv-rs

    View full-size slide

  38. KOBA789/batch-recv-rs
    39
    MFU UY SY
    DSPTTCFBN@DIBOOFMVOCPVOE

    MFUDIVOL7FD@SY
    CBUDI@SFDW

    VOXSBQ

    DPMMFDU

    ࢖͍ํ

    View full-size slide

  39. εϨουؒͷϝοηʔδύογϯάʹ࢖͑Δ

    Multi-producer multi-consumerͳFIFOΩϡʔ
    SenderͱReceiver͔ΒͳΔ
    MFU UY SY
    DSPTTCFBN@DIBOOFMVOCPVOE

    KOBA789/batch-recv-rs
    40
    crossbeam_channel

    View full-size slide

  40. KOBA789/batch-recv-rs
    41
    fn recv(&self) -> Result
    Ωϡʔͷத͔Β࠷ॳͷཁૉΛऔΓग़͢ϝιου
    ཁૉ͕ͳ͚Ε͹εϨουΛϒϩοΫ͢Δʢॏཁʣ
    crossbeam_channel::Receiver

    View full-size slide

  41. KOBA789/batch-recv-rs
    42
    fn try_iter(&self) -> TryIter
    Ωϡʔͷத਎ΛΠςϨʔλͱͯ͠ಘΔϝιου
    impl<'a, T> Iterator for TryIter<'a, T>
    ΋ͪΖΜIteratorτϨΠτΛ࣮૷͍ͯ͠Δ
    crossbeam_channel::Receiver

    View full-size slide

  42. KOBA789/batch-recv-rs
    43
    fn take(self, n: usize) -> Take
    ΠςϨʔλ͔Βઌ಄nݸͷཁૉ͚ͩͷΠςϨʔλΛ࡞Δϝιου
    std::iter::Iterator

    View full-size slide

  43. KOBA789/batch-recv-rs
    44
    MFUGJSTUTFMGSFDW

    MFUSFTUTFMGUSZ@JUFS
    UBLF O

    MFUJUFSWFD

    JOUP@JUFS

    DIBJO SFTU

    0L JUFS

    ૊Έ߹ΘͤΔ

    View full-size slide

  44. 45
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  45. batch_recv(3)
    46
    1
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  46. 47
    1
    working...
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  47. 48
    1
    working...
    2
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  48. 49
    1
    working...
    2
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  49. 50
    1
    working...
    2
    5
    4
    3
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  50. 51
    1
    working...
    2
    4 3
    5
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  51. 52
    2
    4 3
    5
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  52. batch_recv(3)
    53
    2
    4 3
    5
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  53. 54
    2
    4 3
    5
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  54. 55
    2
    4 3
    5
    working...
    }
    batched
    Ωϡʔ
    ϝΠϯ

    εϨου

    View full-size slide

  55. 56
    Q. ΫΤϦ݁Ռ͸

    Ͳ͏΍ͬͯฦ͢?

    View full-size slide

  56. ΫΤϦ݁Ռ͸Ͳ͏΍ͬͯฦ͢?
    57
    futures::sync::oneshot
    Ұ౓͚͔ͩ͠ૹΕͳ͍ΩϡʔͷΑ͏ͳ΋ͷ
    SenderɹɹͱReceiverɹɹ͔ΒͳΔ
    ೖ ग़
    ʹ஋ΛೖΕΔͱ ͔Βग़ͯ͘Δ
    ೖ ग़

    View full-size slide

  57. 58
    ೖ 1

    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  58. 59
    ೖ 1

    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  59. 60
    ೖ 1

    A
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  60. 61
    ೖ 1
    ग़ A
    A
    Ωϡʔ
    ϝΠϯ

    εϨου
    ΫΤϦ

    εϨου

    View full-size slide

  61. 62
    Q.ΫΤϦ͕

    ͨ͘͞Μ͋ͬͨΒ?

    View full-size slide

  62. 63
    ೖ 2

    ೖ 1
    ೖ 3


    View full-size slide

  63. 64
    ೖ 2

    ೖ 1
    ೖ 3


    όοναΠζ෼ͷ

    εϨου͕ඞཁ
    ͜ΕͰ͸ࠔΔ

    View full-size slide

  64. 65
    Tokio + futures-rs
    https://tokio.rs/

    View full-size slide

  65. Tokio + futures-rs
    66
    ɾΠϕϯτϧʔϓͷ࣮૷
    ɾ1ݸҎ্ͷFutureΛ࣮ߦ
    ɾϊϯϒϩοΩϯά I/O API
    ɾFutureτϨΠτ
    ɾJSͷPromiseͱ

    ಉ༷ͷ֓೦
    ɾFutureͷίϯϏωʔλ
    ɾɹɹɹɹ΋͜Εͷػೳ
    Tokio futures-rs
    ೖ ग़

    View full-size slide

  66. Tokio + futures-rs
    67
    futures::sync::oneshot
    Ұ౓͚͔ͩ͠ૹΕͳ͍ΩϡʔͷΑ͏ͳ΋ͷ
    ɹɹ͸ΠϕϯτϧʔϓͰ؂ࢹͰ͖Δ

    View full-size slide

  67. 68
    ΠϕϯτϧʔϓΛ࢖͑͹

    1εϨουͰͨ͘͞Μͷ

    "ग़"Λ଴ͪ߹ΘͤͰ͖Δ

    View full-size slide

  68. 69
    ೖ 2

    ೖ 1
    ೖ 3


    View full-size slide

  69. Tokio + futures-rs
    70
    ɾΩϡʔͷ޲͜͏ͷεϨου͔Β஋Λฦͯ͠΋Β͏͜ͱ͕Ͱ͖Δ
    ɾϒϩοΩϯάͳॲཧΛFutureʹม׵͍ͯ͠ΔͱΈͳͤΔ
    oneshot::Senderɹ ΛΩϡʔ΁ೖΕΔύλʔϯ

    View full-size slide

  70. 71
    KOBA789/batch-loader-rs
    https://github.com/KOBA789/batch-loader-rs

    View full-size slide

  71. KOBA789/batch-loader-rs
    72
    ɾKeyͷΠςϨʔλ͔ΒValueͷ഑ྻΛฦ͢ϝιουΛ

    ࣮૷Ͱ͖Ε͹SQLҎ֎ʹ΋ద༻Մೳ
    ɾࠓճͷྫͰ͸Key=ϢʔβʔID, Value=ςʔϒϧͷߦ
    Key-Valueతͳ೚ҙͷΫΤϦΛόονԽ

    View full-size slide

  72. ࠷ޙʹ
    73
    ɾͱʹ͔͘ϚϧνεϨου͕҆શ
    ɾϚϧνεϨουͱΠϕϯτۦಈͷ͍͍ͱ͜औΓ͕Ͱ͖Δ
    ɾ҆৺ͯ͠ߴ଎ԽʹऔΓ૊ΊΔ
    ɾ൚༻ϥΠϒϥϦΛ࡞Γ΍͍͢ܕγεςϜ
    Rustͷ͍͍ͱ͜Ζ

    View full-size slide

  73. 74
    ͜͏ͯ͠

    ϓογϡ௨஌഑৴ج൫͸

    RustͰॻ͖௚͞Ε·ͨ͠

    View full-size slide

  74. 75
    Ruby͚ͩ͡Όͳ͍
    ద੾ͳݴޠͰ

    Ϋʔϧʹ՝୊ղܾ

    View full-size slide

  75. 76
    RustΛॻ͘Πϯλʔϯγοϓ
    https://internship.cookpad.com/2018/spring/

    View full-size slide