Entity Framework Core Interview Question #121
What is the difference between optimistic and pessimistic concurrency?
Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate
Quick Interview Answer
Optimistic concurrency allows multiple users to read data without holding long-lived locks and detects conflicts when an update is attempted. EF Core directly supports this pattern through concurrency tokens.
Detailed Explanation
Optimistic concurrency allows multiple users to read data without holding long-lived locks and detects conflicts when an update is attempted. EF Core directly supports this pattern through concurrency tokens.
Pessimistic concurrency relies on database locking so competing operations are blocked or restricted while a resource is being used. It can be appropriate for some critical workflows but increases contention and requires careful database-specific design.
The choice depends on conflict frequency, transaction duration, business guarantees, and database behavior.
Code Example
// Optimistic concurrency is commonly modeled with:
modelBuilder.Entity<Order>()
.Property(o => o.RowVersion)
.IsRowVersion();