<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Markus Machel</title>
    <description>The latest articles on DEV Community by Markus Machel (@gandacraft).</description>
    <link>https://dev.to/gandacraft</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4280846%2F61beb3a6-03e7-4df0-be87-3e31fb2293d4.png</url>
      <title>DEV Community: Markus Machel</title>
      <link>https://dev.to/gandacraft</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gandacraft"/>
    <language>en</language>
    <item>
      <title>Transaction isolation levels</title>
      <dc:creator>Markus Machel</dc:creator>
      <pubDate>Thu, 03 Dec 2026 14:31:56 +0000</pubDate>
      <link>https://dev.to/gandacraft/transaction-isolation-levels-19j0</link>
      <guid>https://dev.to/gandacraft/transaction-isolation-levels-19j0</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://csfundamentalstrainer.com/guides/transaction-isolation-levels" rel="noopener noreferrer"&gt;CS Fundamentals Trainer&lt;/a&gt;, where the guide has interactive code tabs and diagrams.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When two transactions touch the same data at the same time, the database has to decide what each of them is allowed to see of the other. That decision is the &lt;strong&gt;isolation level&lt;/strong&gt;, the "I" in ACID. Choose too weak a level and you get bugs that appear once a month under load and vanish when you try to reproduce them. Choose too strong a level and you pay in speed and in transactions that must be retried. This guide shows the problems that can happen, the four standard levels, what real databases actually do, and the tools you have to fix each problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What isolation promises
&lt;/h2&gt;

&lt;p&gt;A transaction groups statements so that they succeed or fail together. Isolation adds a promise about concurrency: &lt;strong&gt;each transaction behaves as if it ran alone&lt;/strong&gt;, even though many run at once.&lt;/p&gt;

&lt;p&gt;The strongest form of that promise is &lt;strong&gt;serializability&lt;/strong&gt;: the outcome of running transactions concurrently is the same as if they had run one after the other, in some order. Real systems often weaken the promise to go faster, and the SQL standard names the weaker levels by the odd behaviours they still allow. Those behaviours are called &lt;strong&gt;anomalies&lt;/strong&gt;, and you need to be able to recognise each.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The anomalies
&lt;/h2&gt;

&lt;p&gt;Each example shows two transactions, T1 and T2, with time running downwards.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dirty read
&lt;/h3&gt;

&lt;p&gt;T2 reads data that T1 has written but not committed. If T1 rolls back, T2 acted on something that never existed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UPDATE accounts SET balance = 0 WHERE id = 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT balance FROM accounts WHERE id = 1&lt;/code&gt; → &lt;strong&gt;0&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROLLBACK&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(decides, based on 0, to refuse an order)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Non-repeatable read
&lt;/h3&gt;

&lt;p&gt;T1 reads a row twice and gets different answers, because T2 changed and committed it in between.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT price FROM items WHERE id = 7&lt;/code&gt; → 10&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;UPDATE items SET price = 12 WHERE id = 7&lt;/code&gt;, &lt;code&gt;COMMIT&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT price FROM items WHERE id = 7&lt;/code&gt; → &lt;strong&gt;12&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Phantom read
&lt;/h3&gt;

&lt;p&gt;T1 runs the same &lt;em&gt;query&lt;/em&gt; twice and gets a different &lt;em&gt;set of rows&lt;/em&gt;, because T2 inserted (or deleted) a row that matches.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT count(*) FROM orders WHERE total &amp;gt; 100&lt;/code&gt; → 5&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;INSERT INTO orders (total) VALUES (150)&lt;/code&gt;, &lt;code&gt;COMMIT&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT count(*) FROM orders WHERE total &amp;gt; 100&lt;/code&gt; → &lt;strong&gt;6&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The difference from the non-repeatable read: no row T1 had already seen changed. A new row appeared. Locking the rows you read cannot stop that, because the row did not exist yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lost update
&lt;/h3&gt;

