SlideShare a Scribd company logo
1 of 15
Concurrency Control
What is Concurrency Control?
• Imagine you and several friends are collaborating on
editing a shared document. Without any rules, you
could all end up making changes at the same time,
leading to conflicts, overwrites, and general chaos.
Concurrency control is like the set of rules that
determines who gets to edit the document and when.
The goal is to ensure everyone can make changes
without messing up each other's work.
Lock – based protocol
• Lock-based protocols are a common way to implement concurrency control in
databases. Think of them as traffic lights:
• Shared Lock (Green Light): This is like the "read-only" mode. Multiple people can
view the document simultaneously. For example, several airline employees might
need to simultaneously see a flight's remaining seats to book passengers.
• Exclusive Lock (Red Light): Only one person gets to make active changes to the
document at a time. Consider a bank account balance. You wouldn't want
multiple transactions trying to withdraw or deposit money from the same
account at the exact same moment, or you'd end up with an incorrect balance.
Example
• What is Concurrency Control?
• Imagine you and several friends are collaborating on editing a shared document. Without any
rules, you could all end up making changes at the same time, leading to conflicts, overwrites, and
general chaos. Concurrency control is like the set of rules that determines who gets to edit the
document and when. The goal is to ensure everyone can make changes without messing up each
other's work.
• Lock-Based Protocols: The Traffic Lights of Databases
• Lock-based protocols are a common way to implement concurrency control in databases. Think of
them as traffic lights:
• Shared Lock (Green Light): This is like the "read-only" mode. Multiple people can view the
document simultaneously. For example, several airline employees might need to simultaneously
see a flight's remaining seats to book passengers.
• Exclusive Lock (Red Light): Only one person gets to make active changes to the document at a
time. Consider a bank account balance. You wouldn't want multiple transactions trying to
withdraw or deposit money from the same account at the exact same moment, or you'd end up
with an incorrect balance.
Real-World Examples
• Ticket Booking Systems: When you book a movie or a flight ticket
online, the system uses locks. While you're selecting your seat, a
shared lock is probably placed on it, preventing others from grabbing
it at the same time. Once you move to the payment stage, that likely
changes into an exclusive lock to make sure no one else can book the
same seat while you're completing the transaction.
• ATM Transactions: At an ATM, when you're withdrawing money, an
exclusive lock is placed on your account. This prevents any other
transaction (either another ATM withdrawal or an online transfer)
from messing with your balance while you're in the middle of the
process.
Important Considerations
• Deadlocks: Sometimes transactions can get stuck. Imagine
Transaction A has a lock on Data item 1 and needs Data item 2, while
Transaction B has a lock on Data item 2 and needs Data item 1.
Neither can proceed! Systems need ways to detect and resolve
deadlocks.
• Performance: Locks add overhead. Databases aim to find a balance
between allowing a lot of people to work on data simultaneously
(concurrency) and making sure the data stays accurate (consistency).
Pitfalls of lock-based protocols
1. Deadlocks
The classic lock-based problem. Imagine these two scenarios:
• Traffic Gridlock: Two cars arrive at a four-way intersection simultaneously. Car A wants to
turn left and needs the lane Car B currently occupies. Car B also wants to turn left and
needs the lane Car A occupies. They're both stuck, waiting for the other to move, leading
to a gridlock.
• Library Book Standoff: Two students, Alice and Bob, need two specific books for a
project. Alice picks up Book 1, and Bob picks up Book 2. They both realize they need the
other person's book to complete their work. Neither wants to put their book down first,
creating a deadlock.
Databases can face similar situations. Transactions can hold locks on different data items,
and end up in a circular waiting pattern where everyone's waiting for a resource
someone else has locked. Systems need ways to detect and break such deadlocks, often
by rolling back (undoing) one of the transactions to free up resources.
• Starvation
• The Forgotten Diner: A popular restaurant consistently
prioritizes large groups and walk-ins. A solo diner might keep
waiting for a table, even if smaller tables are technically
available. The diner gets "starved" of service.
• In databases, a transaction needing a large number of locks
might continuously get delayed while a stream of smaller
transactions needing fewer locks keep getting processed.
This can lead to the larger transaction perpetually waiting or
"starving" for resources.
What is the Two-Phase Locking Protocol (2PL)?
2PL is a concurrency control method designed to help prevent some of
the issues we discussed earlier. It establishes rules for when
transactions can acquire and release locks:
• Growing Phase: A transaction can acquire locks on data items it
needs, but it cannot release any locks yet. Imagine it's like gathering
all your tools and materials before starting a project.
• Shrinking Phase: The transaction can start releasing locks, but it can
no longer acquire new ones. This is like the cleanup phase of your
project—you can put tools away, but you shouldn't need any new
ones at this stage.
Example
• Growing Phase
• Framing Crew: Gets a lock on the lumber supply, preventing
others from taking wood they need.
• Plumbing Crew: Gets a lock on the plumbing pipes and fixtures.
• Electrical Crew: Gets a lock on wiring, outlets, etc.
• Shrinking Phase
• Framing Crew: Finishes their work, releases the lock on the
lumber (other crews can now use it).
• Plumbing Crew: Finishes, releases the lock on pipes.
• Electrical Crew: Finishes, releases the lock on wiring.
Why 2PL is Useful
• 2PL helps ensure that if multiple transactions are working on the
same data, their results can be serialized (meaning you could have
achieved the same outcome if they ran one after the other). This
helps maintain the consistency of your data.
Benefits of 2PL
• Deadlock Prevention: There can be some scenarios where 2PL helps
prevent deadlocks, since locks are released in an orderly way after
one stage is fully complete.
• Ensures Serializability: It helps guarantee order and consistency. In
our house-building example, the house could still be built in a valid
way if the crews had done their work one after the other, even if they
wouldn't have all been working at the same time.
Time stamp based Control
• At the heart of timestamp-based concurrency control is the idea of
assigning a unique timestamp to each transaction when it enters the
database system. Think of it like a label with the exact time of arrival.
These timestamps determine the order in which transactions should
be processed to keep the database consistent.
Conflicts? Rollback or Ignore:
• Older Writes, Younger Reads: No problem. The younger transaction
can read the data written by the older one.
• Younger Writes, Older Tries to Read: The older transaction needs the
data as it was before the younger write. Solution: Roll back (undo) the
younger transaction and restart it, letting the older one proceed.
• Younger Writes, Older Tries to Write: The younger write has already
happened. Solution: Often, the older transaction's write is simply
ignored.
Real-World Example: Online Ticket Sales
Imagine you're trying to buy concert tickets online, along with
thousands of other fans.
• Timestamps: Every ticket purchase attempt receives a timestamp at
the exact moment the transaction begins.
• Seat Conflict: You and another fan try to purchase the same seat at
virtually the same time. Your transaction might have a timestamp a
fraction of a second earlier than the other person's.
• The Resolution: Because your transaction is 'older,' it will succeed.
The other fan's transaction, even though it might have been a tiny bit
later, might be either rolled back (forcing them to restart their
purchase attempt) or simply ignored (with a message saying the seat
was no longer available).
Benefits of Timestamp-Based Control
• Prevents Deadlocks: The strict ordering by timestamps eliminates the
type of circular waits that lead to deadlocks.
• No Waiting: Transactions don't get stuck waiting for locks to be
released

