[{"data":1,"prerenderedAt":150},["ShallowReactive",2],{"term-p\u002Fpessimistic-locking":3,"related-p\u002Fpessimistic-locking":132},{"id":4,"title":5,"acronym":6,"body":7,"category":114,"description":115,"difficulty":116,"extension":117,"letter":16,"meta":118,"navigation":77,"path":119,"related":120,"seo":126,"sitemap":127,"stem":130,"subcategory":6,"__hash__":131},"terms\u002Fterms\u002Fp\u002Fpessimistic-locking.md","Pessimistic Locking",null,{"type":8,"value":9,"toc":108},"minimark",[10,15,19,23,26,30,97,101,104],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 — The Vibe Check",[16,17,18],"p",{},"Pessimistic locking assumes conflicts are likely, so it locks the row the moment you read it. Nobody else can touch it until you are done. Use SELECT FOR UPDATE and the row is yours until you COMMIT. Great for 'only one person can buy the last ticket' scenarios.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"Pessimistic locking acquires an exclusive lock on a row (or set of rows) at read time using SELECT FOR UPDATE. The lock is held until the transaction commits or rolls back, preventing other transactions from reading or modifying the locked rows. It prevents race conditions but reduces concurrency and can cause deadlocks if not managed carefully.",[11,27,29],{"id":28},"show-me-the-code","Show Me The Code",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-sql shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","BEGIN;\n-- Lock the row so nobody else can change it\nSELECT * FROM tickets\nWHERE id = 1 AND available = true\nFOR UPDATE;\n\n-- Now safely decrement\nUPDATE tickets SET available = false WHERE id = 1;\nCOMMIT;\n","sql","",[38,39,40,48,54,60,66,72,79,85,91],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,47],{},"BEGIN;\n",[41,49,51],{"class":43,"line":50},2,[41,52,53],{},"-- Lock the row so nobody else can change it\n",[41,55,57],{"class":43,"line":56},3,[41,58,59],{},"SELECT * FROM tickets\n",[41,61,63],{"class":43,"line":62},4,[41,64,65],{},"WHERE id = 1 AND available = true\n",[41,67,69],{"class":43,"line":68},5,[41,70,71],{},"FOR UPDATE;\n",[41,73,75],{"class":43,"line":74},6,[41,76,78],{"emptyLinePlaceholder":77},true,"\n",[41,80,82],{"class":43,"line":81},7,[41,83,84],{},"-- Now safely decrement\n",[41,86,88],{"class":43,"line":87},8,[41,89,90],{},"UPDATE tickets SET available = false WHERE id = 1;\n",[41,92,94],{"class":43,"line":93},9,[41,95,96],{},"COMMIT;\n",[11,98,100],{"id":99},"when-youll-hear-this","When You'll Hear This",[16,102,103],{},"\"Use SELECT FOR UPDATE (pessimistic locking) to guarantee only one transaction can reserve the ticket.\" \u002F \"Pessimistic locking hurts throughput under high concurrency.\"",[105,106,107],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":50,"depth":50,"links":109},[110,111,112,113],{"id":13,"depth":50,"text":14},{"id":21,"depth":50,"text":22},{"id":28,"depth":50,"text":29},{"id":99,"depth":50,"text":100},"database","Pessimistic locking assumes conflicts are likely, so it locks the row the moment you read it. Nobody else can touch it until you are done.","advanced","md",{},"\u002Fterms\u002Fp\u002Fpessimistic-locking",[121,122,123,124,125],"Optimistic Locking","Locking","Deadlock","Transaction","Race Condition",{"title":5,"description":115},{"changefreq":128,"priority":129},"weekly",0.7,"terms\u002Fp\u002Fpessimistic-locking","Jtp1JWbnIYztQSPOpmiUpC3_P8r46BvmWYFiWbn9YRM",[133,138,141,144,147],{"title":123,"path":134,"acronym":6,"category":135,"difficulty":136,"description":137},"\u002Fterms\u002Fd\u002Fdeadlock","general","intermediate","A deadlock is when two things are each waiting for the other to go first, and neither ever does. Thread A holds Lock 1 and wants Lock 2.",{"title":122,"path":139,"acronym":6,"category":114,"difficulty":116,"description":140},"\u002Fterms\u002Fl\u002Flocking","Locking prevents two transactions from modifying the same data at the same time. It is how databases coordinate concurrent access.",{"title":121,"path":142,"acronym":6,"category":114,"difficulty":116,"description":143},"\u002Fterms\u002Fo\u002Foptimistic-locking","Optimistic locking assumes conflicts are rare so it does not lock the row upfront. Instead, it adds a version number to each row.",{"title":125,"path":145,"acronym":6,"category":135,"difficulty":136,"description":146},"\u002Fterms\u002Fr\u002Frace-condition","A race condition is when two parts of your code are racing to do something at the same time and the winner isn't guaranteed — leading to unexpected, hard-t...",{"title":124,"path":148,"acronym":6,"category":114,"difficulty":136,"description":149},"\u002Fterms\u002Ft\u002Ftransaction","A transaction groups multiple database operations into one all-or-nothing bundle. Either ALL of them succeed, or NONE of them happen.",1776518301710]