educative.io

How to store <PostId, UserId> in the cache?

I assume the cache is a plain key-value system like Memcached, in which keys has to be unique. In the newsfeed cache, each user_id maps to multiple posts, and each post_id maps to multiple users. How do you store this? And how do you query for all the post in a given user’s newsfeed?

Hi Jack.

You can store complex data structures as values in a key-value cache like Memcached. For a newsfeed system, you might use the user_id as the key and a list of post_ids as the value. This list represents the user’s newsfeed and can be updated whenever a new post is added to the user’s newsfeed.

Here’s a simplified example:

cache = {
    "user1": ["post1", "post2", "post3"],
    "user2": ["post2", "post4", "post5"],
    ...
}

To query all the posts in a given user’s newsfeed, you would simply retrieve the value associated with the user’s user_id. For example, cache["user1"] would return ["post1", "post2", "post3"].

This is a simplified example; a real-world implementation would need to handle more complexities. For example, you might need to store additional information about each post (like the posting time) to sort the newsfeed. You might also need to handle cases where the newsfeed is too large to fit in the cache.

Usually size of value field is in the range of KB to MB. That should be enough to keep top N posts for a user. If user needs to see something older that is not in the cache, then we will need to consult the database. We will need to control that most of our requests are served from caches, and load on databases remain controlled, and steady.

If you have any further questions, feel free to reach out to us.

Thank you.