SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Concurrent Transactions
          Alan Medlar

      amedlar@cs.ucl.ac.uk
Concurrent Transactions
• We want our database to be accessible to
  many clients concurrently
 • Multiple processors or cores accessing
    same storage (containing whole database)
 • Multiple processors distributed over a
    network each with local storage (each
    holding a portion of the database)
Concurrent Transactions

• To increase performance we want to allow
  multiple database transactions, being
  processed on separate processors, to take
  place at once
• Run into problems when two (or more) of
  these transactions want to access the same
  data!
Schedules


• Concerned with the order of execution
• Consider two transactions, T and T :
                            1      2
T1          T2           T1            T2
 read(X)                                read(X)
X = X-100                             tmp = X*0.1
 write(X)                              X=X-tmp
 read(Y)                                write(X)
Y = Y+100                               read(Y)
 write(Y)                              Y=Y+tmp
              read(X)                   write(Y)
            tmp = X*0.1    read(X)
             X=X-tmp      X = X-100
              write(X)     write(X)
              read(Y)      read(Y)
             Y=Y+tmp      Y = Y+100
              write(Y)     write(Y)
Schedules

• Schedules of T  and T2 are serial due to
                1
  one transaction finishing before the other
• If Tand T2 were to happen at the same
     1
  time, forcing serialisation would limit
  performance!
T1          T2
 read(X)
X = X-100
 write(X)
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2
                         read(X)
                        X = X-100
                         write(X)
Despite these                         read(X)
transactions not                    tmp = X*0.1
being serialised, the                X=X-tmp
schedule ensures the                  write(X)
resulting database is    read(Y)
consistent              Y = Y+100
                         write(Y)
                                      read(Y)
                                     Y=Y+tmp
                                     write(Y)
T1          T2
 read(X)
X = X-100
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2
                        read(X)
                       X = X-100
                                     read(X)
Here the database
                                   tmp = X*0.1
gets into an
                                    X=X-tmp
inconsistent state
                                     write(X)
because the write(X)
                        write(X)
of T2 is lost due to
                        read(Y)
being overwritten by
                       Y = Y+100
T1!
                        write(Y)
                                     read(Y)
                                    Y=Y+tmp
                                    write(Y)
Conflict Analysis
•   What went wrong in the example?
    •   Ordering of reads and writes caused an inconsistency
Conflict Analysis
•   What went wrong in the example?
    •   Ordering of reads and writes caused an inconsistency
•   Use conflict analysis to identify the cause of the
    problem...
    •   Intuition: reads and writes applied in the same order as
        a serial schedule will always result in a consistent state
    •   Examine the schedule and try to rearrange them into
        the same order as a serial schedule by swapping
        instructions (paying special attention to “conflicts”)
Conflicts
• Looking consecutive reads and writes:
  • If they access different data items, they can
     be applied in any order
  • If they access the same data items, then
     the order of operations may be
     important, i.e. they may conflict
Conflicts
• Looking consecutive reads and writes:
  • If they access different data items, they can
     be applied in any order
  • If they access the same data items, then
     the order of operations may be
     important, i.e. they may conflict
• (This is simplified! We are not considering
  insertion and deletion)
Conflict Rules
•   In a schedule (only reads and writes) for consecutive
    instructions i1 and i2 :
    •   i1 = read(x), i2 = read(x) : no conflict
    •   i1 = read(x), i2 = write(x) : conflict
        (ordering dictates whether i1 gets value of write or
        previous state)
    •   i1 = write(x), i2 = read(x) : conflict
    •   i1 = write(x), i2 = write(x) : conflict
        (writes do not affect one another, but will affect next
        read)
Conflict Serialization
•   If a schedule S is transformed into schedule S’
    by a series of non-conflicting instruction
    swaps, the S and S’ are conflict equivalent
•   A schedule is conflict serializable if it is conflict
    equivalent to a serial schedule
•   A schedule that is conflict serializable will
    produce the same final state as some serial
    schedule and will leave the database in a
    consistent state
T1          T2
 read(X)
X = X-100
 write(X)
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2
 read(X)
X = X-100
 write(X)
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2
 read(X)
X = X-100
 write(X)
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2            T1          T2
 read(X)                   read(X)
X = X-100                 X = X-100
 write(X)                  write(X)
              read(X)      read(Y)
            tmp = X*0.1   Y = Y+100
             X=X-tmp       write(Y)
              write(X)                  read(X)
 read(Y)                              tmp = X*0.1
