Skip to content
tezvyn:

Shared & Exclusive Locks: The Read vs. Write Rule

Source: geeksforgeeks.orgEasyHow cards are made

Shared & Exclusive Locks: The Read vs. Write Rule

A Shared (S) lock is like many people reading a library book at once; an Exclusive (X) lock is one person writing in it alone. Databases use S/X locks to manage concurrency, preventing writes from corrupting reads.

Why it exists

Databases need to handle many operations at once without corrupting data. If one transaction is updating a record while another is reading it, the second transaction could see incomplete, invalid data (a "dirty read"). Shared and exclusive locks were invented to enforce order and prevent these race conditions, ensuring data integrity.

The mental model

Think of data in a database as a book in a library. A Shared (S) lock is like letting multiple people read the book inside the library. As long as no one is writing, many people can read simultaneously. An Exclusive (X) lock is like checking the book out. Only one person can have it, they can read and write in it, and no one else can even look at it until it's returned.

How it works

A transaction must acquire a lock before accessing a data item. There are two types. First, a Shared (S) lock, used for reading. Multiple transactions can hold an S-lock on the same item at the same time. Second, an Exclusive (X) lock, used for writing. Only one transaction can hold an X-lock on an item, and if it does, no other transaction can hold any lock (S or X) on that item. If a transaction requests a lock that is incompatible with an existing lock, it must wait until the conflicting lock is released.

When to use it

S/X locking is the default concurrency control mechanism in most relational databases to ensure ACID properties, specifically Isolation. It's used whenever multiple transactions might access the same data, guaranteeing that reads are consistent and writes are atomic. It allows for high read concurrency while protecting data during modifications.

When not to use it

Aggressive or long-held locks can severely limit concurrency, causing other transactions to wait and reducing system throughput. In write-heavy systems, this can lead to frequent contention and deadlocks. Some high-throughput systems may use alternative strategies like Optimistic Concurrency Control, which avoids locking by assuming conflicts are rare.

One canonical example

Transaction T1 needs to update a user's balance. It requests and is granted an Exclusive (X) lock on the user's record. While T1 is calculating the new balance, Transaction T2 tries to read the same record for a report. T2 requests a Shared (S) lock, but it must wait because T1 holds an incompatible X-lock. Once T1 finishes its update, commits, and releases the lock, T2's S-lock is granted, and it reads the new, correct balance.

Interview question

Consider a scenario where Transaction A is updating a record, holding an Exclusive (X) lock. If Transaction B then attempts to read the same record, what is the immediate consequence for Transaction B?

  • a.Transaction B's request for a Shared (S) lock is denied, and the transaction is rolled back.
  • b.Transaction B must wait until Transaction A releases its Exclusive (X) lock.Correct
  • c.Transaction B successfully acquires a Shared (S) lock and reads the record's current state.
  • d.The Exclusive (X) lock held by Transaction A is automatically downgraded to a Shared (S) lock.
Why?

The card explicitly states that if a transaction requests a lock incompatible with an existing one, it must wait. An Exclusive (X) lock, used for writing, is incompatible with any other lock, including a Shared (S) lock requested for reading. Therefore, the reading transaction must wait, rather than reading potentially inconsistent data or forcing a rollback.

Just read this? Test yourself on what you have been reading.

Read the original → geeksforgeeks.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.

See open roles