I am building a website with node.js express webserver and i use Mongo db database.

On my website people can register a profile they can upload pictures and write text.

Now i hear often that many things can be load from a cache then the website run faster.

Now i ask myself if i need to code something into my script to make for example the Mongo db use cache for requests or if i need to code anything to make the browser or webserver to use pictures from cache.

Can somebody help me to understand this cache thing?


Unless your software (e.g. WordPress) has some sort of caching built in or as an addon you can do it by manipulating headers.

This article explains it well https://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/

In short, you need to add some headers that instruct the visitor's browser how long before content is regarded as "stale".

Here are some examples from that page:

ExpiresActive On
ExpiresDefault A0

# 1 YEAR - doesn't change often
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A31536000

# 1 WEEK - possible to be changed, unlikely
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
ExpiresDefault A604800

# 3 HOUR - core content, changes quickly
<FilesMatch "\.(txt|xml|js|css)$">
ExpiresDefault A10800

There are a lot of other resources on the web about this.


Implementing server side caching is usually the best way to make your web app perform better. The idea is to store data locally in memory between requests to reduce database lookups.

  1. First request for /page
  • Database hit for contents of page (100ms)
  • Store contents in local cache (10ms)
  • Return page to user (100ms)
  • Total request time: 210ms
  1. All subsequent requests for /page
  • Get contents of page from local cache (10ms)
  • Return page to user (100ms)
  • Total request time: 110ms

Node.js Express has a module local cache: https://www.npmjs.com/package/node-cache

You can also use network caches based on memcached or redis. They are great for distributed web apps running on multiple servers because the cache is distributed and shared between all the servers. Even though they are a bit slower than local memory caches, they can have a larger capacity, be used by multiple web servers, and don't take up memory resources on the web server itself. Here is an article about using them in node.js express: Simple server side cache for Express.js with Node.js | Medium and here is a comparison of the two: caching - Memcached vs. Redis? - Stack Overflow

Regardless of whether or not you use a cache, the other technique you should look into is reducing database queries per page. Ideally each page would make just one database query for the main content. However, it is very common for a page to make multiple, even dozens, of database queries. In particular, watch for the pattern of:

list = db_query("select product_id from products where product_category=?", page.category)
for itemid in list {
   item = db_query("select product_name from products where product_id=?", itemid)

which should be replaced with a single db query like:

products = db_query("select product_name, product_id from products where product_category=?", page.category)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.