
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. FastAPI is designed to be fast, scalable, and secure.
FastAPI has built-in support for async/await, which makes it perfect for building high-performance APIs. This is because async/await allows for non-blocking I/O operations, making your API more responsive and efficient.
To create high-performance APIs with FastAPI, you can use the built-in support for asynchronous programming. This enables your API to handle multiple requests concurrently, resulting in faster response times and improved scalability.
By using FastAPI's async/await support, you can write efficient and scalable code that takes full advantage of modern hardware and software capabilities.
On a similar theme: Grpc Performance
What is gRPC?
gRPC is a high-performance, open-source framework for making remote procedure calls using Protobuf for serialization. It's designed for speed, efficiency, and strong typing.
gRPC was developed by Google and is part of the Cloud Native Computing Foundation (CNCF). It's an open-source framework for building high-performance, language-agnostic, and platform-independent RPC systems.
gRPC allows developers to define the structure of their APIs using Protocol Buffers, a language-agnostic binary serialization format. This means developers can define their service interfaces once.
gRPC supports multiple programming languages, including C++, Java, Python, Ruby, Go, Node.js, and many others. With gRPC, developers can generate client and server-side code for multiple languages automatically.
gRPC uses HTTP/2 as its transport protocol, which provides several benefits over HTTP/1.1.
A different take: Grpc over Http
Combining gRPC with FastAPI
Combining gRPC with FastAPI can lead to a very high performance outcome.
FastAPI is ideal for building external REST APIs, but it's not limited to REST - you can use it alongside gRPC.
FastAPI's async and await keywords allow for efficient handling of I/O operations, enabling the server to process other requests while waiting for I/O completion.
gRPC excels with backend-to-backend communication, providing compact, fast Protobuf messages.
This integration is natural due to FastAPI's support for asynchronous processing, which is a perfect match for gRPC's asynchronous capabilities.
The combination of FastAPI and gRPC enables the creation of high-performance asynchronous servers.
Additional reading: Asynchronous Grpc
Installation and Setup
To start building a FastAPI gRPC project, you'll need to install the gRPC library. This is done by installing the grpcio-tools package, which allows you to compile .proto files and generate client and server code automatically.
gRPC is a great choice for building high-performance APIs, and with FastAPI, you can take advantage of its asynchronous capabilities. This means you can handle multiple client requests concurrently without blocking each other.
To set up the gRPC server, you'll need to configure it to use the asyncio module, which is Python's built-in asynchronous I/O library. This will allow you to handle I/O operations efficiently and make the most of your server's capabilities.
Recommended read: Grpc New Client
1. Library Installation
To install the necessary library for gRPC, you'll need to install the grpcio-tools package. This package is used to compile the .proto files that are used to generate client and server code automatically. The grpcio-tools package is a crucial part of the gRPC setup process. It allows you to create a communication interface between clients and servers based on the .proto files. gRPC relies on these .proto files to generate the necessary code.
A fresh viewpoint: Grpc Tools
4. Server 설정

For a server setup, we're using a gRPC server based on FastAPI, which is an asynchronous server.
This type of server has the advantage of managing a thread pool, allowing it to handle multiple client requests simultaneously without affecting each other.
The gRPC server is built with grpc.aio.server, which is compatible with Python's asyncio module, making it ideal for efficient I/O operation handling.
One of the benefits of this setup is that even if one request is delayed, it won't impact the others.
gRPC Basics
gRPC is a high-performance RPC framework that enables building scalable and efficient APIs.
It uses Protocol Buffers for serialization and deserialization, which provides a compact and efficient way to represent data.
gRPC is designed to be lightweight and flexible, making it suitable for a wide range of use cases.
gRPC services are defined using Protocol Buffers, which allows for strong typing and code generation.
This enables developers to write client and server code in a variety of programming languages, including Python.
Channel
A channel in gRPC is a crucial component that establishes and maintains connections between a client and a server. It's built on top of the HTTP/2 protocol, which allows for efficient reuse of connections.
The client sends requests and receives responses through the channel. This is a fundamental aspect of how gRPC works.
A single channel can handle multiple gRPC calls, optimizing performance and resource usage. This is a significant advantage of using channels in gRPC.
Here are the key benefits of using channels in gRPC:
- Efficient reuse of connections through the HTTP/2 protocol
- Ability to handle multiple gRPC calls through a single channel
Proto File Writing
Writing a .proto file is the first step in implementing gRPC.
The .proto file is used to define the protocol buffers, a serialization format developed by Google that gRPC uses to exchange data between the client and server.
In this case, the client and server needed to define the message format and service definition in the .proto file.
The data schema for review data and the data that the community service needs to receive had differences, making the interface definition complex.
Additional reading: C# Grpc Service
To meet the tight deadline, the client decided to use a dict object to convert to a string for transmission and then convert it to JSON on the server side for validation.
This approach was chosen because the community service's existing post registration, modification, and deletion logic was well modularized, allowing the reuse of existing logic and making it easy to use Pydantic from the FastAPI framework for validation and data management.
The code also uses decorators to implement async_run (async coroutine) and init_middlewares (bugsnag and newrelic settings) for reusing the logic for bug tracking and server execution.
Curious to learn more? Check out: When to Use Grpc
Restaurant Analogy: Rest & Explanation
Imagine you run a restaurant where customers can place orders in two ways.
In one way, anyone with access can see your menu. This is similar to a REST API, where anyone can make a GET request to see the available data.
You can think of your menu as a resource that can be accessed by anyone. Customers can see what's available, but they can't make any changes.
However, in a real restaurant, you wouldn't let just anyone start making changes to the menu. That's where gRPC comes in, which provides a more controlled way of making requests.
For example, you might have a specific endpoint for customers to place orders, but only allow authorized staff to see the orders.
Validation and Testing
Validation is crucial in FastAPI gRPC to ensure the integrity of the data being exchanged between the client and server.
Pydantic is used to validate server-side data, allowing you to define validation rules for each field in your model. This helps catch errors early and prevents bugs from making it to production.
To add additional validation logic for specific fields, you can use @field_validator and @model_validator. If validation fails, a ValidationError is raised.
This approach helps maintain data consistency and reliability in your FastAPI gRPC application.
Pydantic 유효성 검사
Pydantic 유효성 검사는 서버 사이드에서 요청받은 데이터를 유효성 검증하는 데 사용됩니다.
Pydantic을 사용하여 모델의 각 필드에 대해 유효성 검사를 정의할 수 있습니다.
특정 필드에 대한 추가적인 검증이 필요할 때는 @field_validator 및 @model_validator를 사용하여 검사 로직을 추가할 수 있습니다.
유효성 검사를 실패하게 되면 ValidationError가 발생하도록 구현되어 있습니다.
7 Test Code

