Muhammad Fari Madyan
Hire me
FastAPI · part 2 of 6

Python FastAPI E-Learning Part 2: Async ORM & Type Safety – SQLAlchemy 2.0 Async, Pydantic v2 & N+1 Prevention

Master modern async database persistence and schema validation in Python 3.12: SQLAlchemy 2.0 Async engine, eliminating N+1 query traps with selectinload, avoiding MissingGreenlet errors with expire_on_commit=False, and sub-millisecond serialization using Pydantic v2.

Muhammad Fari Madyan

· 9 min read

Baca dalam Bahasa Indonesia →

The Asynchronous Persistence Challenge in Python

When moving an enterprise backend from PHP or Ruby to Python, developers often run into a serious misconception: "Wrapping database queries in an async def route automatically makes them asynchronous."

In reality, if you use a synchronous database driver (such as plain psycopg2, or legacy SQLAlchemy 1.4 patterns), every query silently blocks the single-threaded asyncio event loop. If 200 students fetch their CBT exam payload at the same time, every coroutine waits in line behind synchronous socket I/O. Throughput collapses and response latency skyrockets.

To get real non-blocking concurrency in mfarim/fastapi-elearning-react, we built the persistence and data validation layer on three pillars:

  1. SQLAlchemy 2.0 Async Engine with asyncpg: Native asynchronous PostgreSQL connection pooling.
  2. Deterministic Eager Loading: Explicit selectinload strategies that eliminate lazy-loading traps and MissingGreenlet exceptions.
  3. Pydantic v2 Validation Pipeline: Sub-millisecond payload parsing and JSON serialization powered by Rust (pydantic-core).
┌────────────────────────────────────────────────────────────────────────┐ │ FASTAPI REQUEST PIPELINE ARCHITECTURE │ └───────────────────────────────────┬────────────────────────────────────┘ │ HTTP Request (JSON Body) ▼ ┌────────────────────────────────────────────────────────────────────────┐ │ PYDANTIC V2 DTO LAYER │ │ • Rust-accelerated schema parsing & field validation │ │ • Strict type casting: UUIDs, ISO datetime, Enums │ └───────────────────────────────────┬────────────────────────────────────┘ │ Validated Model Object ▼ ┌────────────────────────────────────────────────────────────────────────┐ │ FASTAPI DEPENDENCY INJECTION (get_db) │ │ • Yields AsyncSession from async_sessionmaker │ │ • Automatic commit on success / Rollback on unhandled exception │ └───────────────────────────────────┬────────────────────────────────────┘ │ AsyncSession (Non-blocking I/O) ▼ ┌────────────────────────────────────────────────────────────────────────┐ │ SQLALCHEMY 2.0 ASYNC ORM (asyncpg) │ │ • Explicit 2.0 select() statements (No legacy Query objects) │ │ • Strategic eager loading: selectinload() & joinedload() │ └───────────────────────────────────┬────────────────────────────────────┘ │ TCP Stream (asyncio event loop; libuv via uvloop) ▼ ┌──────────────────┐ │ PostgreSQL 16 │ └──────────────────┘

1. Async Engine & Connection Pool Architecture

In backend/app/core/database.py, the database engine is configured for high-concurrency workloads with asyncpg, the asynchronous binary PostgreSQL driver:

PYTHON
# backend/app/core/database.py
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import DeclarativeBase
from app.core.config import settings

# 1. Asynchronous connection pool
engine = create_async_engine(
    settings.DATABASE_URL,       # postgresql+asyncpg://user:pass@host:5432/elearning
    echo=False,                  # Keep False in production to avoid logging overhead
    future=True,                 # Enforce SQLAlchemy 2.0 syntax standards
    pool_pre_ping=True,          # Test connection health before checkout (reconnect dead sockets)
    pool_size=10,                # Persistent connection pool per worker process
    max_overflow=20,             # Burst capacity under sudden exam rush spikes
)

# 2. Asynchronous Session Factory
AsyncSessionLocal = async_sessionmaker(
    bind=engine,
    autocommit=False,
    autoflush=False,
    expire_on_commit=False,      # CRITICAL: Prevent MissingGreenlet post-commit
    class_=AsyncSession,
)

class Base(DeclarativeBase):
    """Modern DeclarativeBase replaces legacy declarative_base()"""
    pass

Why expire_on_commit=False is Non-Negotiable

In traditional synchronous SQLAlchemy, calling session.commit() immediately expires all in-memory attributes on your model instances. The next time you access exam.title or exam.questions, SQLAlchemy quietly issues an implicit SELECT behind the scenes (lazy loading).

In an asynchronous runtime, that implicit I/O can't happen, because you can't put await in front of an attribute access like user.roles. Reading an expired attribute after a commit raises the dreaded error:

TEXT
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call a greenlet-waiting function in a different greenlet.

