Understand Caching and LRU Cache

Learn about the need for caches and one particular cache type: the least recently used cache.

Understand the need for cache

Let's start with an example. You've likely heard about Domain Name System (DNS) servers. A DNS server is a directory consisting of domain names and their corresponding IP addresses so the user can reach a server. For example, when go to the URL https://www.educative.io in a browser, the following operations are performed:

  1. The request hits a DNS server, which can be maintained by your Internet Service Provider (ISP).

  2. In the DNS server, the URL is mapped to its IP address, because all the servers and websites are hosted on a physical computer.

  3. This IP address is used to hit the server hosting the website. The server sends a response and the web page is displayed in our browsers.

The graphic below illustrates this process:

This is a very high-level view of DNS, but there are a lot of things going on during this process. Now, let us assume that the DNS server is not located near our location. So, at great distances, it may take around 100ms to get the IP address for a URL. If we are hitting the URL multiple times in a span of a few hours, then there is an unnecessary wait time of 100ms for every request.

We need some sort of mechanism, so that the first request to the URL may take 100ms, but subsequent requests to the same URL don't take as long to get the IP address.

So how can we do this?

Obviously, we can store the mapping of URLs and their corresponding IP addresses in our browser's storage and the browser can refer to it for subsequent hits to the same URLs. The ...

Access this course and 1400+ top-rated courses and projects.