DEV Community

Sanket Saxena
Sanket Saxena

Posted on • Updated on

Concept of Cache HIT and Cache MISS: In-Memory Cache

“Caching is the concept of saving commonly/ frequently used data in memory and using them on behalf of the actual data source when the same types of operations or data are requested.”

Two key terms in the world of caching are "Cache Hit" and "Cache Miss." Here we will see a super simple analogy to understand the concept of Cache HIT and Cache MISS in terms of In-Memory Cache.

You can assume you are using AWS ElastiCache or AWS DynamoDB DAX for better understanding.

Analogy

Think about a classroom having some Students and a Teacher.

CACHE MISS
Step 1: One of the students asks a question about which the Teacher was not confident or didn't know the answer.
Step 2: The teacher checks it on the Internet to get the answer.
Step 3: The teacher responds to the student who asks this question.

Image description

CACHE HIT
Step 4: The next day another student asks the SAME question to the teacher and this time the teacher knows the answer because the Answer was saved in his Memory.
Step 5: The teacher responds to the student who asks this question without going to check on the Internet.

Image description

In the above example:
Students are the USERS | The teacher is the CACHE | Internet is the SOURCE

Now Technically

CACHE MISS
Whenever your application requests data and If the data doesn't exist in the cache or has expired, your application requests the data from your data store. Your data store then returns the data to your application. Your application next writes the data received from the store to the cache. This is known as Cache Miss.

Image description

CACHE HIT
Whenever your application requests data, it first requests the In-Memory cache. If the data exists in the cache and is current, Cache returns the data to your application. This is known as a Cache Hit.

Image description

IMPORTANT: There is a concept of ADD TTL to the cached data means when the data is written to the cache it has a TTL value example: 12hrs which means after 12hrs the cached data will be removed from the cache and the request for that data will again direct to the SOURCE.

Thanks!

Top comments (0)