Choosing A Concurrency Model, Optimistic Or Pessimistic
1. Locking and Blocking with SQL Server Choosing a concurrency Model – Optimistic or Pessimistic Vinod Kumar M Technology Evangelist - Microsoft www.ExtremeExperts.com http://blogs.sqlxml.org/vinodkumar
2. Why Study Concurrency? Contention Degrades Performance Most Tuning Tools Don’t Consider Concurrency Issues You Can Write Better Applications
3. Concurrency Models Pessimistic Concurrency Preventative approach Limited concurrent access Uses locking to avoid conflicts Optimistic Concurrency Allows full concurrent access Row versioning allows access to consistent data Conflict detection avoids inconsistent updates
7. Read X (10) Compute X=X+15 (25) Write X Compute X=X+10 (20) Read X (10) Write X Classic unsynchronized update T1 X=20 X=25 X=10 X=25 Time T2 Lost Update
8. Read X (10) Compute X=X+15 (25) Rollback Use value ofX that was nevercommitted to DB Read X (25) Write X T2 sees effects of T1 that were never committed T1 X=10 X=25 X=10 Time T2 Dirty Read
9. Read X (10) Compute X=X+15 (25) Commit Read X (10) Read X (25) Non-Repeatable Read Transaction sees only committed data but data changes when referenced multiple times T1 Write X X=10 X=25 Time T2
10. Jones,6 Insert The “Phantom” Problem Transaction’s input set defined by a predicate Transaction should not see additions/deletions from input set Must prevent other transactions from inserting even if we don’t see the effects! T1 Jones,6 Smith,4 Smith,4 Time Brewer,7 Brewer,7 Select count (*) where rank > 3 (2) Select count (*) where rank > 3 (3) T2
11. IsolationLevels True Isolation Is Expensive Trade off between correctness and concurrency ANSI SQL Defines Four Isolation Levels based on which phenomena are allowed No specification as to how to achieve isolation SQL Server 2000 implements all ANSI isolation levels using Pessimistic Concurrency Model SQL Server 2005 provides Optimistic Concurrency Alternatives Optimistic implementation of read committed New Snapshot Isolation
12.
13. Locking Decision Current Activity Isolationlevel Scan type(Range, Table, Probe) Locking strategy(Table, Page, Row) # of rows in scan # of rows/page Operation type (scan, update) Lock hints sp_IndexOption Settings
16. Blocking Occurs when one process requests a lock on the same resource held by another process in an incompatible mode Blocking and Isolation Summary for Pessimistic Concurrency Writers block writers in all levels Writers block readers in Read Committed and higher Readers block writers in Repeatable Read and higher
18. Process 1 X Resource A Resource B X Process 2 WhatisDeadlock? Two Processes Mutually Blocking Each Other
19. Blocked Process Threshold Generates an event when a task(s) blocked for greater than ‘threshold’ ‘Threshold’ is a configurable option There is a background thread (Lock Monitor) that periodically checks tasks that are blocked. You can use event to identify tasks
21. Summary Concurrency Models Transactions in SQL Server Isolation Levels Pessimistic Concurrency Control Optimistic Concurrency Control
22. Additional Resources Inside SQL Server 2005: The Storage Engine (Microsoft Press, available now) Blogs: http://blogs.msdn.com/sqlserverstorageengine http://blogs.msdn.com/sqltips http://blogs.msdn.com/sqlcat http://sqlblog.com/blogs/kalen_delaney Scripts available at www.InsideSQLServer.com/conferences http://sqlblog.com/blogs/kalen_delaney