Is it smart to keep static files in memory in node js?

By | January 12, 2018
Questions:

When serving static files in Node I am simply using fs.readFile and then throwing the data onto the response content.

I am tempted to store the results of fs.readFile in a variable and then skip using fs.readFile when the file is requested a second time and just write the content out from the variable in memory.

My questions:

  1. Are there any issues I could run into with this approach? Is there a better way?
  2. (a high level overview) How do servers like nginx and Apache cache files to serve to users?
Answers:

Are there any issues I could run into with this approach? Is there a better way?

Yep, you can run out of memory. You can keep in cache only what is hot.

How do servers like nginx and Apache cache files to serve to users?

They keep it in memory indeed but additionally they implement some LRU (or LRU-alike) strategy to invalidate caches.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *