View profile

In-process caching and other Adventures in Nodeland - Issue #19

Matteo Collina
Matteo Collina
Hey Everyone, this is a new edition of Adventures in Nodeland that focus on… Caching! We will cover an old module, a couple of new modules, and a few interesting articles. Enjoy!

GitHub - fastify/fastify-etag: Automatically generate etags for HTTP responses, for Fastify
I’m always surprised when I go back to an old module and see how well engineered something is. In this case, I would like to cover fastify-etag.
ETags are a key part of the HTTP/1.1 caching mechanism https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html. ETags are hashes that are computed as part of a response, then then they are sent back for subsequent requests: in case the hash matches, it returns a 304 and no content.
The most important part for deploying ETags at scale is how fast it can compute those ETags. Using a cryptographic hashing algorithm like SHA1, SHA256 can significantly increase the CPU load of our Node.js processes.
Not one, but two caches
Last week I spent some time researching new pattern for implementing in-process caching. Why is this important? Shouldn’t we just use Redis or Memcached for caching? They would add only a few milliseconds to the overall response time (watch my talk at RedisConf on the topic https://www.youtube.com/watch?v=0L0ER4pZbX4).
In process caches have the key advantage of completely removing even those few milliseconds, making deployment simpler (one less component) and more resilient (no central cache). The downside of this approach is that data could easily get out of sync between servers. However this is how HTTP caching works… and it’s generally ok for customers.
Anyway, the first cache is InfiniCache: a cache that rely on WeakRef and FinalizationRegistry to cache things in the process memory, up until a garbage collection cycle happens. However this cache does not perform really well: for example mnemonist LRUCache is an order of magnitude faster (https://yomguithereal.github.io/mnemonist/lru-cache.html).
GitHub - mcollina/infinicache
The second cache is significantly more useful, meet async-cache-dedupe. This cache allows you to wrap an async function and it make sure that any calls will be deduplicated for the same parameter: you will not be fetching the same data twice from the database in a given time slot. Check it out!
GitHub - mcollina/async-cache-dedupe: Async cache with dedupe support
Tests, tests, tests..
In a new episode of OpenHive.js, we cover Testing as a main topic with Yoni Goldberg, the author of https://testjavascript.com/. Check it out!
OpenHive.js: Yoni Goldberg on Integration Tests with JavaScript
Interesting Articles
I have often though that Github Sponsor was a significant issue for managing own taxes in Italy - I have been so scared to figure it out that I did not even try. The following is a really good writeup for how the Sweden government taxes the proceedings of Github Sponsors.
Open source and taxes: A Swedish perspective
Recently I researched how Fargate worked. I was extremely surprised when I read that they moved to firecracker (https://firecracker-microvm.github.io/), the micro-vm technology that powers AWS Lambda. This is a really interesting read:
Under the hood: AWS Fargate data plane | Amazon Web Services
I am hearing different opinions on Github Actions from different friends: for some it’s not reliable enough… for others is such a fundamental piece of tech to migrate 4000 github repos. As of now Github Action is my CI of choice, even if it’s not always reliable the simplicity of use and configuration makes is a no-brainer. What do you think?
A Tale of Moving 4000 GitHub repositories to GitHub Actions | by Lou Bichard | DAZN Engineering | May, 2021 | Medium
Fastify
My friends at Prisma have created a page dedicated to Fastify! Check it out:
Fastify & Prisma | Next-Generation ORM for SQL DBs
NearForm
James M Snell
I was going to announce this on Monday but the news hit @NearForm's internal slack today so I figured I'd go ahead.... July 31st will be my last official day with NearForm. I am moving on to a new adventure but not saying what it is until August 1st...
I’m a bit sad to announce that my colleague James Snell is leaving NearForm. I’m also incredibly happy for him as I know he would build amazing new things at his new job… he is definitely not leaving the JavaScript community! Anyhow, we are hiring a Node.js Core Engineer:https://grnh.se/5c39f3d43us.
Thanks
As usual, thank you so much for reading through my rumblings! Let me know if you have any question!
Did you enjoy this issue? Yes No
Matteo Collina
Matteo Collina @matteocollina

I write about my journey as a core contributor of Node.js, as an author and a maintainer of many modules - including Fastify and Pino. In addition, I speak at conferences, and I will add links to all my talks in case you missed one.

If you don't want these updates anymore, please unsubscribe here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.