r/FastAPI • • Dec 07 '21

Question Why is my FastAPI application able to make several hundred HTTP requests/second when the requests are synchronous?

I created a small project for benchmarking sync/async operations in FastAPI APIs.

It seems that calls that should be blocking (e.g. open() or requests.get()) are running perfectly in parallel.

This seems great because it means I don't really need async and async libraries when I thought I did. However, I'm not familiar enough with FastAPI to make that judgement yet. Can someone explain what's going on here? And if using async even makes sense if synchronous calls run in parallel?

I put the code for reproducing my experiments on a public repository here. Repo contains a quick start guide for running the stress tests.

Experiments

General settings for all experiments:

  • Simple FastAPI application with two routes

    • /sync/... for synchronous operations

    • /async/... for asynchronous operations

  • API is run under Gunicorn with a single worker

    • Worker class set to uvicorn.workers.UvicornWorker

  • API is stress tested using Locust

    • Only one endpoint per test

    • 500 users @ 50 users/sec spawn rate

    • Each user waits 1 second between requests

    • Tests run for 1 minute

Loading JSON from disk

Users hit an endpoint that loads a small JSON file from disk and returns it. The /sync/json endpoint uses the standard open() method to read the file (should be blocking). The /async/json endpoint uses aiofiles.open() instead.

/sync/json statistics:

  • Num. requests: 15550

  • Avg. response time: 745 ms

  • Avg. requests/second: 259.16

/async/json statistics:

  • Num. requests: 15304

  • Avg. response time: 778 ms

  • Avg. requests/second: 255.06

With an avg. response time of 745 ms, it's simply not possible for open() to be blocking with 15550 requests successfully handled in 1 minute.

Making HTTP requests

Users hit an endpoint that makes a GET request to https://jsonplaceholder.typicode.com/todos/1 and returns the result. The /sync/http endpoint uses the httpx.Client() synchronous client, which should be blocking. Was also tested with requests.get() with similar results. The /async/http endpoint uses the httpx.AsyncClient() asynchronous client.

/sync/http statistics:

  • Num. requests: 6422

  • Avg. response time: 917 ms

  • Avg. requests/second: 107.03

/async/http statistics:

  • Num. requests: 3946

  • Avg. response time: 5951 ms (I did not switch up sync/async here)

  • Avg. requests/second: 65.76

Again, with the average request taking 917 ms to complete, httpx.Client.get() can not be blocking when it successfully made 6422 GET requests in 1 minute.

Also interestingly, the synchronous HTTP client seems to perform much, much better than the asynchronous one. No requests failed in either case, and the tests were run several times to ensure that one wasn't being rate-limited while the other was.

Hope someone can help me clear this up, thanks in advance!

21 Upvotes

8 comments sorted by

5

u/cantremembermypasswd Dec 07 '21

Functions without the async are not synchronous. They are sent to a thread pool https://fastapi.tiangolo.com/async/#path-operation-functions

1

u/Iklowto Dec 07 '21

I see - but in that case, I have trouble seeing when async would ever make sense, save for the scenario where I'm using a library that only contains async functions and must be awaited. Maybe you can clear the picture for me?

3

u/GroundbreakingRun927 Dec 08 '21

If you want to make many requests at once.

As a concrete example, you're making a price comparison api. I ask your api what company has the best price for an RTX 3000. You have to make 50 requests to different companies API's to check the price and each one takes 1 second. with async you could do:

price_responses = await asyncio.gather_all(httpx.get(price_url) for price_url in video_card_urls)

which fires everything at once. So in ~1 second you have all the responses,. Could you also do that with multiprocessing threadpool? Sure, but generally you'll get worse performance and have less flexibility. The asyncio toolkit is very robust.

1

u/Schmiddi-75 Aug 26 '24

With async you usually don't have the overhead that you get with threads.

0

u/[deleted] Dec 07 '21

[deleted]

1

u/Iklowto Dec 07 '21

I'm sorry, but I simply don't see why I should do that.

In other languages/frameworks that use async/await, I would agree, since not using async/await for blocking IO would force my API to handle requests sequentially.

But as per the results of my experiments, this is not the case with FastAPI. The IO-bound work I'm doing - open() and httpx.Client.get() rather than aiofiles.open() and httpx.AsyncClient.get() - is not blocking. The requests are clearly being handled in parallel (or concurrently) with or without async/await.

In fact, in the case of HTTP requests, I can handle more requests per second without async/await than with it. In each experiment, the response times clearly show that there is no way the requests are processed sequentially in any case.

1

u/n1EzeR Dec 08 '21

Just curious, what are specifications of the machine that was used for testing?

2

u/Iklowto Dec 08 '21

MacBook Pro 2020, 2 GHz Quad-Core Intel Core i5 and 16GB memory

1

u/yetanotheritdude Dec 08 '21

Very interesting experiment! Thanks for sharing it!