&lt;p&gt;Two transactions read a value, calculate a new one, and write it back. The second write overwrites the first, which is silently lost.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2FJSUgdGl0bGU6IExvc3QgdXBkYXRlOiB0d28gZGVwb3NpdHMgb2YgMTAsIG9ubHkgb25lIGNvdW50cwpzZXF1ZW5jZURpYWdyYW0KICAgIHBhcnRpY2lwYW50IFQxCiAgICBwYXJ0aWNpcGFudCBEQiBhcyBEYXRhYmFzZSAoYmFsYW5jZSAxMDApCiAgICBwYXJ0aWNpcGFudCBUMgogICAgVDEtPj5EQjogcmVhZCBiYWxhbmNlICgxMDApCiAgICBUMi0-PkRCOiByZWFkIGJhbGFuY2UgKDEwMCkKICAgIFQxLT4-REI6IHdyaXRlIDExMCBhbmQgY29tbWl0CiAgICBUMi0-PkRCOiB3cml0ZSAxMTAgYW5kIGNvbW1pdAogICAgTm90ZSBvdmVyIERCOiBTaG91bGQgYmUgMTIwLiBUaGUgZmlyc3QgZGVwb3NpdCBpcyBsb3N0Lg%3D%3D%3Ftype%3Dpng%26bgColor%3DFFFFFF" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2FJSUgdGl0bGU6IExvc3QgdXBkYXRlOiB0d28gZGVwb3NpdHMgb2YgMTAsIG9ubHkgb25lIGNvdW50cwpzZXF1ZW5jZURpYWdyYW0KICAgIHBhcnRpY2lwYW50IFQxCiAgICBwYXJ0aWNpcGFudCBEQiBhcyBEYXRhYmFzZSAoYmFsYW5jZSAxMDApCiAgICBwYXJ0aWNpcGFudCBUMgogICAgVDEtPj5EQjogcmVhZCBiYWxhbmNlICgxMDApCiAgICBUMi0-PkRCOiByZWFkIGJhbGFuY2UgKDEwMCkKICAgIFQxLT4-REI6IHdyaXRlIDExMCBhbmQgY29tbWl0CiAgICBUMi0-PkRCOiB3cml0ZSAxMTAgYW5kIGNvbW1pdAogICAgTm90ZSBvdmVyIERCOiBTaG91bGQgYmUgMTIwLiBUaGUgZmlyc3QgZGVwb3NpdCBpcyBsb3N0Lg%3D%3D%3Ftype%3Dpng%26bgColor%3DFFFFFF" alt="Diagram" width="672" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the bug behind "read, change in application code, save" patterns, and the classic case of two people editing the same record.&lt;/p&gt;

&lt;p&gt;Whether the database stops it depends on the engine and the level. In my test on PostgreSQL 16, &lt;strong&gt;Read Committed&lt;/strong&gt; let it happen (final balance 110), while &lt;strong&gt;Repeatable Read&lt;/strong&gt; failed the second write with &lt;code&gt;could not serialize access due to concurrent update&lt;/code&gt; (SQLSTATE &lt;code&gt;40001&lt;/code&gt;), so the application could retry. That check is PostgreSQL's; MySQL's InnoDB at Repeatable Read does not raise an error here, because its updates always act on the latest committed version of the row. I could not run MySQL, so that last point rests on its documentation [1]. Do not rely on the level to save you: use one of the fixes in section 5.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write skew
&lt;/h3&gt;

&lt;p&gt;Two transactions each read the same set of rows, each decide the rule still holds, and each write to a &lt;em&gt;different&lt;/em&gt; row. Neither overwrote the other, yet together they break the rule.&lt;/p&gt;

