r/learnjavascript 17h ago

In flight is not an in memory cache

This week I made an npm package (Inflight) to solve the concurrent repetitive queries to database or cache

Reached +500 weekly downloads

The idea is to cache the Promise of a db query (Not the response of the query).

So in a high concurrency system, where the same data (like: cr7 or messi profile) is requested by many users at the same time, only one query goes to cache or database.

some interesting benchmarks:

  • Duration: 30s
  • Cache TTL: 5s
  • Concurrency: 100
  • Unique keys: 10
Metric With Inflight Without Inflight
Query Per Second ~1,130,360 ~174,950
Total queries 33,911,000 5,248,600
DB calls 60 517
Cache calls 3,391,021 5,248,600

**Insights:*\*

  • DB calls saved: **56x*\*
  • Cache calls saved: **10x*\*
  • Total queries growth: **6.5x*\* (5.2M → 33.9M)

more benchmarks here: https://github.com/ademmenh/inflight/tree/main/benchmarks

npm package: https://www.npmjs.com/package/@inflightjs/inflight

github repo: https://github.com/ademmenh/inflight (PRs, issues, starts)

2 Upvotes

4 comments sorted by

2

u/Full-Hyena4414 16h ago

Pretty standard strategy but cool

1

u/_dext 14h ago

thank you, check the Usage on the README, its really easy to use !

1

u/-goldenboi69- 6h ago

Don't use npm

1

u/whimsy_hearth_42 6h ago

Adding dependencies for promise deduplication is a mistake. I implement this pattern directly in my application code to avoid supply chain risk