Y = Y+100                              X=X-tmp
 write(Y)                               write(X)
              read(Y)                   read(Y)
             Y=Y+tmp                   Y=Y+tmp
             write(Y)                   write(Y)
T1          T2
 read(X)
X = X-100
              read(X)
            tmp = X*0.1
             X=X-tmp
              write(X)
 write(X)
 read(Y)
Y = Y+100
 write(Y)
              read(Y)
             Y=Y+tmp
             write(Y)
T1          T2
                        read(X)
                       X = X-100
                                     read(X)
These instructions
                                   tmp = X*0.1
cannot be swapped
                                    X=X-tmp
because they are in
                                     write(X)
conflict!
                        write(X)
This schedule cannot    read(Y)
be re-ordered into a   Y = Y+100
serial schedule...      write(Y)
                                     read(Y)
                                    Y=Y+tmp
                                    write(Y)
Serializability

• Paramount correctness criteria for
  concurrent database transactions
• Ensures isolation between transactions
• Ensures consistency despite other
  transactions
Locking
•   We can get a serialisable schedule by
    enforcing mutual exclusion on data items: this
    could be achieved using simple locking
    •   shared lock - to read data items, can only
        be granted if there are either no locks or
        only shared locks on a data item
    •   write lock - for writing, can only be granted
        if there are no other locks on the data item
T1             T2
write_lock(X)
   read(X)
 X = X-100
  write(X)
 unlock(X)
                write_lock(X)
                   read(X)
                tmp = X*0.1
                 X=X-tmp
                  write(X)
                 unlock(X)
write_lock(Y)
   read(Y)
 Y = Y+100
  write(Y)
 unlock(Y)
                write_lock(Y)
                   read(Y)
                 Y=Y+tmp
                  write(Y)
                 unlock(Y)
T1             T2               T1             T2
write_lock(X)                   write_lock(X)
   read(X)                         read(X)
 X = X-100                       X = X-100
  write(X)                                      write_lock(X)
 unlock(X)                                         read(X)
                write_lock(X)                   tmp = X*0.1
                   read(X)                       X=X-tmp
                tmp = X*0.1                       write(X)
                 X=X-tmp                         unlock(X)
                  write(X)        write(X)
                 unlock(X)       unlock(X)
write_lock(Y)                   write_lock(Y)
   read(Y)                         read(Y)
 Y = Y+100                       Y = Y+100
  write(Y)                        write(Y)
 unlock(Y)                       unlock(Y)
                write_lock(Y)                   write_lock(Y)
                   read(Y)                         read(Y)
                 Y=Y+tmp                         Y=Y+tmp
                  write(Y)                        write(Y)
                 unlock(Y)                       unlock(Y)
T1             T2               T1               T2
write_lock(X)                   write_lock(X)
   read(X)                         read(X)
 X = X-100                       X = X-100
  write(X)                                        write_lock(X)

                                             s!
 unlock(X)                                           read(X)
                                         ail
                                       f
                write_lock(X)                     tmp = X*0.1
                                     k
                                   oc
                   read(X)                         X=X-tmp
                                 l
                tmp = X*0.1                         write(X)
                 X=X-tmp                           unlock(X)
                  write(X)        write(X)
                 unlock(X)       unlock(X)
write_lock(Y)                   write_lock(Y)
   read(Y)                         read(Y)
 Y = Y+100                       Y = Y+100
  write(Y)                        write(Y)
 unlock(Y)                       unlock(Y)
                write_lock(Y)                     write_lock(Y)
                   read(Y)                           read(Y)
                 Y=Y+tmp                           Y=Y+tmp
                  write(Y)                          write(Y)
                 unlock(Y)                         unlock(Y)
Problem: Immediate Modification



• If during a transaction our database uses
  immediate modification we have a
  problem, transactions are not isolated!
T1                  T2
write-lock(P)
   read(P)
 P = P - 100
  write(P)
  unlock(P)
                    read-lock(P)
                      read(P)
                     unlock(P)
                    read-lock(Q)
                      read(Q)
                     unlock(Q)
                printf(“%dn”, P + Q)
write-lock(Q)
  read(Q)
Q = Q + 100
  write(Q)
 unlock(Q)
T1                  T2

                                                  }
                                        P = 100
                                                      150
