Building a Scalable REST API with FastAPI and Choreo

sachini ranasinghe
Choreo Tech Blog
Published in
6 min readSep 2, 2024
Image by Christo Ras from Pixabay

In today’s rapidly evolving digital landscape, delivering high-performance applications is more important than ever. One of the cornerstones of such applications is the ability to build scalable and efficient APIs that can handle increasing demand without compromising on speed or reliability. Enter FastAPI and Choreo — two powerful tools that, when combined, offer a seamless experience for developing and scaling APIs.

FastAPI has gained significant traction among developers for its speed, simplicity, and modern features, such as type hints and automatic interactive documentation. On the other hand, Choreo is an internal developer platform as a service, that empowers developers to build, manage, and scale APIs effortlessly with minimal overhead. In this blog post, we’ll explore how to leverage the strengths of FastAPI and Choreo to create a robust REST API that can handle the demands of modern applications.

Why Choose FastAPI?

FastAPI is an excellent framework for developing web APIs. It’s designed to be easy to use, yet incredibly powerful, making it a favorite among both new and experienced developers.

  • High Performance: Powered by Starlette and Pydantic, FastAPI is known for being one of the fastest Python frameworks, thanks to its use of asynchronous capabilities.
  • Ease of Use: It’s easy to learn, and the code is straightforward to write and maintain.
  • Automatic Documentation: FastAPI automatically generates interactive API documentation.
  • Type Safety: Its type hints improve code quality and help catch errors early.

FastAPI’s asynchronous capabilities and built-in validation make it an excellent choice for high-performance APIs, especially when combined with the right tools for scaling and management.

Why Deploy on Choreo?

Choreo is a platform that simplifies the process of building, deploying, and managing APIs. It offers seamless integration with various services, robust monitoring, and automatic scaling, making it an ideal companion for FastAPI applications. Choreo simplifies the deployment process by offering:

  • DevOps Automation: Streamlines CI/CD processes with minimal configuration.
  • Business Insights: Provides detailed insights and analytics for your applications.
  • Security and Governance: Ensures your application meets the necessary security standards.
  • Observability: Built-in tools for monitoring and logging.
  • Scalability: Automatically scales your application based on demand.

In this blog, we will walk through the deployment of a containerized FastAPI application, known as ArtStay API, which is designed to manage various aspects of a rental business. This API not only handles full CRUD (Create, Read, Update, Delete) operations for reservations but also provides features for managing detailed information about themed houses and facilitating the rental of electric motorbikes. With the ArtStay API, users can view available houses, check facilities, manage bookings, and reserve motorbikes — all through a unified platform.

Prerequisites

  • Choreo user account
  • Containerized FastAPI application

Step 1: Create a Service Component

  1. Log in to Choreo.
  2. Create a new project by providing a name and description.
  3. Set up a new service within the project for the FastAPI application.
  • Click the Service component card (or select the Create button and then select Service component card).
  • Enter the name and description field values
  • Authorize the GitHub Application
  • Grant Repository Access
  • Choose a repository and an associated branch to connect to the Choreo component
  • Choreo provides the capability for users to select Buildpacks. They convert your source code into a secure, efficient, production-ready container image without a Dockerfile.
  • The python FastAPI application should have the following repository structure to get it deployed successfully in Choreo.

For a service application to be build and deployed correctly, including the endpoints.yaml/component-config.yaml file is essential. You can find more details about advanced configuring options provided by Choreo in here.

  • Select Python as the Buildpack and provide the details Python Project Directory and the Python language version

In Python projects, it is mandatory to have a Procfile with the web process type in the project root directory. You can find more information about the structure for a python project here.

Step 2: Build and Deploy the containerized component

  • Click on the Build option in the left navigation menu
  • Click on the Build Latest button to build the latest commit

If the build is successful you can then click on the Deploy option in the left navigation menu.

  • Select Deploy from the drop down as we don’t have any environment variables to configure.

Step 3: View Details about the Running Replicas

We can leverage Choreo’s runtime capabilities to ensure the scalability of the Deployed application.

  • In the left navigation menu, click DevOps. You can see several capabilities listed. This allows you to monitor real-time container logs, CPU and memory usage, and running instances in each environment.
  • From this you can optimize your application’s performance. Choreo also allows you to redeploy resources efficiently and troubleshoot issues by analyzing container conditions and events. This real-time observability and control over deployments help ensure that your FastAPI application scales effectively and remains resilient in production environments.

You can find more information on DevOps capabilities provided by Choreo by following the link.

Step 4: Increase the number of replicas

  • Click on Scaling under the DevOps option. By default Scale to Zero option is enabled for your service

We can increase the number of replicas where your service is deployed. This will allow it to handle more incoming requests concurrently. This improves the service’s availability and reliability, as the load is distributed across multiple instances. If one instance fails, others continue to serve requests, ensuring continuous operation.

  • Click on the + button in the Max Replicas option and Update.

Choreo dynamically scales deployments up to meet high HTTP traffic demand, up to the specified number of replicas. If the pending requests surpass the defined threshold under Number of pending requests to spawn a new pod, Choreo automatically adds a new replica to handle the increased load.

Performance Optimization Tips

Asynchronous Programming

FastAPI supports asynchronous programming out of the box. By using async def for your endpoints, you can handle multiple requests concurrently, improving your API’s performance

Caching with Choreo

Implement caching strategies to reduce the load on your API. Choreo’s caching features can be easily integrated to store frequently accessed data, reducing response times.

Find more information about caching capabilities provided by Choreo here.

Conclusion

By combining FastAPI with Choreo, you can build and scale high-performance APIs with ease. Whether you’re handling a small project or a large-scale application, this combination provides the tools necessary to maintain optimal performance as your API grows. Start experimenting with these technologies today and take your API development to the next level!

References

Develop Components with Git

Deploying an application with buildpacks

Auto Scale Containerized Applications

--

--