Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Webアプリケーションのキャッシュ戦略とそのパターン / Pattern and Strate...

moznion
March 04, 2017

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

YAPC::Kansai OSAKA 2017の資料です

moznion

March 04, 2017
Tweet

More Decks by moznion

Other Decks in Technology

Transcript

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

    දͨ͢Ίʹ࢖༻͞Εͨɽ(தུ) ͜ͷ༻ޠ͸ࠓ೔Ͱ͸ࢀরͷہॴੑΛ ར༻͢ΔΑ͏ʹ؅ཧ͞Εͨ೚ҙͷهԱΛࢦͯ͠΋࢖༻͞ΕΔʯ ― σΠϏου A ύλʔιϯ, δϣϯ ̡ ϔωγʔ ஶ, ੒ా ޫজ ༁ ίϯϐϡʔλͷߏ੒ͱઃܭɼୈ5൛ [Լ] ❞ ❞
  2. Ωϟογϡʁ ʮΩϟογϡ: ෺ΛӅͨ͠Γ֨ೲͨ͠Γ͓ͯ͘͠ͷʹ҆શͳ৔ॴʯ ― Webster ৽ੈքถޠࣙయɼΧϨοδ൛ ୈ3൛ ʮΩϟογϡͱ͸ௐ΂Δඞཁͷ͋Δ΋ͷΛஔ͍͓ͯ҆͘શͳ৔ॴͷ ͜ͱͰ͋ΔɽΩϟογϡͱ͍͏༻ޠ͸ɼ࠷ॳͷ঎༻ίϯϐϡʔλʹ ͓͍ͯɼϓϩηοαͱओهԱͱͷؒʹૠೖ͞ΕͨهԱ֊૚ϨϕϧΛ

    දͨ͢Ίʹ࢖༻͞Εͨɽ(தུ) ͜ͷ༻ޠ͸ࠓ೔Ͱ͸ࢀরͷہॴੑΛ ར༻͢ΔΑ͏ʹ؅ཧ͞Εͨ೚ҙͷهԱΛࢦͯ͠΋࢖༻͞ΕΔʯ ― σΠϏου A ύλʔιϯ, δϣϯ ̡ ϔωγʔ ஶ, ੒ా ޫজ ༁ ίϯϐϡʔλͷߏ੒ͱઃܭɼୈ5൛ [Լ] ❞ ❞
  3. Ωϟογϡʁ ʮΩϟογϡ: ෺ΛӅͨ͠Γ֨ೲͨ͠Γ͓ͯ͘͠ͷʹ҆શͳ৔ॴʯ ― Webster ৽ੈքถޠࣙయɼΧϨοδ൛ ୈ3൛ ʮΩϟογϡͱ͸ௐ΂Δඞཁͷ͋Δ΋ͷΛஔ͍͓ͯ҆͘શͳ৔ॴͷ ͜ͱͰ͋ΔɽΩϟογϡͱ͍͏༻ޠ͸ɼ࠷ॳͷ঎༻ίϯϐϡʔλʹ ͓͍ͯɼϓϩηοαͱओهԱͱͷؒʹૠೖ͞ΕͨهԱ֊૚ϨϕϧΛ

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

    ϦΫΤετ ii. Ұ࣍ετϨʔδ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε
  5. ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ұ࣍ετϨʔδ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε ͕͜͜ωοΫ ͷ৔߹
  6. ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε Cache Storage ii iii
  7. ΞϓϦέʔγϣϯϨΠϠͰͷΩϟογϡ Server Client Primary Storage i ii iii iv i.

    ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ݁ՌΛฦ͢ iv. Ϩεϙϯε Cache Storage ii iii Ұ࣍ετϨʔδΑΓ Ωϟγϡ͕ߴ଎ͳΒ ૣ͘ฦͤΔ
  8. HTTPϨΠϠͰͷΩϟογϡ Server Client i ii i. ίϯςϯπ͘Εʙ ii. ͸͍Αʙ contents

    ίϯςϯπ͕ωοτϫʔΫʹ৐Δ (serverͷཪଆͰ͸ߋʹॏ͍ॲཧ͕૸͍ͬͯΔ͔΋) contents
  9. HTTPϨΠϠͰͷΩϟογϡ Server Client contents contents i ii i. ίϯςϯπ͘Εʙ ii.

    ΋͏࣋ͬͯ·͢Α ίϯςϯπ͸ωοτϫʔΫʹ৐Βͳ͍ ʮ΋͏࣋ͬͯ·͢Αʯͱ͍͏৘ใ͕৐Δ
  10. ಛఆίϯϙʔωϯτͷෛՙܰݮ Server Client Client . . . Storage Cache Storage

    Ωϟογϡʹಀ͕ͯ͠ ΍Δ͜ͱͰ աෛՙίϯϙʔωϯτͷ ෛՙΛܰݮ͢Δ
  11. ಛఆίϯϙʔωϯτͷෛՙܰݮ Server Client Client . . . Storage Cache Storage

    ͔͠͠Ωϟογϡίϯϙʔωϯτ͕ ૿͑Δ͜ͱͰγεςϜͱͯ͠ͷ ނোՄೳੑ͸૿Ճ͢Δʂ
  12. Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query…

    ͜͜ͰαʔϏε͕٧·Δ (աෛՙͰDB͕μ΢ϯ͢Δ͔΋ʁ)
  13. ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Request

    (via HTTP) ֎෦αʔϏεʹ౎౓ϦΫΤετΛ ඈ͹͍ͯͯ͠͸஗͍
  14. ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Cache

    ΩϟογϡΛ஥հ͢Δ͜ͱͰ ߴ଎ʹ݁ՌΛಘΔͱͱ΋ʹ ෆཁͳϦΫΤετΛ࡟ݮ͢Δ
  15. Ωϟογϡ͕ͿͬյΕͯ΋ٽ͔ͳ͍ - Ωϟογϡ͸յΕΔ΋ͷͱͯ͠ӡ༻ͨ͠΄͏͕޾ͤ - Ωϟογϡ͕ͿͬյΕͨΒ·ͨ࡞Ε͹ྑ͍ - Let it crash -

    ͨͩɼͿͬյΕͨλΠϛϯάͰٸܹʹෛՙ্͕͕Δ
 Α͏ͳঢ়گʹ͚ͩͳΒͳ͍Α͏޻෉͢΂͖
  16. Server Client Primary Storage ii iii Cache Storage Brokerύλʔϯ i

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ώοτͨ͠ͷͰ݁ՌΛฦ͢ iv. Ϩεϙϯε
  17. Server Client Primary Storage ii iii Cache Storage Brokerύλʔϯ i

    ii iii iv v vi i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. Ωϟογϡʹແ͍ͷͰ
 Ұ࣍ετϨʔδ໰͍߹Θͤ iv. ݁ՌΛΩϟογϡʹೖΕΔ v. ݁ՌΛฦ͢ vi. Ϩεϙϯε
  18. Brokerύλʔϯ Pros - ΞΫηεස౓ͷߴ͍ίϯςϯπ͸Ωϟογϡʹ৐Γ
 ௿͍ίϯςϯπ͸Ωϟογϡʹ৐Βͳ͍
 লϝϞϦ - σʔλͷ੔߹ੑΛอͪ΍͍͢ (Ωϟογϡ͕৽઱) Cons

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

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. ώοτͨ͠ͷͰ݁ՌΛฦ͢ iv. Ϩεϙϯε
  20. Inventoryύλʔϯ Server Client Primary Storage ii iii Cache Storage i

    ii iii iv i. ϦΫΤετ ii. Ωϟογϡ໰͍߹Θͤ iii. Ωϟογϡʹແ͍ iv. σϑΥϧτ஋Λฦ͢ (ඇಉظͰ) iv’. Ұ࣍ετϨʔδ໰͍߹Θͤ v’. ݁ՌΛΩϟογϡ iv’ v’
  21. Warmerύλʔϯ Server Client Cache Storage i ii iii iv contents

    contents v i. ίϯςϯπੜ੒࣌ʹ
 ίϯςϯπΛΩϟογϡ ii. ϦΫΤετ iii. Ωϟογϡ໰͍߹Θͤ iv. ίϯςϯπฦ͢ v. Ϩεϙϯε
  22. Warmerύλʔϯ Pros - ࠷ॳ͔ΒΩϟογϡܦ༝ͰίϯςϯπΛฦͤΔ - Cache Thundering Herd ໰୊ͷҰղܾ Cons

    - ίϯςϯπ͕ੜ੒͞ΕΔܥ͡Όͳ͍ͱ࢖͑ͳ͍ - ϥΠϑαΠΫϧͷ؅ཧ͕൥ࡶʹͳΔ (ಉظͱ͔) - ϝϞϦΊͬͪΌ৯͏৔߹͕͋Δ - Ωϟογϡ͕شൃͨ࣌͠ʹͲ͏͢Δͷ͔ʁ
  23. Pool & Flushύλʔϯ Server Client Primary Storage ii iii Cache

    Storage i ii iii i. ϦΫΤετ ii. ΩϟογϡʹཷΊΔ iii. Ϩεϙϯε (͋ΔλΠϛϯάͰ) x. ӬଓετϨʔδʹॻ͖ग़͠ x
  24. HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ϦΫΤετ ii. ϔομ෇͚ͯฦ͢

    - Expires - Cache-Control - Last-Modified - ETag contents
  25. HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ৚݅ͷϔομ෇͚ͯ
 ϦΫΤετ -

    If-Modified-Since - If-None-Match ii. ৚݅ʹैͬͯฦ͢ contents contents
  26. HTTP HeaderΛ༻͍ͨΩϟογϡ Server Client i ii i. ৚݅ͷϔομ෇͚ͯ
 ϦΫΤετ -

    If-Modified-Since - If-None-Match ii. ৚݅ʹैͬͯฦ͢ contents contents ແବʹίϯςϯπΛฦ͢ඞཁ͕ແ͘ͳΔ
  27. Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query

    ΫΤϦ (͋Δ͍͸ςʔϒϧ)Λ௚ͤʂʂʂ ద੾ʹνϡʔχϯά͞ΕͨΫΤϦʹ͸
 ΩϟογϡͳͲෆཁ
  28. Ͳ͏΍ͬͯ΋ΫΤϦ͕஗͍ Server Client Client . . . RDBMS Slow Query

    Slave Slave ΩϟογϡʹཔΒͣ SlaveΛฒ΂Δ΋Α͠
  29. ֎෦αʔϏεʹґଘ͍ͯ͠Δ Server Client Client . . . Outer Service Request

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