On Thu, Oct 26, 2023 at 08:32:25AM -0600, Luveh Keraph wrote:
> Consider the following sequence:
>
>
127.0.0.1:6379> SADD My:Set SomeKey
> (integer) 1
> 127.0.01:6379> SISMEMBER My:Set SomeKey
> (integer) 1
>
127.0.0.1:6379> DEL SomeKey
> (integer) 1
>
127.0.0.1:6379> KEYS SomeKey
> (empty array)
> 127.0.01:6379> SISMEMBER My:Set SomeKey
> (integer) 1
> 127.0.01:6379> SMEMBERS My:Set
> 1) "SomeKey"
>
> In spite of the fact that SomeKey has been deleted from the database, it
> still appears as a member of the My:Set set.
*Technically*, it's not the *key* that appears as a member of the set, it's
a string that just happens to have the same name as a key. Redis is not a
relational data store, and attempts to use it as one often end in sadness.
> Is it possible to do things so that when a particular key is deleted it is
> also automatically removed from all the sets that it may belong to?
Not in open source Redis, as far as I'm aware, but there appears to be a
keyspace trigger feature in the Redis Cloud commercial product:
https://redis.io/docs/interact/programmability/triggers-and-functions/concepts/triggers/keyspace_triggers/
> If not, what is the strategy recommended to adopt in order to make sure
> that any key deleted from the database is removed from the sets it belongs
> to, bearing in mind that we may be talking about thousands of sets with
> thousands of elements each?
I can think of two ways:
1) Do it in your application, and ensure atomicity with a MULTI/EXEC block;
or
2) Write lua procedures that do all the necessary work of managing the
sets alongside manipulation of the data in the keys themselves, and call
those instead of the low-level data manipulation operations directly.
- Matt