Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(caching): coalesce concurrent requests to wrap function #599

Merged
merged 1 commit into from
Oct 27, 2023
Merged

fix(caching): coalesce concurrent requests to wrap function #599

merged 1 commit into from
Oct 27, 2023

Conversation

douglascayers
Copy link
Contributor

Please check if the PR fulfills these requirements

  • Followed the Contributing guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Fixes #417

In the v4.x version, the caching.wrap() function would coalesce multiple concurrent requests to minimize how many times the generator function was invoked. This was a helpful optimization to reduce load on downstream systems like databases and APIs.

This pull request adds that cache miss relief logic back by using the promise-coalesce module, which is said logic in a generic, reusable function.

For tests, the #417 issue test demonstrates the undesired behavior. The updated wrap() tests demonstrate the desired behavior by coalescing requests.

Thanks

@jaredwray
Copy link
Owner

Hi @douglascayers - thanks for putting this together. A couple of questions around this:

  • With this change is there anything breaking or should everything work as it with this added functionality?
  • Since you added this module, will you be able to support and maintain it? If not, I would prefer adding the code directly into this project.
  • Can you update the readme with an example of how this works?

@douglascayers
Copy link
Contributor Author

douglascayers commented Oct 25, 2023

@jaredwray Thanks for the follow up. I've rebased this PR and have answers to your questions below. Thanks

  • With this change is there anything breaking or should everything work as it with this added functionality?

This is a drop-in patch update. No breaking changes. Users don't have to make any changes to benefit from this change.

The functional difference is that cache manager's wrap function is more efficient when multiple concurrent requests occur for the same cache key.

When there's a cache miss, instead of calling the user's function to fetch a value to cache once per request (which can exhaust downstream API rate limits, be slower, etc), one promise goes off to do the work and each of the other concurrent requests receive a promise that promises to resolve to the same value as the first promise that's actually executing the user's function.

  • Since you added this module, will you be able to support and maintain it? If not, I would prefer adding the code directly into this project.

Yes, I intend to support it. I see value in the coalescing logic as a standalone helper library, which is agnostic to caching though that is the main driver for the solution.

With that said, I'm not opposed to adding the code directly to this project if you all want to maintain it going forward.

  • Can you update the readme with an example of how this works?

The API for the wrap function to users is not changed, if that's what you're asking. And if so, the README for cache manager maybe doesn't need to change since users don't have to do anything differently than they do now. If this PR is merged, then users will notice an efficiency boost when concurrent requests call wrap function for the same cache-key.

This PR includes new tests to demonstrate the problem when wrap is called concurrently with and without coalescing logic.

I also have an example on the promise-coalesce project's README, too.

Let me know if you have any other questions or concerns, thanks

@codecov-commenter
Copy link

codecov-commenter commented Oct 27, 2023

Codecov Report

Merging #599 (d1753dc) into master (66246e9) will not change coverage.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff            @@
##            master      #599   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          292       295    +3     
  Branches        75        76    +1     
=========================================
+ Hits           292       295    +3     
Files Coverage Δ
src/caching.ts 100.00% <100.00%> (ø)

@jaredwray jaredwray merged commit b07f4b1 into jaredwray:master Oct 27, 2023
@jaredwray
Copy link
Owner

@douglascayers - thanks so much for all the work on this and we will be working to get this deployed in the next couple weeks.

@douglascayers
Copy link
Contributor Author

Fantastic, thanks!

@douglascayers douglascayers deleted the feature/417-coalesce-wrap-caching branch October 27, 2023 11:35
renovate bot referenced this pull request in tf2pickup-org/server Nov 17, 2023
[![Mend Renovate logo
banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[cache-manager](https://togithub.com/node-cache-manager/node-cache-manager)
| [`5.2.4` ->
`5.3.1`](https://renovatebot.com/diffs/npm/cache-manager/5.2.4/5.3.1) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/cache-manager/5.3.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/cache-manager/5.3.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/cache-manager/5.2.4/5.3.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/cache-manager/5.2.4/5.3.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>node-cache-manager/node-cache-manager (cache-manager)</summary>

###
[`v5.3.1`](https://togithub.com/node-cache-manager/node-cache-manager/releases/tag/v5.3.1)

[Compare
Source](https://togithub.com/node-cache-manager/node-cache-manager/compare/v5.3.0...v5.3.1)

Major fix as `5.3.0` did not have the dist folder on release.

#### What's Changed

- updating to always run vitest with coverage by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/605](https://togithub.com/node-cache-manager/node-cache-manager/pull/605)
- fix(refreshThreshold): don't run if ttl is -1 by
[@&#8203;mihirgupta0900](https://togithub.com/mihirgupta0900) in
[https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604)
- fix(caching): coalesce concurrent requests to `wrap` function by
[@&#8203;douglascayers](https://togithub.com/douglascayers) in
[https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599)
- upgrading lru-cache to 10.0.2 by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/609](https://togithub.com/node-cache-manager/node-cache-manager/pull/609)
- upgrading eslint, prettier, and types to latest by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/610](https://togithub.com/node-cache-manager/node-cache-manager/pull/610)

#### New Contributors

- [@&#8203;mihirgupta0900](https://togithub.com/mihirgupta0900) made
their first contribution in
[https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604)
- [@&#8203;douglascayers](https://togithub.com/douglascayers) made their
first contribution in
[https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599)

**Full Changelog**:
jaredwray/cacheable@5.2.4...v5.3.1

###
[`v5.3.0`](https://togithub.com/node-cache-manager/node-cache-manager/releases/tag/v5.3.0)

[Compare
Source](https://togithub.com/node-cache-manager/node-cache-manager/compare/5.2.4...v5.3.0)

#### What's Changed

- updating to always run vitest with coverage by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/605](https://togithub.com/node-cache-manager/node-cache-manager/pull/605)
- fix(refreshThreshold): don't run if ttl is -1 by
[@&#8203;mihirgupta0900](https://togithub.com/mihirgupta0900) in
[https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604)
- fix(caching): coalesce concurrent requests to `wrap` function by
[@&#8203;douglascayers](https://togithub.com/douglascayers) in
[https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599)
- upgrading lru-cache to 10.0.2 by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/609](https://togithub.com/node-cache-manager/node-cache-manager/pull/609)
- upgrading eslint, prettier, and types to latest by
[@&#8203;jaredwray](https://togithub.com/jaredwray) in
[https://github.com/node-cache-manager/node-cache-manager/pull/610](https://togithub.com/node-cache-manager/node-cache-manager/pull/610)

#### New Contributors

- [@&#8203;mihirgupta0900](https://togithub.com/mihirgupta0900) made
their first contribution in
[https://github.com/node-cache-manager/node-cache-manager/pull/604](https://togithub.com/node-cache-manager/node-cache-manager/pull/604)
- [@&#8203;douglascayers](https://togithub.com/douglascayers) made their
first contribution in
[https://github.com/node-cache-manager/node-cache-manager/pull/599](https://togithub.com/node-cache-manager/node-cache-manager/pull/599)

**Full Changelog**:
jaredwray/cacheable@5.2.4...v5.3.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tf2pickup-org/server).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy41OS44IiwidXBkYXRlZEluVmVyIjoiMzcuNTkuOCIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

callbackFiller removed in 5.0.0
3 participants