Blogs


The power of caching

 August 28, 2021, 04:09 PM

 6 min read

caching
web
servers
http
headers
performance
loading
time
cdn

We all love fast things. Cars, planes, bikers, runners, etc, we love them all, but specially when it comes to technology. Oh yes. One of the crucial agendas marketing teams from almost every technology related company tries to promote is how fast their so called product is. A very intuitive way through which this can be achieved is by a process called caching.


What is caching?

In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. (wikipedia). Essentially, it is storing data in such a way, or place so that if there is any need for that data in the future, it can be retrieved faster, reducing latency through optimization.

But how does it work? Let us say, for example, that there is a team of explorers on their way to climb a mountain. Before their endeavor, they will set up a camp at the base of the mountain where they will stock up their food and supplies for some weeks. Once they embark on their journey, along the way they will set up several bases where they will keep some of their food and supplies. They do this so that whenever they feel the need to retrieve some supplies while they are climbing, they only need to do so to the nearest base that they have set up instead of going all the way down the mountain. In essence, they are caching their food and supplies, making their climb easier.

Likewise, caching in the technological world works in a similar way. Hashmap/dictionary caching, browser caching, DNS (domain name system) caching, CDN (content delivery network) caching are all examples of optimization through caching.

Hashmap/dictionary caching

For computational heavy algorithms like calculating the factorial of a given number, caching can be used. A dictionary, hashmap, object, or anything similar can be used to store the value of already calculated factorials. The factorial of 10 is simply 10 multiplied by the factorial of 9 and so on and so forth. As this is a recursive function, we can store already calculated factorials instead of recalculating them every time we run the algorithm for a given number to speed up the process. Hashmap or dictionary caching can be used for any type of algorithm which requires a lot of calculations, specially if it builds up exponentially.

DNS (domain name system) caching

The Domain Name System is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network. It associates various information with domain names assigned to each of the participating entities. (wikipedia). For example, the address of this website is not arafatzaman.pythonanywhere.com. It is probably a string of numbers or an IP address. Since it is difficult for humans to work with number addresses, we register domains, which is a string of characters, to map them to the designated IP address for ease of use. So where does caching come in? Well, when you search up a particular website in your browser, if you have never visited that website, your browser will search up its DNS profile in a DNS server to retrieve its IP address and then ping that address for an HTTP request. It will then store the address in the DNS server so that next time you or anyone else connected to that DNS server will not have to look it up again.

Browser caching

You ever visit a website for the first time and it takes a good while to load? And then any subsequent visits to that same site take mere seconds to load up? Why is that? As you have guessed by now, it is caching working its charm. Almost every website will be made of plenty of static-ish code which takes a long time to load. To counter this problem, your browser stores some of the static files in your local hard drive so that when you visit it next time, it is loaded way faster. Modern browsers automatically do the caching, however it can be customized to fit the developer's needs through the use of service workers, which is a topic for another time. Haha.

CDN (content delivery network)

A content delivery network, or content distribution network, is a geographically distributed network of proxy servers and their data centers. The goal is to provide high availability and performance by distributing the service spatially relative to end users. (wikipedia). Some static files, like jQuery,js, bootstrap.js, bootstrap.css etc are files used across the globe. Now what CDN does is it has proxy servers geographically spread throughout the world caching these files. When a user requests for the file, the central server calculates the nearest server to the user and passes on the request to that nearest server. Then the user downloads the static file from the proxy server, which the browser might cache to speed it up even further.


Even though this is something not really talked about much by the media, personally I think it is truly a marvelous innovation we humans have accomplished and I believe sometimes we must step back and look and how things are done.


Arafat

Mohammad Arafat Zaman

"Technophile"


Go back

Mohammad Arafat Zaman © 2024


All rights reserved