sqlsql-serverisolation-level

Difference between "read commited" and "repeatable read" in SQL Server


I think the above isolation levels are so alike. Could someone please describe with some nice examples what the main difference is?


Solution

  • Read committed is an isolation level that guarantees that any data read was committed at the moment is read. It simply restricts the reader from seeing any intermediate, uncommitted, 'dirty' read. It makes no promise whatsoever that if the transaction re-issues the read, will find the Same data, data is free to change after it was read.

    Repeatable read is a higher isolation level, that in addition to the guarantees of the read committed level, it also guarantees that any data read cannot change, if the transaction reads the same data again, it will find the previously read data in place, unchanged, and available to read.

    The next isolation level, serializable, makes an even stronger guarantee: in addition to everything repeatable read guarantees, it also guarantees that no new data can be seen by a subsequent read.

    Say you have a table T with a column C with one row in it, say it has the value '1'. And consider you have a simple task like the following:

    BEGIN TRANSACTION;
    SELECT * FROM T;
    WAITFOR DELAY '00:01:00'
    SELECT * FROM T;
    COMMIT;
    

    That is a simple task that issue two reads from table T, with a delay of 1 minute between them.

    If you follow the logic above you can quickly realize that SERIALIZABLE transactions, while they may make life easy for you, are always completely blocking every possible concurrent operation, since they require that nobody can modify, delete nor insert any row. The default transaction isolation level of the .Net System.Transactions scope is serializable, and this usually explains the abysmal performance that results.

    And finally, there is also the SNAPSHOT isolation level. SNAPSHOT isolation level makes the same guarantees as serializable, but not by requiring that no concurrent transaction can modify the data. Instead, it forces every reader to see its own version of the world (its own 'snapshot'). This makes it very easy to program against as well as very scalable as it does not block concurrent updates. However, that benefit comes with a price: extra server resource consumption.

    Supplemental reads: