What is connection resiliency (retry on failure) in EF Core?
Quick Answer
Connection resiliency automatically retries operations that fail with transient database errors (timeouts, Azure SQL throttling). Enable it with options.UseSqlServer(cs, o => o.EnableRetryOnFailure()). It's essential for Azure SQL, which briefly drops connections during failovers.
Connection resiliency makes EF Core automatically retry when it hits a transient error — a timeout, or Azure SQL throttling you or doing a failover. You turn it on with EnableRetryOnFailure, and for a cloud database it's basically mandatory because connections get dropped during failovers. The catch is transactions: if I open my own transaction and a retry happens, EF would only replay part of it, so I have to get the execution strategy and wrap the entire unit of work in strategy.ExecuteAsync so the whole thing retries as one.
- Uses an execution strategy that catches a known list of transient error numbers and retries with backoff.
- Gotcha: with retries enabled you can't start a user-initiated transaction (
BeginTransaction) the normal way — a retry would replay only part of it. Wrap the whole unit of work instrategy.ExecuteAsync(async () => { ... })so the entire block is retried atomically. - Also apply resilience to raw
SqlConnectionusage separately (it doesn't inherit EF's strategy).