Fastapi: performance

Created on 25 Oct 2020  路  6Comments  路  Source: tiangolo/fastapi

I've got the info that fastapi has very high performance while is the site below, fastapi is much lower than PHP, Go, and also lower than python framework -- falcon(No. 65), so is it high performance? Any possible to improve it?

https://github.com/the-benchmarker/web-frameworks
No. 117
117 python (3.8) fastapi (0.6) 26 078

question

Most helpful comment

Since FastAPI is built with Python i do not think comparing Java frameworks and Python frameworks is a fair comparison, FastAPI says I'm one of the fastest framework available in Python and my performance is on par with NodeJS and Go, so here is a small conclusion from @dbanty, about NodeJS and Go's performance when compared with FastAPI,

FastAPI's Performance

If you ask "is it fast" about anything, there will be evidence both for and against. I think the point of linking to TechEmpower instead of listing numbers directly is so that people can explore on their own and see if FastAPI makes sense for their workloads. However, we may be able to do a better job of guiding people about what is "fast" about FastAPI.

For the numbers I'm about to share, I'm using TechEmpowers "Round 19" looking at only the "micro" classification (which is what FastAPI falls under) for "go", "javascript", "typescript", and "python". I don't use Go or NodeJS in production, so I'm picking some popular frameworks which appear under this micro category to compare: "ExpressJS" (javascript), "NestJS" (typescript), and "Gin" (golang). I don't know how their feature sets compare to FastAPI.

Plain Text

I believe this is what most of the comparisons above me are using. FastAPI is much slower than nest/express which is much slower than Gin. Exactly what people are saying above. If your primary workload is serving text, go with Go.

Data Updates

Requests must fetch data from a database, update, and commit it back, then serialize and return the result to the caller. Here FastAPI is much faster than NestJS/Express which are much faster than Gin.

Fortunes

This test uses an ORM and HTML templating. Here all the frameworks are very close to each other but, in order from fastest to slowest, were Gin, NestJS, FastAPI, Express.

Multiple Queries

This is just fetching multiple rows from the database and serializing the results. Here, FastAPI slightly edges out Gin. Express and NestJS are much slower in this test.

Single query

Single row is fetched and serialized. Gin is much faster than the rest which are, in order, FastAPI, NestJS, and Express.

JSON serialization

No database activity, just serializing some JSON. Gin blows away the competition. Express, then Nest, then FastAPI follow.

So the general theme of all the tests combined seems to be if you're working with large amounts of data from the database, FastAPI is the fastest of the bunch. The less database activity (I/O bound), the further FastAPI falls and Gin rises. The real takeaway here is that the answer to "is it fast" is always "it depends". However, we can probably do a better job of pointing out FastAPI's database strengths in the sections talking about speed.

All 6 comments

The benchmark you sent is based on PlainTextResponse and looks like a very poor benchmark.

There are a lot more accurate benchmarks available like TechEmpower's

Thanks, ycd! It looks better. While frankly, it still has some performance gap to the level of Go/Java.

Since FastAPI is built with Python i do not think comparing Java frameworks and Python frameworks is a fair comparison, FastAPI says I'm one of the fastest framework available in Python and my performance is on par with NodeJS and Go, so here is a small conclusion from @dbanty, about NodeJS and Go's performance when compared with FastAPI,

FastAPI's Performance

If you ask "is it fast" about anything, there will be evidence both for and against. I think the point of linking to TechEmpower instead of listing numbers directly is so that people can explore on their own and see if FastAPI makes sense for their workloads. However, we may be able to do a better job of guiding people about what is "fast" about FastAPI.

For the numbers I'm about to share, I'm using TechEmpowers "Round 19" looking at only the "micro" classification (which is what FastAPI falls under) for "go", "javascript", "typescript", and "python". I don't use Go or NodeJS in production, so I'm picking some popular frameworks which appear under this micro category to compare: "ExpressJS" (javascript), "NestJS" (typescript), and "Gin" (golang). I don't know how their feature sets compare to FastAPI.

Plain Text

I believe this is what most of the comparisons above me are using. FastAPI is much slower than nest/express which is much slower than Gin. Exactly what people are saying above. If your primary workload is serving text, go with Go.

Data Updates

Requests must fetch data from a database, update, and commit it back, then serialize and return the result to the caller. Here FastAPI is much faster than NestJS/Express which are much faster than Gin.

Fortunes

This test uses an ORM and HTML templating. Here all the frameworks are very close to each other but, in order from fastest to slowest, were Gin, NestJS, FastAPI, Express.

Multiple Queries

This is just fetching multiple rows from the database and serializing the results. Here, FastAPI slightly edges out Gin. Express and NestJS are much slower in this test.

Single query

Single row is fetched and serialized. Gin is much faster than the rest which are, in order, FastAPI, NestJS, and Express.

JSON serialization

No database activity, just serializing some JSON. Gin blows away the competition. Express, then Nest, then FastAPI follow.

So the general theme of all the tests combined seems to be if you're working with large amounts of data from the database, FastAPI is the fastest of the bunch. The less database activity (I/O bound), the further FastAPI falls and Gin rises. The real takeaway here is that the answer to "is it fast" is always "it depends". However, we can probably do a better job of pointing out FastAPI's database strengths in the sections talking about speed.

Sorry for letting me in the conversation, but in the topics described where FastAPI falls off in terms of performance, those could be resolved using, for example, orjson for json serialization, and integrating Cython on the stack (uvicorn, Starlette and FastAPI). One excellent article about this can be read here:

https://www.nexedi.com/NXD-Blog.Multicore.Python.HTTP.Server

Some parts of the stack already are Cython based, like uvloop and httptools.

In terms of database, if using Postgres, asyncpg is also Cython based and is great.

Thank you, victoraugustolls! It's a nice high performance project. I've tried it with c and easily got more than 20k rps. It may cost some time to convert it to python wrapping and will test later. Thanks.

@littleforce163 does your problem resolved? If it is do you mind closing the issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kkinder picture kkinder  路  3Comments

updatatoday picture updatatoday  路  3Comments

vnwarrior picture vnwarrior  路  3Comments

danielgtaylor picture danielgtaylor  路  3Comments

PyDataBlog picture PyDataBlog  路  3Comments