Entity Framework Core Interview Question #192
How should database deadlocks be handled in an EF Core application?
Testing, Production Practices & Senior Real-World Scenarios Senior Advanced
Quick Interview Answer
First reduce the likelihood of deadlocks by keeping transactions short, accessing shared resources in consistent order, using suitable indexes, and avoiding unnecessarily broad locks. Because deadlocks can still occur under concurrency, treat database-reported deadlock victims as potentially transient when the provider and operation support safe retry. Investigate recurring deadlocks using database diagnostics rather than only increasing retries.
Detailed Explanation
First reduce the likelihood of deadlocks by keeping transactions short, accessing shared resources in consistent order, using suitable indexes, and avoiding unnecessarily broad locks. Because deadlocks can still occur under concurrency, treat database-reported deadlock victims as potentially transient when the provider and operation support safe retry. Investigate recurring deadlocks using database diagnostics rather than only increasing retries.
Code Example
// Keep the transaction focused and retry only
// through a deliberate transient-failure strategy.