%3CLINGO-SUB%20id%3D%22lingo-sub-383281%22%20slang%3D%22en-US%22%3ESerializable%20vs.%20Snapshot%20Isolation%20Level%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383281%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20May%2016%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EBoth%20the%20serializable%20and%20snapshot%20isolation%20levels%20provide%20a%20read%20consistent%20view%20of%20the%20database%20to%20all%20transactions.%26nbsp%3B%20In%20either%20of%20these%20isolation%20levels%2C%20a%20transaction%20can%20only%20read%20data%20that%20has%20been%20committed.%26nbsp%3B%20Moreover%2C%20a%20transaction%20can%20read%20the%20same%20data%20multiple%20times%20without%20ever%20%3CB%3E%20observing%20%3C%2FB%3E%20any%20concurrent%20transactions%20making%20changes%20to%20this%20data.%26nbsp%3B%20The%20unexpected%20read%20committed%20and%20repeatable%20read%20results%20that%20I%20demonstrated%20in%20my%20prior%20few%20posts%20are%20not%20possible%20in%20serializable%20or%20snapshot%20isolation%20level.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENotice%20that%20I%20used%20the%20phrase%20%22without%20ever%20%3CB%3E%20observing%20%3C%2FB%3E%20%3CI%3E%20%3C%2FI%3E%20any%20...%20changes.%22%26nbsp%3B%20This%20choice%20of%20words%20is%20deliberate.%26nbsp%3B%20In%20serializable%20isolation%20level%2C%20SQL%20Server%20acquires%20key%20range%20locks%20and%20holds%20them%20until%20the%20end%20of%20the%20transaction.%26nbsp%3B%20A%20key%20range%20lock%20ensures%20that%2C%20once%20a%20transaction%20reads%20data%2C%20no%20other%20transaction%20can%20alter%20that%20data%20-%20not%20even%20to%20insert%20phantom%20rows%20-%20until%20the%20transaction%20holding%20the%20lock%20completes.%26nbsp%3B%20In%20snapshot%20isolation%20level%2C%20SQL%20Server%20does%20not%20acquire%20any%20locks.%26nbsp%3B%20Thus%2C%20it%20is%20possible%20for%20a%20concurrent%20transaction%20to%20modify%20data%20that%20a%20second%20transaction%20has%20already%20read.%26nbsp%3B%20The%20second%20transaction%20simply%20does%20not%20%3CB%3E%20observe%20%3C%2FB%3E%20the%20changes%20and%20continues%20to%20read%20an%20old%20copy%20of%20the%20data.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESerializable%20isolation%20level%20relies%20on%20pessimistic%20concurrency%20control.%26nbsp%3B%20It%20guarantees%20consistency%20by%20assuming%20that%20two%20transactions%20might%20try%20to%20update%20the%20same%20data%20and%20uses%20locks%20to%20ensure%20that%20they%20do%20not%20but%20at%20a%20cost%20of%20reduced%20concurrency%20-%20one%20transaction%20must%20wait%20for%20the%20other%20to%20complete%20and%20two%20transactions%20can%20deadlock.%26nbsp%3B%20Snapshot%20isolation%20level%20relies%20on%20optimistic%20concurrency%20control.%26nbsp%3B%20It%20allows%20transactions%20to%20proceed%20without%20locks%20and%20with%20maximum%20concurrency%2C%20but%20may%20need%20to%20fail%20and%20rollback%20a%20transaction%20if%20two%20transactions%20attempt%20to%20modify%20the%20same%20data%20at%20the%20same%20time.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIt%20is%20clear%20there%20are%20differences%20in%20the%20level%20of%20concurrency%20that%20can%20be%20achieved%20and%20in%20the%20failures%20(deadlocks%20vs.%20update%20conflicts)%20that%20are%20possible%20with%20the%20serializable%20and%20snapshot%20isolation%20levels.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EHow%20about%20transaction%20isolation%3F%26nbsp%3B%20How%20do%20serializable%20and%20snapshot%20differ%20in%20terms%20of%20the%20transaction%20isolation%20that%20they%20confer%3F%26nbsp%3B%20It%20is%20simple%20to%20understand%20serializable.%26nbsp%3B%20For%20the%20outcome%20of%20two%20transactions%20to%20be%20considered%20serializable%2C%20it%20must%20be%20possible%20to%20achieve%20this%20outcome%20by%20running%20%3CB%3E%20one%20transaction%20at%20a%20time%20%3C%2FB%3E%20in%20some%20order.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESnapshot%20does%20%3CB%3E%20not%20%3C%2FB%3E%20guarantee%20this%20level%20of%20isolation.%26nbsp%3B%20A%20few%20years%20ago%2C%20%3CA%20href%3D%22http%3A%2F%2Fresearch.microsoft.com%2F~Gray%2F%22%20mce_href%3D%22http%3A%2F%2Fresearch.microsoft.com%2F~Gray%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Jim%20Gray%20%3C%2FA%3E%20shared%20with%20me%20the%20following%20excellent%20example%20of%20the%20difference.%26nbsp%3B%20Imagine%20that%20we%20have%20a%20bag%20containing%20a%20mixture%20of%20white%20and%20black%20marbles.%26nbsp%3B%20Suppose%20that%20we%20want%20to%20run%20two%20transactions.%26nbsp%3B%20One%20transaction%20turns%20each%20of%20the%20white%20marbles%20into%20black%20marbles.%26nbsp%3B%20The%20second%20transaction%20turns%20each%20of%20the%20black%20marbles%20into%20white%20marbles.%26nbsp%3B%20If%20we%20run%20these%20transactions%20under%20serializable%20isolation%2C%20we%20must%20run%20them%20one%20at%20a%20time.%26nbsp%3B%20The%20first%20transaction%20will%20leave%20a%20bag%20with%20marbles%20of%20only%20one%20color.%26nbsp%3B%20After%20that%2C%20the%20second%20transaction%20will%20change%20all%20of%20these%20marbles%20to%20the%20other%20color.%26nbsp%3B%20There%20are%20only%20two%20possible%20outcomes%3A%26nbsp%3B%20a%20bag%20with%20only%20white%20marbles%20or%20a%20bag%20with%20only%20black%20marbles.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20we%20run%20these%20transactions%20under%20snapshot%20isolation%2C%20there%20is%20a%20third%20outcome%20that%20is%20not%20possible%20under%20serializable%20isolation.%26nbsp%3B%20Each%20transaction%20can%20simultaneously%20take%20a%20snapshot%20of%20the%20bag%20of%20marbles%20as%20it%20exists%20before%20we%20make%20any%20changes.%26nbsp%3B%20Now%20one%20transaction%20finds%20the%20white%20marbles%20and%20turns%20them%20into%20black%20marbles.%26nbsp%3B%20At%20the%20same%20time%2C%20the%20other%20transactions%20finds%20the%20black%20marbles%20-%20but%20only%20those%20marbles%20that%20where%20black%20when%20we%20took%20the%20snapshot%20-%20not%20those%20marbles%20that%20the%20first%20transaction%20changed%20to%20black%20-%20and%20turns%20them%20into%20white%20marbles.%26nbsp%3B%20In%20the%20end%2C%20we%20still%20have%20a%20mixed%20bag%20of%20marbles%20with%20some%20white%20and%20some%20black.%26nbsp%3B%20In%20fact%2C%20we%20have%20precisely%20switched%20each%20marble.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20following%20graphic%20illustrates%20the%20difference%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97254iDB450CA755260E29%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWe%20can%20demonstrate%20this%20outcome%20using%20SQL%20Server.%26nbsp%3B%20Note%20that%20snapshot%20isolation%20is%20only%20available%20in%20SQL%20Server%202005%20and%20must%20be%20explicitly%20enabled%20on%20your%20database%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ealter%20database%20%3CI%3E%20database_name%20%3C%2FI%3E%20set%20allow_snapshot_isolation%20on%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EBegin%20by%20creating%20a%20simple%20table%20with%20two%20rows%20representing%20two%20marbles%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20marbles%20(id%20int%20primary%20key%2C%20color%20char(5))%20%3CBR%20%2F%3E%20insert%20marbles%20values(1%2C%20'Black')%20%3CBR%20%2F%3E%20insert%20marbles%20values(2%2C%20'White')%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ENext%2C%20in%20session%201%20begin%20a%20snaphot%20transaction%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eset%20transaction%20isolation%20level%20snapshot%20%3CBR%20%2F%3E%20begin%20tran%20%3CBR%20%2F%3E%20update%20marbles%20set%20color%20%3D%20'White'%20where%20color%20%3D%20'Black'%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20before%20committing%20the%20changes%2C%20run%20the%20following%20in%20session%202%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eset%20transaction%20isolation%20level%20snapshot%20%3CBR%20%2F%3E%20begin%20tran%20%3CBR%20%2F%3E%20update%20marbles%20set%20color%20%3D%20'Black'%20where%20color%20%3D%20'White'%20%3CBR%20%2F%3E%20commit%20tran%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20commit%20the%20transaction%20in%20session%201%20and%20check%20the%20data%20in%20the%20table%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ecommit%20tran%20%3CBR%20%2F%3E%20select%20*%20from%20marbles%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EHere%20are%20the%20results%3A%3C%2FP%3Eid%20color%20%3CBR%20%2F%3E%20-----------%20-----%20%3CBR%20%2F%3E%201%20White%20%3CBR%20%2F%3E%202%20Black%20%3CBR%20%2F%3E%3CP%3EAs%20you%20can%20see%20marble%201%20which%20started%20out%20black%20is%20now%20white%20and%20marble%202%20which%20started%20out%20white%20is%20now%20black.%26nbsp%3B%20If%20you%20try%20this%20same%20experiment%20with%20serializable%20isolation%2C%20one%20transaction%20will%20wait%20for%20the%20other%20to%20complete%20and%2C%20depending%20on%20the%20order%2C%20both%20marbles%20will%20end%20up%20either%20white%20or%20black.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383281%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20May%2016%2C%202007%20Both%20the%20serializable%20and%20snapshot%20isolation%20levels%20provide%20a%20read%20consistent%20view%20of%20the%20database%20to%20all%20transactions.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383281%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on May 16, 2007

