r/learnjava • • Jul 24 '21

[Concurrency] What will you do if multiple users access your application at the same time?

I'm an experienced Software Engineer but very weak in concurrency because of no prior experience in that. I've been interviewing with several companies in which I was asked similar kind of questions as given below:

  1. If you are designing a fantasy sports application in which there's a contest which can handle only 100 users. If 99 users are already registered for the contest and multiple users hit the PARTICIPATE button at the same time to become the 100th user, then how will you handle this in your application?

  2. If you are designing a chess game, where multiple users are selecting the users of same level of competence to play with them. Then suppose at same time, users A and B choose the user C (to play with) at the same time, then how will you handle this?

I usually answer this by saying that I'll use synchronized block in Java or on Database side, I'll use Locking concept. But I'm not sure of either of them. So, can anyone tell here how do you answer such questions? Should one answer this in terms of Java Multithreading or DBMS or both? Please can anyone also tell me any resources to understand the theory to answer such type of questions or any website or YouTube channel where solutions are given to these kind of questions?

58 Upvotes

11 comments sorted by

4

u/mairtinomarta Jul 24 '21
  1. Queue FIFO

  2. Queue FIFO

4

u/loomynartylenny Jul 24 '21

been a hot minute since I last did synchronized stuff, but this is what I'd do:

  • The server would already have different ClientHandler threads running for each client currently on the application (so like as soon as they connected, before they even hit the 'participate' or 'play chess' button, there's a thread running for that client)

  • These ClientHandler threads would all run with do/while loops, waiting for a request (ClientRequest) to come in from the client, and then returning the result of that request to the client.

  • These threads would all communicate with a single main server object (maybe we call it the RequestHandler or something idk), and would have a whole load of methods in synchronized blocks for dealing with the various requests that can come in, called by the ClientHandler upon recieving a request from the client.

  • So, for the participate example: we have a bool participate() method.

    • For ease of explanation, this will simply return true if the client can participate, false otherwise (in practice, we may need to consider passing the actual ClientHandler and ClientRequest objects to this method, and perhaps returning a Response object with info about the actual response that we can send back to the client).

  • So, in the bool participate() method, we'd first declare a local success bool to return at the end of the method (this doesn't need to be synchronized). Then, in a synchronized block, we'd see if we already have 100 participants. If we don't have 100 participants, we can participate, so we'd add this client to the participants, and we'd know that the success is true. Otherwise, if we already have 100 participants, we're full, so the success would be false. Then, we'd end the synchronized block, and return the response to the thread that called this. And then we can move on to the next thread.

  • So, when we have 99 participants already, the first thread that actually reaches the synchronized block in the participate() method will successfully be added as a participant. All the others will fail.

Here's a particularly abstracted version of the RequestHandler, for the sole purpose of demonstrating the whole synchronization stuff

package server;
 /* A particularly abstracted version of the RequestHandler, which all of the ClientHandlers would attempt to call the 'participate()' method of. */ 
class RequestHandler{
    
    /* how many people are currently participating */
    private int participants;
    
    /* for synchronization purposes */
    private final Object syncObject = new Object();
    
    /* 0 participants. */
    RequestHandler(){
        participants = 0;
    }
    
    /*
    attempts to see if we can participate.
    if we don't have 100 participants, we increment participants and return true.
    if we already have 100, we're full, so they can't participate
    @returns true if we are in the first 100, false otherwise
    */
    bool participate(){
        bool success = false;
        synchronized (syncObject){
            // fancy code for actually handling the request should go in here tbh
            if (participants > 100){
                participants++;
                success = true;
            }
        }
        return success;
    }
    
}

And yes, in practice, the participate() method would need to do quite a bit more. But the basic 'put the important stuff within the synchronized block' should get the job done.

2

u/mw52588 Jul 24 '21

At work one of the biggest challenges is how to handle state transitions. It's not enough to just put in logic to handle transactional writes at the database or put in some synchronized logic. Also, transactional writes in like dynamoDb are not immediately propagated and does not guarantee it's in the database. It's not 100% going to alleviate all race conditions. What if someone patches the same thing twice at different times? How do you know if they actually meant to duplicate the item or if it was a retry? You may end up with duplicate items that you don't want. What if you have multiple services patching the same thing but are racing against each other? How can you guarantee that it will only write once?

One possible solution is to compare etags which can help ensure that you are only writing once and any additional writes will be rejected with a preconditioned failure. This is called idempotency. The etag that is being used is now not valid anymore. Therefore we know the item was already updated and not current anymore. Update to a new etag once the database has been updated.

3

u/novaa31 Jul 24 '21

Following.

3

u/khooke Jul 24 '21

"multiple users hit the PARTICIPATE button at the same time to become the 100th user, then how will you handle this"

There are two aspects to this type of question:

- how do you handle this from a user experience point of view - what does the user see? how do you communicate that there are no more entries remaining? how do you track this?

- how do you handle multiple users accessing a resource with finite capacity, e.g. only 100 available contest entries

If you approach this type of question by jumping straight in and talking about concurrency and other techniques, as an interviewer I'll make a note that while technically capable, you missed the big picture aspects to the question.

How you answer this type of question would give me a good idea whether as a developer can you implement solutions given a problem and some guidance with a preferred approach, verses whether you evaluate pros and cons of solutions from end to end, looking at the big picture as an experienced developer/architect.

There's no right or wrong answer btw, this type of question is looking at how you approach a problem.

1

u/Danji1 Jul 25 '21 edited Jul 25 '21

I'm an experienced Java dev but funnily enough have very little experience with multithreading barring spinning up some threads for simple I/O and basic syncronising and locking.

My suggestion would be one of:

  • Use some sort of durable messaging bus like Kafka/AMQ.

  • Transaction locking at DB level.

0

u/[deleted] Jul 24 '21

[deleted]

3

u/10-kinds-of-people Jul 24 '21

I'm interested why you wouldn't synchronize the method that picks the user rather than lock at the DB level. How does the Business Logic know "who won"?

7

u/[deleted] Jul 24 '21

How do you synchronize something across multiple machines ?

1

u/nutrecht Jul 25 '21

I'm interested why you wouldn't synchronize the method that picks the user

Because that will completely break if you deploy multiple instances of your service, which is the default in Java back-ends nowadays.