I am building my own analytics platform, which I want to work like this...

  • A JS script in each of my webpages collects data about users' clicking behaviours and sends that data to a PHP (or other convenient language) script on my server. This server-side script receives the data from the first AJAX request and stores it in a variable.

  • Then, since there is another AJAX request waiting, the server-side script receives the data from that AJAX request and stores that in a variable.

  • Then, when not many AJAX requests are waiting, the server-side script takes the data which is now stored in variables and inserts it into a MySQL (or similar) database.

  • Finally, the server-side script removes all of those variables from memory (because they are no longer needed), and waits for more AJAX requests to come in.

What is the most simple way to accomplish what I have described here?

2 Answers 2


For almost any major programming language, there are existing queue components which can help you to implement an asynchronous execution of arbitrary tasks, which is what you described in your question. Better don't reinvent the wheel. For example, for PHP, this older stackoverflow Q&A gives you several alternatives. You will find more options by googling for "php asynchronous task". Or you use something like node.js, which heavily encourages usage of asynchronous callbacks.


This sounds like premature optimization. It is actually built into the network stack that connections queue up until the server is ready to process so no need to reinvent the wheel.

Just inserting one row into mysql is very fast. There is no difference with regards to the time it takes to insert one row to mysql or pushing to a persistent queue.

If you have actual performance problems you may consider using something else than php which still uses a thread model. Newer platforms like nodejs and asp.net(with async/await) utilize a async model which don't occupy a thread while waiting but instead uses callbacks leaving the thread free to process other requests while e.g writing to databases.

  • In .NET and most other compiled languages you could even use async with a threadpool if this is a high-perf solution. Or does ASP.NET handle that itself without explicit coding?
    – jaskij
    Sep 13, 2019 at 8:30
  • I believe that is the default behaviour in asp.net Sep 13, 2019 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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