Implementing ZADD If Key Exists
rozumem.xyz·20h·
Discuss: Hacker News

I recently had to make some performance optimizations. These are my favorite kind of coding task as they usually require some sort of creativity and tradeoff. This time around I also learned more about Redis and practiced using AI to generate code.

During my refactor, I came across a need to upsert a (member, score) into a Redis sorted set only if the Redis sorted set exists. This upsert would be done hundreds of times a second for different keys, so it had to be fast.

The basic ZADD command offers a XX flag which adds a (member, score) only if the member exists in the sorted set. But what I was looking for was a flag to add a (member, score) only if the sorted set exists. Unfortunately, no flag exists for this use case. If no sorted set exists, Redis will create a ne…

Similar Posts

Loading similar posts...