More Related Content

Similar to Concurrency Control in RDBMS for Students.pptx

NoSQL and Einstein's theory of relativity
NoSQL and Einstein's theory of relativityNoSQL and Einstein's theory of relativity
NoSQL and Einstein's theory of relativityLars Marius Garshol
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiProfessor Lili Saghafi
 
Building Autonomous Services
Building Autonomous ServicesBuilding Autonomous Services
Building Autonomous ServicesMatthias Noback
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Bob Pusateri
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Bob Pusateri
 
Building on quicksand microservices indicthreads
Building on quicksand microservices  indicthreadsBuilding on quicksand microservices  indicthreads
Building on quicksand microservices indicthreadsIndicThreads
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Codemotion
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Bob Pusateri
 
Transaction conccurency
Transaction conccurencyTransaction conccurency
Transaction conccurencyEsraa Farrag
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Sachintha Gunasena
 
Concurrency control ms neeti
Concurrency control ms neetiConcurrency control ms neeti
Concurrency control ms neetineeti arora
 
Concurrency control ms neeti
Concurrency control ms neetiConcurrency control ms neeti
Concurrency control ms neetineeti arora
 
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 Corda
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 CordaTriple Entry Accounting: A BlockChain Use Case for Banks With R3 Corda
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 CordaDebajani Mohanty
 
Real time database
Real time databaseReal time database
Real time databasearvinthsaran
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Bob Pusateri
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Bob Pusateri
 
