Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A Java application on SQL Server suffers frequent deadlocks between a reporting SELECT and order updates. The DBA proposes enabling READ_COMMITTED_SNAPSHOT. What do you check before turning it on?

RCSI will remove most reader-writer blocking and those deadlocks, but it changes what reads return: the last committed version instead of waiting for the current one. Before turning it on I would check for code that relied on blocking reads for correctness, such as check-then-update logic, and fix it with UPDLOCK or proper locking; size and monitor tempdb's version store, which grows with long transactions; expect 14 extra bytes per modified row and the page splits that follow; and plan a short window, because the change needs exclusive access to the database.

Why this question is asked

When reports and order updates deadlock, enabling READ_COMMITTED_SNAPSHOT is usually the right answer, and DBAs propose it for good reason. But it is a semantic change to every query in the database, not just a performance switch, and turning it on blindly can trade a visible deadlock for an invisible race condition. Interviewers ask this to see whether we know what to verify first. In this answer, we will recap why the deadlocks happen now, then go through the checks in order: semantics and races, tempdb, row size and page splits, and how to switch it on safely.

1Why the Report and the Updates Deadlock Today

Under SQL Server's default locking READ COMMITTED, the reporting SELECT takes shared locks as it scans, while each order update holds exclusive locks on the rows it changes. If the report holds a shared lock on page A and waits for a row on page B that an update has locked, while that update waits to lock something on page A, neither can continue. SQL Server picks a victim and returns error 1205 to it. With RCSI, the report takes no shared locks, reads committed versions instead, and the cycle can no longer form between those two.

So enabling RCSI will very likely fix the symptom. The checks are about what else changes.

Step throughThe deadlock today
Report SELECTLock managerOrder update
shared lock on page A
exclusive lock on row in page B
wants row in page B: waits
wants page A: waits
deadlock: update chosen as victim, error 1205

5 messages in order. Play them, or step through them one at a time.

With RCSI the report takes no shared locks, so this cycle cannot form.

2Check 1: Code That Relied on Blocking Reads

Today, a read that meets an uncommitted change waits for it and then sees the result. Some code depends on that without anyone knowing. The typical case is check-then-act: read a value, decide, then update. Under locking READ COMMITTED the read might wait for a concurrent writer and see its committed result; under RCSI it immediately sees the old version and makes its decision on stale data.

SQL
-- Reserve stock: check, then update (in one transaction)
SELECT @available = quantity FROM stock WHERE sku = 'A1'; -- RCSI: old committed value, no wait
IF @available >= 1
UPDATE stock SET quantity = quantity - 1 WHERE sku = 'A1';
-- Two sessions can both read 1 and both decrement: quantity -1

The fix is to make such reads take the lock they need. WITH (UPDLOCK) takes an update lock, so a second session's check waits until the first commits. Better still is to fold the check into the write, so there is no separate read at all.

3Fixing the Races Before the Switch

Here are both fixes. The first keeps the structure; the second removes the race entirely.

Java
-- Fix A: lock the row we read
SELECT @available = quantity FROM stock WITH (UPDLOCK, ROWLOCK) WHERE sku = 'A1';
-- Fix B: conditional update, check the row count
UPDATE stock SET quantity = quantity - 1 WHERE sku = 'A1' AND quantity >= 1;
-- @@ROWCOUNT = 1 -> reserved, 0 -> out of stock
-- Where one query must keep the old blocking behaviour under RCSI:
SELECT quantity FROM stock WITH (READCOMMITTEDLOCK) WHERE sku = 'A1';

To find these patterns we search the code base and stored procedures for SELECTs followed by UPDATEs of the same table in one transaction, and in Java for JPA code that reads an entity and writes back a value derived from it without a @Version column or a pessimistic lock. Optimistic locking with @Version in JPA is already safe under RCSI, because the update checks the version it read.

4Check 2: tempdb and the Version Store

Under RCSI, every update and delete copies the previous version of the row into the version store, which is in tempdb unless Accelerated Database Recovery is on (SQL Server 2019 and later), in which case versions live in the user database. Versions are kept as long as any transaction might still need them, so one long-running transaction, such as a forgotten open transaction or a three-hour batch, keeps every version created since it started.

So before the switch we size tempdb for the peak update rate times the longest transaction, put it on fast storage, and add monitoring. After the switch we watch it closely for the first weeks.

