vmayer
vmayer

Reputation: 1063

Redis: Expiration of secondary indexes

I'm curious about best practices as far as expiring secondary indexes in Redis.

For instance, say I have object IDs with positions that are only considered valid for 5 seconds before they expire. I have:

objectID -> hash of object info

I set these entries to automatically expire after 5 seconds.

I also want to look up objects by their zip code, so I have another mapping:

zipCode -> set or list of objectID

I know there's no way to automatically expire elements of a set, but I'd like to automatically remove objectIDs from that zip code mapping when they expire.

What is the standard practice for how to do this? If there were an event fired upon expiration I could find the associated zip code mapping and remove the objectID, but not sure there is (I will be using Go).

Upvotes: 0

Views: 1197

Answers (1)

for_stack
for_stack

Reputation: 22946

If there were an event fired upon expiration I could find the associated zip code mapping and remove the objectID, but not sure there is

YES, there is an expiration notification, check this for an example.

You can have a client subscribing to the expiration event, and delete items from the zipCode set or list.

However, this solution has two problems:

  1. The notification is NOT reliable. If the client disconnects from Redis, or just crashes, client will miss some notifications. In order to make it more reliable, you can have multiple clients listening to the notification. If one client crashes, others can still remove items from zipCode.
  2. It's NOT atomic. There's a time window between the key expiration and removing items from zipCode.

If you're fine with these 2 problems, you can try this solution.

Upvotes: 2

Related Questions