Single URLSession for all requests versus per request, performance

I wouldn't put it that way, as creating a new TCP connection is really just a byproduct of the fact that URLSession persists quite of bit of internal state, which includes your connections, and you recreate that state every time you create a new URLSession. Fundamentally, separate URLSessions for every request is an anti-pattern because URLSession is expensive to create and tear down. By creating a separate URLSession for each request, especially if your requests occur in parallel, you will see:

  • Higher memory usage, as each URLSession maintains its own state.
  • Higher CPU usage, as each URLSession must set up and tear down that state.
  • Higher latency, as the time taken to create URLSessions will increase the overall time for network requests, both from creating the instance yourself as well as reestablishing connections to the same server.
  • Increased server load, as creating a new URLSession for each request prevents connection reuse, a fundamental HTTP optimization (especially once you move to HTTP/2 or HTTP/3). This includes not only TCP setup and teardown but TLS work as well.

Apple's OSes have various internal optimizations around maximum connections per host. There's no real way to predict how separate URLSessions connecting to the same host will interact. Not only do URLSessions establish a limited number of connections themselves, but there seems to be some sort of internal maximum per-host across all URLSessions which we can't influence. However, those connections are better reused from within one URLSession rather than many, especially with HTTP/2 and HTTP/3, since they multiplex over a single connection.

Of course, as downloads always go to disk first, so serializing from them needs to pay that cost as well. This doesn't seem to have anything to do with the question at hand though.

(Also, stop hard coding HTTP 1.1 on the server side, there are more optimizations to be had.)

So no, there's never a case where you want to use one URLSession per request.

2 Likes