SQL
-- Version store size per database
SELECT DB_NAME(database_id) AS db, reserved_page_count * 8 / 1024 AS version_store_mb
FROM sys.dm_tran_version_store_space_usage;
-- shop 2350
-- Longest running transactions that keep versions alive
SELECT session_id, elapsed_time_seconds
FROM sys.dm_tran_active_snapshot_database_transactions
ORDER BY elapsed_time_seconds DESC;

5Check 3: Row Size and Page Splits

Each row version needs a pointer to its older version, so SQL Server adds 14 bytes of versioning information to rows as they are modified while versioning is on. On a table where pages are packed full, an update that adds 14 bytes may no longer fit, so the page splits into two half-full pages. Right after enabling RCSI, a busy table can see a wave of page splits, fragmentation and some extra I/O.

We can soften it by rebuilding the busiest indexes with a fill factor that leaves some room, for example 90, before or soon after the switch, and by checking the free space on the hottest tables. The effect settles once most rows carry the extra bytes.

6Switching It On Safely

The change itself needs exclusive access: ALTER DATABASE ... SET READ_COMMITTED_SNAPSHOT ON waits until it is the only active connection in the database. With connection pools that is never true on its own, so we either plan a short window or force other sessions out with WITH ROLLBACK IMMEDIATE, which rolls back their open transactions.

SQL
ALTER DATABASE shop SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
SELECT is_read_committed_snapshot_on FROM sys.databases WHERE name = 'shop'; -- 1
Step throughBefore enabling RCSI
  1. Find check-then-update codeSELECT then UPDATE, entities without @Version
  2. Fix with UPDLOCK or conditional updates
  3. Size and monitor tempdbpeak update rate x longest transaction
  4. Leave room on hot pagesfill factor before the switch
  5. Test under load in staging
  6. Switch in a short windowWITH ROLLBACK IMMEDIATE

6 steps in order. Play them, or step through them one at a time.

The order I would follow is: fix the known check-then-act code; test the application with RCSI on in a staging copy under a realistic load; size and monitor tempdb; schedule the window; switch; and watch deadlock reports, version store size and page splits for a few weeks. Writer-writer deadlocks can still happen, so the system_health extended event session, which captures deadlock graphs by default, remains useful.

7Follow-up questions

Can we test RCSI without switching the whole database?

Not exactly, because RCSI is a database-wide option. But we can enable ALLOW_SNAPSHOT_ISOLATION and run chosen transactions at SNAPSHOT, which uses the same versioning, to see the version store load and application behaviour, or simply run the full test suite against a staging copy with RCSI on.

Will RCSI remove all deadlocks?

No. It removes deadlocks between readers and writers. Deadlocks between two writers, for example two transactions updating the same rows in different orders, remain and need fixing with a consistent update order or shorter transactions.

Does Hibernate behave differently under RCSI?

Hibernate's plain reads simply get committed versions without waiting. Optimistic locking with @Version keeps working, because the version check happens in the UPDATE. Code that used PESSIMISTIC_WRITE already takes an update lock, which the SQL Server dialect expresses with lock hints such as UPDLOCK, so it is unaffected.

8Conclusion

In this answer, we saw that RCSI will remove the reader-writer deadlocks because reads stop taking shared locks, but that it changes semantics: reads return the last committed version instead of waiting. So before turning it on we find and fix check-then-update code with UPDLOCK or conditional updates, size and monitor the tempdb version store, prepare for the 14 extra bytes per modified row and the page splits they cause, and plan a short window, because the switch needs exclusive access to the database.

What to say in the interviewAbout 90 seconds aloud

Open with

I agree RCSI is very likely the right fix, because the deadlock is between shared locks from the report and exclusive locks from the updates, and with RCSI readers stop taking shared locks.

Then, if they want more

But it changes semantics for every query in the database, so I would check four things first. First, code that relied on blocking reads. Today a read that meets an uncommitted update waits and sees the result; under RCSI it immediately sees the last committed version. Check-then-update logic, like reading stock and then decrementing it, can then race.

I would search for those patterns and fix them with UPDLOCK on the read, or better a conditional update that checks the row count. JPA entities with @Version are already safe. Second, the version store.

Updates copy old versions into tempdb, and a long transaction keeps them all alive, so I would size tempdb for the peak update rate and the longest transaction, and monitor it with the version store DMVs.

Third, modified rows get 14 extra bytes of version information, which causes page splits on full pages right after the switch, so I would rebuild the hottest indexes with some fill factor. Fourth, the ALTER DATABASE needs exclusive access, so I would schedule a short window and use WITH ROLLBACK IMMEDIATE, after testing under load in staging.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Java application on SQL Server suffers frequent deadlocks between a reporting SELECT...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/127ff7ae4d/