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

Achieve atomic updates with node-redis zIncrBy for multiple members #2813

Open
unscrew opened this issue Aug 6, 2024 · 2 comments
Open

Achieve atomic updates with node-redis zIncrBy for multiple members #2813

unscrew opened this issue Aug 6, 2024 · 2 comments

Comments

@unscrew
Copy link

unscrew commented Aug 6, 2024

Description

What's the best practice to achieve atomic updates with node-redis client for multiple members from nodejs?
https://github.com/redis/node-redis/blob/master/packages/client/lib/commands/ZINCRBY.ts

Also, do you have a doc stating request/response examples for clients?

@leibale
Copy link
Contributor

leibale commented Aug 7, 2024

ZINCRBY only supports incrementing 1 key at a time, but you can wrap multiple of them with MULTI & EXEC:

await client.multi()
  .zIncrBy('key', 1, '1')
  .zIncrBy('key', 2, '2')
  .exec()

regarding "request/response" docs:

  • you have some examples in the examples folder
  • you can use redis.io, we try to match the names and order of arguments as much as possible.
  • you can use typescript autocomplete/types
  • unfortunately we don't have autogenerated examples based on typescript yet

@unscrew
Copy link
Author

unscrew commented Aug 9, 2024

Thanks @leibale. If we don't know now the number of members, would auto-pipelineing be the recommended approach like below? or do recommend something else?

const promises = [];
for (const member of members) {
    promises.push(nodeRedisClient.zIncrBy(key, val, member));
}
const allResults = await Promise.allSettled(promises);
// check allResults

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants