2

I have a web API that is using EF Core 5.0.12. I use the swagger web interface for debugging. I have noticed the first time I call a particular controller action in swagger it is slow (six seconds or more). Subsequent calls to that same action are much faster. I am not entirely sure how swagger calls get faster. In my Asp.Net Core SPA calls made to the API just seems slow every time.

Which got me thinking... Since, Asp.Net Core by default, disposes of the entity context after each call, would I benefit from using DbContext Pooling to avoid an EF query compile each time?

I am aware of a prior SO question here. But it seems a bit dated since I am using the latest stable versions of Ef Core and Asp.Net Core do the given answers still apply?

Randy
  • 1,137
  • 16
  • 49
  • I usually don't worry about initial hits being slow. It's due to initial handshaking in networking between machines or caching somewhere. Whether it's on the database side, application side, or even browser side (for images and stuff). – Blue Eyed Behemoth Nov 30 '21 at 13:39
  • I just read what you said again about your SPA, every request seems slow? You should add some benchmarking to see where your slowdown truly is. – Blue Eyed Behemoth Nov 30 '21 at 13:42

1 Answers1

1

The first call takes longer probably because of the connection to the database being initiated. After that, it uses Connection pooling to reuse the same connection for subsequent requests.

It's worth mentioning that DbContext uses an underlying connection object, but the fact that every request creates a new DbContext doesn't mean a new connection TCP handshake is made. Hence, there probably won't be any noticeable performance improvements just by replacing DbContext by DbContextPool.

Bruno Farias
  • 785
  • 8
  • 22