Concurrency Flashcards
(24 cards)
What is concurrence?
Multiple processes or threads manipulate the same data at the same time
What is a trasaction?
An interaction between a client and a server (it involves request/response)
How are concurrency problems avoided?
By ensuring isolation, each transaction is treated as the only operation being performed. The problem arises when multiple transactions try to interact with each other.
What is offline concurrency
It focuses on controlling the sequence and interaction of transactions over time (situations where there’s more than one simple transaction at a time)
Concurrency problems to avoid:
1- Lost updates: User A is editing a piece of data, B starts editing the same data and finishes their edit, User A finishes and submits the data, overwriting user B’s changes
2- inconsistent Readings: a transaction reads data that is in the middle of being updated by another transaction
What are the excution contexts
1- Interaction contexts
2- Processing contexts
3- DB access context
Explain Interaction Contexts
-Request: from outside the system, processed to compute a response, processing is done in the server while client is on hold ex: visiting a website)
-Session: a series of client/server interactions over time, commonly request/response between login and logout (using the website)
Explain processing context:
-Process: a heavyweight excution context that isolates data (has its own memory and resources)
-Thread: a lightweight excution context that runs within a process (share memory therefore concurrency problems)
Explain DB Access context:
Transaction: coordinates requests accessing the same data to avoid concurrency issues
How do we avoid concurrency problems?
Isolation (coordinating access between transactions)
What is concurrency control?
A mechanism used to control access to shared data
What are the measures of concurrency control
1- Optimistic
2- Pessimistic
Explain the difference between pessimistic control and optimistic control
Scenario: 2 users simultaneously edit a file
Optimistic locking: when two users edit the same file, the system allows both of them to edit, and only at the end does it check if they had made conflicting changes. If there’s conflict, one of the user’s changes is rejected
Pessimistic: when two users edit the file, the system locks the file for one user, the second will need to wait until it’s unlocked before they make changes
When do we use pessimistic locking and when do we use optimistic locking
Go for optimistic when conflicts are rare/consequences not critical,
got for pessimistic when there’s a big risk of conflict or critical consequences
When do inconsistent readings happen?
When data is read while another process is modifying it at the same time
How do we prevent inconsistent readings?
By using a pessimistic lock which enforces strict locking rules:
-Multiple users can read simultaneously (shared lock)
-Only one user can write at a time (exclusive lock)
-If a write lock is active, no one else can read or write
-Writers must wait if readers are holding locks, ensuring data consistency
What’s a deadlock?
A situation where multiple users block each other by holding locks on resources the other needs, no one can proceed, creating a circular wait (user A locks File 1 and needs file 2. user 2 locks file 2 and needs file 1)
Ways to prevent deadlocks
-System detects a deadlock and selects a victim to forcefully release their locks
-Locks with expiry time
-Users acquire all necessary locks at the start of the session
-Locks are always obtained in a fixed order
Define transactions
Main concurrency tool in enterprise applications with bounded sequence of actions, start and endpoint (transactions with a short span, usually one request, are more efficient)
Transaction Properties
ACID:
Atomicity: All or nothing
Consistency: Transactions between consistent states
Isolation: Ensures transactions don’t interfere with each other
Durability: Consistent transactions persist even after failures
Define system transactions
SQL commands to open and close transactions
Define business transactions
They are at a higher level than system transactions, example: online banking
How to avoid concurrency problems in business transactions?
-Atomicity and persistence: apply changes when the user agrees, and keep track of the changes
-Consistency: Application is responsible for implementing and maintaining business rules
-Isolation: Each session operates independently
Explain offline concurrency control
When transactions span multiple requests, concurrency problems must be managed
-Optimistic offline lock: Optimistic pattern for business transaction
-Pessimistic Offline Lock: Prevents conflict but is more complex to implement and has less response capacity