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

Erlang/OTP で作るリアルタイムサーバー

yamionp
June 01, 2019

Erlang/OTP で作るリアルタイムサーバー

Erlang & Elixir Fest 2019 で発表した「 Erlang/OTP で作るリアルタイムサーバー」 の資料になります.

yamionp

June 01, 2019
Tweet

Other Decks in Technology

Transcript

  1. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 3 ձࣾ঺հ ઃཱ ೥݄೔ ࠃ಺ࢠձࣾ ւ֎ࢠձࣾ ࣄۀ಺༰

    ϞόΠϧήʔϜࣄۀ ࣾ ࣾ 93ࣄۀ 73 "3 .3ͳͲ ࣾ ࣾ ϒϩοΫνΣʔϯࣄۀ ࣾ ઃཱ ೥݄೔ ࠃ಺ࢠձࣾ ւ֎ࢠձࣾ ࣄۀ಺༰ ϞόΠϧήʔϜࣄۀ ࣾ ࣾ 93ࣄۀ 73 "3 .3ͳͲ ࣾ ࣾ ϒϩοΫνΣʔϯࣄۀ ࣾ ˞೥݄ݱࡏ ࣄۀྫϞόΠϧήʔϜࣄۀͷ໊ࣗࣾٛ഑৴ͷҰ෦ ͳͲʜ
  2. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ࣗݾ঺հ ▸ ਗ਼ਫ༎ޗ!ZBNJPOQ ▸ 2011೥ ೖࣾ ▸

    2015೥ R&D΁ 4 ήʔϜαʔόʔͷ։ൃ ڞ௨ج൫ͷ։ൃ ϦΞϧλΠϜαʔόʔͷ։ൃ ڞ௨ج൫։ൃɾӡ༻νʔϜϚωʔδϟʔ ࣍ੈ୅ڞ௨ج൫ͷ։ൃ
  3. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 15 ͓ख఻͍ͷ࣮ྫ ϚΠΫϩαʔϏε܈ 0DVMVT4UPSF 'BDFCPPL ʜ "1*

    ೝূ ՝ۚ ഑৴ 6OJUZ IUUQTWBSLDPKQ ϓϥοτϑΥʔϜ܈ 1SPYZ 1VC4VC ഑৴ελδΦ ήʔϜαʔό
  4. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ͜Ε·ͰͷιʔγϟϧήʔϜ ▸ ඇಉظίϛϡχέʔγϣϯ ▸ ಉ࣌ʹ༡ΜͰ͍ͳ͍ͷʹҰॹʹ༡ΜͰ͍Δײ֮ ▸ χίχίಈը

    ▸ ٕज़త੍໿ ▸ ϞόΠϧ8FCಛ༗ͷ࢓༷ ▸ ॲཧೳྗɾόοςϦʔ ▸ ճઢ଎౓ɾྉۚʢύέࢮʣ 18
  5. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ୺຤ؒͷ௚઀௨৴ 11 ͸ʁ ▸ ಠྗͰ௚઀ͭͳ͕Δͷ͸೉͍͠ ▸ ૝ఆ͞ΕΔ΄΅શͯͷ୺຤͸/"5ͷԼʹ͋Δ

    ▸ 8J'J؀ڥ΋ؚΊΔͱɺͲΕ͚ͩؤுͬͯ΋ܨ͕Βͳ͍୺຤͕͋Δ ▸ ୆਺͕૿͑ͨ࣌ͷશମͷ௨৴ྔ͕രൃ͢Δ ▸ αʔόʔαΠυϩδοΫ͕͋Δ৔߹ʹθϩ͔Β։ൃ͕ඞཁ 28
  6. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ͳͥطଘͷ੡඼ͷ࠾༻Ͱ͸ͳ͔͔ͬͨ ▸ 8JOEPXT4FSWFS͸ զʑʹͱͬͯ ӡ༻ίετ͕ߴ͍ ▸ ػೳաଟ

    ▸ ήʔϜͰͷ௨৴ʹಛ༗ͳ࢓༷΁ͷରԠ ▸ མͱͨ͘͠ͳ͍ɻ౔೔ʹ৸͍ͯΒΕΔγεςϜ͕ཉ͔ͬͨ͠ ▸ طଘγεςϜͱͷ૬ੑ 33
  7. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 39 ೝূ App 1. game start API

    2. IP:Port, Token, Topic 3. Connect Token 4. Token 5. OK 6. OK 1VC4VC
  8. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ඼࣭ॏࢹ ▸ υΩϡϝϯτ ▸ ҉໧ͷϧʔϧΛ࡞Βͳ͍ ▸ 4QIJOYΛ࠾༻

    QBDLFUEJBH  ▸ ෛՙࢼݧ ▸ ੑೳΛอূ͢Δ 40 ▸ ࣗಈςετ ▸ σάϨͤ͞ͳ͍ ▸ &OEUP&OEςετʹͩ͜ΘΔ ▸ λΠϜΞ΢τ ▸ ಉ࣌ॲཧ
  9. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ͳͥTCP͔ʁ ▸ /"5௒͑ ▸ ౸ୡอূ ▸ ॱংอূ

    ▸ ᫔᫓੍ޚ ▸ ήʔϜʹΑͬͯ͸5$1ͷΈͰ΋े෼ͳ৔߹΋ଟ͍ λʔϯ੍ͳͲ 41
  10. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE PUB/SUB(ग़൛/ߪಡ)Ϟσϧͱ͸ ▸ ඇಉظϝοηʔδϯάϞσϧͷҰछ ▸ 4VCTDSJCF ߪಡ ͍ͯ͠ΔϢʔβʔʹϝοηʔδΛ1VCMJTI

    ग़൛ ͢Δ ▸ 4VCଆ͸5PQJDͱ͍͏୯ҐͰߪಡ͢Δ ▸ 1VCଆ͸5PQJDʹϝοηʔδΛૹΔ ▸ 1VCଆ͸4VCଆΛҙࣝ͢Δඞཁ͕ͳ͘ɺૄ݁߹ʹͳΔ 49
  11. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE ࣮ࡍͷήʔϜͰͷ࢖ΘΕํ Room/1234 Room/1234/User/A A B C PubSub

    Room/1234/User/B Room/1234/User/C ࢀՃऀ͸ શମ༻ Topic ͱ ݸਓ༻ Topic Λ Subscribe ͢Δ Room/1234/User/D D
  12. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 69 ϓϩηεσβΠϯ ranch sessions_sup (simple_one_for_one) arkps_sup (one_for_one)

    session_udp_sup (simple_one_for_one) ErlangVM udp_sup (one_for_one) gen_udp session (system process) session_udp link link
  13. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 70 ϓϩηεσβΠϯ ranch session (system process) session_udp

    sessions_sup (simple_one_for_one) arkps_sup (one_for_one) session_udp_sup (simple_one_for_one) ErlangVM udp_sup (one_for_one) gen_udp link link
  14. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 73 ݻఆϔομ 1 bit 16 32 Type

    (ϝοηʔδͷछྨ) Length (શମͷ௕͞) SenderTimestamp (ૹ৴ऀ࣌ؒ) ※ޙड़ ReceiverTimesamp (ड৴ऀ࣌ؒ) ※ޙड़ Payload (ૹ৴಺༰)
  15. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 76 TLVϕʔε 1 16 32 Type Length

    SenderTimestamp ReceiverTimesamp SectionType SectionLength Value SectionType SectionLength Value
  16. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 79 ݻఆϔομ 1 bit 16 32 Type

    (ϝοηʔδͷछྨ) Length (શମͷ௕͞) SenderTimestamp (ૹ৴ऀ࣌ؒ) ※ޙड़ ReceiverTimesamp (ड৴ऀ࣌ؒ) ※ޙड़ Payload (ૹ৴಺༰)
  17. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 80 RTTܭଌ Client Timer Timer 1ms 1000ms

    Sender 1 ड৴͔Βૹ৴·Ͱʹ NT͔͔ͬͨͷͰ ड͚औͬͨʹΛ଍͢ Receiver 301 1300ms 321ms ͜ͷ࣌఺Ͱࣗ෼ͷλΠϚʔ͕ ͳͷͰΛҾ͍ͯ355͸NT PubSub 300ms
  18. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 81 RTTܭଌ Client Timer Timer 1 1000

    Sender Receiver 1 0 ※ ˞ॳճ͸૬खͷ5JNFS͕ Θ͔Βͳ͍ͷͰ ૹ৴·ͰʹNT͔͔ͬͨͷͰ ड͚औͬͨʹΛ଍͢ Sender Receiver 1300 301 1300 321 ͜ͷ࣌఺Ͱࣗ෼ͷλΠϚʔ͕ ͳͷͰ355͸NT 421 ૹ৴·ͰʹNT͔͔ͬͨͷͰ ड͚औͬͨʹΛ଍͢ Sender Receiver 421 1400 1420 ͜ͷ࣌఺Ͱࣗ෼ͷλΠϚʔ͕ ͳͷͰ355͸NT 521 1520 ͜ͷ࣌఺Ͱࣗ෼ͷλΠϚʔ͕ ͳͷͰ355͸NT Sender Receiver 521 1500 ड৴͔Βૹ৴·ͰʹNT͔͔ͬͨͷͰ ड͚औͬͨʹΛ଍͢ PubSub
  19. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 87 user/room 8 message/user/sec 0.2 waittime[ms]5000 rooms

    100 1000 2000 3000 3500 3750 (૝ఆ) users 800 8000 16000 24000 28000 30000 (૝ఆ) message/sec 1280 12800 25600 38400 44800 48000 (࣮ଌ) message/sec 1270 12783 25560 38328 44787 21054 (Server)CPU [%] 66.5 360.4 404.7 560.9 635.8 444.4 (Server)RAM [%] 0.6 2.4 3.7 6.2 7.2 16.5 RTT Med [ms] 1 2 5 12 19 34 RTT Avg [ms] 14 11 12 14 27 1260 RTT Max [ms] 44 49 75 356 536 62798 End to End Med [ms] 1 3 8 18 39 460 End to End Avg [ms] 2 12 13 21 50 3017 End to End Max [ms] 747 738 754 1003 1315 100979 66.5 360.4 404.7 560.9 635.8 444.4 1270 12783 25560 38328 44787 21054 0 100 200 300 400 500 600 700 800 0 10000 20000 30000 40000 50000 60000 70000 80000 800 8000 16000 24000 28000 30000 8user/room 1msg/5s (Server)CPU [%] () message/sec
  20. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 88 user/room 8 message/user/sec 1 waittime[ms]1000 rooms

    100 400 800 900 1000 (૝ఆ) users 800 3200 6400 7200 8000 (૝ఆ) message/sec 6400 25600 51200 57600 64000 (࣮ଌ) message/sec 6360 25527 48195 53240 25154 (Server)CPU [%] 275.8 618.3 709.3 774.7 766.7 (Server)RAM [%] 0.5 1.1 2.7 3 11.2 RTT Med [ms] 1 1 5 12 5 RTT Avg [ms] 10 7 24 40 494 RTT Max [ms] 45 48 491 1100 34295 End to End Med [ms] 2 11 36 80 310 End to End Avg [ms] 10 13 54 120 970 End to End Max [ms] 554 550 981 2923 44120 275.8 618.3 709.3 774.7 766.7 6360 25527 48195 53240 25154 0 100 200 300 400 500 600 700 800 0 10000 20000 30000 40000 50000 60000 70000 80000 800 3200 6400 7200 8000 8user/room 1msg/s (Server)CPU [%] () message/sec
  21. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 89 user/room 8 message/user/sec 15 waittime[ms] 66.6666666666667

    rooms 10 50 70 80 90 100 (૝ఆ) users 80 400 560 640 720 800 (૝ఆ) message/sec 9600 48000 67200 76800 86400 96000 (࣮ଌ) message/sec 9096 46047 58600 60764 63539 26390 (Server)CPU [%] 229.8 694.7 776.6 775.6 787.9 786.2 (Server)RAM [%] 0.4 0.5 0.6 0.7 0.7 8 RTT Med [ms] 38 2 7 10 15 24 RTT Avg [ms] 30 14 15 17 23 194 RTT Max [ms] 45 67 93 122 522 20686 End to End Med [ms] 19 19 24 33 46 73 End to End Avg [ms] 18 21 27 37 50 293 End to End Max [ms] 618 631 657 718 1231 39470 229.8 694.7 776.6 775.6 787.9 786.2 9096 46047 58600 60764 63539 26390 0 100 200 300 400 500 600 700 800 0 10000 20000 30000 40000 50000 60000 70000 80000 80 400 560 640 720 800 8user/room 15msg/s (Server)CPU [%] () message/sec
  22. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE FPROF ͰϓϩϑΝΠϦϯά ▸ TFOE͕஗͍ ▸ QPSU@DPNNBOEͰඇಉظʹͨ͠Βղܾ ▸

    HFO@VEQUDQTFOE͕ϘτϧωοΫͳͱ͖ʹ΍Δ͜ͱ
 IUUQRJJUBDPNNVSVSVJUFNTCFCCBDFC 92
  23. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 93 user/room 8 message/user/sec 0.2 waittime[ms]5000 rooms

    100 2000 3750 6250 7500 8750 (૝ఆ) users 800 16000 30000 50000 60000 70000 (૝ఆ) message/sec 1280 25600 48000 80000 96000 112000 (࣮ଌ) message/sec 1281 25639 48124 78742 94555 97731 (Server)CPU [%] 46.9 312.2 426.8 625 723.2 718 (Server)RAM [%] 2 6.8 9.8 14 17.3 18.7 1281 25639 48124 78742 94555 97731 0 100 200 300 400 500 600 700 800 0 20000 40000 60000 80000 100000 120000 140000 160000 800 16000 30000 50000 60000 70000 8user/room 1msg/5s (Server)CPU [%] () message/sec
  24. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 94 user/room 8 message/user/sec 1 waittime[ms]1000 rooms

    100 400 800 1000 1200 1400 1600 (૝ఆ) users 800 3200 6400 8000 9600 11200 12800 (૝ఆ) message/sec 6400 25600 51200 64000 76800 89600 102400 (࣮ଌ) message/sec 6356 25472 51202 63890 75912 88143 99033 (Server)CPU [%] 139.4 269.1 425 500.3 625.8 699.2 765.3 (Server)RAM [%] 2 3.6 5 5 5.2 5.2 6.6 6356 25472 51202 63890 75912 88143 99033 0 100 200 300 400 500 600 700 800 0 20000 40000 60000 80000 100000 120000 140000 160000 800 3200 6400 8000 9600 11200 12800 8user/room 1msg/s (Server)CPU [%] () message/sec
  25. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 95 user/room 8 message/user/sec 15 waittime[ms] 66.6666666666667

    rooms 10 50 70 80 90 100 110 120 130 140 (૝ఆ) users 80 400 560 640 720 800 880 960 1040 1120 (૝ఆ) message/sec 9600 48000 67200 76800 86400 96000 105600 115200 124800 134400 (࣮ଌ) message/sec 8912 45967 64648 74035 83211 91581 99596 106775 112889 116739 (Server)CPU [%] 161.5 374.2 489.7 535.8 582.1 632.1 666.4 703.7 726.9 784.7 (Server)RAM [%] 2.1 2.3 2.3 2.3 2.3 2.2 1.8 2.6 3 3.4 8912 45967 64648 74035 83211 91581 99596 106775 112889 116739 0% 100% 200% 300% 400% 500% 600% 700% 800% 0 20000 40000 60000 80000 100000 120000 140000 10 50 70 80 90 100 110 120 130 140 8user/room 15msg/s (Server)CPU [%] () message/sec
  26. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 99 HEART BEAT App PubSub PubSub PubSub

    HTTP API A 2019-01-01 00:00:00 alive B 2019-01-01 00:00:00 alive C 2019-01-01 00:00:00 alive -4 sec -4 sec -4 sec ఆظత(5ඵʹ1ճ)ʹAPIαʔόʔͷAPIΛݺͼग़͠ RDBMSʹ࠷ऴݺͼग़͠೔࣌, εςʔλεΛอଘ
  27. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 100 HEART BEAT App PubSub PubSub PubSub

    HTTP API A 2019-01-01 00:00:00 dead B 2019-01-01 01:00:00 alive C 2019-01-01 01:00:00 alive A B C -3604 sec -4 sec -4 sec αʔόʔো֐
  28. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 101 HEART BEAT App PubSub PubSub PubSub

    HTTP API A 2019-01-01 00:00:00 dead B 2019-01-01 01:00:00 alive C 2019-01-01 01:00:00 alive A B C -3604 sec -4 sec -4 sec ௨৴ো֐
  29. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 102 HEART BEAT App PubSub PubSub PubSub

    HTTP API A 2019-01-01 00:00:00 dead B 2019-01-01 01:00:00 alive C 2019-01-01 01:00:00 alive A B C -3604 sec -4 sec -4 sec ௨৴ো֐͔Β෮چ
  30. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 103 HEART BEAT App PubSub PubSub PubSub

    HTTP API A 2019-01-01 00:00:00 dead B 2019-01-01 01:00:00 alive C 2019-01-01 01:00:00 alive A B C -3604 sec -4 sec -4 sec Ұ౓ࢮ๢൑ఆͨ͠αʔόʔͷ෮ؼ͸ ઈରʹೝΊͳ͍ ௨৴ো֐͔Β෮چ
  31. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 107 ͳͥ෮ؼΛೝΊͳ͍͔ App 1. RoomA Server? 1VC4VC

    2. Server B IP, Port … 1VC4VC Server A Server B RoomA Server B RoomB Server B Connection
  32. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 110 ͳͥ෮ؼΛೝΊͳ͍͔ App Connection 1. heartbeat 2.

    OK 1VC4VC 1VC4VC Server A Server B RoomA Server A RoomB Server B 1. RoomA Server? 2. Server B IP, Port …
  33. $PQZSJHIU $ HVNJ*OD"MM3JHIUT3FTFSWFE 111 ͳͥ෮ؼΛೝΊͳ͍͔ App Connection 1. heartbeat 2.

    OK 1VC4VC 1VC4VC Server A Server B RoomA Server B RoomB Server B 1. RoomA Server? 2. Server B IP, Port … Connection ಉ͡αʔόʔʹ͍Δ΂͖Ϣʔβʔ͕ ผͷαʔόʔʹ