Why Caching is essential in Web development?
What is Caching?
Caching is the process of temporarily storing copies of data in a faster storage location to improve access speed and system performance.
Let take an simple example of it suppose Caching is like keeping your favorite snacks in a kitchen drawer; instead of going to the store every time you want a snack, you can quickly grab one from the drawer.
Here are some benefits of it in our web service
Reduce latency
Cut off Bandwidth
Reduce the load on server
Caching can be found in various locations and systems, including:
Web Browsers
Content Delivery Networks (CDNs) {Blog}
Proxy Servers {Blog}
Application Data Folders
Database Caches
Terminology we uses over here are :
Client
Origin Server
Stale Content - It refers to expire content.
Fresh Content - It refers to new content.
Cache Validation - It refers to process check the validity of cache content and get it updated if it get expires.
Cache Invalidation - It refers that it remove all stale content (remove all expired content)
How does content get cached ?
HTTP Headers play an crucial role over here, client request for the data then server sent the file with some headers.
Let discuss about caching headers:
Expires
Pragma
Cache-control
Expires and Pragma used in the HTTP 1.0 but everything that can be dine by this two are done by cache-control also that is mostly preferred in HTTP 1.1.
Expire Header:-
→ Absolute expiry date
→ Can’t be more than a year
→ Clock have to be in sync
Pragma Header(only have value : no cache):-
→ To prevent caching
→ HTTP 1.0 depreciated header
→ cache-control should be preferred
Cache-Control Header:-
It is an Multi value header
Cache-Control Value | Explanation |
public | Anyone can cache this response, including browsers and shared caches. |
private | Only the user's browser can cache this response; shared caches cannot. |
no-cache | The response can be cached, but must be revalidated with the server before being used again. |
no-store | The response cannot be cached at all; it must be fetched from the server every time. |
max-age=<seconds> | The response can be cached and reused for the specified number of seconds before it expires. |
s-maxage=<seconds> | Similar to max-age , but specifically for shared caches like CDNs, overriding other age settings. |
max-stale[=<seconds>] | The client can accept a stale response if it's within the specified time limit. |
min-fresh=<seconds> | The client wants a fresh response that is still valid for at least the specified number of seconds. |
stale-while-revalidate=<seconds> | The cache can use stale responses while it checks for a fresh one in the background for the given time. |
stale-if-error[=<seconds>] | If fetching a fresh response fails, the client will accept a stale one for the specified duration. |
must-revalidate | Once a cached response becomes stale, it must be revalidated with the server before use. |
proxy-revalidate | Similar to must-revalidate , but only applies to shared caches and not private ones. |
immutable | The response body will not change over time, so it can be cached indefinitely without revalidation. |
no-transform | The response should not be altered or transformed by any intermediate caches. |
only-if-cached | The client will only use cached responses and will not make a network request if the data is not cached. |
Validators Headers:
It check the cache content weather it still be used or we have to update.
etag (entity tag)
if-none-match
last-modified
if-modified-since
I hope you enjoyed this blog and found valuable insights into the importance of caching for web services. By enhancing performance, reducing load times, and optimizing resource usage, caching plays a crucial role in delivering a seamless user experience. As you implement caching strategies, you'll not only improve your website's efficiency but also boost user satisfaction and engagement. Thank you for reading, and I look forward to sharing more helpful information with you in the future!
Subscribe to my newsletter
Read articles from Shivam joshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by