Skip to content

Suppose You Want to Use Multiple Requests at The Same Time without Inconsistency Issues

Emory's eyes are locked onto their monitor. It is as if Emory is looking at the screen, but at the same time it is like they are looking through it. For the last 30 minutes, the following sequence of events have been repeating: First, Emory would type something, click the mouse, and look hopefully at the monitor awaiting some sort of result. Sometimes the outcome is the rather preferable one, relieving them from the nightmare that has haunted them many times. That is not always the case. Emory's expression, as usual, does not change much. It's still the same cold expression you are familiar with. However, you know something within Emory has changed. Having to work with Emory for months has given you the ability to read Emory like a book. You can tell what they are thinking purely through perceiving their face and tension. Each has a different meaning. They must have a problem right now. As you take a peek at their monitor, your body shivers. It's the prototype application you have just completed. This is clearly not a good sign.

"Could you come here for a second?" The voice coming from Emory's desk made you tremble.

Seconds ago, you realized how bad the situation you are in, and now it's getting worse and worse. Riley, who supposedly has been sitting beside you a moment ago, is now gone without a trace.

"Also, bring Riley here.I know you are there." Emory added. You hear a crash in other room as you hear Emory's command. You can imagine Riley had tried to run away from the responsibility. This can't get any worse right... You silently wish. At least you are not facing the wrath of your coworker alone.

"Can you see my screen?" Emory asks you to see the screen, while Riley slowly shuffles towards Emory's desk.

You see a bunch of duplicated entries accross the screen. Emory then executes several other operations for five more times. Sometimes the entries are working as intended, and in others some entries are duplicated. It's not consistent. You take a guess on the source of the problem.

"Isn't this a race condition?"

"So, you do know." Emory replies. "The test did pass, but there is a bug in multithreading. It wasn't there in the last version. What happened?"

"Riley and I had an idea that time. So we proposed a prototype that utilized multithreading to do parallel job. So I think that's the cause. "

Emory sighs. "You should asked me or Alex for a review first. "

"We are so sorry. We thought we need to make it quick. But..We will take the responsibility! " Riley exclaims. They who always have kept their mouth shut finally starts to talk. You think Riley, the original proposer of the idea felt responsible. After all, you only coded what Riley asked.

Emory then boots their IDE and opens the aforementioned problematic code. They look at it for some minutes before taking out a piece of paper.

"Let's fix it." Emory suggested."It might be you and Riley's fault here. Normally that is the case. Our manager will see it like that. But.. I dislike that kind of system."

You and Riley are surprised to hear Emory's words. Both of you expected scolding from the team lead for ignoring their authority because of the urgency for the change. On the contrary, Emory is willing to help.

"I think a mistake made by a member of the team is the responsibility of the team. Don't be surprised. As I said before, I hate the idea of blaming just one person."

That line, and also that attitude. You have seen time and time again, and that is what you admire about Emory. It's not fear that Emory uses to rule. It's responsibility and respect.

"Shall we start?" Riley proposes.

Emory does not respond, as usual. They simply turn their eyes to the monitor and starts writing out some possible problems.

The Race Condition and Critical Section

"As you have guessed, this problem is called the race condition. Basically, multiple threads are trying to operate on a section at the same time. The program normally guarantees the correctness based on the correct order of execution. That's not the case with the race condition. Since multiple threads are accessing the same part at the same time, we can't really say for sure what the execution order is like at that point. " Emory starts to explain the state of the program right now. The condition known as the race condition is a well known concurrency issue.

"The tests can't detect them and I also don't know where the issue is coming from. How can we spot the source of problem?" Riley asked.

"I think the use of an unsafe thread operation is the source. " You try to answer.

Emory writes something on the paper as they comment on your statement just now. "If it is an atomic operation, it's easy to spot. I believe you and Riley know about critical sections. It's a section where the multi thread access breaks their operation and data integrity. Most of the time it's related to data access and data write. "

"So it's like two or more threads are writing data at the same time or there is a thread who tries to read data from an incomplete operation by the other thread. That data might be rolled back by the other thread but that one thread might have already used the incomplete data on their operation. "

"So a solution would be identifying the critical section and limit the access to that section? "

"Correct. " Emory replies. "It's called synchronization. This concept simply only allows one thread to access a critical section at one time. After the operation is finished, that section is released and other threads will able to work on it. The process repeats until all threads' operations are finished. "

"There are some possibility of the critical section in our application. We might need to analyze it and try it one by one. "

"It's sounds like exhausting work, but it's what it is. "

Emory stands up and walks in the direction of the other room, leaving you with a curious Riley. "I will talk to Rory." Emory says. "Can I leave this matter to you?"

"I will go with you. " You propose. Seeing your superior take the responsibility of your own mistake, you can't sit still here while imagining what would happen to Emory in the other room.

"Stay here. " Emory rejects your intention and walks away, leaving you feeling devastated.

"Let's fix this problem as soon as possible. " Riley suggests. Riley walks to their table without looking at you. They must feel even worse than you. After all it's their idea that led to this mess. Knowing what kind of person Emory is, you could imagine how many times this has happened before. You didn't want to rely on Emory's kindness forever. You have always wanted to help them. That cold superior that have saved you time and time again since the first day you have been here. You can't do anything about this situation except fix your own mess.

Work Allocation System

Like the previous application you helped fix, this application creates new jobs and divides them based on the competence needed. However, this application creates multiple jobs at once, allowing for increased productivity. It also utilizes multi-threading on job service to handle multiple jobs at once. This is likely where the application failed.

There are several job types available to be created, they are:

  • Coding Jobs
  • Design Jobs
  • Security Jobs

You can create new jobs at /employee-list/ by clicking the "add another job" button. Select the category and the name you want then press add.

Each job type can only be solved by certain employees with the following rule

  • Frontend employees can only do coding and design jobs
  • Backend employees can only do coding and security jobs
  • Fullstack employees can do everything, be it coding, design, and security.

As such, to test the application, you need to check it with coding jobs to make sure it all goes well. The order of the job does not matter in this case, but it is important to not have duplication nor have a job missing from the list.

Tasks

  • Identify the critical section. Write your argument why the said section is the critical section in README.md
  • Fix the concurrency issue(s) and describe your solution in the README.md
  • Make sure there are no other concurrency issues
  • Please answer these questions in the README.md
    • What is concurrency control?
    • Using your own words, explain what is the critical section?
    • What is race condition? In this practice, what is causing the race condition? Explain other concurrency issues that you know besides race condition.

Optional Tasks

  • Fix some code quality issues in the program
  • Testing: add some tests for the service package. Currently, not all methods have tests.

Hints

  • The order of the input doesn't matter. However, make sure no duplicate entries or inconsistent data presents.

Last update: 2022-03-01 20:49:09
Created: 2022-03-01 20:49:09
Back to top