Building cache-based architecture

Caching is the process of temporarily storing data or files in an intermediary location between the requester and the permanent storage, for the purpose of making future requests faster and reducing network throughput. Caching increases the application speed and lowers the cost. It allows you to reuse previously retrieved data. To increase application performance, caching can be applied at various layers of the architecture such as the web layer, application layer, data layer, and network layer.

Normally, the server's random access memory (RAM) and in-memory cache engines are utilized to support application caching. However, if caching is coupled to a local server, then the cache will not be persisting data, in case of a server crash. Now, most of the applications are in a distributed environment, so it's better to have a dedicated caching layer that should be independent of the application life cycle. When you applied horizontal scaling to your application, all servers should be able to access the centralized caching layer to achieve the best performance.

The following diagram depicts the mechanism of caching in various layers of solution architecture:

Caching at the architecture layers

As shown in the preceding diagram, the following are the caching mechanisms at each layer of architecture:

  • Client side: Client-side caching is applied to user devices such as mobile and desktop. This caches the previously visited web content to respond faster to a subsequent request. Each browser has its own caching mechanism. HTTP caching makes the application faster by caching content at the local browser. The cache-control HTTP header defines browser caching policies for both client request and server response. These policies define where the content should be cached and how long it will persist, which is known as Time to Live (TTL). Cookies are another method used to store information at the client machine in order to respond to the browser faster.
  • DNS cache: When a user types the website address over the internet, the public Domain Name System (DNS) server looks up the IP address. Caching this DNS resolution information will reduce the website's load time. DNS can be cached to a local server or browser after the first request and any further request to that website will be faster.
  • Web caching: Much of the request involves retrieving web content such as images, video, and HTML pages. Caching these assets near to the user location can provide a much faster response for a page load. This also eliminates disk read and server load time. A content distribution network (CDN) provides a network of edge locations where static content such as high-resolution images and videos can be cached. It's very useful for reading heavy applications such as games, blogs, e-commerce product catalog pages, and more. The user session contains lots of information regarding user preference and their state. It provides a great user experience to store the user's session in its own key-value store and applies to cache for quick user response.
  • Application caching: At the application layer, caching can be applied to store the result of a complex repeated request to avoid business logic calculations and database hits. Overall, it improves application performance and reduces the load on the database and infrastructure.
  • Database caching: Application performance highly depends upon speed and throughput provided by the database. Database caching allows you to increase database throughput significantly and lower data retrieval latency. A database cache can be applied in front of any kind of relational or non-relational database. Some database providers integrate caching, while applications handle local caching.

Redis and Memcached are the most popular caching engines. While Memcached is faster (it is good for low-structure data and stores data in a key-value format), Redis is a more persistent caching engine and is capable of handling complex data structures required for an application such as a gaming leader board. Let's learn about a few more caching design patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.117.109