AQA 7517, section 4.10.5
Client-server database systems
In a client-server database system, the database is held on a server, run by a database management system (DBMS). Many clients (users and programs) connect to the server over a network, and send requests (such as SQL queries) to it.
It provides simultaneous (concurrent) access to the database for multiple clients. For example, a booking system has hundreds of customers searching and booking at the same time. Because the data is held in one place, everyone sees the same, current data.
The problem: concurrent access
Letting many clients read and write at once is useful, but it can damage the integrity of the data. The classic problem is the lost update.
Example: a lost update
A bank account has £100. Two clients update it at the same time.
- Client A pays in £50. Client B withdraws £30.
- The correct final balance is .
| Step | Client A | Client B | Balance in database |
|---|---|---|---|
| 1 | Reads the balance (100) | 100 | |
| 2 | Reads the balance (100) | 100 | |
| 3 | Calculates and writes 150 | 150 | |
| 4 | Calculates and writes 70 | 70 |
The final balance is £70, which is wrong. Client B worked from the old balance, so Client A's deposit was overwritten and lost. Updates were lost if two clients edit a record at the same time.
Controlling concurrent access
The DBMS must make sure that simultaneous operations don't interfere with one another, so the database stays correct (integrity). It groups operations into transactions (a set of steps that must be treated as a unit, such as read, calculate and write). There are several techniques.
Record locks
A record lock stops other clients from using a record while one client is working on it.
- Before updating a record, a client locks it.
- Others who want to use it have to wait until it is unlocked.
- When the update is complete, the lock is released.
Applied to the example: A locks the balance, reads 100, writes 150 and unlocks. Then B can read 150, and writes . The result is correct.
- Advantage: simple and effective.
- Disadvantage: clients may have to wait, which slows things down. Two clients can each hold a lock that the other needs, so both wait forever. This is called deadlock, and the system must detect it and cancel one of the transactions.
Serialisation
Serialisation makes sure that the result of running transactions at the same time is the same as if they had run one after another, in some order (serially). The simplest way is to run transactions strictly one at a time, but that is slow, so the DBMS allows them to overlap only when this can't change the result.
- It guarantees correctness: no transaction sees half-finished work from another.
- It limits how much can happen at once.
Timestamp ordering
With timestamp ordering, each transaction is given a timestamp when it starts, which gives an order of age. The DBMS makes sure that conflicting operations are performed in timestamp order. If a transaction tries to do something that would break that order (for example, to write a value that a younger transaction has already read), it is cancelled and restarted with a new timestamp.
To check this, each record stores the timestamp of the last transaction to read it and the last transaction to write it. Then:
- a transaction can't write a record that a younger transaction has already read or written;
- a transaction can't read a record that a younger transaction has already written.
For example, transaction A starts at 10:00:01 and transaction B at 10:00:02. B reads the balance, so the record's read timestamp becomes 10:00:02. When A then tries to write the balance, the record has been read by a younger transaction, so A is aborted and restarted. A's write can't overwrite a value that B has already used.
- Advantage: no locks, so no deadlock.
- Disadvantage: some transactions are restarted, which wastes work.
Commitment ordering
Commitment ordering makes sure that transactions are committed (made permanent) in an order that is consistent with the way they depend on each other. If transaction A has to come before B because of the data they share, then A must commit before B, so that the combined result is the same as a serial one. It allows more concurrency than locking everything, while preserving integrity.
Comparison
| Technique | How it works | Main drawback |
|---|---|---|
| Record locks | A client locks a record while using it | Waiting, and possible deadlock |
| Serialisation | Result must equal running the transactions one after another | Limits the amount of concurrency |
| Timestamp ordering | Operations are done in order of transaction timestamps | Some transactions are restarted |
| Commitment ordering | Transactions commit in an order consistent with their dependencies | More complex to manage |
You need to know that these techniques exist and what problem they solve. The detailed algorithms aren't needed.
Why integrity matters
If the data is wrong, decisions based on it are wrong: money is lost, a seat is double-booked, or stock levels are incorrect. A client-server database must therefore protect the integrity of the data while serving many users at the same time.
Worked example
Task: two customers try to book the last seat on a flight at the same moment. Explain the problem and a way to prevent it.
The problem: both read "1 seat available". Both book it, and both write "0 seats left". Two customers now hold a booking for one seat. One update has overwritten the other's check, so the data is inconsistent.
Prevention with record locks: when the first customer starts to book, the seat record is locked. The second customer's request waits. The first writes "0 seats" and unlocks. The second now reads 0 seats, and is told that the flight is full. Only one booking is made, so the integrity is preserved.
Exam tip: describe the problem (lost updates when two clients edit the same record at once) with a short example, then name a technique (record locks, serialisation, timestamp ordering or commitment ordering) and explain how it solves the problem. Mention deadlock as a drawback of locks. Lock the record, not the whole table or database. For timestamp ordering, say that each record holds the timestamps of its last read and last write.
Check yourself
What does a client-server database system provide?
Simultaneous access to the database for multiple clients.
What is the lost update problem?
When two clients read and edit the same record at the same time, one client's update overwrites the other's, so one change is lost and the data is wrong.
How does a record lock prevent lost updates?
It stops other clients from accessing a record while one client is updating it, so the updates happen one after another.
What is a disadvantage of using record locks?
Clients have to wait for locks to be released, which slows things down, and two clients can end up waiting for each other (deadlock).
Name four techniques used to control concurrent access
Record locks, serialisation, timestamp ordering and commitment ordering.