Make your Node APIs faster with these 6 lines of code

Making things simple

It is hard to make something simple.

I was set out to make our node app more performant. Performance is hard and complicated and time consuming and did I mention hard?

So, I opted for in-memory caching. This is what my code looked like before caching

With the use of memory-cache, we could implement caching with just few lines (in bold).

Adding just caching, we were able to reduce our most complex request from 700ms to less than 50ms.

Of course, you would need a way to invalidate your cache, which is as simple as putting it in cache

I know, the following questions would be coming to your mind:

  • All my caching goes for a toss whenever my server restarts?
  • What if my cache key is unique for most of my requests?
  • Do I have enough RAM on my box?
  • If I am sitting behind a load-balancer with many servers, what would be my cache-hit ratio?
  • My App is more UPSERT than read

Sure, in the above scenarios and may be more, this might not make sense. There you can go with Redis/memcached.

--

--

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

Love podcasts or audiobooks? Learn on the go with our new app.

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
Abhinav Dhasmana

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine