SlideShare a Scribd company logo
1 of 8
A Green Solution
that fixed a real-life Race Condition problem
by Kai Zhou
1
Problem
Production system now lacks functionality to provide concurrent customers
assuredly different set of numbers for their number selection.
So there might be confliction that if two or more customers both have chosen
number N within a same time span which is considerably short, any customer
might fail to order the number N because it may have been assigned to
another customer a moment ago.
Requirement
Improve “Get Available Number List” service module, make it return different
number-lists when it is called concurrently so that any certain number will not
be presented in more than one returned number-lists within a short time
span which can be defined as lock period.
2
Solution
The solution uses RDBMS’ inherent features such as
• Row Level Lock;
• Primary Key constraint
to resolve the problem described above.
An Oracle PL/SQL implementation is presented in later slides. The same logic
can also be written in other languages such as Pro*c, Java etc with the SQL.
And the final delivery can be a C library or a Web Services etc., depends on
the tech circumstances where the solution is implemented.
3
this slide unless you have read the script from following slides and want
to have a test
TEST
Take the following steps:
1) Create table available_phone_number_pool ( phone_number numeric(10) primary
key ) ; then insert 1000 records into it.
---- This table will mock the existing table in the production system that contains
available phone numbers.
2) Create table phone_number_selection_history ( phone_number numeric(10)
primary key, last_lock_time datetime)
---- This is the new table introduced to the production to help with the solution.
3) Use SQL*Plus to run the script concurrently multiple times from different terminal and
observe the screen output of their execution.
4
/* The Script -- non-conflict number selection */
set serveroutput on size 1000000
var v_c number
declare
cursor c_get_number_selection_list is
select num_pool.phone_number, num_select.last_lock_time
from available_phone_number_pool num_pool,
phone_number_selection_history num_select
where num_pool.phone_number=num_select.phone_number (+)
and (
num_select.last_lock_time is null
or
num_select.last_lock_time < sysdate - 10/(60*24)
);
v_rec c_get_number_selection_list%rowtype;
v_count int;
5
/* To be continued */
Implementation
begin
dbms_output.put_line('hellow world!');
v_count:=0;
open c_get_number_selection_list;
loop
<<next>>
fetch c_get_number_selection_list into v_rec;
if c_get_number_selection_list%notfound then
goto out;
end if;
if v_rec.last_lock_time is null then
begin
insert into phone_number_selection_history
values ( v_rec.phone_number, sysdate );
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line(‘Number ‘ ||v_rec.phone_number ||‘ is skipped ');
goto next;
end;
dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’);
commit;
v_count:=v_count+1;
/* to be continued */ 6
else
begin
update phone_number_selection_history
set last_lock_time= sysdate
where phone_number= v_rec.phone_number
and last_lock_time < sysdate - 10/(60*24) ;
exception
when NO_DATA_FOUND then
dbms_output.put_line (‘Number '|| v_rec.phone_number || ‘ is skipped’);
goto next;
end;
dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’);
commit;
v_count:=v_count+1;
end if;
if v_count=10 then
goto out;
end if;
end loop;
<<out>>
end;
7
Why it is GREEN ?
A costly solution may request a new column to be added to the
existing production DB table ( which is obviously bad idea), or it
may generate temporary DB data which keeps growing thus a
clean-up job must be added to production’s operation schedule,
or it may hire too complicated techs to resolve concurrency
problem … resulted stability will be questioned.
However with our green solution, apart from the necessary
Service Module change (which implements the logic described
above ), only one new DB table is added to the production, and it
does NOT need initialization or cleanup hence causing no extra
maintenance cost. More importantly, the resulted stability is
predicted as the logic is simple and natural.
8

More Related Content

What's hot

Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Thuan Nguyen
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSmohdoracle
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Thuan Nguyen
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handlingSmitha Padmanabhan
 

What's hot (20)

Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
PLSQL
PLSQLPLSQL
PLSQL
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
Function and types
Function  and typesFunction  and types
Function and types
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 

Similar to A green solution to solve a race condition problem

Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.comHarrisGeorg12
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comStephenson22
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comWilliamsTaylorza48
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Abhishek Bose
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newLast7693
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newscottbrownnn
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab neweyavagal
 
