Concurrency Issues in Web based Applications

Introduction

As we know that today’s most of our applications are web based and web applications can be accessed from all over the world, when more than one user access our application at the same time concurrency issues for example deadlocks, race condition occurs, it only occurs under certain timing conditions. It is very difficult to track down it since it may disappear when you try to debug it. It may occur very rarely and these are often not detected during in house testing although we can identify them through manual or automation procedures.

History

If we look at history there were notorious concurrency bugs, one of them was Northeast blackout in 14 August 2003, which cause widespread of power outage in Unites States of America because of race condition issue in software. You can read more about this event from Wikipedia page.

https://en.wikipedia.org/wiki/Northeast_blackout_of_2003

Contemporary society is dependent on software, software runs in Airplanes, handheld devices, nuclear reactor controls, air traffic control and so on, so software is everywhere.

Identification of Concurrency Issues

First of all we should identify which software module may have concurrency issues, once we identify we can produce it. Most of the applications have these issues because developers think sequentially, so they miss them.

  1. Employee Termination

If we send multiple requests at the same time to terminate an employee. System will terminate the single employee for multiple times. Although we should not terminate an employee if it is already terminated with the same reason.

// Active=1, Terminated =0;

Mov A, “1”   // A=Active, “1”, employee status is checked

Add A, “0” // A=Terminate, “0” is added into a status

Mov Status, A     // A=Terminated mean “0” is updated.

  1.  Add New Hire

If our application takes a unique username for adding a new user and developer has placed the logic on saving check if username is already exists give message “Username already exists, try different one”

If Locking mechanism is not implemented and two different user sent the saving request of same username at the same time, system will same them, because two requests were received by procedure and it saved to DB.

First Name: Stephen

Last Name: Synder

UserName: US

First Name: John

Last Name: Doe

UserName: US

  1. Concurrent updating the info

If an employer tries update the employee info and at the same time a manager tries to delete that user, then concurrency issues will arise in below cases.

  • If first request received by procedure is for deletion and then updating request received then an employer will face an exception.
  • If first request received by procedure is for updating and then deletion request received then a procedure will be executed correctly.
  •  If both requests were received at the same time even without delay of a milli seconds then race condition will occur and time out will happen.

4.Concurrent TransactionsSimple procedureBalance=Balance+amount;

  • }
  • Customer #1
  • {
  • Void deposit (int amount)
  • Let’s take an example of two customers one sends $13 and other sends $10 at the same time. Customer# 1 has account balance $100 and Customer# 2 has account balance $150.
  • If multiple users at the same time save the transactions then wrong balance will be shown if locking mechanism is not implemented.
  • Mov A, Balance   // A=100, balance is checked.
    Mov B, Balance    //B=150, balance is checked.
    Customer#1 balance will be check and it will be $100 and will move to register A and if context switch happen then  
    •        And then on context switch $160 will be added in register B,
    •        On context switch $113 will be moved to Customer #2 Balance instead of $160.
    Case#2
    • Customer #1 balance will be checked, which will be $100,
    •       On next context switch $160 will be added in register B.
    •       On context switch $160 will be moved to Customer #2 Balance
    •      On context switch $160 will be moved to Customer #1 Balance instead of $113.
  •       And then on context switch $113 will be added in register A,
  •        On context switch $113 will be moved to Customer #1 Balance
  •        On next context switch $113 will be added in register A
  •       Customer #2 balance will be checked, which will be $150,
  • Mov Balance, A    // A=113, balance is updated.
  • ADD A, amount     // A=113, $13 is deposited into a balance
  • Online Banking Simple Scenario:Suppose you have a $25 in your account and we want to pay an electricity and Broadband bill but electricity bill is $10 and Broadband internet bill is $15. As you have $25 in your account and bill exceeds the amount $20+$10=$30.
  • If concurrency is not handled in your online bank account, what will happen if you submit two request at the same time?

Simple procedure

Void OnlineTransfer (int amount)

{

Balance=Balance-amount;

}

  1.  Electricity Bill case

Mov A, Balance   // A=25, balance is checked.

ADD A, amount   // A=25-10=15, $15 is deposited into a balance

Mov Balance, A    // A=15, balance is updated.

  1.  Broadband Internet bill case

Mov B, Balance   // A=25, balance is checked.

ADD B, amount    // A=25-15=10, $10 is deposited into a balance

Mov Balance, B   // A=10, balance is updated.

Explanation: 

Case#1

Balance will be check and it will be $25 and will move to register B and if context switch happen then

  •        Balance will be checked, which will be $25,
  • On next context switch 25-15=10, $10 will be added in register B
  • And then on context switch 25-10=15, $15 will be added in register A,
  • On context switch $10 will be updated into register B.
  • On context switch $10 will be updated into register A.

Case#2

Balance will be check, witch is $25 and will move to register A and if context switch happen then

  • Balance will be checked, which will be $25,
  • On next context switch 25-10=15, $15 will be added in register A.
  • And then on context switch 25-15=10, $10  will be added in register B,
  • On context switch $15 will be updated into register A.
  • On context switch $15 will be updated into register B.

In both cases wrong balance is updated, after paying two bills we are left in our account $15 in one case and $10 in one case although our earlier total account balance was $25.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: