How to implement a counter "this item browse another N people"?

How would you implement such a counter? How to build a scalable solution? As an example - booking.com counter of number of users viewing the property.
April 4th 20 at 12:58
4 answers
April 4th 20 at 13:00
Solution
On the page to do periodically send status "object viewing". Fold in Redis as HashSet with the structure:
name hash = object id
key = user id
value = timestamp
Choose the timeout for example for 5 seconds and consider all entries older than 5 seconds is invalid.
To build meter take the number of valid elements in the hash for a particular object.
To hash growing need to remove expired elements because Redis is not able to invalidinput elements HashSet. This can be done periodically or you can write a lua script which will do the insert of a new element and simultaneously remove expired.
April 4th 20 at 13:02
It was a marketing ploy. You can do a random counter. If only the user has realized that he has competitors and quickly bought the right product.
well, even if it was a marketing ploy, you can show real data (see, for contactmaker it would be convenient for anon to go and see the article through the eyes of the user).
In this case, the best way to use websocket and already using a built-in method ws to count the number of connections and sends the client - sean_Bechtelar commented on April 4th 20 at 13:05
@sean_Bechtelar, no. Real data often will be zero, and this is not what you need for marketing. - Cole.DuBuque commented on April 4th 20 at 13:08
@Cole.DuBuque, I understand that well. but I described how to solve the problem in the case when real data is needed - sean_Bechtelar commented on April 4th 20 at 13:11
@sean_Bechtelar, the actual data is not needed by anyone in the "currently browsing". - Cole.DuBuque commented on April 4th 20 at 13:14
@Cole.DuBuque, @ambrose random counter all clear. But first, it's easy raschehlyaetsya and trust of the users from falling. Secondly, the question was exactly about the technical side of the question, purely teoriticheski, and the answer is "a marketing ploy" doesn't really fit in the format of Habra, isn't it? - deshawn16 commented on April 4th 20 at 13:17
@sean_Bechtelar, the solution with sockets scales poorly, and Yes, as an option. - deshawn16 commented on April 4th 20 at 13:20
@deshawn16, if your audience - the users are able to "uncover" isn't a stereotype made by the randomizer, you do not abuse their trust, using a similar marketing ploy. In principle. - Cole.DuBuque commented on April 4th 20 at 13:23
April 4th 20 at 13:04
If you solve the problem technically, you first need to define the refresh rate or accuracy. That means the user is now watching this page? Active browser tab? But if he looked away? )) And do you really want to have such high precision, what business problem does it solve?
Vskidku there are 3 ways:
1) "old-fashioned": to build the nginx logs so that they are easy to parse and to take the number of requests for the desired page in the scan interval
2) to gather statistics on the level of the router application in redis (as recently proposed above)
3) the most accurate, but too expensive, this websocket to open the page and read where they came from.
April 4th 20 at 13:06
Using the SignalR library, the number of active users stored in Redis Server

Find more questions by tags Designing softwareWeb Development