&lt;p&gt;The standard example: a hospital needs at least one doctor on call. Alice and Bob are both on call, and both ask to go off call at the same moment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2FJSUgdGl0bGU6IFdyaXRlIHNrZXc6IGJvdGggZG9jdG9ycyBsZWF2ZSwgbm9ib2R5IGlzIG9uIGNhbGwKc2VxdWVuY2VEaWFncmFtCiAgICBwYXJ0aWNpcGFudCBBIGFzIFQxIChBbGljZSkKICAgIHBhcnRpY2lwYW50IERCIGFzIERhdGFiYXNlCiAgICBwYXJ0aWNpcGFudCBCIGFzIFQyIChCb2IpCiAgICBBLT4-REI6IFNFTEVDVCBjb3VudCgqKSBXSEVSRSBvbl9jYWxsICgyKQogICAgQi0-PkRCOiBTRUxFQ1QgY291bnQoKikgV0hFUkUgb25fY2FsbCAoMikKICAgIEEtPj5EQjogVVBEQVRFIEFsaWNlIFNFVCBvbl9jYWxsID0gZmFsc2UKICAgIEItPj5EQjogVVBEQVRFIEJvYiBTRVQgb25fY2FsbCA9IGZhbHNlCiAgICBBLT4-REI6IENPTU1JVAogICAgQi0-PkRCOiBDT01NSVQKICAgIE5vdGUgb3ZlciBEQjogWmVybyBkb2N0b3JzIG9uIGNhbGwuIEVhY2ggc2F3IDIgYW5kIGVhY2ggZGlkIG5vdGhpbmcgIndyb25nIi4%3D%3Ftype%3Dpng%26bgColor%3DFFFFFF" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2FJSUgdGl0bGU6IFdyaXRlIHNrZXc6IGJvdGggZG9jdG9ycyBsZWF2ZSwgbm9ib2R5IGlzIG9uIGNhbGwKc2VxdWVuY2VEaWFncmFtCiAgICBwYXJ0aWNpcGFudCBBIGFzIFQxIChBbGljZSkKICAgIHBhcnRpY2lwYW50IERCIGFzIERhdGFiYXNlCiAgICBwYXJ0aWNpcGFudCBCIGFzIFQyIChCb2IpCiAgICBBLT4-REI6IFNFTEVDVCBjb3VudCgqKSBXSEVSRSBvbl9jYWxsICgyKQogICAgQi0-PkRCOiBTRUxFQ1QgY291bnQoKikgV0hFUkUgb25fY2FsbCAoMikKICAgIEEtPj5EQjogVVBEQVRFIEFsaWNlIFNFVCBvbl9jYWxsID0gZmFsc2UKICAgIEItPj5EQjogVVBEQVRFIEJvYiBTRVQgb25fY2FsbCA9IGZhbHNlCiAgICBBLT4-REI6IENPTU1JVAogICAgQi0-PkRCOiBDT01NSVQKICAgIE5vdGUgb3ZlciBEQjogWmVybyBkb2N0b3JzIG9uIGNhbGwuIEVhY2ggc2F3IDIgYW5kIGVhY2ggZGlkIG5vdGhpbmcgIndyb25nIi4%3D%3Ftype%3Dpng%26bgColor%3DFFFFFF" alt="Diagram" width="892" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Write skew is the anomaly people forget, and it is the one that separates "snapshot" levels from truly serializable ones. It appears in bookings (two people reserve the last room), unique usernames without a unique constraint, and any "check, then act" across several rows [2][3].&lt;/p&gt;

&lt;h3&gt;
  
  
  Read skew
&lt;/h3&gt;

&lt;p&gt;A cousin of the non-repeatable read, seen across &lt;em&gt;two rows&lt;/em&gt; that belong together. T1 reads the first row, T2 moves money between the two rows and commits, then T1 reads the second row, and T1's picture of the data never existed. Take two accounts with 100 each (200 in total). T1 reads account 1 (100). T2 moves 50 from account 1 to account 2 and commits. T1 reads account 2:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level (PostgreSQL 16)&lt;/th&gt;
&lt;th&gt;What T1 saw&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;100 + 150&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;250&lt;/strong&gt; (money appeared)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;100 + 100&lt;/td&gt;
&lt;td&gt;200 (a consistent snapshot)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Backups and reports are the classic victims, which is why a long read needs a snapshot (section 6).&lt;/p&gt;