Blockchain Technology for Public and Commercial Libraries
Blockchain Technology for Public and Commercial LibrariesBlockchain Technology for Public and Commercial Libraries
Blockchain Technology for Public and Commercial LibrariesDavid Nzoputa Ofili
 

Similar to Concurrency Control in RDBMS for Students.pptx (20)

NoSQL and Einstein's theory of relativity
NoSQL and Einstein's theory of relativityNoSQL and Einstein's theory of relativity
NoSQL and Einstein's theory of relativity
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
 
Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
 
Rdbms
RdbmsRdbms
Rdbms
 
Building Autonomous Services
Building Autonomous ServicesBuilding Autonomous Services
Building Autonomous Services
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
 
Building on quicksand microservices indicthreads
Building on quicksand microservices  indicthreadsBuilding on quicksand microservices  indicthreads
Building on quicksand microservices indicthreads
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
Transaction conccurency
Transaction conccurencyTransaction conccurency
Transaction conccurency
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
 
Concurrency control ms neeti
Concurrency control ms neetiConcurrency control ms neeti
Concurrency control ms neeti
 
Concurrency control ms neeti
Concurrency control ms neetiConcurrency control ms neeti
Concurrency control ms neeti
 
What is a deadlock
What is a deadlockWhat is a deadlock
What is a deadlock
 
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 Corda
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 CordaTriple Entry Accounting: A BlockChain Use Case for Banks With R3 Corda
Triple Entry Accounting: A BlockChain Use Case for Banks With R3 Corda
 
Real time database
Real time databaseReal time database
Real time database
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
 
Blockchain Technology for Public and Commercial Libraries
Blockchain Technology for Public and Commercial LibrariesBlockchain Technology for Public and Commercial Libraries
Blockchain Technology for Public and Commercial Libraries
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

