Webアプリケーションのキャッシュ戦略とそのパターン / Pattern and Strategy of Web Application Caching

672ee2598bea695496f11022a9a9c4c8?s=47 moznion
March 04, 2017

Webアプリケーションのキャッシュ戦略とそのパターン / Pattern and Strategy of Web Application Caching

YAPC::Kansai OSAKA 2017の資料です

672ee2598bea695496f11022a9a9c4c8?s=128

moznion

March 04, 2017
Tweet

Transcript

  1. 2.
  2. 9.

    Ωϟογϡʁ ʮΩϟογϡ: ෺ΛӅͨ͠Γ֨ೲͨ͠Γ͓ͯ͘͠ͷʹ҆શͳ৔ॴʯ ― Webster ৽ੈքถޠࣙయɼΧϨοδ൛ ୈ3൛ ʮΩϟογϡͱ͸ௐ΂Δඞཁͷ͋Δ΋ͷΛஔ͍͓ͯ҆͘શͳ৔ॴͷ ͜ͱͰ͋ΔɽΩϟογϡͱ͍͏༻ޠ͸ɼ࠷ॳͷ঎༻ίϯϐϡʔλʹ ͓͍ͯɼϓϩηοαͱओهԱͱͷؒʹૠೖ͞ΕͨهԱ֊૚ϨϕϧΛ

    දͨ͢Ίʹ࢖༻͞Εͨɽ(தུ) ͜ͷ༻ޠ͸ࠓ೔Ͱ͸ࢀরͷہॴੑΛ ར༻͢ΔΑ͏ʹ؅ཧ͞Εͨ೚ҙͷهԱΛࢦͯ͠΋࢖༻͞ΕΔʯ ― σΠϏου A ύλʔιϯ, δϣϯ ̡ ϔωγʔ ஶ, ੒ా ޫজ ༁ ίϯϐϡʔλͷߏ੒ͱઃܭɼୈ5൛ [Լ] ❞ ❞
  3. 13.

    Ωϟογϡʁ ʮΩϟογϡ: ෺ΛӅͨ͠Γ֨ೲͨ͠Γ͓ͯ͘͠ͷʹ҆શͳ৔ॴʯ ― Webster ৽ੈքถޠࣙయɼΧϨοδ൛ ୈ3൛ ʮΩϟογϡͱ͸ௐ΂Δඞཁͷ͋Δ΋ͷΛஔ͍͓ͯ҆͘શͳ৔ॴͷ ͜ͱͰ͋ΔɽΩϟογϡͱ͍͏༻ޠ͸ɼ࠷ॳͷ঎༻ίϯϐϡʔλʹ ͓͍ͯɼϓϩηοαͱओهԱͱͷؒʹૠೖ͞ΕͨهԱ֊૚ϨϕϧΛ

    දͨ͢Ίʹ࢖༻͞Εͨɽ(தུ) ͜ͷ༻ޠ͸ࠓ೔Ͱ͸ࢀরͷہॴੑΛ ར༻͢ΔΑ͏ʹ؅ཧ͞Εͨ೚ҙͷهԱΛࢦͯ͠΋࢖༻͞ΕΔʯ ― σΠϏου A ύλʔιϯ, δϣϯ ̡ ϔωγʔ ஶ, ੒ా ޫজ ༁ ίϯϐϡʔλͷߏ੒ͱઃܭɼୈ5൛ [Լ] ❞ ❞
  4. 14.

    Ωϟογϡʁ ʮΩϟογϡ: ෺ΛӅͨ͠Γ֨ೲͨ͠Γ͓ͯ͘͠ͷʹ҆શͳ৔ॴʯ ― Webster ৽ੈքถޠࣙయɼΧϨοδ൛ ୈ3൛ ʮΩϟογϡͱ͸ௐ΂Δඞཁͷ͋Δ΋ͷΛஔ͍͓ͯ҆͘શͳ৔ॴͷ ͜ͱͰ͋ΔɽΩϟογϡͱ͍͏༻ޠ͸ɼ࠷ॳͷ঎༻ίϯϐϡʔλʹ ͓͍ͯɼϓϩηοαͱओهԱͱͷؒʹૠೖ͞ΕͨهԱ֊૚ϨϕϧΛ

    දͨ͢Ίʹ࢖༻͞Εͨɽ(தུ) ͜ͷ༻ޠ͸ࠓ೔Ͱ͸ࢀরͷہॴੑΛ ར༻͢ΔΑ͏ʹ؅ཧ͞Εͨ೚ҙͷهԱΛࢦͯ͠΋࢖༻͞ΕΔʯ ― σΠϏου A ύλʔιϯ, δϣϯ ̡ ϔωγʔ ஶ, ੒ా ޫজ ༁ ίϯϐϡʔλͷߏ੒ͱઃܭɼୈ5൛ [Լ] ❞ ❞ جຊతͳ֓೦ͱͯ͠͸ͦͷ௨Γ Ͱ΋͜Εϓϩηοαͷ࿩ͩʂ
  5. 21.

    ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ұ࣍ετϨʔδ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε
  6. 22.

    ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ұ࣍ετϨʔδ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε ͕͜͜ωοΫ ͷ৔߹
  7. 24.

    ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε Cache Storage ii iii
  8. 25.

    ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε Cache Storage ii iii Ұ࣍ετϨʔδΑΓ Ωϟγϡ͕ߴ଎ͳΒ ૣ͘ฦͤΔ
  9. 31.

    HTTPϨΠϠͰͷΩϟογϡ Server Client i ii i. ίϯςϯπ͘Εʙ ii. ͸͍Αʙ contents

    ίϯςϯπ͕ωοτϫʔΫʹ৐Δ (serverͷཪଆͰ͸ߋʹॏ͍ॲཧ͕૸͍ͬͯΔ͔΋) contents
  10. 34.

    HTTPϨΠϠͰͷΩϟογϡ Server Client contents contents i ii i. ίϯςϯπ͘Εʙ ii.

    ΋͏࣋ͬͯ·͢Α ίϯςϯπ͸ωοτϫʔΫʹ৐Βͳ͍ ʮ΋͏࣋ͬͯ·͢Αʯͱ͍͏৘ใ͕৐Δ
  11. 46.

    ಛఆίϯϙʔωϯτͷෛՙܰݮ Server Client Client . . . Storage Cache Storage

    Ωϟογϡʹಀ͕ͯ͠ ΍Δ͜ͱͰ աෛՙίϯϙʔωϯτͷ ෛՙΛܰݮ͢Δ
  12. 47.

    ಛఆίϯϙʔωϯτͷෛՙܰݮ Server Client Client . . . Storage Cache Storage

    ͔͠͠Ωϟογϡίϯϙʔωϯτ͕ ૿͑Δ͜ͱͰγεςϜͱͯ͠ͷ ނোՄೳੑ͸૿Ճ͢Δʂ
  13. 52.
  14. 60.

    Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query…

    ͜͜ͰαʔϏε͕٧·Δ (աෛՙͰDB͕μ΢ϯ͢Δ͔΋ʁ)
  15. 74.

    ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Request

    (via HTTP) ֎෦αʔϏεʹ౎౓ϦΫΤετΛ ඈ͹͍ͯͯ͠͸஗͍
  16. 76.

    ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Cache

    ΩϟογϡΛ஥հ͢Δ͜ͱͰ ߴ଎ʹ݁ՌΛಘΔͱͱ΋ʹ ෆཁͳϦΫΤετΛ࡟ݮ͢Δ
  17. 98.

    Ωϟογϡ͕ͿͬյΕͯ΋ٽ͔ͳ͍ - Ωϟογϡ͸յΕΔ΋ͷͱͯ͠ӡ༻ͨ͠΄͏͕޾ͤ - Ωϟογϡ͕ͿͬյΕͨΒ·ͨ࡞Ε͹ྑ͍ - Let it crash -

    ͨͩɼͿͬյΕͨλΠϛϯάͰٸܹʹෛՙ্͕͕Δ
 Α͏ͳঢ়گʹ͚ͩͳΒͳ͍Α͏޻෉͢΂͖
  18. 109.

    Server Client Primary Storage ii iii Cache Storage Brokerύλʔϯ i

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ώοτͨ͠ͷͰ݁ՌΛฦ͢ iv. Ϩεϙϯε
  19. 110.

    Server Client Primary Storage ii iii Cache Storage Brokerύλʔϯ i

    ii iii iv v vi i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. Ωϟογϡʹແ͍ͷͰ
 Ұ࣍ετϨʔδ໰͍߹Θͤ iv. ݁ՌΛΩϟογϡʹೖΕΔ v. ݁ՌΛฦ͢ vi. Ϩεϙϯε
  20. 111.

    Brokerύλʔϯ Pros - ΞΫηεස౓ͷߴ͍ίϯςϯπ͸Ωϟογϡʹ৐Γ
 ௿͍ίϯςϯπ͸Ωϟογϡʹ৐Βͳ͍
 লϝϞϦ - σʔλͷ੔߹ੑΛอͪ΍͍͢ (Ωϟογϡ͕৽઱) Cons

    - ࠷ॳʹίϯςϯπʹདྷͨਓ͕ॏ͘ͳΔ - Ωϟογϡʹ৐͍ͬͯͳ͍࣌ʹେྔʹΞΫηε͕དྷΔͱ
 ෳ਺ͷϦΫΤετ͕Ұ࣍ετϨʔδʹૹΒΕΔ
 Cache Thundering Herd ໰୊ (ޙड़)
  21. 113.

    Inventoryύλʔϯ Server Client Primary Storage ii iii Cache Storage i

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ώοτͨ͠ͷͰ݁ՌΛฦ͢ iv. Ϩεϙϯε
  22. 114.

    Inventoryύλʔϯ Server Client Primary Storage ii iii Cache Storage i

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. Ωϟογϡʹແ͍ iv. σϑΥϧτ஋Λฦ͢ (ඇಉظͰ) iv’. Ұ࣍ετϨʔδ໰͍߹Θͤ v’. ݁ՌΛΩϟογϡ iv’ v’
  23. 117.

    Warmerύλʔϯ Server Client Cache Storage i ii iii iv contents

    contents v i. ίϯςϯπੜ੒࣌ʹ
 ίϯςϯπΛΩϟογϡ ii. ϦΫΤετ iii. Ωϟογϡ໰͍߹Θͤ iv. ίϯςϯπฦ͢ v. Ϩεϙϯε
  24. 118.

    Warmerύλʔϯ Pros - ࠷ॳ͔ΒΩϟογϡܦ༝ͰίϯςϯπΛฦͤΔ - Cache Thundering Herd ໰୊ͷҰղܾ Cons

    - ίϯςϯπ͕ੜ੒͞ΕΔܥ͡Όͳ͍ͱ࢖͑ͳ͍ - ϥΠϑαΠΫϧͷ؅ཧ͕൥ࡶʹͳΔ (ಉظͱ͔) - ϝϞϦΊͬͪΌ৯͏৔߹͕͋Δ - Ωϟογϡ͕شൃͨ࣌͠ʹͲ͏͢Δͷ͔ʁ
  25. 120.

    Pool & Flushύλʔϯ Server Client Primary Storage ii iii Cache

    Storage i ii iii i. ϦΫΤετ ii. ΩϟογϡʹཷΊΔ iii. Ϩεϙϯε (͋ΔλΠϛϯάͰ) x. ӬଓετϨʔδʹॻ͖ग़͠ x
  26. 123.

    HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ϦΫΤετ ii. ϔομ෇͚ͯฦ͢

    - Expires - Cache-Control - Last-Modified - ETag contents
  27. 124.

    HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ৚݅ͷϔομ෇͚ͯ
 ϦΫΤετ -

    If-Modified-Since - If-None-Match ii. ৚݅ʹैͬͯฦ͢ contents contents
  28. 125.

    HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ৚݅ͷϔομ෇͚ͯ
 ϦΫΤετ -

    If-Modified-Since - If-None-Match ii. ৚݅ʹैͬͯฦ͢ contents contents ແବʹίϯςϯπΛฦ͢ඞཁ͕ແ͘ͳΔ
  29. 159.

    Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query

    ΫΤϦ (͋Δ͍͸ςʔϒϧ)Λ௚ͤʂʂʂ ద੾ʹνϡʔχϯά͞ΕͨΫΤϦʹ͸
 ΩϟογϡͳͲෆཁ
  30. 160.

    Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query

    Slave Slave ΩϟογϡʹཔΒͣ SlaveΛฒ΂Δ΋Α͠
  31. 162.

    ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Request

    (via HTTP) ֎෦αʔϏεʹґଘ͠ͳ͍ ํ๏Λߟ͑Δ
  32. 170.

    Q?