In this section, we'll dive into the world of test code, specifically how it's used to ensure a stable service in production. We've used pytest to write asynchronous test code.
Pytest's fixture is used to create mock request data, allowing for dynamic data creation and consistent usage in the test environment. This is especially important for ensuring data integrity.
The key to these tests lies in response data validation, where we verify that the response returns the expected message class and that the created review ID matches the requested review ID.
Real-World Example
In a real-world scenario, you might have a mobile or web frontend that needs data via REST. This frontend can communicate with a set of backend services.
These backend services are responsible for tasks like authentication, payments, and analytics, and they need to talk to each other. FastAPI can be used to serve REST APIs, while gRPC can be used for lightning-fast internal calls between services.
A hybrid model that combines FastAPI and gRPC gives you the best of both worlds, allowing for efficient and scalable communication between different parts of your application.
Discover more: Grpc vs Rest Performance
Benefits and Use Cases
FastAPI is a powerful framework that can support both human-facing clients and backend-to-backend communication. This is a key benefit of using FastAPI.
REST is great for human-facing clients, making it a good choice for applications that need to interact with users directly.
gRPC, on the other hand, is optimized for high-speed communication between backend services.
Here's a comparison of the two:
By choosing the right protocol for the job, you can give each client the best experience possible.
Why Use Both?
Using both REST and gRPC in a FastAPI project can be beneficial in certain scenarios. You can support both types of APIs in the same project.
Mobile or frontend users are better suited for REST APIs, while internal systems, microservices, or partners can use gRPC. This is because gRPC is generally faster and more efficient for internal communication.
Having both options available is like having both walk-in and phone ordering at your restaurant. It allows you to cater to different needs and preferences.
Here's a breakdown of when to use each:
Key Takeaways

Here's the article section:
REST is great for human-facing clients, making it a popular choice for applications that require a user-friendly interface. This is because REST is designed with simplicity and ease of use in mind.
gRPC, on the other hand, is optimized for backend-to-backend communication, making it a great choice for applications that require high-speed data transfer between services. This is particularly useful for applications that require real-time data processing.
FastAPI is a powerful framework that can support both REST and gRPC, allowing developers to choose the best approach for each client. This flexibility makes it a popular choice for modern web development.
Here's a comparison of REST and gRPC:
Project Structure and Code
When organizing your project, you might structure it like this:
- main.py – This is where you start your REST API, which handles walk-ins.
- grpc_server.py – This file starts a gRPC server, which is used for phone orders.
- scripts/grpc_only.py – If you want to run just the gRPC server separately, this is the file to use.
Having separate files for different components of your project can make it easier to manage and maintain.
Project File Examples
Project File Examples are a great way to organize your project. You might have multiple files that serve different purposes, like starting your REST API or gRPC server.
Let's take a look at some examples. A main project file could be called main.py, which starts your REST API for walk-ins.
Here are some project file examples:
- main.py – Starts your REST API (walk-ins)
- grpc_server.py – Starts a gRPC server in the same project (for phone orders)
- scripts/grpc_only.py – If you want to run just the gRPC server separately
These file names give you a clear idea of what each file does, making it easier to navigate and work on your project.
Build Everything
Building everything can take around 5-10 minutes, depending on your internet connection speed and computer's hardware specs.
This is because Docker images and Python + requirements dependencies need to be downloaded and built.
You might encounter an error about a port being in use, which is likely due to something on your machine already running on port 8000.
To resolve this, you'll need to modify the docker-compose.yml file according to your specific needs.
Featured Images: pexels.com


