Edward M Smith
Edward M Smith

Reputation: 10627

Best way to handle read/unread messages in Redis activity feed?

I have an activity feed system that uses Redis sorted sets.

Events happen and a message is placed into a sorted set for each relevant user, with a timestamp for the score.

The messages are then distributed to the users, either when they log in, or through a push if the user is currently logged in.

I'd like to differentiate between messages that have been "read" by the user and ones that are still unread.

From my understanding, I can't just have a "read/unread" property as part of the member as changing it would cause the member to be different and therefore be added a second time, instead of replacing the current member.

So, what I'm thinking is that for each user, I have to sorted sets - an "unread" set and a "read" set.

A little less sure on how to deliver them. I can't just union them as I lose the distinction between read/unread unless I inverse the score on the unread ones, for instance.

Returning the two sets individually (and merging them in code) makes paging difficult. I'd like to be able to the 20 most recent messages regardless of read/unread status.

So, questions:

  1. Is the read set/unread set way the best way to do it? Is there a better way?
  2. What's the best way to return subsets of the merged/union'd data.

Upvotes: 4

Views: 1899

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230481

Instead of trying to update the member, you could just pop it and insert a new version. Should be no problem, because you know both member and its timestamp.

Upvotes: 4

Related Questions