What I have
This is a prototype. I have a pool of 100 clients connected to the server via websockets reporting things and awaiting for commands. The server polls the commands
database table of type MEMORY
in a loop using a query with WHERE client_id=?
. I can insert a combination of client_id
+command
to that table, and once I do that, the corresponding loop will match and SELECT
it and pass it back to the client.
What's the problem
The approach sounds like it would work, but as far as I understand I'm talking about n
simultaneous database connections and queries in an endless loop (n
being the number of clients), which doesn't sound effective. It'd be much better to do one query in one loop and then somehow check the client_id
, if any, and distribute the results to the corresponding clients.
This reminds me of the approach where you're selecting articles first and then for () {}
the resultset and do separate queries to get the details foe each of the items, which results in n+1
queries being made. The solution to that is doing a big query with JOIN
s and also preloading the other data that doesn't fit into the main JOIN
ed query. There should be the similarly more effective way to do the database polling too.
UPDATE: I found this answer in the related section, and it says pretty much the same thing:
Hammering your database isn't really a good idea. While I'm pretty sure you've realized this, others might not have. I remember a friend of mine tried to use a php script and a Javascript AJAX function in a loop for a semi-real time game. He very quickly realized that performance degraded as more people joined, simply because he was executing a ton of queries per second which hammered the database.
So polling the database for each client sounds as unscalable and ineffective as building an AJAX chat application.
What I'm asking for
I guess that every possible programming approach must have been named and covered by now, so what is this one called? What is the common advice/approach here?
MEMORY
table type mentioned above, I guess it utilises the same logic as memcached right?