ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

HTTP ์™„๋ฒฝ๊ฐ€์ด๋“œ 7์žฅ Cache๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

์šฐ์„  Web Caching ์ด๋ž€?

 

HTTP ํ†ต์‹  ์‹œ response์˜ body ์— ํ•ด๋‹นํ•˜๋Š” content document๋ฅผ

๋กœ์ปฌ์— ์ €์žฅํ•˜์—ฌ ํ•ด๋‹น document์˜ ์š”์ฒญ์ด ์žˆ์„๋•Œ ์„œ๋ฒ„์—์„œ response๋กœ ๋ฐ›๋Š” ๋Œ€์‹  ๋กœ์ปฌ์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 

์šฐ์„  web cache์— ๋Œ€ํ•ด ์ตํž ๊ด€์  3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

  1. ์บ์‹ฑ์ด ์–ด๋–ป๊ฒŒ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ ์‹œํ‚ค๋Š”์ง€
  2. ์–ด๋–ป๊ฒŒ ์ตœ์‹  ์ƒํƒœ์˜ ์บ์‹œ๋ฅผ ์œ ์ง€ํ•˜๋Š”์ง€
  3. ์บ์‹ฑ๊ณผ ์„œ๋ฒ„๊ฐ„์˜ ์šด์šฉ ๋ฐฉ๋ฒ•

์ด ๊ด€์ ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ๊ธ€์„ ์ฝ์–ด์ฃผ์‹œ๋ฉด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์บ์‹ฑ ํ–ˆ์„๋•Œ์˜ ์žฅ์  3๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.

  1. ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋˜ ๋ณด๋‚ด์ง€ ์•Š์•„๋„ ๋จ
  2. ๋„คํŠธ์›Œํฌ ๋ณ‘๋ชฉํ˜„์ƒ ๊ฐ์†Œ
  3. ๋น ๋ฅธ ๋กœ๋“œ

 

์ด ์„ธ๊ฐ€์ง€๊ฐ€ ๋‹ค ๊ฐ™์€ ๋งฅ๋ฝ์ด๊ธฐ๋„ ํ•œ๋ฐ.
์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋ฉด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ํšจ๊ณผ๋“ค์ž…๋‹ˆ๋‹ค.


์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ์žฅ์ ์œผ๋กœ ์„œ๋ฒ„ ์ธก์—์„œ ๋“œ๋Š” ํ†ต์‹  ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์„ธ๋ฒˆ์งธ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์ธก๋ฉด์ธ๋ฐ,

์‘๋‹ต ๋ฐ”๋””์˜ ์ฝ˜ํ…์ธ ์—์„œ ๊ฐ’์„ ๋ฐ›๋Š” ๋Œ€์‹  ๋กœ์ปฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๋น ๋ฅธ ํŽ˜์ด์ง€ ๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

์บ์‹ฑ์˜ ์˜ˆ์‹œ๋กœ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜๋‚˜๋Š” ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋™์‹œ์— ๋ชฐ๋ ธ์„๋•Œ
๊ฐ™์€ ์‘๋‹ต์„ ๋ฐ˜๋ณตํ•ด์„œ ์ฃผ๊ฒŒ ๋˜๋Š”๋ฐ ๊ทธ๊ฑธ ์บ์‹œ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋งŽ์€ ์‚ฌ๋žŒ์ด ๋ชฐ๋ ค๋„ ์บ์‹œ๋กœ ์„œ๋ฒ„ ๋ถ€ํ•˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‘๋ฒˆ์งธ๋Š” ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์˜ ๋ฌผ๋ฆฌ์  ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ€๋•Œ ๊ทธ ์‚ฌ์ด์— ์บ์‹œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์น˜ (eg. CDN)๋ฅผ ๋ฐฐ์น˜ํ•˜์—ฌ ์บ์‹œ๋ฅผ ๊ฑฐ๊ธฐ ์ €์žฅํ•˜๋ฉด ๋ฌผ๋ฆฌ์  ๊ฑฐ๋ฆฌ๊ฐ€ ์ค„์–ด๋“ค์–ด ํ†ต์‹  ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์บ์‹œํ–ˆ์„๋•Œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ ๊ณผ ํ•ด๊ฒฐ๋ฐฉ์•ˆ์ž…๋‹ˆ๋‹ค.

  1. ์บ์‹ฑํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ตœ์‹  ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ → Revalidations๋กœ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
    1. ๋ณ€๊ฒฝ์‚ฌํ•ญ ์—†์œผ๋ฉด 304 Not Modified response
    2. ๋ณ€๊ฒฝ์‚ฌํ•ญ ์žˆ์œผ๋ฉด 200 + full content
    3. ์‚ญ์ œ๋์œผ๋ฉด 404
  2. ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์บ์‹ฑ ํ•  ์ˆ˜์—†๊ณ 
  3. ๋‚ฎ์€ ์บ์‹œ hit rate์ด ์žˆ์Šต๋‹ˆ๋‹ค → 2, 3๋ฒˆ ๋ชจ๋‘ ์—ฐ๊ด€์„ฑ, ์ธ๊ธฐ ๋†’์€ ๋ฐ์ดํ„ฐ๋“ค์„ ์บ์‹œ์— ์ €์žฅํ•จ์œผ๋กœ์จ ํ•ด๊ฒฐ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  4. ์บ์‹œ hit์˜ response๊ณผ ์„œ๋ฒ„์˜ response ๋ชจ๋‘ ์‘๋‹ต์ฝ”๋“œ 200 → ์‘๋‹ต ํ—ค๋”์˜ date๊ฐ’ or Age๋กœ ๊ตฌ๋ถ„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

