Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:24:56+00:00 2026-05-27T18:24:56+00:00

I have the following problem: I have a multithreaded server-side application, where every request

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:24:57+00:00Added an answer on May 27, 2026 at 6:24 pm

    You could just synchronize the calculation part, i.e. use double checked locking:

    boolean dataExists = checkDB();
    if( !dataExists ) {
      synchronized( someSharedObject ) {
        //another thread might have been waiting too and now has put the data into the DB
        //thus check again
        if( !checkDB() ) {
          calculateDataAndWriteToDB();
        }  
      }
    }
    //do whatever you want with the existing data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem: Multithreaded WPF application, Model View Presenter Implementation. Presenters and
I have following problem. I have created a tab based Application with three Views
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem: I want to check (C#) if a thread has finished
I have following problem. I want to create a file system based session storage
I have following problem: My webservice application returns xml data in following order: <my_claims>
I have a multithreaded application that runs using a custom thread pool class. The
I have following problem Bundle bundle = new Bundle(); bundle.putString(url, someUrl); This works fine.
I Have following problem: class Request<T> { private T sw; public Request() { //How
I have following problem, Code: String a=Yeahh, I have no a idea what's happening

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.