write-lock(P)
                                        Q = 50
   read(P)
 P = P - 100
  write(P)
  unlock(P)
                    read-lock(P)
                      read(P)
                     unlock(P)
                    read-lock(Q)
                      read(Q)
                     unlock(Q)
                printf(“%dn”, P + Q)
write-lock(Q)
  read(Q)
Q = Q + 100
  write(Q)
 unlock(Q)
T1                  T2

                                                  }
                                        P = 100
                                                      150
write-lock(P)
                                        Q = 50
   read(P)
 P = P - 100
  write(P)

                                                  }
                                        P= 0
                                                      50
  unlock(P)
                                        Q = 50
                    read-lock(P)
                      read(P)
                     unlock(P)
                    read-lock(Q)
                      read(Q)
                     unlock(Q)
                printf(“%dn”, P + Q)
write-lock(Q)
  read(Q)
Q = Q + 100
  write(Q)
 unlock(Q)
T1                  T2

                                                  }
                                        P = 100
                                                      150
write-lock(P)
                                        Q = 50
   read(P)
 P = P - 100
  write(P)

                                                  }
                                        P= 0
                                                      50
  unlock(P)
                                        Q = 50
                    read-lock(P)
                      read(P)
                     unlock(P)
                    read-lock(Q)
                      read(Q)
                     unlock(Q)
                printf(“%dn”, P + Q)
write-lock(Q)
  read(Q)
Q = Q + 100
  write(Q)

                                                  }
                                        P= 0
                                                      150
 unlock(Q)
                                        Q = 150
T1                  T2
write-lock(P)
   read(P)
 P = P - 100
                                         Solution: release locks
  write(P)

                                           as late as possible!
write-lock(Q)
  read(Q)
                                        (ensures serial schedule,
Q = Q + 100

                                        but potential deadlocks!)
  write(Q)
 unlock(Q)
  unlock(P)
                    read-lock(P)
                      read(P)
                    read-lock(Q)
                      read(Q)
                     unlock(Q)
                     unlock(P)
                printf(“%dn”, P + Q)
2-Phase Locking
•   One of many methods to ensure serializability


•   Growth phase: transactions can only acquire
    locks
•   Shrinking phase: transactions can only release
    locks
•   Once a transaction has started to release locks
    it cannot acquire anymore...
T1                  T2
T1 Growth Phase      write-lock(P)
                        read(P)
                      P = P - 100
                       write(P)
                     write-lock(Q)
                       read(Q)
                     Q = Q + 100
                       write(Q)
T1 Shrinking Phase    unlock(Q)
                       unlock(P)
                                                             T2 Growth Phase
                                         read-lock(P)
                                           read(P)
                                         read-lock(Q)
                                           read(Q)
                                                             T2 Shrinking Phase
                                          unlock(Q)
                                          unlock(P)
                                     printf(“%dn”, P + Q)
Deadlock

• Two or more transactions are waiting for
  locks that the other holds, hence none of
  those involved make progress
• Dealt with in different ways, not covered in
  this course...
Summary

• Most concurrent transactions have no
  conflicts: they either all access different
  data items or else only perform reads
• For the transactions that might conflict we
  can enforce serializability to maintain the
  ACID properties
Next: Distributed Transactions

Weitere ähnliche Inhalte

Was ist angesagt?

Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inference
asimnawaz54
 
Largedictionaries handout
Largedictionaries handoutLargedictionaries handout
Largedictionaries handout
csedays
 

Was ist angesagt? (18)

Prml
PrmlPrml
Prml
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Slides mc gill-v4
Slides mc gill-v4Slides mc gill-v4
Slides mc gill-v4
 
Predicates and Quantifiers
Predicates and QuantifiersPredicates and Quantifiers
Predicates and Quantifiers
 
Matrix calculus
Matrix calculusMatrix calculus
Matrix calculus
 
Harmonic Analysis and Deep Learning
Harmonic Analysis and Deep LearningHarmonic Analysis and Deep Learning
Harmonic Analysis and Deep Learning
 
Lesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusLesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of Calculus
 
Predicate & quantifier
Predicate & quantifierPredicate & quantifier
Predicate & quantifier
 
Quantifier
QuantifierQuantifier
Quantifier
 
Predicates and Quantifiers
Predicates and Quantifiers Predicates and Quantifiers
Predicates and Quantifiers
 
Lesson 16: Derivatives of Exponential and Logarithmic Functions
Lesson 16: Derivatives of Exponential and Logarithmic FunctionsLesson 16: Derivatives of Exponential and Logarithmic Functions
Lesson 16: Derivatives of Exponential and Logarithmic Functions
 
