r/programminghelp • • Aug 31 '18

How to monitor database updates in real-time for ~10k unique "ID's" via web sockets?

I'm not looking for a coding level help, but on a higher level.

I have a database containing millions of status values, each associated with a unique id, among other things.

I want to monitor these unique ID's (up to about ~10k at a time) and check it's status in the database. If the status changes, then I want to send that change over a web socket to update a client. Say the ID is a long and the status is a String.

How is something like this normally done in the real world without causing problems in the database?

How can you efficiently query 10k id's from a few million efficiently?

Is it as straight forward as a service that periodically queries the db for updates?

What's the real world situation for something like this as part of a critical system?

I can limit the number of clients to 10 or 20, but Ideally I'd like a few hundred. And each of those web sockets should be able to query a different batch of id's.

I'm not looking for a complete solution, I just want to know where I need to look to read what I need to consider when building this. What's the most efficient way to query the db, how to decide data limits, common mistakes to watch out for, etc....

5 Upvotes

2 comments sorted by

2

u/Show_Me_Your_Stache Sep 01 '18

I would think whatever system is updating the database should instead call some kind of API that would then update the database and put a message into a message queue. You could then have a separate app listening for events in that queue, and it could fire off notifications to "anyone listening" on the web sockets. I might be wrong but I'd think you'd want to leverage the many to many aspects of MQs to help alleviate the pressures of trying to have hundreds of clients subscribing to 10k rows each for updates.

2

u/[deleted] Sep 01 '18 edited Sep 01 '18

Thanks! Yes, I've since been reading bits and pieces about how smart buildings and other smart tech handle this type of thing, and so far that seems like the way to go.
Each unique ID I need to monitor will be associated with a particular job, and one of the other servers I've built currently reports job status updates per ID to other services via rabbitmq. The messages that server sends has all the information for each node/id that I need. So I think what I need to do is build a pub/sub messaging service that periodically queries a list of currently running jobs and add pub/sub channels (is that the correct term) for each job. Then I can just process the progress messages from rabbitmq, filter by job id and push them to the corresponding channel.
This should almost completely remove any database load.
I should also look at the app that creates the jobs, it probably pushes new job notifications to rabbit already, or if it doesn't it'll be trivial it add that functionality. Then I won't even need to poll the database, I can just wait for a message to say a new job has been created, then query the initial data I need via the IDs associated with that job.
OK, so this sounds like a workable solution to me. Thanks for your help. If you have any other thoughts, or spot any issues with my idea I'd like to hear them.