Caching as a part Software Architecture: 5 min read

How to boost the software performance by caching

Web caching (or HTTP caching)

Web caching can be either shared or private. While a shared cache stores resources for reuse by more than one user, a private cache is designed for a single user. We will explore more about private cache as it fits to the most of use cases when developing mobile or web applications.

  • cache-control specifies max-age of a copied resource and whether the cache is a public or private (i.e. can be used by shared cache or intended for a single user only). However, you may decide to set it to no-store (i.e. no caching allowed) or no-cache (requires to check the server for validation before returning the cache)
  • etag specifies a version of resource, thus the cache is able to check whether the content was changed or not. Etags also help to deal with simultaneous updates of a resource, but it is another topic.
Figure 1: Example of response headers with a caching control
Figure 2: Web caching

Application caching

Imagine you have an API which provides information on upcoming local holidays and events. 90% of your web requests are read operations, which is getting events for specific period of time from the database, transforming them into a viewable state, and providing it back to the user. Users can’t remember the details of the events and thus they tend to return back to your application to fetch the same data from time to time. Moreover, as certain events come closer to the current data, more and more users request very similar data.

const Memcached = require('memcached');let memcached = new Memcached("127.0.0.1:11211")
// configuring the memcachedMiddleware which is going
// to be used between our server and client requests
let memcachedMiddleware = (duration) => {
return (req,res,next) => {
let key = "__express__" + req.originalUrl || req.url;
memcached.get(key, function(err,data){
if (data) {
res.send(data);
return;
} else {
res.sendResponse = res.send;
res.send = (body) => {
memcached.set(key, body, (duration*60), function(err){
//
});
res.sendResponse(body);
}
next();
}
});
}
};
// Finally use the middleware, which ensure that
// no request is served for this endpoint before
// is checked against our cache storage
app.get("/products", memcachedMiddleware(20), function(req, res) {
[...]
});
Figure 3: Application caching & web caching

Summary notes

Combined both web & application caching are significant tools to increase performance and responsiveness of an app instance. These techniques are essential tools in modern distributed systems. We can upgrade our design from previous article How to Scale Your Applications: 5 min read” to have a clear view of the core message in this article:

--

--

Software Engineer, Social capitalist, Historian

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store