Search code examples
redisstackexchange.redisredis-cluster

What happens when you don't UNWATCH WATCH'd keys in Redis


While poking through Redis I came across the WATCH command -- which is a facet of Redis transactions.

The related section on Transactions explains in a bit more detail, how WATCH can work with other Redis concurrency commands.

However, one thing that confuses me is: What happens if I call WATCH on keys, but don't (for whatever reason) UNWATCH them? Is there some cache of WATCH'd keys that fills and then starts discarding older WATCH'd keys? Will this cause latency issues?

Any comments would be helpful :)


Solution

  • What happens if I call WATCH on keys, but don't (for whatever reason) UNWATCH them?

    These keys will be added to a watched key list.

    Is there some cache of WATCH'd keys that fills and then starts discarding older WATCH'd keys?

    NO. Watched keys won't be removed unless you unwatch it (calls UNWATCH, EXEC or DISCARD), or the connection is closed.

    Will this cause latency issues?

    If there're keys watched, each time you modify a key (no matter whether it 's a watched key or not), Redis costs some CPU circle to handle key watch related stuff. So you'd better unwatch keys ASAP.