11 Handling failures

 

This chapter covers

  • Making transactional database updates
  • Retrying Entity Framework Core operations
  • Using Polly to retry operations

Code can fail for many reasons. Maybe a dependency isn’t responding, or something else has a lock on a resource you want. Your customers may have found an unexpected way to use your application or hit corner cases you didn’t anticipate. If we adopt a mindset that our code will fail, we can move to adopt strategies that handle failures well. In this chapter, we’ll look at common .NET tools and techniques that isolate problems, retry, and compensate.

11.1 Operating in the real world

Sometimes, it seems that the things we build today don’t last long. Even though it’s only 20 years old, my driveway is slowly crumbling, and the concrete needs to be removed and replaced. Yet the Roman Pantheon, built almost two millennia ago, is still standing strong. Researchers recently discovered that the reason why this concrete lasts so long is that it has properties that allow it to heal. Whether by accident or intention, Roman concrete was designed to handle failure. My driveway, by contrast, was designed to be inexpensive.

11.2 EF Core

11.2.1 Database transactions

11.2.2 Retrying on transient faults

11.3 Polly

11.3.1 Simulating HTTP errors

11.3.2 Other Polly capabilities

Summary