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

RedisのCPU負荷対策パターン

 RedisのCPU負荷対策パターン

by id:y_uuki

Yuuki Tsubouchi (yuuk1)

August 19, 2017
Tweet

More Decks by Yuuki Tsubouchi (yuuk1)

Other Decks in Programming

Transcript

  1. ͦͯ͠෺ޠ͸
    Կ౓໨͔ͷRedis CPU
    ෛՙରࡦΛܴ͑Δ

    View full-size slide

  2. RedisͷCPUෛՙରࡦ
    ύλʔϯ
    id:y_uuki
    Kyoto.ͳΜ͔#3

    View full-size slide

  3. Redis
    ͔ͭͬͯ·͔͢ʁ

    View full-size slide

  4. RedisͰCPUফඅ͕ϘτϧωοΫʹͳΔέʔε
    • ܦݧతʹඵؒ਺ສίϚϯυ໨҆
    • ΫΤϦͷछྨ΍ΞΫηεύλʔϯʹΑͬͯɺ
    ಉ͡ϋʔυ΢ΣΞͰ͋ͬͯ΋CPUར༻཰͸ม
    Խ͢Δ
    • CPUੑೳʹΑͬͯɺಉ͡ϫʔΫϩʔυͰ͋ͬ
    ͯ΋CPUར༻཰͸มԽ͢Δ

    View full-size slide

  5. RedisͷCPUϦιʔεফඅߏ଄
    • γϯάϧεϨουͰಈ࡞͢ΔͨΊɺϚϧνί
    Ξ؀ڥͰ͋ͬͯ΋ɺ1CPUίΞ͔͠࢖͑ͳ͍
    • MySQL/PostgreSQL͸ίωΫγϣϯ͝ͱʹ
    εϨου/ϓϩηεΛىಈ͢ΔϚϧνεϨο
    υ/ϓϩηεϞσϧ

    View full-size slide

  6. RedisͷCPUෛՙͱઓ͏ͨΊʹ
    ͲͷΑ͏ͳ࡞ઓΛͱΓ͏Δ͔

    View full-size slide

  7. εέʔϧΞοϓ εέʔϧΞ΢τ
    CPU1ίΞੑೳ͋͛Δ
    ਨ௚෼ׂ
    ࢀর༻εϨʔϒ
    ਫฏ෼ׂ - ࣗલ
    ਫฏ෼ׂ - Redis Cluster
    CPUෛՙ࡟ݮ
    RedisύΠϓϥΠϯ
    Lua scripting
    Linux RPS
    (Redis modules)

    View full-size slide

  8. εέʔϧΞοϓ
    • RedisαʔόͷCPUΫϩοΫΛߴ͍΋ͷʹมߋ
    • EC2ͳΒc4ܥͷΠϯελϯε
    • ίΞ਺Λ૿΍ͯ͠΋͋·Γҙຯ͸ͳ͍
    • CPUར༻཰͕90%͔Β40%ʹͳͬͨ͜ͱ͕͋Δ
    • Xeon E5-2650L v2 @1.70GHz (Ivy Bridge)
    • Xeon E5-2670 v3 @2.30GHz (Haswell)
    • CPUΫϩοΫʹରͯ͠ઢܗʹมԽ͢ΔΘ͚Ͱ͸
    ͳ͍

    View full-size slide

  9. CPUෛՙ࡟ݮ

    View full-size slide

  10. CPUෛՙ࡟ݮ - Linux RPS
    • redisεϨουͱΧʔωϧͷωοτϫʔΫI/Oε
    ϨουΛผʑͷίΞͰॲཧ͢Δ
    • RPSͰύέοτॲཧΛCPU0Ͱड͚ͯ
    • redisεϨουͷCPU AffinityͰύέοτॲཧͱผ
    # echo ‘3’ > /sys/class/net/eth0/queues/rx-0/rps_cpus
    # taskset -pc 3-4 $(cat /run/redis/redis-server.pid)

    View full-size slide

  11. https://speakerdeck.com/yuukit/linux-network-performance-improvement-at-hatena

    View full-size slide

  12. CPUෛՙ࡟ݮ - RedisύΠϓϥΠϯ
    • ΫϥΠΞϯτ͕ϨεϙϯεΛ·ͨͣʹޙଓͷϦΫΤετ
    Λ౤ೖ͢Δ͜ͱͰɺύέοτԟ෮ճ਺ΛݮΒ͢
    • ωοτϫʔΫI/OॲཧΛ·ͱΊ࣮ͯߦͰ͖ΔͷͰɺCPU
    ॲཧ͕ݮΔ
    • 1ճͷread(2)Ͱෳ਺ͷίϚϯυΛಡΈग़ͤΔͷͰɺγ
    εςϜίʔϧճ਺͕ݮΓίϯςΩετεΠον΋ݮΔ
    • ਅͷRedisύΠϓϥΠϯ(by id:ichirin2501)
    https://redis.io/topics/pipelining

    View full-size slide

  13. CPUෛՙ࡟ݮ - Lua scripting
    • ͋ΔίϚϯυͷ݁ՌΛ࣍ͷίϚϯυ͕ར༻͠
    ͍ͯΔҰ࿈ͷίϚϯυྻ͸ύΠϓϥΠϯ࣮ߦ
    ෆՄ
    • Lua scriptingͳΒҰ࿈ͷίϚϯυྻΛluaίʔ
    υͱͯ͠ૹΓࠐΈɺ݁Ռ͚ͩड͚औΕΔ
    • ύΠϓϥΠϯಉ༷ɺΫϥΠΞϯτͱͷԟ෮ճ
    ਺͕ݮΓɺI/OγεςϜίʔϧΛ·ͱΊΒΕΔ
    https://redis.io/commands/eval

    View full-size slide

  14. εέʔϧΞ΢τ

    View full-size slide

  15. εέʔϧΞ΢τ-ࢀর༻εϨʔϒ
    • Redis͸ϚελʔɾεϨʔϒߏ੒͕ͱΕΔ
    • ࢀরΫΤϦΛεϨʔϒʹ޲͚ͯϚελʔͷෛՙΛ
    ࡟ݮ
    • εϨʔϒͷલஈʹϩʔυόϥϯα͕ඞཁ
    • MySQLͳͲͷRDBMSͰΑ͘ͱΒΕΔ࡞ઓ
    • ϨϓϦέʔγϣϯ஗Ԇʹ஫ҙ
    • ॻ͖ࠐΜͩ͸ͣͷσʔλ͕·ͩεϨʔϒʹ఻ൖ
    ͍ͯ͠ͳ͍Մೳੑ͕͋Δ

    View full-size slide

  16. εέʔϧΞ΢τ- ਨ௚෼ׂ
    • RedisͷϚελʔϊʔυΛෳ਺༻ҙ͢Δ
    • ػೳA͸Ϛελʔ1Λ࢖͏ɺػೳB͸Ϛελʔ2Λ
    ࢖͏
    • ػೳͷͿΜ͚ͩɺϚελʔϊʔυΛӡ༻͢Δίε
    τ͸͋Δ
    • ͋ΔҰͭػೳͷෛՙ͕ࢧ഑తͳ৔߹ʹ͸࢖͑ͳ͍
    • Ωϟογϡ༻్ͳΒ͚ͦͩ͜memcachedΛར༻

    View full-size slide

  17. εέʔϧΞ΢τ- ਫฏ෼ׂ
    • γϟʔσΟϯάͱ΋ݺͿ
    • ಉ͡༻్ͷ֤ΩʔΛෳ਺ͷϚελʔϊʔυʹ෼ࢄ
    • ΩʔͱϊʔυͷϚοϐϯάΛࣗ෼Ͱ΍Δ
    • Ϛοϐϯάํ๏͸༷ʑ
    • ϢʔβID xxx ͸ϊʔυYʹϚοϓ
    • ϢʔβID yyy ͷ৒༨͕ z ͳΒϊʔυXʹϚοϓ
    • γϟʔυΛ૿ݮͨ͠ͱ͖ͷσʔλ࠶഑ஔ͕೉͢͠
    ͗ΔͷͰͱʹ͔͘΍Γͨ͘ͳ͍

    View full-size slide

  18. εέʔϧΞ΢τ- Redis ClusterʹΑΔਫฏ෼ׂ
    • ࠷ऴԞٛ
    • Ωʔͷ෼ࢄ഑ஔͱ࠶഑ஔ/ϑΣΠϧΦʔόʔͳͲα
    ϙʔτ
    • redis-tribͱ͍͏ެࣜπʔϧͰࣗಈ࠶഑ஔ΋Ͱ͖Δ
    • ࠷௿6ϊʔυඞཁ
    • database͕1͔͔ͭ͑͠ͳ͍
    • ෳ਺Ωʔʹର͢ΔίϚϯυ࣮ߦʹ஫ҙ
    • 1000ϊʔυ͙Β͍·Ͱ͸εέʔϧ͢ΔΒ͍͠

    View full-size slide

  19. Redis modules
    • Redus 4.0͔ΒϞδϡʔϧػೳ͕࣮૷͞Εͨ
    • Redisͷ಺෦APIΛ࢖ͬͯಠࣗͷσʔλߏ଄΍
    ίϚϯυΛ࣮૷Ͱ͖ΔʢΒ͍͠ʣ
    • ΞϓϦέʔγϣϯʹ࠷దԽͨ͠σʔλߏ଄Λ
    ࡞Ε͹ɺΊͪΌͪ͘ΌCPUޮ཰Α͘ͳΔͷͰ
    ͸ʁʁʁʁʁ

    View full-size slide

  20. εέʔϧΞοϓ εέʔϧΞ΢τ
    CPU1ίΞੑೳ͋͛Δ
    ਨ௚෼ׂ
    ࢀর༻εϨʔϒ
    ਫฏ෼ׂ
    Redis Cluster
    CPUෛՙ࡟ݮ
    RedisύΠϓϥΠϯ
    Lua scripting
    Linux RPS
    (Redis modules)

    View full-size slide

  21. ෺ޠ͔Βઓུ΁

    View full-size slide

  22. ઓུΛཱͯΔͨΊʹ
    ͱΓ͏Δઓज़Λ
    ஌͓ͬͯ͘

    View full-size slide