Introduction to Cache Part 1

Cache is a high speed data storing layer that stores a very small portion of data, to serve future requests faster compared to if data was accessed from primary storage systems.

Caches are key-value stores(average complexity O(1)) that can be set up at many levels to increase throughput and reduce latency.

Caching is the process to store most recently used item in the storage layer.

In this blog we will cover basics of caching, the motivation, some facts about caching, levels of caching, advantages, disadvantages and use cases.

Need for caching:

Cache improves page load time and reduces lot of load on servers and/or databases. The main purpose of caching is be able to do frequent tasks more quickly to serve customers efficiently.

Let’s use a simple example to understand importance of caching —

Fetching website without cache

Without using caches: Suppose, user requests a website from the browser. The browser fetches the html page from the server and renders it to the user. Let’s say after awhile, the user again requests the same website, browser will again fetch html page from the server and render it.

Under this model, the browser has to do complete round trip to fetch html page every time — even very popular ones that are frequently used. Internet connection is the slowest link in the entire process and we want to setup something, so that these round trips can be minimized.

Fetching website with cache

With caches: So, we have a cache in hard disk which stores html pages that are received from the server.

When user requests a website from browser for the first time, the browser fetches the html page from server, stores a copy in cache and displays it to browser. When the user requests the same website next time, the browser checks in the cache. If the html page exists in cache, the browser renders the page from cache. Otherwise, the browser fetches page from server, stores a copy in cache and renders it. In this case, the browser is faster with a cache and the client is served more efficiently.

Suppose, the client requests for a website that isn’t available in cache. In this case, the browser is less efficient with a cache than without one, because the browser takes the time to look into the cache first.

One of the challenges of cache design is to minimize the impact of cache search. The latency time of searching in the cache should be so small compared to the time taken to get html page that it can be ignored.

Facts about caching:

Types of caches:

Within application, caching is done at two main levels:

(a) Database level: Hash the query as key and storing the database query result as value in cache. The drawbacks include deleting entire cached query if one field might have changed and complex queries are hard to delete.

(b) Object level: Storing the data as an object, rather than storing query result. Easy to add object-level caching if your application is already structured with DAO layer. The object can be removed when underlying data is changed and object level allows for asynchronous processing.

Advantages:

Disadvantages:

Use case:

and many more ….

Popular in-memory caches are Redis and Memcached.

To understand structure and algorithm of cache, go through the problem on Leetcode — LRU Cache.

In the upcoming blog, i will be covering cache update in detail

Follow me if you want to get notified to my upcoming blogs on system design.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SAKSHI CHHABRA

Master's student in Computer Science from University of Florida. I love to write and help others, so here am i.