3

Let's say we have a web application where the users can login and have a status (available, not available). There is server application that has to know the users status. This can be synchronised by sending a message from the webapp when the user status changes.

The question is how to get efficiently all the users (a lot of users) status when the server application starts up?

Options I can think of:

  1. The server app asks for a list of user, status from the webapp (I don't think this is efficient)
  2. The server app reads the user, status from the database directly (what if the server app slows down the database because of this huge user list?)

Is there a better way doing this?

  • users or user's? – Ewan Nov 23 '17 at 13:56
  • I think the answer is going to be along the lines of "don't". Can you add some more info about why you need the states and what you are doing with them so that we can suggest alternatives? – Ewan Nov 23 '17 at 13:57
  • Never mind. Actually we don't need this. The server app can get the user's status when it needs it and not when it start up. Thanks! – norbert.mate Nov 23 '17 at 15:22
4

You were a bit vague on number of users, active users, and servers. But here's one way.

Define an "active user" as having used the app in the last K seconds, and arrange for user web clients to PING the service every K/2 seconds. This might update an RDBMS table directly, or might send to your cluster of servers via pub-sub e.g. 0mq or kafka. Cluster members could use your favorite database, or an in-core representation like a hash map combined with an LRU to age out inactive users.

Each PING event simply updates a stored timestamp for that user. When your server app starts up or performs another action that makes it need to know about actives, it simply issues a time-limited query to the database: select userid from active where ping_time > now() - K. A timestamp index on that table will let you scale up to a very large number of historic registered users, as inactives are stored in disk blocks that the query ignores. Scaling to large number of users that are active Right Now is a separate challenge, for which this active users query will be the least of your worries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.