❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Learning Notes #64 – E-Tags and Last-Modified Headers

20 January 2025 at 16:57

Today morning, i started with a video on E-Tags (came as first in youtube suggestion). In this blog i jot down my notes on E-Tags and how it helps in saving bandwidth. Also how Last-Modified header is better than E-Tags.

In the world of web development, ensuring efficient resource management and improved performance is crucial. Two key mechanisms that help in achieving this are E-Tags (Entity Tags) and the Last-Modified header.

These HTTP features facilitate caching and conditional requests, reducing bandwidth usage and improving user experience.

What is an E-Tag?

An Entity Tag (E-Tag) is an HTTP header used for web cache validation. It acts as a unique identifier for a specific version of a resource on the server. When a resource changes, its E-Tag also changes, enabling clients (e.g., browsers) to determine if their cached version of the resource is still valid.

How E-Tags Work

1. Response with E-Tag: When a client requests a resource, the server responds with the resource and an E-Tag in the HTTP header.


HTTP/1.1 200 OK
ETag: "abc123"
Content-Type: application/json
Content-Length: 200

2. Subsequent Requests: On subsequent requests, the client includes the E-Tag in the If-None-Match header.


GET /resource HTTP/1.1
If-None-Match: "abc123"

3. Server Response

If the resource hasn’t changed, the server responds with a 304 Not Modified status, saving bandwidth,


HTTP/1.1 304 Not Modified

If the resource has changed, the server responds with a 200 OK status and a new E-Tag,


HTTP/1.1 200 OK
ETag: "xyz789"

Benefits of E-Tags

  • Precise cache validation based on resource version.
  • Reduced bandwidth usage as unchanged resources are not re-downloaded.
  • Improved user experience with faster loading times for unchanged resources.

What is the Last-Modified Header?

The Last-Modified header indicates the last time a resource was modified on the server. It’s a simpler mechanism compared to E-Tags but serves a similar purpose in caching and validation.

How Last-Modified Works

1. Response with Last-Modified: When a client requests a resource, the server includes the Last-Modified header in its response,


HTTP/1.1 200 OK
Last-Modified: Wed, 17 Jan 2025 10:00:00 GMT
Content-Type: image/png
Content-Length: 1024

    2. Subsequent Requests: On future requests, the client includes the If-Modified-Since header.

    
    GET /image.png HTTP/1.1
    If-Modified-Since: Wed, 17 Jan 2025 10:00:00 GMT
    

    3. Server Response

    If the resource hasn’t changed, the server responds with a 304 Not Modified status,

    
    HTTP/1.1 304 Not Modified
    

    If the resource has changed, the server sends the updated resource with a new Last-Modified value,

    
    HTTP/1.1 200 OK
    Last-Modified: Thu, 18 Jan 2025 12:00:00 GMT
    

    E-Tags and Last-Modified headers are powerful tools for improving web application performance. By enabling conditional requests and efficient caching, they reduce server load and bandwidth usage while enhancing the user experience. Remember, these 2 are pretty old mechanisms, which are been used tilldate.

    ❌
    ❌