Introduction to Caching.

Introduction to Caching.

What is Caching?

Caching is the process of storing frequently accessed data temporarily in cache memory so that it can be retrieved faster.

Caching is used to improve the performance of applications and websites by reducing the amount of time needed to fetch data from slower storage systems.

Caching can also help in reducing network congestion by reducing the number of requests sent to servers or databases.

We usually cache those data that do not change frequently.

For Example -

When caching is not used, the Client sends a request to the server, which then retrieves the information from the database and sends it back to the client. However, retrieving data from the database is a slow process and can affect the overall performance of our application.

Adding a cache to the system can divide the traffic and lower the calls to our database. Now when data is requested by the client, the server first checks the cache for the information and if it is present there, it'll return the data quickly. If it is not found in the cache then it'll retrieve it from the database, stores it in the cache then respond to the client.

Importance of cache

  • Better Performance: Caching allows frequently accessed data to be stored in memory, reducing the time it takes to retrieve that data from slower storage mediums like disks. This can greatly improve the performance of applications and websites.

  • Reduced server load: By caching frequently accessed data, servers can handle more requests with fewer resources, reducing the load on the server and improving scalability.

  • Cost savings: Caching can reduce the amount of hardware required to handle a given amount of traffic, saving money on infrastructure costs.

Types of Caches

There are multiple types of caching, some of them are as follows-

  • In-memory caching.

  • Disk caching.

  • Distributed caching.

  • Browser caching.

Cache vs Storage

Cache and storage are quite different from each other -

  • Cache stores data that are being frequently accessed while storage stores all types of data.

  • Cache memory is quite expensive in comparison to storage that's why only a small amount of data is used to store in the cache.

  • Cache data are usually short-time lived and do not store data for a long time. Cache systems are designed to improve performance and not for durable data storage.

Where caching is used

  • Web browsers: Browsers cache web page resources such as images, stylesheets, and scripts to reduce loading times for subsequent visits.

  • Content delivery networks (CDNs): CDNs cache website content on servers located around the world to deliver it faster to users who are geographically farther away from the website's origin server. It is used to deliver static resources such as images, videos, CSS files, and other multimedia content.

  • Databases: Database caching can improve query performance by storing frequently accessed data in memory instead of fetching it from disk each time.

  • Application servers: Caching commonly-used application data like user sessions or configuration settings can improve response times and reduce the load on backend systems.

  • Operating systems: OS-level caching improves system performance by storing frequently accessed files or disk blocks in memory instead of reading them from disk each time they're needed.

Caching tools

  • Memcached - A high-performance, distributed memory object caching system that is used to speed up dynamic web applications.

  • Redis - An open-source, in-memory data structure store that can be used as a cache, database or message broker.

  • Nginx - An open-source web server software that can also act as a reverse proxy and cache for HTTP, HTTPS, SMTP, POP3 and IMAP protocols.

  • Squid - A widely-used open-source proxy server and web cache that speeds up web requests by storing copies of frequently accessed content.

Conclusion

We have seen how caching works and how it can improve the performance of our application. In short, It is a process of temporarily storing frequently accessed data in a faster memory system(Cache) to improve the performance of our application. It can reduce the load on servers and improve scalability, resulting in cost savings.

Do share with others as well if you got to learn something. Do follow me on Hashnode for more such blogs and comment down anything that you like to add, It'll help others. You can connect with me on Twitter as well.

Happy coding✌️

Did you find this article valuable?

Support Aryan Srivastava by becoming a sponsor. Any amount is appreciated!