Both the serializable and snapshot isolation levels provide a read consistent view of the database to all transactions.  In either of these isolation levels, a transaction can only read data that has been committed.  Moreover, a transaction can read the same data multiple times without ever observing any concurrent transactions making changes to this data.  The unexpected read committed and repeatable read results that I demonstrated in my prior few posts are not possible in serializable or snapshot isolation level.


Notice that I used the phrase "without ever observing any ... changes."  This choice of words is deliberate.  In serializable isolation level, SQL Server acquires key range locks and holds them until the end of the transaction.  A key range lock ensures that, once a transaction reads data, no other transaction can alter that data - not even to insert phantom rows - until the transaction holding the lock completes.  In snapshot isolation level, SQL Server does not acquire any locks.  Thus, it is possible for a concurrent transaction to modify data that a second transaction has already read.  The second transaction simply does not observe the changes and continues to read an old copy of the data.


Serializable isolation level relies on pessimistic concurrency control.  It guarantees consistency by assuming that two transactions might try to update the same data and uses locks to ensure that they do not but at a cost of reduced concurrency - one transaction must wait for the other to complete and two transactions can deadlock.  Snapshot isolation level relies on optimistic concurrency control.  It allows transactions to proceed without locks and with maximum concurrency, but may need to fail and rollback a transaction if two transactions attempt to modify the same data at the same time.


