Reputation: 21553
I am using Redis to implement a 'feed' system in my social networking app similar to Instagram/facebook.
I have a key "user:id:feed"
which contains a sorted set of ids that point to pictures. What I am wondering is, when a particular photo is deleted, how can I make sure that photo id is also deleted in all feeds that contain that photo id?
Would I have to loop through each and every feed set and delete it? Is that a common pattern in redis?
Thanks
Upvotes: 2
Views: 2334
Reputation: 73226
The common pattern in Redis is to cross-reference items in various data structures. You have to do it explicitly (there is no automatic referential integrity mechanism with Redis).
Here you have a many-to-many relationship between feeds and photos. If you need to add/remove both feeds and photos, you will probably want to use 2 distinct data structures to materialize this relationship.
user:id:feed -> sorted sets of photo:n (referring to photos)
photo:n -> set of user:id:feed (referring to feeds)
So you can efficiently retrieve the photos from the feeds, and the feeds from the photos. For instance to delete a photo:
So the delete operation is finally costing two roundtrips. You can decrease to 1 roundtrip by using a Lua server-side script.
Upvotes: 2