upstash / redis-js

HTTP based Redis Client for Serverless and Edge Functions
https://docs.upstash.com/redis
MIT License
666 stars 51 forks source link

Is "watch" supported? #720

Closed reimavronicolas closed 9 months ago

reimavronicolas commented 9 months ago

Hi, not sure if I'm overlooking it, but is the watch command supported?

I'm looking to do something like:

const key = "account:spaces"
await redis.watch(key)
const count = await redis.scard(key)
await redis.multi().sadd(key, {count: count+1}).exec()
await redis.unwatch(key)
ogzhanolguncu commented 9 months ago

This is all the supported commands by our redis server: https://upstash.com/docs/redis/overall/rediscompatibility. It's currently supported by the server, but not implemented in the SDK. I'll see what I can do this week to support this command.

ogzhanolguncu commented 9 months ago

Unfortunately, we can't support watch,unwatch and discard commands via SDK. And the reason is Redis transactions are stateful and bound the connection itself, that’s why we don’t support them in REST api.

https://upstash.com/docs/redis/features/restapi#rest-redis-api-compatibility

mikechabot commented 5 months ago

@ogzhanolguncu Thanks for confirming the unavailability of watch, but I'm not sure I fully understand the explanation. Would you be able to expand a bit? How might we support concurrent operations that may conflict without the use of watch to detect these potential changes?

ogzhanolguncu commented 5 months ago

Well, our SDK designed to work with mainly serverless environments where TCP(stateful) calls are not allowed. And, since watch like commands require long running connections we can't allow it. Maybe you can use Upstash Lock to have more control over concurrent operations?