I'm using redis to design user timeline (as in tweeter). I'm using sorted set to store json value with timestamp as score
zrange feed:user:10000001 0 10 WITHSCORES
1) "{\"postId\":10000411,\"userId\":10000001}"
2) "1639073241754"
3) "{\"postId\":10000412,\"userId\":10000002}"
4) "1639073241748"
When user A unfollow B, how do I remove all posts of user B from timeline of user A?
As querying the json data is not supported in sorted set (and redis), Oher responses on SO suggested to store json value in hash having postId
as subkey and storing postId
as value in sorted set. but then where do I store userId
to look for it when deleting based on it? How do I implement this feature?
CodePudding user response:
I believe you should completely refactor the way you're storing data. I suggest this way:
- having a sorted set of
feed:user:{userId}
which only store postId. - having a set of
user:post:{userId}
which store set of user's postId
So in order to "remove all posts of user B from timeline of user A", you need to first perform a query of like redis> SMEMBERS user:post:{userBId}
to retrieve all B's postId and store it into a variable "results
" and then perform this command ZREM feed:user:{userAId} results