M150 A Fall2010 T01
M150 A Fall2010 T01M150 A Fall2010 T01
M150 A Fall2010 T01abdalodainat
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 handseyavagal
 

Similar to A green solution to solve a race condition problem (20)

Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should Know
 
Oopp Lab Work
Oopp Lab WorkOopp Lab Work
Oopp Lab Work
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Express 070 536
Express 070 536Express 070 536
Express 070 536
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
M150 A Fall2010 T01
M150 A Fall2010 T01M150 A Fall2010 T01
M150 A Fall2010 T01
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Matopt
MatoptMatopt
Matopt
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 hands
 
overview of c#
overview of c#overview of c#
overview of c#
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

A green solution to solve a race condition problem

  • 1. A Green Solution that fixed a real-life Race Condition problem by Kai Zhou 1
  • 2. Problem Production system now lacks functionality to provide concurrent customers assuredly different set of numbers for their number selection. So there might be confliction that if two or more customers both have chosen number N within a same time span which is considerably short, any customer might fail to order the number N because it may have been assigned to another customer a moment ago. Requirement Improve “Get Available Number List” service module, make it return different number-lists when it is called concurrently so that any certain number will not be presented in more than one returned number-lists within a short time span which can be defined as lock period. 2
  • 3. Solution The solution uses RDBMS’ inherent features such as • Row Level Lock; • Primary Key constraint to resolve the problem described above. An Oracle PL/SQL implementation is presented in later slides. The same logic can also be written in other languages such as Pro*c, Java etc with the SQL. And the final delivery can be a C library or a Web Services etc., depends on the tech circumstances where the solution is implemented. 3
  • 4. this slide unless you have read the script from following slides and want to have a test TEST Take the following steps: 1) Create table available_phone_number_pool ( phone_number numeric(10) primary key ) ; then insert 1000 records into it. ---- This table will mock the existing table in the production system that contains available phone numbers. 2) Create table phone_number_selection_history ( phone_number numeric(10) primary key, last_lock_time datetime) ---- This is the new table introduced to the production to help with the solution. 3) Use SQL*Plus to run the script concurrently multiple times from different terminal and observe the screen output of their execution. 4
  • 5. /* The Script -- non-conflict number selection */ set serveroutput on size 1000000 var v_c number declare cursor c_get_number_selection_list is select num_pool.phone_number, num_select.last_lock_time from available_phone_number_pool num_pool, phone_number_selection_history num_select where num_pool.phone_number=num_select.phone_number (+) and ( num_select.last_lock_time is null or num_select.last_lock_time < sysdate - 10/(60*24) ); v_rec c_get_number_selection_list%rowtype; v_count int; 5 /* To be continued */ Implementation
  • 6. begin dbms_output.put_line('hellow world!'); v_count:=0; open c_get_number_selection_list; loop <<next>> fetch c_get_number_selection_list into v_rec; if c_get_number_selection_list%notfound then goto out; end if; if v_rec.last_lock_time is null then begin insert into phone_number_selection_history values ( v_rec.phone_number, sysdate ); exception when DUP_VAL_ON_INDEX then dbms_output.put_line(‘Number ‘ ||v_rec.phone_number ||‘ is skipped '); goto next; end; dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’); commit; v_count:=v_count+1; /* to be continued */ 6
  • 7. else begin update phone_number_selection_history set last_lock_time= sysdate where phone_number= v_rec.phone_number and last_lock_time < sysdate - 10/(60*24) ; exception when NO_DATA_FOUND then dbms_output.put_line (‘Number '|| v_rec.phone_number || ‘ is skipped’); goto next; end; dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’); commit; v_count:=v_count+1; end if; if v_count=10 then goto out; end if; end loop; <<out>> end; 7
  • 8. Why it is GREEN ? A costly solution may request a new column to be added to the existing production DB table ( which is obviously bad idea), or it may generate temporary DB data which keeps growing thus a clean-up job must be added to production’s operation schedule, or it may hire too complicated techs to resolve concurrency problem … resulted stability will be questioned. However with our green solution, apart from the necessary Service Module change (which implements the logic described above ), only one new DB table is added to the production, and it does NOT need initialization or cleanup hence causing no extra maintenance cost. More importantly, the resulted stability is predicted as the logic is simple and natural. 8