It is clear there are differences in the level of concurrency that can be achieved and in the failures (deadlocks vs. update conflicts) that are possible with the serializable and snapshot isolation levels.


How about transaction isolation?  How do serializable and snapshot differ in terms of the transaction isolation that they confer?  It is simple to understand serializable.  For the outcome of two transactions to be considered serializable, it must be possible to achieve this outcome by running one transaction at a time in some order.


Snapshot does not guarantee this level of isolation.  A few years ago, Jim Gray shared with me the following excellent example of the difference.  Imagine that we have a bag containing a mixture of white and black marbles.  Suppose that we want to run two transactions.  One transaction turns each of the white marbles into black marbles.  The second transaction turns each of the black marbles into white marbles.  If we run these transactions under serializable isolation, we must run them one at a time.  The first transaction will leave a bag with marbles of only one color.  After that, the second transaction will change all of these marbles to the other color.  There are only two possible outcomes:  a bag with only white marbles or a bag with only black marbles.


If we run these transactions under snapshot isolation, there is a third outcome that is not possible under serializable isolation.  Each transaction can simultaneously take a snapshot of the bag of marbles as it exists before we make any changes.  Now one transaction finds the white marbles and turns them into black marbles.  At the same time, the other transactions finds the black marbles - but only those marbles that where black when we took the snapshot - not those marbles that the first transaction changed to black - and turns them into white marbles.  In the end, we still have a mixed bag of marbles with some white and some black.  In fact, we have precisely switched each marble.


The following graphic illustrates the difference:



We can demonstrate this outcome using SQL Server.  Note that snapshot isolation is only available in SQL Server 2005 and must be explicitly enabled on your database:



alter database database_name set allow_snapshot_isolation on


Begin by creating a simple table with two rows representing two marbles:



create table marbles (id int primary key, color char(5))
insert marbles values(1, 'Black')
insert marbles values(2, 'White')


Next, in session 1 begin a snaphot transaction:



set transaction isolation level snapshot
begin tran
update marbles set color = 'White' where color = 'Black'


Now, before committing the changes, run the following in session 2:



set transaction isolation level snapshot
begin tran
update marbles set color = 'Black' where color = 'White'
commit tran


Finally, commit the transaction in session 1 and check the data in the table:



commit tran
select * from marbles


Here are the results:

id color
----------- -----
1 White
2 Black

As you can see marble 1 which started out black is now white and marble 2 which started out white is now black.  If you try this same experiment with serializable isolation, one transaction will wait for the other to complete and, depending on the order, both marbles will end up either white or black.