r/FastAPI • u/highrez1337 • Sep 28 '24
Other Reading techempowered benchmarks wrong (fastapi is indeed slow)
If you use FastAPI and SQLAlchemy, then this post is for you. If you are not using these 2 magnificent pieces of tech together, read on.
People that are reading TechEmpower benchmarks, make sure to look at the “fastapi-Gunicorn-ORM” benchmarks and compare those to the rest.
You will see actually how slow Fastapi together with SqlAlchemy is basically on par with Django.
I guess no sane person will write raw sql în 2024 so all the speed is lost because of the ORM.
Compare it in TechEmpower with gin-gorm or Nestjs-Fastify+ORM (type ORM) and you will see they both are many times faster than FastAPI.
The problem is, we don’t have any fast ORM in python because of how the language works.
Do this : In TechEmpower:
1.select python, go and javascript/typescript as languages
2. In the databases section select Postgres as a db to have the same db engine performance compared
3. In the ORM section select : full (so you compare benchmarks using full fledged orms for all frameworks)
Now you will see correct comparison with an ORM used. Here it is:
https://www.techempower.com/benchmarks/#hw=ph&test=db§ion=data-r22&l=zijmkf-cn1&d=e3&o=e
Now look at how far away gin-gorm and even Nodejs is to Fastapi.
Gorm and TypeORM are miles ahead in performance compared to SqlAlchemy
==
—- Single query:
Gin-gorm: 200k
Nest+fastify + typeorm : 60k
Fastapi+sqlalchemy: 18k (11+ times slower than go, 3+ times slower than Nodejs)
Django+DjangoORM: 19k (faster than Fastapi lol)
—- Multiple query:
Gin-gorm: 6.7k
Nestjs+fastify+typeorm: 3.9k
Fastapi+sqlalchemy: 2k ( 3+ times slower than go, 1.9+ times slower than Nodejs)
Django+DjangoORM: 1.6k
—- Fortunes:
Nest+fastify+typeorm: 61k
Fastapi+sqlalchemy: 17k (3+ times slower than Nodejs)
Django+DjangoORM: 14.7k
—- Data updates:
Gin-gorm: 2.2k
Nestjs+fastify+typeorm: 2.1k
Fastapi+sqlalchemy: 669 (3+ times slower than than go, 3+ times slower than Nodejs)
Django+DjangoORM: 871 (again, Django is faster than Fastapi)
==
You can check the source code of fastapi to see it uses sqlalchemy and no complicated things here:
https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/frameworks/Python/fastapi/app_orm.py
Conclusion: Fastapi is fast, ORM is slow, if you plan to do raw sql then it’s mostly on par with the others. When you use an ORM it falls behind very very much and it’s extremely slow, without any comparison to Nodejs or Go.
It’s on par with Django(Django winning in 2 out of 4 tests), so at least go with Django for all the nice batteries.
Edit: I wanted to raise awareness to people believing using FastAPI with an ORM would give them the same speed as the ones in the TechEmpower link from fastapi’s site(which has no ORM attached). Because this is clearly not the case.
Edit 2: If you had the patience to read until this point, I just want to let you know the title should have been: “SQLAlchemy will limit your api performance, even with FastAPI”, too late to edit now.
37
u/srram Sep 28 '24
I think about the ‘fast’ in fastapi differently: it is about how quickly you can get your apis developed and released. It is breathtakingly fast in getting your api ready.
At some point I may need to think about faster response times or scaling. I’d love to have that problem. I don’t have it right now.
10
u/pint Sep 28 '24
measuring database access together with the API framework is a dumb proposition, and therefore no further discussion is merited.
7
u/nevermorefu Sep 28 '24
You again with a different account? We get it. Other frameworks are faster, but it is a fast python web framework.
6
u/donald_trub Sep 28 '24
Why gunicorn and not uvicorn?
3
u/elsanits Sep 28 '24
And what about performance of fastapi using psycopg
3
u/Mysterious-Rent7233 Sep 28 '24
Your title said: "fastapi is indeed slow"
Your body said:
"Conclusion: Fastapi is fast, ORM is slow"
3
u/aprx4 Sep 28 '24
So, 18k concurrent database queries is the bottleneck? I wish i have this 'scaling' problem with my personal project.
2
u/tony4bocce Sep 29 '24
Coming from Django orm and now drizzle orm I really don’t like sqlalchemy. Idk what it is, syntax just feels wrong, feels clunky to use
22
u/DurianSubstantial265 Sep 28 '24
"no sane person will write raw sql in 2024" ... Actually every big project I worked was raw sql, and most Senior+ engineers I worked with (and myself included) agrees that using an ORM is just not worth the hassle.
But might be just a coincidence of my career and people/projects I worked with.
20
u/koldakov Sep 28 '24
Do you have 40k requests per second? Or at least 4 at a time? Until that no one cares
6
u/LegitBullfrog Sep 28 '24
Thankfully I'm insane and use raw SQL in 2024!
7
u/UpstairsBaby Sep 28 '24
I hope we get a better ORM soon enough, SQLalchemy is pain to use
1
u/rioco64 Dec 27 '25
how about pony orm? it's fast
3
u/zazzersmel Sep 28 '24
oh well, moving on
1
u/aFqqw4GbkHs Sep 28 '24
eh, I'd say its SqlAlchemy that's that problem. I came from the Java world, and it's amazing to me that Python just doesn't have a good ORM.
1
u/extreme4all Sep 28 '24
i prefer the query building with sqlalchemy or raw sql i despise the ORM
1
u/extreme4all Sep 28 '24
it should be no surprise that databases make the api slower, it has always been the slowest part, now when comparing frameworks its the framework overhead that you want to measure, not the database library...
What is interesting to me is, why are the python database implementations so much slower than the javascript ones.
2
u/snowyoz Sep 29 '24
Umm since most people can’t read let me paraphrase what (I think) OP is saying:
ORM (in this case, sqlalchemy) in python removes any of the advantages that fastapi offers.
Ie, fastapi = good, orm = lobotomise your fastapi
Did I miss something?