This blog was written by Guilherme Oenning who is a Senior Software Developer at SoftwareONE. He truly believes that learning never ends and sharing knowledge is key to growth. When not programming, he can be found running or cycling around Dublin, Ireland.
Express is the most extensible web framework I’ve seen so far. The framework’s middleware architecture makes it easy to plug-in extra features with minimal effort and in a standardized way.
For this article, we’ll cover a very small and simple, yet powerful and useful middleware that will help you boost your express web application performance without any external dependency.
About server side cache
Caching is a commonly used technique to improve the performance of any application, be it desktop, mobile or web. When dealing with web applications we can make good use of client-side caching using response headers that all browsers currently support. But, what if we have a complex and heavy page that takes 2 second to generate the HTML output?Even if we enable client-side cache for this page, the web server will still need to render the page for each different user accessing our web application. Think about the home page of a large news portal; do they process their HTML over and over again for each visitor?
This is where server-side cache comes in handy. The goal of server side cache is responding to the same content for the same request independently of the client’s request. In our example above, the first request that reaches our server would still take 2 seconds to generate the HTML, but the following requests would hit the cache instead and the server would be able to send the response in a few milliseconds.
There are many ways of doing it, it could be done with NGINX or a CDN like CloudFlare, but in this example we’ll see how to do it with Node.js and Express with minimal work and in a flexible way.
Show me the code!
Our goal here is to enable server-side cache for our application with minimal effort. So, let’s do it!
We’ll make use of memory-cache npm module in order to be able to add content to cache. Our cache middleware is the following.
It’ll basically look for a cached value using the request’s URL as the key. If it is found, it is sent directly as the response. If it’s currently not cached, it’ll wrap Express’s send function to cache the response before actually sending it to the client and then calling the next middleware.
This is a very basic example on how to cache a heavy processing page.
Note that the above route contains two middlewares. The first one is the cache reference and the second one is the real middleware that handles the request. In this case, when the server receives the first request for this route, it will wait for 5 seconds before sending to client. But after that, consecutive calls will get the cached response body for the next 10 seconds and, of course, it will not have to wait those 5 seconds anymore.
The downside is if you have something that has to be dynamic. In the above route, we have passed the current date as an argument to the view engine. The cached response body will have this same date until the cache expires (10 seconds in this case).
The cool thing here is that it works for routes that responds with HTML, JSON, XML or any other content-type. It can be used to boost a simple website up to some heavy, complex, REST-based Express application.
You can easily plug it into any existing Express web application by simple adding the cache middleware for each route you may want to cache.
Important: PUT, DELETE and POST methods should never be cached.
For this example we have used a npm module that caches the content in memory, this has some good and bad implications.
- In-memory cache is the fastest option available;It’s easy to work with, no external dependency needed;
- We’ll lose the cached content if the server or the process goes down;
- Since it stores cached content in it’s own process memory, it will not be shared between multiple node.js process;
Another option to solve most of this issues is using a distributed cache service like Redis. It could be done with a single npm module express-redis-cache that already implements the Express a middleware.
You can find it here: https://glitch.com/edit/#!/server-side-cache-express.