Senior Backend Engineer Guide: Architecting High-Throughput Django REST Framework (DRF) APIs, Serializer Optimization & Keyset Cursor Pagination
Django REST Framework (DRF) powers thousands of production SaaS backends, but out-of-the-box `ModelSerializer` nesting and `LIMIT / OFFSET` pagination can bog down when tables grow past 5 million rows. As a Senior Backend Engineer with over a decade of production Django experience, here is how I tune DRF APIs for sub-25ms p95 latency.
1. Why `OFFSET 500000` Kills PostgreSQL & How `CursorPagination` Fixes It
When a client requests `?page=5000` with `PageNumberPagination`, PostgreSQL must scan and discard 500,000 index entries before returning 20 rows ($O(N)$ degradation). Switching to DRF's keyset `CursorPagination` over an indexed `(created_at DESC, id DESC)` tuple keeps page 5,000 just as fast as page 1 ($O(\log N)$):
from rest_framework.pagination import CursorPagination
from rest_framework import serializers, viewsets
class HighThroughputCursorPagination(CursorPagination):
page_size = 50
max_page_size = 200
ordering = "-created_at"
class OptimizedOrderViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = HighThroughputCursorPagination
def get_queryset(self):
return (
Order.objects.filter(tenant_id=self.request.user.tenant_id)
.select_related("customer", "invoice")
.only(
"id", "uuid", "status", "total_cents", "created_at",
"customer__id", "customer__name", "invoice__invoice_number",
)
)
2. Read-Only Serializer Projection & `orjson` Renderer
- Separate Read and Write Serializers: Use lightweight `Serializer` classes (or `.values()` dicts) for high-frequency `GET` list endpoints and full validation `ModelSerializer` classes for `POST`/`PATCH` mutations.
3. Related Backend Engineering Guides
- Hire a Senior Backend Engineer: Python, Django, FastAPI, Golang & PostgreSQL Pillar Guide
- Python, Django, FastAPI & PostgreSQL Backend Hub
⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer
Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:
Book $100 / Rp 1M 1-Day Sprint → Hire on Upwork (100% Job Success) →