The Motivation: Scaling Beyond Synchronous Worker Limits
Earlier on this blog, I documented the full migration of my 2017 junior high school examination platform from Laravel 5.2 to modern Laravel 13, Filament 3, and Livewire 3 (open-sourced at mfarim/laravel-elearning). I followed it with an enterprise companion edition built with Java 21 Spring Boot & React.
The monolithic Laravel platform is still very productive:
- Filament 3 gives you admin CRUD dashboards in minutes.
- Livewire 3 adds reactive question timers without a complex frontend build pipeline.
- Pest PHP provides solid test coverage and IDOR vulnerability hardening.
However, scaling high-stakes Computer-Based Testing (CBT) and Learning Management Systems (LMS) across academic districts, province-wide standardized testing, and AI-assisted educational tooling raises new architectural challenges:
- The "Exam Rush Hour" Concurrency Bottleneck: When 2,000 to 10,000 candidates click "Start Exam" at exactly 08:00:00 AM, traditional synchronous PHP-FPM dedicates one full operating system process to each request (40MB–80MB of RAM per worker). Spawning thousands of these workers leads to process exhaustion, context-switch thrashing, and high server costs.
- Persistent Asynchronous Real-Time Proctoring: Teachers supervising exam halls need continuous live telemetry (remaining time, current question palette, anti-cheat tab-switch flags). Keeping thousands of real-time connections open requires a non-blocking asynchronous event loop that can handle them with minimal memory overhead (~15MB–25MB).
- First-Class AI & Data Science Ecosystem: Academic platforms are quickly adding automated grading, NLP essay evaluation, semantic question similarity search, and automated difficulty calibration. Python is the lingua franca of AI/ML and data science.
- Decoupled Multi-Platform Client Ecosystem: Institutions want progressive web apps and mobile apps (iOS/Android) that consume the same strongly typed, self-documenting REST and WebSocket APIs.
To meet these demands, I built the third companion platform in this series: Python FastAPI + React E-Learning.
Architectural Comparison: Monolith vs. Async Decoupled Architecture
Here is how each critical layer transitions between the two architectures:
| Architectural Concern | Laravel Monolithic Edition | Python FastAPI + React Edition |
|---|---|---|
| Language & Runtime | PHP 8.3, PHP-FPM process-per-request | Python 3.12+ with Uvicorn (ASGI event loop, non-blocking async/await) |
| Backend Framework | Laravel 13 Monolith | FastAPI 0.111+ (High-performance micro-framework) |
| Frontend Framework | Blade templates + Livewire 3 reactive components | React 19 SPA + TypeScript + Vite 6 + Tailwind CSS v4 |
| Data Validation & DTOs | Laravel FormRequest classes | Pydantic v2 (Rust-powered compilation, sub-millisecond parsing) |
| Authentication | Stateful PHP session cookies + CSRF tokens | Stateless OAuth2 Bearer JWT with 1-Click Impersonation Engine |
| Authorization | Laravel Gate & Model Policies | FastAPI Dependency Injection (Depends(require_roles([...]))) |
| Database & Migrations | MySQL 8 with Laravel Artisan migrations | PostgreSQL 16 with SQLAlchemy 2.0 Async Engine + Alembic |
| Caching & Pub/Sub | Redis cache via Laravel Cache facade | Redis 7 via redis-py async connection pool & Pub/Sub channels |
| Real-Time Telemetry | Laravel Reverb / Echo client | Native ASGI WebSockets (/ws/exams/{id}/monitor) with Redis broadcast |
| Batch Processing | Laravel Queues + PhpSpreadsheet | openpyxl streaming reader for student & question .xlsx templates |
| API Documentation | Scramble / Scribe OpenAPI generator | Automated OpenAPI 3.1 with interactive Swagger UI (/docs) & ReDoc |
| Deployment Model | Single container / Linux VPS systemd unit | Multi-container Docker Compose (Nginx, React, FastAPI, Postgres, Redis, MinIO) |
System Architecture Blueprint
Below is the decoupled cloud-ready architecture implemented in the Python FastAPI + React repository:
Key Engineering Highlights & Deep Dive
1. FastAPI Dependency Injection: Stateless JWT & Impersonation Engine
In monolithic applications, admin account impersonation typically relies on stateful server sessions (Auth::loginUsingId($userId)). In a decoupled Single Page Application (SPA), we need stateless JWT tokens that carry immutable security claims and still support on-demand admin impersonation with a full audit trail.
In backend/app/api/v1/auth.py, the impersonation endpoint generates a short-lived token stamped with the target user's identity and the administrator's audit ID:
@router.post("/admin/impersonate/{user_id}", response_model=ApiResponse[AuthResponse])
async def impersonate(
user_id: int,
admin_user: User = Depends(require_roles(["ROLE_ADMIN"])),
db: AsyncSession = Depends(get_db),
):
stmt = (
select(User)
.options(
selectinload(User.roles),
selectinload(User.teacher_profile),
selectinload(User.student_profile).selectinload(Student.classroom),
)
.where(User.id == user_id, User.deleted_at.is_(None))
)
target_user = (await db.execute(stmt)).scalars().first()
if not target_user:
raise HTTPException(status_code=404, detail="Target user not found")
target_roles = [r.name for r in target_user.roles]
token = create_access_token(
subject=target_user.id,
roles=target_roles,
extra_claims={
"is_impersonating": True,
"original_admin_id": admin_user.id,
},
)
auth_data = build_auth_response(target_user, token, admin_user.id)
return ApiResponse.ok(data=auth_data, message=f"Impersonating {target_user.name}")
Seamless Session Recovery
When the administrator finishes diagnosing the issue, they call /api/v1/auth/admin/stop-impersonate. The backend verifies the original_admin_id claim in the token, restores the administrator's original session, and drops the impersonated permissions without asking the admin to log in again.
2. High-Precision CBT Exam Engine: Anti-Cheat & Native WebSocket Telemetry
The examination engine in backend/app/api/v1/exam_runner.py enforces strict server-side rules, so a candidate can't bypass them by changing the system clock on their device.
Server-Driven Timer Calculation
now = datetime.now(timezone.utc) # started_at must be DateTime(timezone=True)
total_duration_sec = exam.duration_minutes * 60
if attempt and attempt.status in ["in_progress"]:
# Calculate exact elapsed duration from server-stamped start time
elapsed = (now - attempt.started_at).total_seconds()
remaining = max(0, int(total_duration_sec - elapsed))
# Enforce hard deadline
if remaining <= 0:
attempt.status = "submitted"
attempt.finished_at = now
await db.commit()
raise HTTPException(
status_code=400,
detail="Time limit reached for this exam attempt"
)
Anti-Cheat Proctoring & Native WebSocket Broadcasting
On the frontend (frontend/src/pages/exams/ExamRunner.tsx), the candidate's testing environment is locked down:
- Right-click and copy-paste events are intercepted.
- Window blur and tab switching trigger HTML5
visibilitychangeevents. - Violations are sent to the backend via
/api/v1/exams/attempts/{id}/violation. When a candidate reaches 5 violations, the server force-finishes the exam.
# Server-side violation handler with instant broadcast
attempt.violations += 1
if attempt.violations >= 5:
attempt.status = "force_finished"
attempt.finished_at = datetime.now(timezone.utc)
await db.commit()
# Broadcast event to teacher proctoring screen via WebSocketManager
await ws_manager.broadcast_to_exam(
exam_id=attempt.examination_id,
event_type="VIOLATION_LOGGED",
data={
"studentId": student.id,
"studentName": student.name,
"violationsCount": attempt.violations,
"status": attempt.status,
}
)
The WebSocket manager broadcasts these events to connected teacher dashboards (ExamMonitor.tsx) almost instantly:
class ConnectionManager:
def __init__(self):
self.active_rooms: Dict[int, Set[WebSocket]] = {}
async def broadcast_to_exam(self, exam_id: int, event_type: str, data: dict):
if exam_id not in self.active_rooms:
return
payload = {"eventType": event_type, "data": data}
message_str = json.dumps(payload, default=str)
dead_connections = set()
for connection in self.active_rooms[exam_id]:
try:
await connection.send_text(message_str)
except Exception:
dead_connections.add(connection)
for dead in dead_connections:
self.disconnect(exam_id, dead)
3. Memory-Safe Batch Processing with openpyxl
When importing large student rosters (e.g., 2,000 enrolled candidates) or question banks with 200 items full of math formulas and multiple-choice options, synchronous scripts often hit CPU timeouts or memory limits.
In FastAPI, we use openpyxl to parse worksheets in memory, validate rows against Pydantic schemas, and perform bulk database inserts inside an asynchronous transaction. The upload itself is still read into memory with await file.read(), so cap the request size (for example with Nginx client_max_body_size, covered in Part 6) to keep memory use bounded:
from openpyxl import load_workbook
import io
@router.post("/students/import-excel", response_model=ApiResponse[Dict[str, int]])
async def import_students_excel(
file: UploadFile = File(...),
db: AsyncSession = Depends(get_db),
admin: User = Depends(require_roles(["ROLE_ADMIN"])),
):
content = await file.read()
wb = load_workbook(filename=io.BytesIO(content), read_only=True, data_only=True)
sheet = wb.active
imported_count = 0
# Process rows safely without loading unnecessary XML DOM structures
for row in sheet.iter_rows(min_row=2, values_only=True):
nis, name, email, gender, classroom_id = row[0], row[1], row[2], row[3], row[4]
if not nis or not name:
continue
# Provision User account + Student profile atomically
await create_student_record(db, nis=str(nis), name=name, email=email, gender=gender, classroom_id=classroom_id)
imported_count += 1
await db.commit()
return ApiResponse.ok(data={"imported": imported_count}, message="Bulk student import completed")
4. SQLAlchemy 2.0 Async Engine & Pydantic v2 Type Safety
Unlike traditional ORMs that hide blocking I/O calls behind property getters, the SQLAlchemy 2.0 async engine requires you to compose queries explicitly:
# Fully typed asynchronous join with relationship preloading
stmt = (
select(Examination)
.options(
selectinload(Examination.teacher),
selectinload(Examination.subject),
selectinload(Examination.classroom),
selectinload(Examination.questions),
)
.where(Examination.id == exam_id, Examination.deleted_at.is_(None))
)
result = await db.execute(stmt)
exam = result.scalars().first()
With Pydantic v2, every request payload and response is validated by Pydantic's Rust engine (pydantic-core), which serializes data up to 5x to 15x faster than legacy Python frameworks.
Frontend Architecture: React 19, TypeScript & Tailwind CSS v4
The companion frontend application (frontend/) is built to stay responsive on classroom desktops and mobile devices:
- Modern React 19 SPA: Built with Vite 6, with fast Hot Module Replacement (HMR) and clean TypeScript interfaces.
- Emerald Design System: A clean slate-and-emerald palette (
#059669,#10b981) built with Tailwind CSS v4, matching the enterprise feel of modern educational platforms. - Zustand State Store: Persists authentication tokens and active user roles, and shows a global top banner while administrative impersonation is active.
- CBT Question Palette: Fast touch and keyboard navigation across 100+ questions with real-time visual status cues:
- 🟢 Green: Answer saved and synchronized to server
- ⚪ Gray: Unanswered question
- 🟡 Yellow: Flagged question under review
- Printable Exam Cards: Automatically generated official CBT exam hall tickets with NIS barcodes.
When to Choose: Laravel vs. Java Spring Boot vs. Python FastAPI
I've rebuilt the same platform on all three technology stacks, so here is a practical comparison for engineering leads:
| Scenario / Requirement | Recommended Stack | Technical Rationale |
|---|---|---|
| Fastest Time to Market / Solo Founder | Laravel 13 + Livewire + Filament | Unmatched developer velocity, built-in admin panel, zero boilerplate, full-stack in a single repo. |
| High Concurrency & Low Memory Usage | Python 3.12 FastAPI + React 19 | Async event loop serves thousands of concurrent I/O connections on minimal RAM (~20MB); ideal for containerized microservices. |
| AI, NLP & Machine Learning Workflows | Python 3.12 FastAPI + React 19 | Native integration with PyTorch, Hugging Face, scikit-learn, LangChain, and automated essay grading engines. |
| Strict Enterprise Compliance & Banking-Grade Typing | Java 21 Spring Boot + React 19 | Compile-time static type guarantees, Virtual Threads (Project Loom), Spring Security declarative filters, enterprise LTS. |
| Decoupled Mobile App Ecosystem | FastAPI or Spring Boot + React | Strongly typed OpenAPI specifications with automated SDK generation for iOS, Android, and web clients. |
Open-Source Repositories
All three companion implementations are open-sourced under the MIT license:
- 🐘 Laravel Monolith Edition: https://github.com/mfarim/laravel-elearning
- Laravel 13, Livewire 3, Filament 3, MySQL 8, Pest PHP
- ☕ Java Spring Boot + React Edition: https://github.com/mfarim/spring-elearning-react
- Java 21 LTS, Spring Boot 3.4, React 19, PostgreSQL 16, Redis 7, WebSocket STOMP
- 🐍 Python FastAPI + React Edition: https://github.com/mfarim/fastapi-elearning-react
- Python 3.12, FastAPI, SQLAlchemy 2.0 Async, Pydantic v2, React 19, Native WebSockets, Redis Pub/Sub
Getting Started Locally
You can start the complete Python FastAPI + React environment with a single Docker Compose command:
# Clone the repository
git clone https://github.com/mfarim/fastapi-elearning-react.git
cd fastapi-elearning-react
# Launch all services (PostgreSQL, Redis, MinIO, FastAPI Backend, React Frontend)
docker compose up -d --build
Once the containers are up, open:
- Web Application: http://localhost:3001
- FastAPI Interactive Swagger Docs: http://localhost:8000/docs
- Alternative ReDoc Specification: http://localhost:8000/redoc
Default Demo Credentials:
- Admin:
[email protected]/password - Teacher:
[email protected]/password - Student:
[email protected]/password
Conclusion & Next Steps
Rebuilding the monolithic Laravel platform with Python FastAPI and React shows how much scalability, and developer joy, a modern asynchronous architecture can unlock. Python is no longer just for scripts or data science notebooks. Combined with FastAPI and ASGI, it is a first-class engine for high-concurrency, real-time distributed applications.
In Part 2, we'll look closely at database migrations: moving from Laravel migrations to asynchronous SQLAlchemy 2.0 models and Alembic migration scripts. Stay tuned!

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.
