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

Watch vs tx #388

Closed
BenjaminBrandmeier opened this issue Jun 11, 2023 · 1 comment
Closed

Watch vs tx #388

BenjaminBrandmeier opened this issue Jun 11, 2023 · 1 comment

Comments

@BenjaminBrandmeier
Copy link

First of all, thanks a lot @keroxp and @uki00a and all the others for your dedication on this project!

In the documentation you are stating the following:

We recommend to use tx() instead of multi()/exec() for transactional operation. MULTI/EXEC are potentially stateful operation so that operation’s atomicity is guaranteed but redis’s state may change between MULTI and EXEC.

WATCH is designed for these problems. You can ignore it by using TxPipeline because pipelined MULTI/EXEC commands are strictly executed in order at the time and no changes will happen during execution.

From my understanding the following code must be implemented via WATCH:

        await redis.watch("key")
        const value = JSON.parse(await redis.get("key") as string)

        value.someProperty = value.someProperty + 1 // may be a complex operation

        await redis.multi()
        await redis.set("key", JSON.stringify(value))
        const execResult = await redis.exec()

        if (!execResult) {
            // key was changed, retry entire operation
        }

From my understanding, using TxPipeline instead of WATCH + MULTI/EXEC is not possible, because when executing tx.get() I'm not able to retrieve and alter the object within the transaction.

    const tx = redisConnection.tx()

    const value = JSON.parse(await tx.get("key") as string) // SyntaxError: Unexpected token 'O', "OK" is not valid JSON

Do you agree that using WATCH is required in this case or is there a way to use tx nevertheless?

@BenjaminBrandmeier
Copy link
Author

I have come to realize that storing JSON directly as a whole string is considered an anti-pattern in Redis. With my WATCH approach I'm solving a concurrency issue but not other disadvantages like efficient value scanning solutions.

Probably will try out RedisJSON together with sendcommand until #372 is resolved.

Closing this issue now.

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

No branches or pull requests

1 participant