Microsoft virtual | Design distributed counter

Hello, I was asked this question in a virtual interview for the Office Engineering team based out of Hyderabad.
Imagine you have to show a counter on the website "micro_soft.com" which shows how many users have opened the website at any point of time.
How will you design such a distributed counter. Consider the scale of the problem. Also consider the fact that when a user has closed the website, the value of the counter should decrease as well.

--
I proposed a brute force solution where every http request would result in an increase of a counter which is stored in a shared database. The approach would be super slow.

I proposed an alternate method as well where we do limited aggregation on the application server whenever there is a request to open the website.Each application server will aggregate the data for a few minutes ( I proposed this duration to be five minutes). After this duration will be over, the data will be flushed to a queue, where a counter service will read it and update the database.

To this the interviewer mentioned that the database could be located in multiple data-centers. How will you make sure the update is correct or not. I mentioned that the primary db would be updated and then the replication would happen across other data centers.

He also mentioned that when the website will be closed, the counter should decrease. How will you handle that?
Also the value of counter mentioned on the website would be stale. Can something be done about it to make it more real-time.

Comments (12)