09d transform & conquer spring2015
09d transform & conquer spring201509d transform & conquer spring2015
09d transform & conquer spring2015
 
Deep generative model.pdf
Deep generative model.pdfDeep generative model.pdf
Deep generative model.pdf
 
Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inference
 
Largedictionaries handout
Largedictionaries handoutLargedictionaries handout
Largedictionaries handout
 
Slides mc gill-v3
Slides mc gill-v3Slides mc gill-v3
Slides mc gill-v3
 
1 hofstad
1 hofstad1 hofstad
1 hofstad
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 

Andere mochten auch

Database chapter 10 questions
Database chapter 10 questionsDatabase chapter 10 questions
Database chapter 10 questions
jandrewsxu
 
Ch17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theoryCh17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theory
meenatchi selvaraj
 
SQL Server Reporting Services
SQL Server Reporting ServicesSQL Server Reporting Services
SQL Server Reporting Services
Ahmed Elbaz
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
koolkampus
 

Andere mochten auch (9)

Database chapter 10 questions
Database chapter 10 questionsDatabase chapter 10 questions
Database chapter 10 questions
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
Ch17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theoryCh17 introduction to transaction processing concepts and theory
Ch17 introduction to transaction processing concepts and theory
 
protocols of concurrency control
protocols of concurrency controlprotocols of concurrency control
protocols of concurrency control
 
SQL Server Reporting Services
SQL Server Reporting ServicesSQL Server Reporting Services
SQL Server Reporting Services
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12c
 
Transaction ও Concurrent execution
Transaction ও Concurrent executionTransaction ও Concurrent execution
Transaction ও Concurrent execution
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 

Ähnlich wie 2011 Db Concurrency

Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009
akabaka12
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution book
José Antonio PAYANO YALE
 
Intro probability 2
Intro probability 2Intro probability 2
Intro probability 2
Phong Vo
 
03 truncation errors
03 truncation errors03 truncation errors
03 truncation errors
maheej
 
Emat 213 study guide
Emat 213 study guideEmat 213 study guide
Emat 213 study guide
akabaka12
 
Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)
Matthew Leingang
 

Ähnlich wie 2011 Db Concurrency (14)

Taylor problem
Taylor problemTaylor problem
Taylor problem
 
Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009
 
Hw2 s
Hw2 sHw2 s
Hw2 s
 
Koc2(dba)
Koc2(dba)Koc2(dba)
Koc2(dba)
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution book
 
stochastic processes assignment help
stochastic processes assignment helpstochastic processes assignment help
stochastic processes assignment help
 
Intro probability 2
Intro probability 2Intro probability 2
Intro probability 2
 
Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)
 
Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)
 
03 truncation errors
03 truncation errors03 truncation errors
03 truncation errors
 
1 - Linear Regression
1 - Linear Regression1 - Linear Regression
1 - Linear Regression
 
Interpolation techniques - Background and implementation
Interpolation techniques - Background and implementationInterpolation techniques - Background and implementation
Interpolation techniques - Background and implementation
 
Emat 213 study guide
Emat 213 study guideEmat 213 study guide
Emat 213 study guide
 
Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

