Adventures in Nodeland logo

Adventures in Nodeland

Subscribe
Archives
July 19, 2021

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

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
GitHub - fastify/fastify-etag: Automatically generate etags for HTTP responses, for Fastify
Automatically generate etags for HTTP responses, for Fastify - fastify/fastify-etag
github.com
GitHub - fastify/fastify-etag: Automatically generate etags for HTTP responses, for Fastify
GitHub - fastify/fastify-etag: Automatically generate etags for HTTP responses, for Fastify
Automatically generate etags for HTTP responses, for Fastify - fastify/fastify-etag
github.com

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
GitHub - mcollina/infinicache
Contribute to mcollina/infinicache development by creating an account on GitHub.
github.com
GitHub - mcollina/infinicache
GitHub - mcollina/infinicache
Contribute to mcollina/infinicache development by creating an account on GitHub.
github.com

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
GitHub - mcollina/async-cache-dedupe: Async cache with dedupe support
Async cache with dedupe support. Contribute to mcollina/async-cache-dedupe development by creating an account on GitHub.
github.com
GitHub - mcollina/async-cache-dedupe: Async cache with dedupe support
GitHub - mcollina/async-cache-dedupe: Async cache with dedupe support
Async cache with dedupe support. Contribute to mcollina/async-cache-dedupe development by creating an account on GitHub.
github.com

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
OpenHive.js: Yoni Goldberg on Integration Tests with JavaScript
OpenHive.js is a JavaScript podcast that features conversations and discussions with open source leaders and key contributors in the JavaScript community.
www.nearform.com
OpenHive.js: Yoni Goldberg on Integration Tests with JavaScript
OpenHive.js: Yoni Goldberg on Integration Tests with JavaScript
OpenHive.js is a JavaScript podcast that features conversations and discussions with open source leaders and key contributors in the JavaScript community.
www.nearform.com

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
Open source and taxes: A Swedish perspective
Open source and taxes seems to be a weird beast in Sweden, and maybe elsewhere as well. Here’s how it works: If I receive a donation for an open source project of mine, then the Swedish tax authority says that I should tax that as income as there’s a reasonable expectation of a service in return.o al Thank
www.linkedin.com

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
Under the hood: AWS Fargate data plane | Amazon Web Services
Today, we launched a new platform version (1.4) for AWS Fargate, which bundles a number of new features and capabilities for our customers. You can read more about these features in this blog post. One of the changes we are introducing in platform version 1.4 is replacing Docker Engine with Containerd as Fargate’s container execution engine. 
aws.amazon.com

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
A Tale of Moving 4000 GitHub repositories to GitHub Actions | by Lou Bichard | DAZN Engineering | May, 2021 | Medium
GitHub Actions is a new(ish) player in the Continuous Integration(CI) market, and many companies will be looking to analyse its capabilities. At DAZN, we’ve just finished the first few phases of our…
medium.com

Fastify

My friends at Prisma have created a page dedicated to Fastify! Check it out:

Fastify & Prisma | Next-Generation ORM for SQL DBs
Fastify & Prisma | Next-Generation ORM for SQL DBs
Prisma is a next-generation ORM that’s used to query your database in an Fastify server. You can use it as an alternative to writing plain SQL queries, to using query builders like knex.js or to traditional ORMs like TypeORM, MikroORM and Sequelize.
www.prisma.io
Fastify & Prisma | Next-Generation ORM for SQL DBs
Fastify & Prisma | Next-Generation ORM for SQL DBs
Prisma is a next-generation ORM that’s used to query your database in an Fastify server. You can use it as an alternative to writing plain SQL queries, to using query builders like knex.js or to traditional ORMs like TypeORM, MikroORM and Sequelize.
www.prisma.io

NearForm

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!

Don't miss what's next. Subscribe to Adventures in Nodeland:
GitHub X YouTube LinkedIn