Luiz Tauffer
Luiz Tauffer

Reputation: 632

Serving React app from FastAPI: advantages vs disadvantages

I’m gradually moving away from Flask/Dash for development of my Data Science and Machine Learning apps, and towards a combination of FastAPI + React due to many advantages such as standardized models with pydantic, auto generation of the rest api documentation, async, full-blown React frontend possibilities, etc…

FastAPI can serve any Flask/Dash app I might want to use for a quick prototype development, and it can also serve a compiled React app if I want to. So here comes my question:

Should I serve a compiled React app through FastAPI in production?
What would be the disadvantages of doing so when compared to running the React app from its own, separate server?
Any specific concerns regarding stability, performance or security in such a stack?

Upvotes: 1

Views: 1700

Answers (1)

Mark
Mark

Reputation: 302

I have a couple of projects (React/FastAPI) with rather high traffic and so far encountered no issues.

Stability and performance wise this depends entirely upon the server you're serving from. It's a little difficult to get all the moving parts working together in a scalable manner, but once accomplished you are set.

What you should avoid however, is serving large resources through your API (common knowledge). In my experience, serving app's index.html and static files has a response time anywhere between 4ms and 350ms.

This is also helped by using a caching mechanism for responses, as well as headers, but depends on how frequently your server restarts and how often you'd like to deploy a new version.

Obviously the number of alternatives and options to deploy & serve your front-end application is almost infinite, and there are definitely better options (so for future commenters, don't get crazy, everyone has preferences and solutions which work for them).

If you have question about how to implement some specific thing mentioned here, feel free to comment.

Upvotes: 1

Related Questions