Improve API Response Time With Cache

Have you ever felt when you first access a website, then access it again and feel a big difference in the time it takes. Cache is one of the technologies that affect the process of opening the site’s pages so that it is faster and does not take a long time.

This technology really helps your activities to access everything on the internet network. In addition, the faster a web page is opened, it can increase user satisfaction.

What is cache ?

In general, a website will be viewed several times a day. And every time it is opened, the browser sends a request to the server page to get the data.

This happens because the website will retrieve the latest posts, from headers, footers, to widgets. However, under certain conditions, the results of the calculations that have been carried out will produce the same data and do not need to be re-calculated and only retrieve the data that has been stored. Therefore, the process is called caching.

In terms of terms, cache is the process of storing temporary data, where the browser, site, or application does not need to download data repeatedly.

How its work ?

Starting from the request received, then data processing is carried out on the database server, which is then sent to the visitor’s browser. Because the caching process is enabled, the server will store HTML files in temporary storage media such as RAM (Random Access Memory) so that they can be accessed quickly.

And when a user reopens or re-accesses your menu or application, the server doesn’t need to do any processing from scratch and just needs to fetch from temporary storage.

cache example flow

The big picture of the caching process can be seen as above, the service we create will first check whether the requested data is available in the cache system or not, if the data is available, there is no need to process data from the beginning.

Why we need cache ?

There are several reasons why we need cache, actually it is not mandatory to use cache but in some cases the use of cache is highly recommended to maintain the stability and performance of the applications we make, here are some cases that can be considered for using cache

Spike in resource usage during high traffic Inconsistent API response time or Website load time Slow database response

This happens because of high requests from clients, repeated requests for the same data waste resources on the server, therefore the use of cache is needed so as to reduce the above problems.

Simple Comparison

For comparison between using cache and not using cache let’s create a simple script to read files that are static and accessed repeatedly, in this example ill use Nodejs as the languange and node-cache for cache library

For the first script, let’s simulate it without using the cache and the code will be as follows

without cache

For the second script, let’s simulate it using cache and the code will look like this:

with cache

Then we run and we compare the results of the execution of the two codes above, the results are as follows

result

As we can see the total time required for the whole process without using the cache is about 3156 milliseconds Whereas when we use the cache it only takes about 6 milliseconds to complete the whole process

Conclusion

With the simple comparison above, of course, by using the performance cache, the resulting performance is much faster

I hope with the above explanation we can determine when and how we will use the cache system in our application

thanks for reading

  3 Views    Likes  

SOFT SKILL YANG HARUS KAMU MILIKI!!!

previous post

Jadi Mahasiswa Aktif : Tips Sukses di Perkuliahan
SOFT SKILL YANG HARUS KAMU MILIKI!!!

next post

SOFT SKILL YANG HARUS KAMU MILIKI!!!

related posts