&lt;h3&gt;
  
  
  Try write skew yourself
&lt;/h3&gt;

&lt;p&gt;You can watch the doctors example happen in two &lt;code&gt;psql&lt;/code&gt; windows (A and B). First, once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;doctors&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_call&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;doctors&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'bob'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, step by step (the number of the step is the order in which you press Enter):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Window A&lt;/th&gt;
&lt;th&gt;Window B&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;BEGIN ISOLATION LEVEL REPEATABLE READ;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;BEGIN ISOLATION LEVEL REPEATABLE READ;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT count(*) FROM doctors WHERE on_call;&lt;/code&gt; → 2&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SELECT count(*) FROM doctors WHERE on_call;&lt;/code&gt; → 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UPDATE doctors SET on_call = false WHERE name = 'alice';&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UPDATE doctors SET on_call = false WHERE name = 'bob';&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;COMMIT;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;COMMIT;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both commits succeed, and &lt;code&gt;SELECT count(*) FROM doctors WHERE on_call;&lt;/code&gt; now returns &lt;strong&gt;0&lt;/strong&gt;. Reset (&lt;code&gt;UPDATE doctors SET on_call = true;&lt;/code&gt;) and repeat with &lt;code&gt;ISOLATION LEVEL SERIALIZABLE&lt;/code&gt; in both windows. The first commit goes through; the second fails with &lt;code&gt;ERROR: could not serialize access due to read/write dependencies among transactions&lt;/code&gt; (SQLSTATE &lt;code&gt;40001&lt;/code&gt;, and the hint says it might succeed if retried). Once more at Read Committed with &lt;code&gt;SELECT name FROM doctors WHERE on_call FOR UPDATE;&lt;/code&gt; as the first query in each window: B now waits at that query until A commits, then sees only one doctor on call and can refuse.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The four standard levels
&lt;/h2&gt;

&lt;p&gt;The SQL standard defines the levels by which of the first three anomalies they must prevent:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Dirty read&lt;/th&gt;
&lt;th&gt;Non-repeatable read&lt;/th&gt;
&lt;th&gt;Phantom read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Read Uncommitted&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Read Committed&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Repeatable Read&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Serializable&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That table is the textbook answer, and it is where many people stop. It is also incomplete: it says nothing about lost update or write skew, and the 1995 paper "A Critique of ANSI SQL Isolation Levels" showed that the standard's definitions are ambiguous and miss several anomalies, including &lt;strong&gt;snapshot isolation&lt;/strong&gt;, which many databases offer under the name "Repeatable Read" [2].&lt;/p&gt;

&lt;h3&gt;
  
  
  What you really get
&lt;/h3&gt;

&lt;p&gt;The names mean different things in different databases. Always read your database's documentation, not the textbook table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Database&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;th&gt;"Repeatable Read" is really...&lt;/th&gt;
&lt;th&gt;"Serializable" is really...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;Snapshot isolation: no dirty reads, non-repeatable reads, or phantoms; &lt;strong&gt;write skew is possible&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Serializable Snapshot Isolation (SSI): truly serializable, by aborting transactions that could cause an anomaly [4][5]
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MySQL (InnoDB)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;Reads use a snapshot taken at the first read; locking reads and writes use next-key locks, which also block phantoms [1]
&lt;/td&gt;
&lt;td&gt;Plain reads also take shared locks, so readers can block writers [1]
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SQL Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Read Committed, which uses locks unless &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; is on (it is on by default in Azure SQL Database, off in SQL Server)&lt;/td&gt;
&lt;td&gt;Lock-based. A separate &lt;code&gt;SNAPSHOT&lt;/code&gt; level gives a transaction-wide snapshot, once &lt;code&gt;ALLOW_SNAPSHOT_ISOLATION&lt;/code&gt; is switched on for the database&lt;/td&gt;
&lt;td&gt;Lock-based; see the Microsoft page for the lock details [6]
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Oracle&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;(not offered; Oracle offers Read Committed, Serializable and a read-only mode)&lt;/td&gt;
&lt;td&gt;Read consistency for the whole transaction, with &lt;code&gt;ORA-08177: Cannot serialize access&lt;/code&gt; when a row it wants to change was changed after it began. This is generally understood to be snapshot isolation, so write skew would still be possible; Oracle's page does not use that term or discuss write skew [7]
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;SQL Server has two switches that change a lot. With &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; on, the ordinary Read Committed level reads row versions (each statement sees a snapshot taken at its start) instead of taking shared locks, so readers stop blocking writers; it is a database-wide setting, and applications that relied on the blocking can behave differently. &lt;code&gt;SNAPSHOT&lt;/code&gt; isolation gives a snapshot for the whole transaction and has to be allowed per database first [6].&lt;/p&gt;