With expire_on_commit=False, model objects keep their loaded in-memory state after commit(), so endpoints can hand them to Pydantic for serialization without extra round-trips or greenlet crashes.


2. Modern 2.0 Model Declarations with Mapped and mapped_column

Legacy SQLAlchemy codebases rely on loose typing (Column(Integer, primary_key=True)), which leads to weak IDE autocompletion and silent type-mismatch bugs.

In backend/app/models/exam.py, all models use Python 3.10+ PEP 484 type annotations through Mapped[T] and mapped_column():

PYTHON
# backend/app/models/exam.py
from datetime import datetime, date
from typing import Optional, List, Any
from sqlalchemy import BigInteger, String, Boolean, Integer, Date, DateTime, ForeignKey, Text, JSON
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.database import Base

class Examination(Base):
    __tablename__ = "examinations"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    teacher_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("teachers.id", ondelete="CASCADE"), nullable=False, index=True
    )
    subject_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("subjects.id", ondelete="CASCADE"), nullable=False
    )
    classroom_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("classrooms.id", ondelete="CASCADE"), nullable=False, index=True
    )
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    duration_minutes: Mapped[int] = mapped_column(Integer, default=60, nullable=False)
    passing_score: Mapped[int] = mapped_column(Integer, default=75, nullable=False)
    status: Mapped[str] = mapped_column(String(20), default="draft", nullable=False)
    
    # Fully typed relationship mappings with explicit cascade behavior
    teacher: Mapped["Teacher"] = relationship("Teacher", lazy="selectin")
    subject: Mapped["Subject"] = relationship("Subject", lazy="selectin")
    classroom: Mapped["Classroom"] = relationship("Classroom", lazy="selectin")
    questions: Mapped[List["Question"]] = relationship(
        "Question", back_populates="examination", cascade="all, delete-orphan"
    )
    attempts: Mapped[List["ExamAttempt"]] = relationship(
        "ExamAttempt", back_populates="examination", cascade="all, delete-orphan"
    )

Notice the static type guarantees:

  • id: Mapped[int] tells Mypy and IDEs that exam.id is guaranteed to be an integer.
  • description: Mapped[Optional[str]] correctly handles nullable text columns.
  • questions: Mapped[List["Question"]] enables full autocompletion on relationship collections.

3. The N+1 Query Trap & Strategy: selectinload vs. joinedload

The N+1 query problem is the number one database performance killer in web applications.

Consider a list of 20 exams. If the backend fetches all 20 exams and then accesses exam.questions in a loop, a naive ORM issues 1 initial query followed by 20 individual queries:

$$\text{Total Queries} = 1 + N = 21 \text{ queries}$$

On an async event loop, this cascade of serial queries destroys throughput. SQLAlchemy 2.0 solves it with explicit loader strategies:

Option A: joinedload (Outer Join)

Performs an SQL LEFT OUTER JOIN in a single query:

PYTHON
stmt = select(Examination).options(joinedload(Examination.questions))
  • Trade-off: For one-to-many collections (e.g., 50 questions per exam), joinedload sends a large Cartesian product of duplicated rows over the wire, which inflates the network payload and the Python deserialization overhead.

Option B: selectinload (Batch IN Query) — The Ideal Solution

Issues exactly two clean queries regardless of the collection size:

PYTHON
# Query 1: Fetch examinations
# SELECT * FROM examinations WHERE status = 'published';

# Query 2: Batch fetch all related questions for all retrieved exams
# SELECT * FROM questions WHERE examination_id IN (1, 2, 3, ..., 20);

In backend/app/api/v1/exams.py, nested eager loads stay clean and readable:

PYTHON
stmt = (
    select(Examination)
    .options(
        selectinload(Examination.teacher),
        selectinload(Examination.subject),
        selectinload(Examination.classroom),
        selectinload(Examination.questions).selectinload(Question.options),
    )
    .where(Examination.id == exam_id, Examination.deleted_at.is_(None))
)
result = await db.execute(stmt)
exam = result.scalars().first()

By chaining .selectinload(Examination.questions).selectinload(...), the entire nested object graph (Exam $\rightarrow$ Questions $\rightarrow$ Options) is fetched in 3 fast indexed queries, and the full statement above runs 6 (one base query plus one per selectinload), which removes N+1 bottlenecks entirely.


4. Async Session Lifecycle & Clean Dependency Injection

FastAPI's dependency injection system makes database transaction boundaries easy to manage. In backend/app/core/database.py:

PYTHON
async def get_db() -> AsyncGenerator[AsyncSession, None]:
    async with AsyncSessionLocal() as session:
        try:
            yield session
            await session.commit()
        except Exception:
            await session.rollback()
            raise
        finally:
            await session.close()

Architectural Benefits:

  1. Automated Commit on Route Completion: Route handlers don't need repetitive await db.commit() boilerplate for standard mutations. When the route exits cleanly, the transaction is committed.
  2. Fail-Safe Rollback: If any unhandled exception or validation error occurs, the session rolls back immediately, preventing orphaned transactions or locked rows.
  3. Guaranteed Socket Return: The finally: block ensures connections are cleanly returned to the pool even during client aborts or network dropouts.

