Backend Engineering

Building Scalable APIs with Django and FastAPI in China

This tutorial covers how to build scalable APIs using Django and FastAPI in the unique context of the Chinese market. We focus on overcoming common pitfalls while integrating with local cloud services and addressing high traffic demands.

In the unique ecosystem of China, where services like WeChat Mini Programs dominate and traditional Google APIs are unavailable, building scalable APIs is a challenge that requires careful consideration of both technology and market demands. At PixelHorizon, we’ve worked with various clients in this landscape, learning valuable lessons about efficiency and scalability.

Common Pitfall: Using Django Alone for High Traffic

Many developers might attempt to use Django alone for building APIs, leveraging Django REST Framework (DRF) for serialization and view handling. While Django is a powerful tool, it can become a bottleneck for high traffic loads due to its synchronous nature. Here’s a simple example:

from rest_framework import serializers, viewsets
from django.shortcuts import get_object_or_404
from .models import Product

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

# urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'products', ProductViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

Problems with the Above Approach

  • Synchronous Processing: Each request to the API waits for the previous one to finish, leading to high latency under load.
  • Scalability Issues: As traffic grows, scaling Django into multiple instances can be resource-intensive and slow.

The Right Approach: Combining Django with FastAPI

To effectively handle high traffic and provide a responsive API, we can use Django for the ORM and FastAPI as the main API framework. FastAPI’s asynchronous capabilities allow us to handle many requests concurrently, making it ideal for the Chinese market's scale requirements.

Step 1: Setting Up FastAPI

First, install FastAPI and an ASGI server like Uvicorn:

pip install fastapi uvicorn

Step 2: Integrate Django with FastAPI

Next, create a FastAPI app that utilizes Django models. Ensure Django is set up, and you have the necessary configurations in settings.py.

Here’s how to structure the integration:

# main.py
import os
import django
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from asgiref.wsgi import WsgiToAsgi

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from myapp.models import Product
from pydantic import BaseModel

app = FastAPI()

# Allow CORS for WeChat Mini Programs and other clients in China
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://your-wechat-domain.com"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

class ProductModel(BaseModel):
    id: int
    name: str
    price: float

@app.get('/products/', response_model=list[ProductModel])
async def get_products():
    products = Product.objects.all()
    return [ProductModel(id=prod.id, name=prod.name, price=prod.price) for prod in products]

# To run the FastAPI app
# uvicorn main:app --host 0.0.0.0 --port 8000

Step 3: Running the Application

You can run the FastAPI server with:

uvicorn main:app --host 0.0.0.0 --port 8000

Advantages of This Approach

  • Asynchronous Handling: FastAPI’s async capabilities allow it to handle numerous requests simultaneously, reducing response times significantly.
  • Integration Flexibility: You can utilize Django’s robust ORM while benefitting from FastAPI’s efficiency, making it easier to scale as traffic increases.
  • CORS Handling: With built-in middleware, you can easily manage cross-origin requests, which is crucial for applications in the Chinese market.

Integrating with Local Cloud Services

When deploying your API, consider using Alibaba Cloud or Tencent Cloud, as they offer services optimized for the local context. Setting up auto-scaling and utilizing their managed databases ensures that your API can handle spikes in traffic without downtime. The setup will usually involve configuring a container service or serverless functions, which integrate well with both FastAPI and Django.

At PixelHorizon, our experience with local cloud providers has shown significant improvements in latency and scalability when compared to traditional hosting solutions.

Bottom line

Combining Django with FastAPI allows developers to create scalable APIs tailored for the unique challenges of the Chinese market. Leveraging asynchronous processing with FastAPI while maintaining Django's ORM capabilities can significantly enhance performance under high traffic conditions.

Building something similar in your market? We'd be happy to talk through the architecture — pixelhorizon.dev/contact.