* ์บ์‹œ hit๋ž€ ์บ์‹œ์˜ ์ƒํƒœ๊ฐ€ freshํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•จ.

 

์บ์‹ฑ์„ ํ•˜๋Š” 6๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.

  1. client์—์„œ ๋ณด๋‚ด๋Š” HTTP request message๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.
  2. message๋ฅผ parsingํ•ฉ๋‹ˆ๋‹ค.
  3. ์บ์‹œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  4. ์บ์‹œ์˜ fresh๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค (์œ ํšจํ•  ๊ฒฝ์šฐ) → ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฐฉ๋ฒ• 4-2
  5. response๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒˆ๋กœ์šด ํ—ค๋”๊ฐ’์„ ์ถ”๊ฐ€ํ•˜์—ฌ reponse๋กœ ๋ณด๋‚ด์ค๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” cache ๊ด€๋ จํ•˜์—ฌ ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ๋Š” response header์˜ ์†์„ฑ๊ฐ’์ž…๋‹ˆ๋‹ค.
    1. Age → ์บ์‹œ ๋ฐœ๊ธ‰ ์ดํ›„ ์ดˆ๊ณผ ์‹œ๊ฐ„ (์ดˆ๋‹จ์œ„)
    2. Expires headers → ๋งŒ๋ฃŒ ์‹œ๊ฐ„
    3. Cache-Control ์บ์‹œ ์ œ์–ด ๊ธฐ๋Šฅ
      1. private public
        1. private: ๊ฐœ์ธ ์ „์šฉ ์บ์‹œ
        2. public: ๊ณต์œ  ์ „์šฉ ์บ์‹œ (proxy cache)
      2. Max-Age ์‘๋‹ต ํ—ค๋”
        1. Cache-Control: max-age=3600 → ์บ์‹œ๋ฐœ๊ธ‰ ์ดํ›„ 3600์ดˆ๊ฐ€ ์ง€๋‚˜์„œ ์บ์‹œ๋ฅผ ์š”์ฒญํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ์žฌ๊ฒ€์ฆํ•ด์•ผํ•จ.
        2. Cache-Control: s-maxage=3600 (only public caches) → ํ”„๋ก์‹œ ์บ์‹œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์šฉ๋„
      3. no-store
        1. Cache-Control: no-store
        2. ๋กœ์ปฌ์— ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป
      4. no-cache
        1. Cache-Control: no-cache
        2. ์บ์‹œ๋Š” ์ €์žฅ. but ๋งค๋ฒˆ ์žฌ๊ฒ€์ฆ, max-age=0 ๊ณผ ๊ฐ™์€ ์˜๋ฏธ → ๊ณ„์† ์‘๋‹ต ์š”์ฒญ
      5. must-revalidate
        1. Cache-Control: must-revalidate
        2. fresh check๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์žฌ๊ฒ€์ฆ ์‹œ๋„
      6. stale-while-revalidate=<seconds>
        1. Age๊ฐ€ ํ•ด๋‹น ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ผ๋‹จ ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„์šฐ๊ณ , ๋น„๋™๊ธฐ์ ์œผ๋กœ ์บ์‹œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

4-2. ์„œ๋ฒ„ ์žฌ๊ฒ€์ฆ (Server Revalidation) : ์œ ํšจ ๊ธฐ๊ฐ„ ๋งŒ๋ฃŒ ์—ฌ๋ถ€ ํ™•์ธ 

    1. ์กฐ๊ฑด๋ถ€ ์š”์ฒญ conditinal GET : ์บ์‹œ๊ฐ€ hit ํ–ˆ์„ ๊ฒฝ์šฐ ์‘๋‹ต์œผ๋กœ ํ—ค๋”๋งŒ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋น„์šฉ์ด ๋‚ฎ์Œ.
    2. If-Modified-Since<date> (IMS) : Last-Modified(๊ธฐ์กด ์‘๋‹ต ํ—ค๋”)๊ฐ’ ์ดํ›„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
    3. If-None-Match<tags> : documnet serial number ๊ฐ’ ๋น„๊ต

 

 

์บ์‹ฑ ์ •์ฑ…

https://web.dev/http-cache/#defining-optimal-cache-control-policy

 

์ถ”๊ฐ€์ ์ธ Cache-Control request directives (์บ์‹œ ์ปจํŠธ๋กค ์š”์ฒญ ์ง€์‹œ์ž)

https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Cache-Control

 

Referense

https://toss.tech/article/smart-web-service-cache

https://www.rfc-editor.org/rfc/rfc5861

 

๋Œ“๊ธ€