Mise en cache

Web

Définition

La pratique consistant à stocker des copies de données fréquemment consultées plus près du demandeur pour réduire la latence et la charge du serveur. La mise en cache web se produit à plusieurs niveaux : navigateur, edge CDN, proxy inverse et application.

Why Caching Exists

Caching stores a copy of frequently requested data closer to where it is needed so future requests can be served without repeating expensive operations — database queries, API calls, or computationally intensive rendering. A well-designed cache dramatically reduces LatencyThe time delay for a data packet to travel from source to destination, typically measured in milliseconds (ms). Lower latency is critical for real-time applications like video calls, gaming, and financial trading. and origin server load. Web caching operates at multiple layers: browser caches, CDNContent Delivery Network. A geographically distributed network of servers that caches and serves content from locations close to end users, reducing latency and improving load times. Major providers include Cloudflare, AWS CloudFront, and Akamai. edge nodes, reverse proxies like Reverse ProxyA server that sits in front of backend servers, forwarding client requests and returning responses on their behalf. Used for SSL termination, load balancing, caching, and hiding the origin server's identity., and application-level in-memory stores like Redis.

Cache Control Headers

HTTP caching behavior is governed by response headers. Cache-Control: max-age=3600 tells both browsers and intermediate caches they may serve the response for 3,600 seconds without revalidating. Cache-Control: no-store prevents any caching — critical for dynamic, personalized, or security-sensitive responses like banking pages. ETag and Last-Modified headers enable conditional requests so a cache can revalidate with the origin using If-None-Match or If-Modified-Since without re-downloading the full response body.

Cache Invalidation Challenges

Knowing when to evict or update cached data is the hard part. Time-based expiry (TTL via TTL (Time to Live)A field in an IP packet header that limits the packet's lifespan by specifying the maximum number of hops it can traverse. Each router decrements the TTL by one; when it reaches zero, the packet is discarded to prevent routing loops. analogy) is simple but may serve stale content. Event-driven invalidation (purging the cache when content changes) is more accurate but adds complexity. CDNContent Delivery Network. A geographically distributed network of servers that caches and serves content from locations close to end users, reducing latency and improving load times. Major providers include Cloudflare, AWS CloudFront, and Akamai. providers offer purge APIs for this purpose. Cache stampede — many simultaneous cache misses hitting the origin at once after expiry — can be mitigated with probabilistic early expiration or request coalescing. Use HTTP Header Analyzer to inspect the caching headers a server actually returns.

Termes associés

Plus dans Web