API with NestJS #58. Using ETag to implement cache and save bandwidth

JavaScript NestJS

We’ve introduced various ways of caching files on the server throughout this series. This article teaches how to help the browser perform caching on the client side. We use the entity tag (ETag) response header to do that.

Imagine having a endpoint that responds with an image. The browser calls it every time we visit a website and gets the image. We could optimize this process by telling the browser if the image changed since the last time the browser fetched it. To do that, we can put the hash of the image in the ETag response header. The hash of a particular image changes only if the image changes.

Image hashing is a process of using an algorithm to get a unique string to an image. If you want to know more about hashes, check out API with NestJS #3. Authenticating users with bcrypt, Passport, JWT, and cookies where we create hashes from passwords.

  • the first time the browser requests , the server creates a hash and sends it in the ETag response header,
  • the second time the browser requests , it sends the ETag value in the request header,
  • the server calculates the hash for the second time,
    • if the hash changes, the server sends the new image,
    • if the hash doesn’t change, the server responds with 304 Not Modified instead.

Creating an ETag for an image

In API with NestJS #55. Uploading files to the server, we’ve implemented a feature of uploading avatars. Let’s create an endpoint that returns an avatar of a particular user and assigns the ETag.

To generate ETags, we can use the etag library that Express uses under the hood. Let’s install it.

users.controller.ts

If you want to know more about the class, check out API with NestJS #54. Storing files inside a PostgreSQL database

Let’s look under the hood of the etag library to see what it is doing:

The interesting part above is that it uses the crypto module built into Node.js. SHA-1 is a hashing algorithm that should not be used in web security, such as SSL certificates. Even though that’s the case, it is good performance-wise, and we can use it for generating ETags.

Also, we can notice, that a valid ETag is always enclosed in double quotes.

Comparing hashes

When the browser requests an image for the second time, it sends the ETag value along with the request in the header.

For the caching to occur, we need to take the above value into account.

users.controller.ts

If the browser requests an avatar before it changes, we respond with 304 Not Modified. Thanks to that, the browser knows that it can safely use the data from the cache.

We can see that the total data transferred over the network is now a lot less on the above screenshot. This is because the browser didn’t fetch the image when it requested it for the second time. This can be relevant to clients using a mobile Internet connection, for example.

Weak ETags

Throughout this article, we’ve created strong ETags. When two strong ETags are a match, it means that the content is byte-for-byte identical.

We can also create weak ETags distinguished by the prefix in the value:

When two weak ETags are a match, it indicates that the content is semantically equivalent. It might not always change when the content changes.

ETag does not need to be a hash

Even if we use a fast hashing algorithm such as SHA1, it takes time to generate a hash. Therefore, sometimes it might be easier to use something else instead of it.

In our application, every file has an id. We can use it to create an ETag. Since we no longer calculate a hash of a file, we can mark the ETag as weak.

users.controller.ts

The browser will always use the image from the cache with the above approach as long as it has the same id.

Even though we’ve marked the ETag as weak, it is up to us how do we handle it.

Automatically generated ETags in Express

Express creates ETags in some situations out of the box for us. Unfortunately, that does not happen when we use the class from NestJS. We omit the method when we use readable streams and pipe them right into the response stream. Thanks to writing the logic of creating ETags, we now know that it usually requires us to create a content hash. This wouldn’t be possible when dealing with streams because Express starts sending the file before having all its content.

If you want to know more about streams, check out Node.js TypeScript #4. Paused and flowing modes of a readable stream

When we look under the hood of Express, we can see that it generates the ETag for us. Express can also take care of comparing the If-None-Match header for us.

users.controller.ts

When we do the above, we don’t omit the method and allow Express to define the ETag automatically. By default, it generates weak ETags, but we can change that.

main.ts

To be able to call the method, we need to use the interface.

We can also define our own function to generate the etag.

Summary

In this article, we’ve learned what ETag is and how to use it to our advantage. This included both writing the logic by ourselves and getting to know how we can rely on Express to do that for us. We’ve also learned that there are both strong and weak ETags and how to tell Express which one we want. Knowing how ETag works can help us improve the performance and decrease the data transmitted over the network.

Series Navigation<< API with NestJS #57. Composing classes with the mixin patternAPI with NestJS #59. Introduction to a monorepo with Lerna and Yarn workspaces >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments