I've come up with a fancy issue of synchronization in node.js, which I've not able to find an elegant solution:
I setup a express/node.js web app for retrieving statistics data from a one row database table.
- If the table is empty, populate it by a long calculation task
- If the record in table is older than 15 minutes from now, update it by a long calculation task
- Otherwise, respond with a web page showing the record in DB.
The problem is, when multiple users issue requests simultaneously, in case the record is old, the long calculation task would be executed once per request, instead of just once.
Is there any elegant way that only one request triggers the calculation task, and all others wait for the updated DB record?