I have the following problem:
I have a multithreaded server-side application, where every request executes a new thread (the standard case). Based on this request, the application checks if the data needed is cached in the DB already. If yes, it fetches it and sends it back to the client. If not, a remote service is called the response is stored in the DB and then fetched to be returned to the client.
The service takes a while to calculate the data, and since the requests to my app are executed asynchronously, I fall into the trap of one request checking the DB, seeing that there is nothing there, then calling the service. Meanwhile however, another request with the same input would check the DB, and since the service takes a while, there still would be nothing there … so it would initiate another call to the service. As a result, the data gets written to DB twice, which screws up any successing operations, relying on its uniqueness (there can’t be two records with the same data)
What solution should I choose?
One idea would be to put specific UNIQUE field constraints in the DB. This way, even if the app tries to write twice, the DB will simply reject it, and the app will throw an exception. However, the service will still be executed multiple times.
Another solution that came to my mind would be to synchronize the method, which calls the web service. This way, every successive request to my app will be put on a waiting stack before the previous operation is finished. This way, if another request with the same input data comes in, while the first one is still waiting for the results from the remote service, it will sit and wait for the operation to be finished. Then, as the second request checks the DB, the data will already be there, and there will be no need to call the serivice again. This will also prevent from having multiple identical records in the DB. The problem with this is that the speed of my server application will be reduced inevitably, because EVERY request will have to wait, even the ones which normally shouldn’t (the ones, for which supposedly we already have data in the DB)
Any other suggestions? I am stuck. How can I implement some sort of conditional synchronization?
You could just synchronize the calculation part, i.e. use double checked locking: