Design a Feature to Show the Number of Users Viewing a Page


🙋 Here are some details you should know about this question:

Details from a candidate:

What would be your main considerations when designing a system that lets users know about the number of other users currently viewing a page (e.g, a hotel/accommodation site)

My first reaction was that this would be a simple distributed key-value cache (accommodation_id --> count), that supports an increment function. When a user views a hotel, just increment the value of that hotel by 1. But we need to decrement too, assuming "current" users means any views within last 5 minutes.

In that case, the cache value could be a list of userId+timestamps? We probably need to store user-id in the cache, because if the same user keep reloading a page, we do not want to increment the count. And then it starts becoming more complex. How would entries older than 5 minutes be removed and not considered in the current count?

There would be a need to replicate the cache geographically, but that would not be too much of an issue since correctness (consistency) is not a requirement here. I assume that a slightly incorrect count can be tolerated for a small amount of time.

Would you consider persisting this data at all? My thinking is that we do not need any persistent storage, since the counts are temporary. We probably would have a separate system that tracks the number of views of an accommodation, which is outside the scope of this problem.


← Back to Main Table