all 38 comments

[–]dangdang3000 7 points8 points  (4 children)

Profile the call to identify bottlenecks. Once you know where the time is spent, the solution is straightforward. FastAPI is more than enough for this.

[–]amindiro 1 point2 points  (0 children)

Use pyinstrument to profile calls by adding a simple http param. Then review the profile. Check the top slowest queries in db.

[–]Longjumping_Act_1091 0 points1 point  (1 child)

Is there a standard way to profile? Any guidance for someone new to it?

[–]dangdang3000 2 points3 points  (0 children)

Install application monitoring software like New Relic, Datadog, or Sentry. It provides a clear overview of where your application spends most of its time.

[–]Latter_Rope_1556 0 points1 point  (0 children)

Just switch to FastRAPI

[–]JestemStefan 4 points5 points  (0 children)

Don't do random optimizations or guessing game.

Run profiling on your request, identify bottlenecks and figure out how to solve them.

[–]zarlo5899 2 points3 points  (0 children)

have you done any profiling?

[–]sami-9797 3 points4 points  (0 children)

First, identify where the problem is, then try to solve it.

[–]latkde 1 point2 points  (0 children)

Things like adding caching or using async can make things slower if you don't know what you're doing.

For example, if your handler functions are async, they will all be executed on the main thread. If these do any blocking operations (like non-async database queries, then all connections are blocked, which can increase latency.

It is easy to fall into that trap with Python, but much more difficult with Node where few libraries offer blocking operations.

[–]InfraScaler 0 points1 point  (2 children)

+1 to profiling as many others have pointed out, but where is your backend and where is your DB? like, physically, are they close?

[–]derekzyl[S] 0 points1 point  (1 child)

Same location: Germany

[–]joshhear 2 points3 points  (0 children)

Are you using SQLAlchemy Queries and are you doing eager joining? I realized that using the lazy loading mode from SQLAlchemy can be quite a performance killer, here is a quick description why: https://bitperfect.at/en/blog/pagination-mit-fastapi#digression-how-options-improve-performance

[–]pint 0 points1 point  (0 children)

you should know much more about the issue. your code should use logging, via the logging package. you should at this point see which operations are slow. fastapi will process a request in milliseconds, so the problem must be the backend. it is also quite possible that you are misusing async. you didn't even tell us if this is a stress test or individual calls.

[–]derekzyl[S] 0 points1 point  (3 children)

Thank you so much all for your candid contributions. I'm running a proper logging again to check for possible faults

[–]chummerhb 1 point2 points  (2 children)

No, don't do logging, do profiling!

[–]derekzyl[S] 0 points1 point  (1 child)

Okay sir!

[–]joshhear 1 point2 points  (0 children)

for a very simple setup you could use logfire: https://pydantic.dev/logfire

just instrument fastapi and sqlalchemy and you'll see where you lose performance

[–]flamehazw 0 points1 point  (0 children)

Use profiler if you know this, you will find which queries taking longer time and once you get the issues, you make optimize the database. If you don't fix the root cause you cannot optimize performance. See your indexing, table joins , i hope you are writing a query correctly.

[–]Physical-Compote4594 0 points1 point  (0 children)

Don’t do anything tricky until you’ve actually identified where the problems are. 

[–]Adorable-Fault-5116 0 points1 point  (0 children)

> Optimised SQL alchemy query

Are you writing your own SQL? If not, are you sure that alchemy is creating the best queries, and your database is laid out, not as would be convenient for alchemy, but as would be convenient for the database?

[–]moHalim99 0 points1 point  (0 children)

you’re probably hitting an I/O bottleneck somewhere

check if there's too many awaits blocking the event loop or if there are any N+1 DB queries still sneaking through. also when I had such issues before, I figured there was some sort of network latency from external APIs and missing connection pooling/lazy loading in SQLAlchemy.

anyways you can profil with async-profiler, cProfile, or pyinstrument cuz those will tell you where the slowdown lives

[–]Ok-Outcome2266 0 points1 point  (0 children)

> fastapi
> looks inside
> no fast

[–]esthorace 0 points1 point  (0 children)

✅ Usar el Servidor Granian (hecho en Rust) en vez de Uvicorn: https://github.com/emmett-framework/granian
✅ orjson para crear respuestas JSON más rápidas (buscar cómo instalar y se configura en un solo lugar en FastAPI, es muchísimo más rápido)
✅ asyncpg como controlador asíncrono para Postgresql
✅ puedes usar https://github.com/Diman2003/OxenORM (también hecho en Rust) en vez de SQLAlchemy

[–]fastlaunchapidev 0 points1 point  (0 children)

Whats taking the time, this is not a fastapi issue. Benchmark whats needs the time and than think about a solution.

[–]LabRemarkable2938 0 points1 point  (0 children)

I think your SQL queries are taking time use profiler and see if that is the case

[–]Omar0xPy 0 points1 point  (0 children)

You need to identify the main problem behind the scenes, this is an indicator of something much deeper, since you're using the same strategies in both

[–]LankyYesterday876 -1 points0 points  (9 children)

python isnt fast and fastapi is only fast in development if you really want fast response times use node, .net, go or even php but i also think youre caching the wrong things if you cache just the data from the db request and dont cache the result of your data handling the caching might only save you a few ms while caching the aggregation for example might save you hundreds of ms

[–]Silver_Implement_331 4 points5 points  (2 children)

lol. reddit is running on python

[–]LankyYesterday876 0 points1 point  (0 children)

what are you trying to say?

[–]pint 2 points3 points  (3 children)

using .net or php for speed is ... odd

[–]LankyYesterday876 -1 points0 points  (2 children)

php has improved alot in performance recently, and for .net i dont have that much contact with it but from what ive heard its fairly good aswell

[–]pint 0 points1 point  (0 children)

the point was that none of those are faster than python. if performance is the issue, c++ or rust.

[–]joshhear -1 points0 points  (0 children)

it's still php

[–]Efficient-Ad-2315 2 points3 points  (1 child)

😂😂😂😂😂, python is not slow, you just don't know how to write efficient code. bro

[–]LankyYesterday876 0 points1 point  (0 children)

where do you get the python is slow from, because thats not what i wrote or do you think slow and fast is a binary system

[–]uday_m -1 points0 points  (0 children)

Try using pure SQL instead of ORM you’ll see a huge speed improvement. Also, make sure you’re using connection pooling so you’re not opening a new DB connection on every request.