&lt;p&gt;Two facts from PostgreSQL's documentation are worth remembering: asking for Read Uncommitted gives you Read Committed (dirty reads are not possible at all), and Repeatable Read there is stronger than the standard requires because it also prevents phantoms [4].&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How databases do it
&lt;/h2&gt;

&lt;p&gt;There are two main techniques, and many systems mix them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Locking (two-phase locking).&lt;/strong&gt; A transaction locks what it reads and writes and holds the locks until it ends. Readers block writers and writers block readers. It gives serializability but can be slow under contention and causes deadlocks, which the database resolves by aborting one transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiversion concurrency control (MVCC).&lt;/strong&gt; The database keeps several versions of each row. A transaction reads from a &lt;strong&gt;snapshot&lt;/strong&gt;, a consistent picture of the data at one moment, so &lt;strong&gt;readers do not block writers and writers do not block readers&lt;/strong&gt;. This is how PostgreSQL, Oracle, and InnoDB's consistent reads work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With MVCC, the isolation level is mostly about &lt;em&gt;when the snapshot is taken&lt;/em&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level (PostgreSQL)&lt;/th&gt;
&lt;th&gt;The snapshot is taken...&lt;/th&gt;
&lt;th&gt;So a repeated read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;at the start of &lt;strong&gt;each statement&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;can see new commits between statements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;at the start of the &lt;strong&gt;first statement&lt;/strong&gt; of the transaction&lt;/td&gt;
&lt;td&gt;always sees the same data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;like Repeatable Read, plus tracking of read/write dependencies&lt;/td&gt;
&lt;td&gt;same, and the database aborts a transaction when a cycle could form&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Snapshots explain both the good and the bad of "Repeatable Read": reports see a stable picture of the data (good), but two transactions each work on their own picture and can both act on facts that the other is changing (write skew).&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Preventing the problems
&lt;/h2&gt;

&lt;p&gt;You have more tools than just "raise the level". Often a smaller change is better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do the change in the database, in one statement
&lt;/h3&gt;

&lt;p&gt;A lost update needs a &lt;em&gt;read, calculate, write&lt;/em&gt; in the application. Make it a single statement and the database does the arithmetic on the current value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- unsafe: the application read the balance earlier&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;110&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- safe: the database calculates from the current value&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In PostgreSQL's Read Committed, if another transaction changed the row meanwhile, the update waits for it to commit, then re-reads the new version of the row and applies the change to that [4]. That is why this simple form is safe.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lock the rows you decide on
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; locks the rows it returns until the transaction ends, so anyone else who wants to change or lock them waits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;doctors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;on_call&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- lock everyone currently on call&lt;/span&gt;
&lt;span class="c1"&gt;-- if the count is 1, refuse; otherwise:&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;doctors&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;on_call&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the two doctors serialise: the second transaction waits for the first, then sees the new state (or, at Repeatable Read, receives a serialization error and retries). Use &lt;code&gt;FOR UPDATE&lt;/code&gt; when you know which rows matter. It does not help against phantoms (a row that does not exist yet cannot be locked).&lt;/p&gt;