Concurrency Control in RDBMS for Students.pptx

  • 2. What is Concurrency Control? • Imagine you and several friends are collaborating on editing a shared document. Without any rules, you could all end up making changes at the same time, leading to conflicts, overwrites, and general chaos. Concurrency control is like the set of rules that determines who gets to edit the document and when. The goal is to ensure everyone can make changes without messing up each other's work.
  • 3. Lock – based protocol • Lock-based protocols are a common way to implement concurrency control in databases. Think of them as traffic lights: • Shared Lock (Green Light): This is like the "read-only" mode. Multiple people can view the document simultaneously. For example, several airline employees might need to simultaneously see a flight's remaining seats to book passengers. • Exclusive Lock (Red Light): Only one person gets to make active changes to the document at a time. Consider a bank account balance. You wouldn't want multiple transactions trying to withdraw or deposit money from the same account at the exact same moment, or you'd end up with an incorrect balance.
  • 4. Example • What is Concurrency Control? • Imagine you and several friends are collaborating on editing a shared document. Without any rules, you could all end up making changes at the same time, leading to conflicts, overwrites, and general chaos. Concurrency control is like the set of rules that determines who gets to edit the document and when. The goal is to ensure everyone can make changes without messing up each other's work. • Lock-Based Protocols: The Traffic Lights of Databases • Lock-based protocols are a common way to implement concurrency control in databases. Think of them as traffic lights: • Shared Lock (Green Light): This is like the "read-only" mode. Multiple people can view the document simultaneously. For example, several airline employees might need to simultaneously see a flight's remaining seats to book passengers. • Exclusive Lock (Red Light): Only one person gets to make active changes to the document at a time. Consider a bank account balance. You wouldn't want multiple transactions trying to withdraw or deposit money from the same account at the exact same moment, or you'd end up with an incorrect balance.
  • 5. Real-World Examples • Ticket Booking Systems: When you book a movie or a flight ticket online, the system uses locks. While you're selecting your seat, a shared lock is probably placed on it, preventing others from grabbing it at the same time. Once you move to the payment stage, that likely changes into an exclusive lock to make sure no one else can book the same seat while you're completing the transaction. • ATM Transactions: At an ATM, when you're withdrawing money, an exclusive lock is placed on your account. This prevents any other transaction (either another ATM withdrawal or an online transfer) from messing with your balance while you're in the middle of the process.
  • 6. Important Considerations • Deadlocks: Sometimes transactions can get stuck. Imagine Transaction A has a lock on Data item 1 and needs Data item 2, while Transaction B has a lock on Data item 2 and needs Data item 1. Neither can proceed! Systems need ways to detect and resolve deadlocks. • Performance: Locks add overhead. Databases aim to find a balance between allowing a lot of people to work on data simultaneously (concurrency) and making sure the data stays accurate (consistency).
  • 7. Pitfalls of lock-based protocols 1. Deadlocks The classic lock-based problem. Imagine these two scenarios: • Traffic Gridlock: Two cars arrive at a four-way intersection simultaneously. Car A wants to turn left and needs the lane Car B currently occupies. Car B also wants to turn left and needs the lane Car A occupies. They're both stuck, waiting for the other to move, leading to a gridlock. • Library Book Standoff: Two students, Alice and Bob, need two specific books for a project. Alice picks up Book 1, and Bob picks up Book 2. They both realize they need the other person's book to complete their work. Neither wants to put their book down first, creating a deadlock. Databases can face similar situations. Transactions can hold locks on different data items, and end up in a circular waiting pattern where everyone's waiting for a resource someone else has locked. Systems need ways to detect and break such deadlocks, often by rolling back (undoing) one of the transactions to free up resources.
  • 8. • Starvation • The Forgotten Diner: A popular restaurant consistently prioritizes large groups and walk-ins. A solo diner might keep waiting for a table, even if smaller tables are technically available. The diner gets "starved" of service. • In databases, a transaction needing a large number of locks might continuously get delayed while a stream of smaller transactions needing fewer locks keep getting processed. This can lead to the larger transaction perpetually waiting or "starving" for resources.
  • 9. What is the Two-Phase Locking Protocol (2PL)? 2PL is a concurrency control method designed to help prevent some of the issues we discussed earlier. It establishes rules for when transactions can acquire and release locks: • Growing Phase: A transaction can acquire locks on data items it needs, but it cannot release any locks yet. Imagine it's like gathering all your tools and materials before starting a project. • Shrinking Phase: The transaction can start releasing locks, but it can no longer acquire new ones. This is like the cleanup phase of your project—you can put tools away, but you shouldn't need any new ones at this stage.
  • 10. Example • Growing Phase • Framing Crew: Gets a lock on the lumber supply, preventing others from taking wood they need. • Plumbing Crew: Gets a lock on the plumbing pipes and fixtures. • Electrical Crew: Gets a lock on wiring, outlets, etc. • Shrinking Phase • Framing Crew: Finishes their work, releases the lock on the lumber (other crews can now use it). • Plumbing Crew: Finishes, releases the lock on pipes. • Electrical Crew: Finishes, releases the lock on wiring.
  • 11. Why 2PL is Useful • 2PL helps ensure that if multiple transactions are working on the same data, their results can be serialized (meaning you could have achieved the same outcome if they ran one after the other). This helps maintain the consistency of your data. Benefits of 2PL • Deadlock Prevention: There can be some scenarios where 2PL helps prevent deadlocks, since locks are released in an orderly way after one stage is fully complete. • Ensures Serializability: It helps guarantee order and consistency. In our house-building example, the house could still be built in a valid way if the crews had done their work one after the other, even if they wouldn't have all been working at the same time.
  • 12. Time stamp based Control • At the heart of timestamp-based concurrency control is the idea of assigning a unique timestamp to each transaction when it enters the database system. Think of it like a label with the exact time of arrival. These timestamps determine the order in which transactions should be processed to keep the database consistent.
  • 13. Conflicts? Rollback or Ignore: • Older Writes, Younger Reads: No problem. The younger transaction can read the data written by the older one. • Younger Writes, Older Tries to Read: The older transaction needs the data as it was before the younger write. Solution: Roll back (undo) the younger transaction and restart it, letting the older one proceed. • Younger Writes, Older Tries to Write: The younger write has already happened. Solution: Often, the older transaction's write is simply ignored.
  • 14. Real-World Example: Online Ticket Sales Imagine you're trying to buy concert tickets online, along with thousands of other fans. • Timestamps: Every ticket purchase attempt receives a timestamp at the exact moment the transaction begins. • Seat Conflict: You and another fan try to purchase the same seat at virtually the same time. Your transaction might have a timestamp a fraction of a second earlier than the other person's. • The Resolution: Because your transaction is 'older,' it will succeed. The other fan's transaction, even though it might have been a tiny bit later, might be either rolled back (forcing them to restart their purchase attempt) or simply ignored (with a message saying the seat was no longer available).
  • 15. Benefits of Timestamp-Based Control • Prevents Deadlocks: The strict ordering by timestamps eliminates the type of circular waits that lead to deadlocks. • No Waiting: Transactions don't get stuck waiting for locks to be released