The Need for Speed: Mastering Caching Techniques for Faster Applications. Part-1

The Need for Speed: Mastering Caching Techniques for Faster Applications. Part-1

What is Caching?

Caching is a strategy where commonly accessed data is stored in a readily accessible location for faster access.

Why is it important?

  • In the fast-paced world of computing, slow applications can frustrate users and impact user experience, caching can significantly improve the application performance hence improving user experience.

  • The database stands as a cornerstone in any application, making it crucial to decrease stress on the database server when designing a scalable system. Caching emerges as the solution to this challenge, providing a means to optimize performance and enhance scalability.

Types of caching?

  • Distributed Caching: Caching data across multiple nodes in a distributed network to ensure high availability and fault tolerance.

  • Edge Caching: Caches content at the edge of a network, typically on content delivery network (CDN) or edge servers, to reduce latency for end-users.

  • Global Caching: Will have a single cache space and all the nodes will use this single space.

Frequently used terms

  • Cache miss: A cache miss occurs when the requested data is not found in the cache memory.

  • Cache hit: Conversely, cache hit is when the requested data is found in the cache memory.

Caching strategies

  • Cache aside

    In the cache aside, the cache is positioned adjacent to the primary database. The application initially looks for the required data in the cache. If the cache does not contain the data (a cache miss), the information is retrieved from the database and subsequently stored in the cache. Conversely, in the event of a cache hit, indicating that the data is present in the cache, the application retrieves and returns the data directly from the cache. This arrangement ensures that frequently accessed data is swiftly available through the cache, minimizing the need for repeated database queries and optimizing overall system performance.

  • Read through

    In a read-through caching strategy, the application does not directly interact with the database to retrieve data. Instead, when the application requests data, the cache itself is responsible for fetching the data from the primary storage (such as a database) if it's not already present in the cache. This process is transparent to the application, which remains unaware of whether the data is in the cache or needs to be fetched from the primary storage.

  • Write through

    In a write-through caching strategy, the application initiates write operations by storing data in the cache. Subsequently, the cache assumes the responsibility of promptly writing the data to the underlying database. This approach ensures that every write action is immediately and consistently reflected in both the cache and the database, emphasizing real-time synchronization.

  • Write back

    In a write-back caching strategy, the process shares similarities with write-through; however, a notable distinction lies in the data-writing mechanism. Rather than immediately updating the database with each write operation, the cache accumulates multiple write operations and executes them collectively in a batch. This approach introduces a level of batching efficiency, as the cache postpones the database updates until a certain threshold or optimal condition is met.

  • Write around

    A write-around caching strategy is integrated with either cache-aside or read-through mechanisms. This arrangement follows a distinctive pattern where data is consistently written directly to the database. When the application reads data, it is retrieved from the cache. In the event of a cache miss, indicating the absence of the required data in the cache, the application fetches the data directly from the database. Subsequently, the retrieved data is updated in the cache to expedite future access.

Did you find this article valuable?

Support Mahesh Choudhury by becoming a sponsor. Any amount is appreciated!