&lt;h3&gt;
  
  
  Other locking tools worth knowing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SELECT ... FOR SHARE&lt;/code&gt;&lt;/strong&gt; takes a shared lock: others can read-lock the same rows too, but nobody can update or delete them until you finish. Use it to make sure a parent row stays put while you insert rows that refer to it (foreign key checks use the same idea).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;&lt;/strong&gt; is the standard way to build a job queue in the database. Each worker claims a row that nobody else holds and skips the ones that are locked, instead of waiting in line:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'taken'&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'new'&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my test, worker A got job 1 and worker B, running at the same time, got job 2 without waiting. (The result is "some job", not a strict order, so this suits queues where order is not a promise.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Advisory locks&lt;/strong&gt; (&lt;code&gt;pg_advisory_xact_lock(key)&lt;/code&gt; in PostgreSQL) lock a number you choose, not a row. They protect things that are not rows: "only one worker may rebuild customer 42's report", "only one instance runs this scheduled job". The &lt;code&gt;_xact_&lt;/code&gt; version is released automatically at the end of the transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimistic locking: a version column
&lt;/h3&gt;

&lt;p&gt;For "read it, edit it for a while, save it" flows such as a form, do not hold a lock while a person types. Keep a version number, and make the update conditional on it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;documents&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;-- $3 is the version you loaded&lt;/span&gt;
&lt;span class="c1"&gt;-- 0 rows updated?  Someone else saved first: tell the user, or merge.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Let a constraint be the judge
&lt;/h3&gt;

&lt;p&gt;If the rule can be written as a constraint, let the database enforce it. A &lt;code&gt;UNIQUE&lt;/code&gt; index stops two transactions both claiming the same username, at any isolation level. A PostgreSQL exclusion constraint can say "no two bookings overlap for the same room". Constraints are checked against the latest data, so they close gaps that snapshots leave open.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Serializable, and retry
&lt;/h3&gt;

&lt;p&gt;When the rule is complicated and you cannot lock or constrain it, ask for &lt;code&gt;SERIALIZABLE&lt;/code&gt; and let the database detect the problems. The price is that it will occasionally abort a transaction with a &lt;strong&gt;serialization failure&lt;/strong&gt; (SQLSTATE &lt;code&gt;40001&lt;/code&gt; in PostgreSQL), and your code &lt;strong&gt;must retry the whole transaction&lt;/strong&gt; [4]. A deadlock (&lt;code&gt;40P01&lt;/code&gt;, section 7) needs exactly the same treatment, so the wrappers below retry on both. That means a transaction is a function you can run again, without side effects outside the database (no emails sent in the middle, no external calls). Here is the retry wrapper in four languages (pick a language on the tabs; your choice is remembered):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;

&lt;span class="no"&gt;RETRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SerializationFailure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DeadlockDetected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 40001 and 40P01&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_serializable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conninfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;work&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conninfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;       &lt;span class="c1"&gt;# a pool in real code&lt;/span&gt;
                &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isolation_level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsolationLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SERIALIZABLE&lt;/span&gt;
                &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="no"&gt;RETRY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c1"&gt;# back off a little, then try again&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"too many serialization failures"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The &lt;a href="https://csfundamentalstrainer.com/guides/transaction-isolation-levels" rel="noopener noreferrer"&gt;original guide&lt;/a&gt; also shows this in Go, JavaScript and C#.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In PostgreSQL, Repeatable Read can fail the same way (an update of a row that another transaction changed meanwhile raises &lt;code&gt;could not serialize access due to concurrent update&lt;/code&gt;), so the same wrapper protects you there.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Choosing a level
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;A sensible choice&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Typical web request: load a row, change it, save it&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Read Committed&lt;/strong&gt;, with atomic updates, optimistic version columns, and constraints&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A report or an export that must see one consistent moment&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Repeatable Read&lt;/strong&gt; (read-only), so the numbers add up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A rule that spans several rows and you cannot lock or constrain it&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Serializable&lt;/strong&gt;, with retries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Money movement&lt;/td&gt;
&lt;td&gt;Atomic updates or explicit row locks in a fixed order (to avoid deadlocks), inside Read Committed or stronger; add constraints such as &lt;code&gt;CHECK (balance &amp;gt;= 0)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Counters and statistics where small errors do not matter&lt;/td&gt;
&lt;td&gt;Whatever is cheapest&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a long report or export that must see one consistent picture and must never be cancelled halfway, PostgreSQL has &lt;code&gt;BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE&lt;/code&gt;. The transaction may wait at the start until it can get a snapshot that is guaranteed free of anomalies, and then it runs without any risk of a serialization failure [4].&lt;/p&gt;

&lt;p&gt;A higher level is not a free upgrade: more aborted transactions, more memory for tracking, and a need to retry in code that may never have expected failure. Raise it for the specific transactions that need it, not for the whole database. Most PostgreSQL applications run on Read Committed with a handful of &lt;code&gt;FOR UPDATE&lt;/code&gt; and unique constraints, and are correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Deadlocks are a different problem
&lt;/h2&gt;

&lt;p&gt;Deadlocks happen when T1 waits for a lock that T2 holds while T2 waits for one that T1 holds. Locking creates them, at any isolation level. The database detects the cycle and aborts one transaction (&lt;code&gt;40P01&lt;/code&gt; in PostgreSQL). Reduce them by locking rows in a consistent order (always the smaller account id first), by keeping transactions short, and by retrying when one happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Isolation across more than one database
&lt;/h2&gt;

&lt;p&gt;Everything above is about one database. Once a business operation spans several services, a database transaction cannot cover it, and you use sagas, outbox patterns and idempotent retries instead. Related terms you may hear:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Serializability&lt;/strong&gt; is about transactions and their order; &lt;strong&gt;linearizability&lt;/strong&gt; is about a single object and real time (once a write finishes, every later read sees it). A database that is both is called &lt;strong&gt;strictly serializable&lt;/strong&gt;. Distributed databases advertise which one they provide, and the difference matters [8].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replica reads&lt;/strong&gt; may return old data even when the primary is fully serializable. Isolation levels describe the primary, not a lagging replica.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. Questions interviewers like to add
&lt;/h2&gt;

&lt;h3&gt;
  
  
  "What is the difference between a non-repeatable read and a phantom?"
&lt;/h3&gt;

&lt;p&gt;A non-repeatable read is a row you already saw that changed or disappeared. A phantom is a new row that now matches your query. Row locks can stop the first one but not the second.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Does Repeatable Read prevent phantoms?"
&lt;/h3&gt;

&lt;p&gt;By the standard, no. In PostgreSQL and in InnoDB's consistent reads, yes (through snapshots), but write skew is still possible in PostgreSQL's version. This is why "it depends on the database" is the correct start to the answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  "How do you avoid overselling the last item in stock?"
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;UPDATE items SET stock = stock - 1 WHERE id = $1 AND stock &amp;gt; 0&lt;/code&gt;, and check that one row was updated. The condition and the change happen atomically; no isolation level needed beyond Read Committed.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Why not always use Serializable?"
&lt;/h3&gt;

&lt;p&gt;It costs throughput and forces retries. Often a unique constraint or one &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; gets the same safety with less cost. Use it where the invariant is hard to express any other way.&lt;/p&gt;

&lt;h3&gt;
  
  
  "What does MVCC cost?"
&lt;/h3&gt;

&lt;p&gt;Old row versions pile up and must be cleaned (&lt;code&gt;VACUUM&lt;/code&gt; in PostgreSQL). A long-running transaction keeps old versions alive and can make tables bloat. Keep transactions short.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Things to mention in an interview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Define isolation and name the six anomalies: dirty read, non-repeatable read, phantom, read skew, lost update, write skew.&lt;/li&gt;
&lt;li&gt;Give the four standard levels, then say the names differ between databases and name PostgreSQL's real behaviour.&lt;/li&gt;
&lt;li&gt;Explain snapshots (MVCC) and that the level decides when the snapshot is taken.&lt;/li&gt;
&lt;li&gt;Show the cheap fixes first: atomic updates, &lt;code&gt;FOR UPDATE&lt;/code&gt;, version columns, constraints.&lt;/li&gt;
&lt;li&gt;Say that Serializable means "retry on failure", and that the transaction must be safe to repeat.&lt;/li&gt;
&lt;li&gt;Mention that replicas and multiple services are a separate problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick recap
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Isolation decides what concurrent transactions can see of each other.&lt;/li&gt;
&lt;li&gt;Six anomalies: dirty read, non-repeatable read, phantom, read skew, lost update, write skew.&lt;/li&gt;
&lt;li&gt;The standard levels are Read Uncommitted, Read Committed, Repeatable Read, Serializable, but each database implements them differently.&lt;/li&gt;
&lt;li&gt;PostgreSQL's Repeatable Read is snapshot isolation (no phantoms, but write skew); only Serializable is fully safe.&lt;/li&gt;
&lt;li&gt;Fix most problems with atomic updates, &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, version columns and constraints.&lt;/li&gt;
&lt;li&gt;Under Serializable (and Repeatable Read), retry the whole transaction on &lt;code&gt;40001&lt;/code&gt; (and on a deadlock, &lt;code&gt;40P01&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Keep transactions short, lock in a consistent order, and raise the level only where it is needed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;MySQL documentation, &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html" rel="noopener noreferrer"&gt;InnoDB Transaction Isolation Levels&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Berenson et al., &lt;a href="https://arxiv.org/abs/cs/0701157" rel="noopener noreferrer"&gt;A Critique of ANSI SQL Isolation Levels&lt;/a&gt;, SIGMOD 1995. The anomalies, and snapshot isolation.&lt;/li&gt;
&lt;li&gt;Martin Kleppmann, &lt;a href="https://dataintensive.net/" rel="noopener noreferrer"&gt;Designing Data-Intensive Applications&lt;/a&gt;, chapter 7 (Transactions). Write skew and the weak isolation levels explained at length.&lt;/li&gt;
&lt;li&gt;PostgreSQL documentation, &lt;a href="https://www.postgresql.org/docs/current/transaction-iso.html" rel="noopener noreferrer"&gt;Transaction Isolation&lt;/a&gt;. What each level does in PostgreSQL, and the retry advice.&lt;/li&gt;
&lt;li&gt;Ports and Grittner, &lt;a href="https://arxiv.org/abs/1208.4179" rel="noopener noreferrer"&gt;Serializable Snapshot Isolation in PostgreSQL&lt;/a&gt;, VLDB 2012.&lt;/li&gt;
&lt;li&gt;Microsoft, &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql" rel="noopener noreferrer"&gt;SET TRANSACTION ISOLATION LEVEL (Transact-SQL)&lt;/a&gt;. SQL Server's levels, &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; and &lt;code&gt;SNAPSHOT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Oracle, &lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/data-concurrency-and-consistency.html" rel="noopener noreferrer"&gt;Data Concurrency and Consistency&lt;/a&gt;. Read committed, serializable and read-only modes, and &lt;code&gt;ORA-08177&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Jepsen, &lt;a href="https://jepsen.io/consistency" rel="noopener noreferrer"&gt;Consistency Models&lt;/a&gt;. A map of isolation and consistency models, with serializability and linearizability side by side.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Practise it:&lt;/strong&gt; &lt;a href="https://csfundamentalstrainer.com/?tag=Databases" rel="noopener noreferrer"&gt;CS Fundamentals Trainer&lt;/a&gt; has 850+ questions on Databases, with spaced repetition so the ones you miss come back. It's free, with no ads and no tracking.&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>sql</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
