Caching

One way to reduce server-client communications is to cache the non-changing but mutable data into a server. There are various architectures to do it.

However, the very first thing to do is defining the caching invalidation policy.

How Caching Works

Caching functions as a temporary storage unit that holds the non-changing data after the intensive data processing. This way, if the client issues the same request again and there are no data alteration from the server side, the system can just reply with the cached version. Of course, if there are data alteration, the cached version gets invalidated and new processing is done and new output are cached again.

This provides a massive speed and performance boost after repeating non-altering data request.

The trick is determine:

  1. when to update the cache (cache invalidation policy)
  2. when to invalidate them (cache invalidation policy)
  3. what to cache

Defining Cache Invalidation Policy

Now remember that we need to invalidate the cache when the actual data are altered. These are triggers and it is upto your design to perform the invalidation

Compulsory

  1. Set a time based deadline. All caches must have deadline. Otherwise, it is wasting resources.
  2. Handle no cache situation. Cache servers are commonly known for crashing or have unavailability. There are situation where the back-end must have the ability to handle a non-functioning cache server.
  3. Resources limit policy. When the cache gets too large / shortage of storage, you need a policy like Belady / delete oldest / delete newest / flush all / FIFO / Random / ... etc.

Optional

  1. Push mechanism. Upon new data write / delete, the back-end clears the cache immediately.
  2. Pull mechanism. On client side, issue a "no-cache" or "refresh cache" instruction to back-end and refresh the data.

Simple Client Side Caching Architecture

The easiest caching architecture is client side caching. In this mode, client keeps a local cache to perform a quick pool instead of requesting everything from the server side. The architecture is as follows:

Advantages

There some advantages using this caching architecture design:

  1. Simple server side implementations by organizing non-changing but mutable data.
  2. Client saves network transmission data automatically.
  3. Client has full control over the caching.
  4. No caching server is needed.


Disadvantages

The disadvantages fusing the caching architecture design:

  1. Server has no control over the caches invalidation aside setting a timeout. Hence, the user experience cannot be guaranteed.
  2. All assets must use timestamp checksum in the naming in order to workaround the no-control caches.
  3. Caching security is completely relying of client's device.

Simple Server Side Caching Architecture

This is the back-end caching architecture design. Usually, the caching server is behind the back-end server and in front of the database server.

Advantages

There some advantages using this caching architecture design:

  1. Faster server side processing.
  2. Client always receive the latest and accurate data.
  3. Caching server may not needed depending on performance demands.
  4. Caching security is on the server internal side.


Disadvantages

The disadvantages fusing the caching architecture design:

  1. Client has no control over cache.
  2. Need a management over the caching policy.
  3. For high performance demand, additional caching server is needed.

Content Distribution Network Caching Architecture

This is the back-end design where the caching server is in-front of back-end server. If distributed correctly across geographical locations, it forms a network known as Content Distribution Network (CDN). The architecture design is as follows:

Advantages

There some advantages using this caching architecture design:

  1. Very fast client side serving
  2. Caching is on Geo-location dedicated server.
  3. Less communications with back-end server since the CDN is primarily serving the client.


Disadvantages

The disadvantages fusing the caching architecture design:

  1. Vulnerable to caching poisoning if not implemented correctly.
  2. If 3rd party CDN service is used, you're essentially licensing the 3rd party service provider to be man-in-the-middle (MitM).
  3. Caching management can be complicated if not implemented correctly.

Combining All At Once

With the design above, today's internet is operating with this ideal architecture. Normally, it is not ideal to implement this architecture one shot during development stage. In fact, the development should be one at a time to ensure your caching management is firm and stable without compromising user experience.

Advantages

There some advantages using this caching architecture design:

  1. Maximum serving speed for internet.
  2. Both pull and push caching mechanism are feasible to implement.


Disadvantages

The disadvantages fusing the caching architecture design:

  1. Vulnerable to caching poisoning if not implemented correctly.
  2. If 3rd party CDN service is used, you're essentially licensing the 3rd party service provider to be man-in-the-middle (MitM).
  3. Caching management can be complicated if not implemented correctly.
  4. Requires a caching strategy and management implementation.

That's all about caching design and architecture.