2011 Db Concurrency

  • 1. Concurrent Transactions Alan Medlar amedlar@cs.ucl.ac.uk
  • 2. Concurrent Transactions • We want our database to be accessible to many clients concurrently • Multiple processors or cores accessing same storage (containing whole database) • Multiple processors distributed over a network each with local storage (each holding a portion of the database)
  • 3. Concurrent Transactions • To increase performance we want to allow multiple database transactions, being processed on separate processors, to take place at once • Run into problems when two (or more) of these transactions want to access the same data!
  • 4. Schedules • Concerned with the order of execution • Consider two transactions, T and T : 1 2
  • 5. T1 T2 T1 T2 read(X) read(X) X = X-100 tmp = X*0.1 write(X) X=X-tmp read(Y) write(X) Y = Y+100 read(Y) write(Y) Y=Y+tmp read(X) write(Y) tmp = X*0.1 read(X) X=X-tmp X = X-100 write(X) write(X) read(Y) read(Y) Y=Y+tmp Y = Y+100 write(Y) write(Y)
  • 6. Schedules • Schedules of T and T2 are serial due to 1 one transaction finishing before the other • If Tand T2 were to happen at the same 1 time, forcing serialisation would limit performance!
  • 7. T1 T2 read(X) X = X-100 write(X) read(X) tmp = X*0.1 X=X-tmp write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 8. T1 T2 read(X) X = X-100 write(X) Despite these read(X) transactions not tmp = X*0.1 being serialised, the X=X-tmp schedule ensures the write(X) resulting database is read(Y) consistent Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 9. T1 T2 read(X) X = X-100 read(X) tmp = X*0.1 X=X-tmp write(X) write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 10. T1 T2 read(X) X = X-100 read(X) Here the database tmp = X*0.1 gets into an X=X-tmp inconsistent state write(X) because the write(X) write(X) of T2 is lost due to read(Y) being overwritten by Y = Y+100 T1! write(Y) read(Y) Y=Y+tmp write(Y)
  • 11. Conflict Analysis • What went wrong in the example? • Ordering of reads and writes caused an inconsistency
  • 12. Conflict Analysis • What went wrong in the example? • Ordering of reads and writes caused an inconsistency • Use conflict analysis to identify the cause of the problem... • Intuition: reads and writes applied in the same order as a serial schedule will always result in a consistent state • Examine the schedule and try to rearrange them into the same order as a serial schedule by swapping instructions (paying special attention to “conflicts”)
  • 13. Conflicts • Looking consecutive reads and writes: • If they access different data items, they can be applied in any order • If they access the same data items, then the order of operations may be important, i.e. they may conflict
  • 14. Conflicts • Looking consecutive reads and writes: • If they access different data items, they can be applied in any order • If they access the same data items, then the order of operations may be important, i.e. they may conflict • (This is simplified! We are not considering insertion and deletion)
  • 15. Conflict Rules • In a schedule (only reads and writes) for consecutive instructions i1 and i2 : • i1 = read(x), i2 = read(x) : no conflict • i1 = read(x), i2 = write(x) : conflict (ordering dictates whether i1 gets value of write or previous state) • i1 = write(x), i2 = read(x) : conflict • i1 = write(x), i2 = write(x) : conflict (writes do not affect one another, but will affect next read)
  • 16. Conflict Serialization • If a schedule S is transformed into schedule S’ by a series of non-conflicting instruction swaps, the S and S’ are conflict equivalent • A schedule is conflict serializable if it is conflict equivalent to a serial schedule • A schedule that is conflict serializable will produce the same final state as some serial schedule and will leave the database in a consistent state
  • 17. T1 T2 read(X) X = X-100 write(X) read(X) tmp = X*0.1 X=X-tmp write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 18. T1 T2 read(X) X = X-100 write(X) read(X) tmp = X*0.1 X=X-tmp write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 19. T1 T2 read(X) X = X-100 write(X) read(X) tmp = X*0.1 X=X-tmp write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 20. T1 T2 T1 T2 read(X) read(X) X = X-100 X = X-100 write(X) write(X) read(X) read(Y) tmp = X*0.1 Y = Y+100 X=X-tmp write(Y) write(X) read(X) read(Y) tmp = X*0.1 Y = Y+100 X=X-tmp write(Y) write(X) read(Y) read(Y) Y=Y+tmp Y=Y+tmp write(Y) write(Y)
  • 21. T1 T2 read(X) X = X-100 read(X) tmp = X*0.1 X=X-tmp write(X) write(X) read(Y) Y = Y+100 write(Y) read(Y) Y=Y+tmp write(Y)
  • 22. T1 T2 read(X) X = X-100 read(X) These instructions tmp = X*0.1 cannot be swapped X=X-tmp because they are in write(X) conflict! write(X) This schedule cannot read(Y) be re-ordered into a Y = Y+100 serial schedule... write(Y) read(Y) Y=Y+tmp write(Y)
  • 23. Serializability • Paramount correctness criteria for concurrent database transactions • Ensures isolation between transactions • Ensures consistency despite other transactions
  • 24. Locking • We can get a serialisable schedule by enforcing mutual exclusion on data items: this could be achieved using simple locking • shared lock - to read data items, can only be granted if there are either no locks or only shared locks on a data item • write lock - for writing, can only be granted if there are no other locks on the data item
  • 25. T1 T2 write_lock(X) read(X) X = X-100 write(X) unlock(X) write_lock(X) read(X) tmp = X*0.1 X=X-tmp write(X) unlock(X) write_lock(Y) read(Y) Y = Y+100 write(Y) unlock(Y) write_lock(Y) read(Y) Y=Y+tmp write(Y) unlock(Y)
  • 26. T1 T2 T1 T2 write_lock(X) write_lock(X) read(X) read(X) X = X-100 X = X-100 write(X) write_lock(X) unlock(X) read(X) write_lock(X) tmp = X*0.1 read(X) X=X-tmp tmp = X*0.1 write(X) X=X-tmp unlock(X) write(X) write(X) unlock(X) unlock(X) write_lock(Y) write_lock(Y) read(Y) read(Y) Y = Y+100 Y = Y+100 write(Y) write(Y) unlock(Y) unlock(Y) write_lock(Y) write_lock(Y) read(Y) read(Y) Y=Y+tmp Y=Y+tmp write(Y) write(Y) unlock(Y) unlock(Y)
  • 27. T1 T2 T1 T2 write_lock(X) write_lock(X) read(X) read(X) X = X-100 X = X-100 write(X) write_lock(X) s! unlock(X) read(X) ail f write_lock(X) tmp = X*0.1 k oc read(X) X=X-tmp l tmp = X*0.1 write(X) X=X-tmp unlock(X) write(X) write(X) unlock(X) unlock(X) write_lock(Y) write_lock(Y) read(Y) read(Y) Y = Y+100 Y = Y+100 write(Y) write(Y) unlock(Y) unlock(Y) write_lock(Y) write_lock(Y) read(Y) read(Y) Y=Y+tmp Y=Y+tmp write(Y) write(Y) unlock(Y) unlock(Y)
  • 28. Problem: Immediate Modification • If during a transaction our database uses immediate modification we have a problem, transactions are not isolated!
  • 29. T1 T2 write-lock(P) read(P) P = P - 100 write(P) unlock(P) read-lock(P) read(P) unlock(P) read-lock(Q) read(Q) unlock(Q) printf(“%dn”, P + Q) write-lock(Q) read(Q) Q = Q + 100 write(Q) unlock(Q)
  • 30. T1 T2 } P = 100 150 write-lock(P) Q = 50 read(P) P = P - 100 write(P) unlock(P) read-lock(P) read(P) unlock(P) read-lock(Q) read(Q) unlock(Q) printf(“%dn”, P + Q) write-lock(Q) read(Q) Q = Q + 100 write(Q) unlock(Q)
  • 31. T1 T2 } P = 100 150 write-lock(P) Q = 50 read(P) P = P - 100 write(P) } P= 0 50 unlock(P) Q = 50 read-lock(P) read(P) unlock(P) read-lock(Q) read(Q) unlock(Q) printf(“%dn”, P + Q) write-lock(Q) read(Q) Q = Q + 100 write(Q) unlock(Q)
  • 32. T1 T2 } P = 100 150 write-lock(P) Q = 50 read(P) P = P - 100 write(P) } P= 0 50 unlock(P) Q = 50 read-lock(P) read(P) unlock(P) read-lock(Q) read(Q) unlock(Q) printf(“%dn”, P + Q) write-lock(Q) read(Q) Q = Q + 100 write(Q) } P= 0 150 unlock(Q) Q = 150
  • 33. T1 T2 write-lock(P) read(P) P = P - 100 Solution: release locks write(P) as late as possible! write-lock(Q) read(Q) (ensures serial schedule, Q = Q + 100 but potential deadlocks!) write(Q) unlock(Q) unlock(P) read-lock(P) read(P) read-lock(Q) read(Q) unlock(Q) unlock(P) printf(“%dn”, P + Q)
  • 34. 2-Phase Locking • One of many methods to ensure serializability • Growth phase: transactions can only acquire locks • Shrinking phase: transactions can only release locks • Once a transaction has started to release locks it cannot acquire anymore...
  • 35. T1 T2 T1 Growth Phase write-lock(P) read(P) P = P - 100 write(P) write-lock(Q) read(Q) Q = Q + 100 write(Q) T1 Shrinking Phase unlock(Q) unlock(P) T2 Growth Phase read-lock(P) read(P) read-lock(Q) read(Q) T2 Shrinking Phase unlock(Q) unlock(P) printf(“%dn”, P + Q)
  • 36. Deadlock • Two or more transactions are waiting for locks that the other holds, hence none of those involved make progress • Dealt with in different ways, not covered in this course...
  • 37. Summary • Most concurrent transactions have no conflicts: they either all access different data items or else only perform reads • For the transactions that might conflict we can enforce serializability to maintain the ACID properties

Hinweis der Redaktion