SQL Server Mastery

Optimistic vs Pessimistic Concurrency

2 Views Updated 5/6/2026

Concurrency Strategies

Concurrency defines how the system handles the "Lost Update" problem. Suppose two users open the same Edit screen. User A saves first. Then User B saves. Does User B overwrite User A's changes? Your choice of strategy determines the user experience.

1. Pessimistic Concurrency (Locking)

You "Lock" the row the moment the user starts editing. No one else can even read it. This is very safe but results in a terrible user experience (locked screens) and low scalability.

2. Optimistic Concurrency (Versioning)

This is the industry standard for web apps. You don't lock anything. When the user saves, you check if the data has changed since they opened the screen. We do this in SQL Server using a RowVersion (or timestamp) column.

UPDATE Users SET Email = @NewEmail 
WHERE Id = @Id AND RowVersion = @OldRowVersion

IF @@ROWCOUNT = 0 THROW 50000, 'Concurrency Error: Data has changed!', 1;

4. Interview Mastery

Q: "When would I ever use Pessimistic concurrency in a modern app?"

Architect Answer: "You use it for extremely high-contention, low-latency scenarios where the 'Cost of Failure' is high. For example, a **Stock Exchange** or a **Seat Booking** system. In these cases, it is better to tell the user 'This seat is currently being booked' (Pessimistic) rather than letting them fill out a 5-page form only to tell them 'Sorry, someone else grabbed it 1 second ago' (Optimistic)."

SQL Server Mastery
1. SQL Server Architecture & Basics
SQL Server Internals: How the Storage Engine works Relational Database Design & Normalization (1NF to 3NF) Data Types Mastery: Choosing the right type for performance
2. Advanced T-SQL Querying
Joins Deep Dive: Inner, Outer, Cross, and Self Joins Subqueries vs CTEs: Writing readable, high-performance code Window Functions: ROW_NUMBER, RANK, and LEAD/LAG Aggregations & Grouping Sets: Building complex reports Set Operators: UNION vs UNION ALL, INTERSECT, and EXCEPT
3. Indexing & Performance Tuning
Clustered vs Non-Clustered Indexes: The physical storage reality Covering Indexes & Included Columns: Reducing I/O costs Index Fragmentation: Why it happens and how to fix it Execution Plans: Reading the Query Optimizer's mind Statistics: Why 'Out of Date' stats kill performance SARGability: Writing queries that actually use indexes
4. Database Programmability
Stored Procedures: Security, Performance, and Best Practices User Defined Functions (UDF): Scalar vs Table-Valued Triggers: Auditing changes and the dangers of hidden logic Views & Indexed Views: Abstraction with performance Error Handling: TRY/CATCH and XACT_STATE()
5. Transactions & Concurrency
Transaction Isolation Levels: Read Uncommitted to Snapshot Locking & Blocking: Analyzing Deadlocks like a Pro Optimistic vs Pessimistic Concurrency
6. Administration & Security
SQL Server Security: Logins, Users, and Roles SQL Injection Prevention: Beyond simple parameterization Backup & Recovery Models: Full vs Simple vs Bulk-Logged Automating Maintenance: SQL Agent Jobs & Rebuilding Indexes
7. Modern SQL & Cloud
SQL Server & JSON: Storing and Querying semi-structured data Temporal Tables: Keeping track of data history automatically Introduction to Azure SQL: Database as a Service (PaaS) SQL Server Developer Interview: Junior to Senior Architect Level