5. Pydantic v2: Rust-Engine Validation & Unified Envelope

In FastAPI, data transfer objects (DTOs) bridge the gap between untrusted incoming HTTP payloads and internal ORM models. In backend/app/schemas/exam.py:

PYTHON
# backend/app/schemas/exam.py
from datetime import datetime
from typing import Optional, List, Any
from pydantic import BaseModel, Field, ConfigDict

class QuestionOptionDto(BaseModel):
    key: str = Field(..., description="Option letter: A, B, C, D, or E")
    text: str = Field(..., min_length=1, description="Option answer text")

class QuestionResponse(BaseModel):
    id: int
    question_text: str
    question_type: str
    options: Optional[Any] = None
    points: int = 1
    difficulty: str = "medium"

    model_config = ConfigDict(from_attributes=True)  # Replaces legacy orm_mode = True

class ExamStartResponse(BaseModel):
    attempt_id: int
    examination_id: int
    duration_minutes: int
    remaining_seconds: int
    started_at: datetime
    questions: List[QuestionResponse]

    model_config = ConfigDict(from_attributes=True)

The Unified API Response Envelope (ApiResponse[T])

To give frontend engineers (working with React, Next.js, or mobile clients) a consistent JSON contract, we define a generic response envelope in backend/app/common/response.py:

PYTHON
# backend/app/common/response.py
from datetime import datetime, timezone
from typing import Generic, TypeVar, Optional, Any
from pydantic import BaseModel, Field

T = TypeVar("T")

class ApiResponse(BaseModel, Generic[T]):
    success: bool = True
    message: str = "Success"
    data: Optional[T] = None
    timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))

    @classmethod
    def ok(cls, data: Optional[T] = None, message: str = "Success") -> "ApiResponse[T]":
        return cls(success=True, message=message, data=data)

    @classmethod
    def error(cls, message: str = "Error", data: Optional[Any] = None) -> "ApiResponse[Any]":
        return cls(success=False, message=message, data=data)

Here it is in a route handler:

PYTHON
@router.get("/{id}", response_model=ApiResponse[ExamStartResponse])
async def get_exam(id: int, db: AsyncSession = Depends(get_db)):
    exam = await exam_service.get_by_id(db, id)
    return ApiResponse.ok(data=exam, message="Exam retrieved successfully")

The resulting JSON output is strictly typed, self-documenting in Swagger UI (/docs), and uniform across the entire platform:

JSON
{
  "success": true,
  "message": "Exam retrieved successfully",
  "data": {
    "attempt_id": 142,
    "examination_id": 12,
    "duration_minutes": 90,
    "remaining_seconds": 5400,
    "started_at": "2026-10-05T08:00:00Z",
    "questions": [
      {
        "id": 101,
        "question_text": "What is the primary benefit of selectinload in SQLAlchemy 2.0?",
        "question_type": "multiple_choice",
        "points": 5,
        "difficulty": "medium"
      }
    ]
  },
  "timestamp": "2026-10-05T08:00:01.124Z"
}

Benchmark: Synchronous ORM vs. Async SQLAlchemy 2.0

An Apache Bench run (ab -n 5000 -c 100) against the exam retrieval endpoint on a 4-core VPS shows how large the concurrency gap is:

MetricSynchronous ORM (Flask + psycopg2)SQLAlchemy 2.0 Async (FastAPI + asyncpg)Improvement
Requests / Second312 req/sec2,840 req/sec~9.1x Faster
Median Latency (p50)320 ms35 ms~9.1x Lower Latency
P99 Latency1,480 ms110 ms13.4x More Consistent
Memory Footprint450 MB (10 fork workers)85 MB (Single event loop)81% Memory Reduction

Key Takeaways for Engineering Leads

  1. Always set expire_on_commit=False on async session factories to prevent post-commit attribute expiration and MissingGreenlet crashes.
  2. Favor selectinload for 1-to-many relationships: It runs two indexed queries and avoids both Cartesian row multiplication and N+1 overhead.
  3. Migrate to Mapped[T] and mapped_column: This modern declarative style catches type mismatches before runtime and gives you reliable IDE autocompletion.
  4. Use Pydantic v2's model_config = ConfigDict(from_attributes=True): It reads attributes directly from ORM instances at Rust-native speed, with no manual dict conversion.

In Part 3, we move to the real-time core of the platform: High-Precision CBT Exam Engine & Native WebSocket Proctoring. We'll cover server-synchronized countdown timers, anti-cheat violation traps, and low-latency Redis Pub/Sub broadcasting.

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 1High-Concurrency Async Architecture & Cloud-Ready Migrationpart 3 →High-Precision CBT Exam Engine & Native WebSocket Proctoring

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan