Summary
- A single-process Go API: reads are served from memory, critical writes such as exams, submissions and accounts go to the relational store synchronously.
- Live classes and recordings over LiveKit and WebRTC; recordings sit in private storage and play only through short-lived signed links.
- Revision-based optimistic concurrency: if another writer changed the revision, the stale writer is rejected.
On this page (10 sections)
- Next.js client Offline queues, token refresh, livekit-client
- Go API Single process, in-memory state, background workers
- PostgreSQL 16 Relational persistence and revisions
- LiveKit and Egress WebRTC rooms and recording
- MinIO / S3 Private bucket, short-lived signed playback
Context
Ders Alanı brings a tutoring centre’s exams, homework, live classes, attendance and report cards into one platform for administrators, principals, teachers, students and parents. I ran design, architecture, back end, front end, infrastructure and operations as the sole developer.
Architecture
Next.js client (persistent offline queues, token refresh, pdf.js, livekit-client)
│ HTTPS / JWT
Go API (single process) ── in-memory state + synchronous persistence for critical writes
│ background workers: exam deadlines, report exports, recording tracking
├── PostgreSQL 16 (relational persistence, revisions, shared rate limits)
├── LiveKit + Egress (WebRTC rooms and recording)
└── MinIO / S3 (private bucket, short-lived signed playback)
In-memory state, synchronous persistence. Reads are served from memory, so latency is low; critical writes such as exams, submissions and accounts go to the relational layer synchronously. If the database is selected but unreachable, the API refuses to start rather than silently falling back to memory, a deliberate choice against silent data loss.
Revision-based optimistic concurrency. Every write locks and increments a revision row; if another writer has moved the revision, the cache is invalidated. With multiple replicas, a stale writer is rejected (fencing).
Go in this architecture
The back end runs as a single Go process: shared state lives in one package with an explicit lock order, background workers run in the same process, and concurrency bugs are hunted in CI with Go’s race detector on the relevant test subset. PDF report cards are generated in the same process with an embedded Turkish font.
Real-time communication: LiveKit and WebRTC
- Live classes stream over WebRTC in LiveKit rooms; the Go back end issues an access token only after checking authorisation.
- Egress writes recordings to a private MinIO/S3 bucket. Anonymous access is disabled; playback goes through the back end with per-resource authorisation and short-lived signed streams.
- Access to a class and access to its recording are checked separately in the API; teacher removal and moderation flows are tested together with LiveKit.
- Live attendance events travel through the same persistent queue pattern used for exams.
An exam flow that does not lose answers
- The server owns the clock; the client’s clock is not trusted. The answer key is never sent to the client during an exam.
- Answers are written to a persistent IndexedDB queue in the browser; nothing is deleted until the server returns a durable receipt.
- Operations are idempotent: sending the same operation again does not change the result, and a conflicting retry with different content returns a distinct error.
- Answers arriving after the deadline follow a recovery policy, with an alert when the queue backs up. Stale tabs, stale packets and restarts each have their own regression tests.
Authorisation and security
- Short-lived JWTs with refresh, bcrypt password hashing, login lockout, and a shared rate limit stored in PostgreSQL so it stays consistent across replicas.
- Access is checked by role, institution and class/teacher relationship together; scenarios such as reading another institution’s student are covered by 403/404 tests.
- Question booklets are served behind an authorisation check, in chunks via HTTP range requests.
- CSP, X-Frame-Options, nosniff, Referrer-Policy, HSTS and a restrictive Permissions-Policy on both the API and the client.
- Email verification and email-based password reset.
Performance: optimise by measuring
A synthetic 300-student load test showed the exam write path did not scale: every write rebuilt and serialised the whole state, serially, under a global write lock. The fix reduced a quadratic scan in state reconstruction to a single pass and added a scoped write path that reads only the relevant rows and writes only the changed rows as a diff.
| Metric (isolated server, synthetic data) | Before | After |
|---|---|---|
| Autosave p50 / p95 | 27 s / 30 s | 19 ms / 751 ms, zero errors |
| Concurrent submission | 134 timeouts | 268/268 successful |
| Exam start p50 | 5.5 s | 2.7 s |
A separate check on real PostgreSQL confirmed no lost writes across 40 concurrent writes and that retries produce no extra writes. These are controlled test results, not real user traffic.
Observability and operations
Prometheus metrics and alerting rules for API downtime, 5xx spikes, database pool saturation, exam write errors, stuck recordings, queue age and client LCP. Written runbooks for start-up, migrations, backup/restore, database outages and rollbacks. Migrations were validated by restoring a real backup into a temporary database.
Bottlenecks and scaling plan
- Single process. In-memory state gives the lowest latency on one replica; fencing and shared rate limits are already in place for running several.
- Write path. Full serialisation under a global lock is gone; the remaining cost is bounded by the number of relevant rows.
- Media. Live-class load sits on LiveKit, separate from the application server; recordings live in object storage.
- Cache consistency. The most expensive class of bug. Revision-based invalidation and field-coverage tests stop a newly added field from silently disappearing from copies.
Testing strategy
Go unit and integration tests, tests against real PostgreSQL, a race-detector subset in CI, front-end unit tests, Playwright browser scenarios, a load-testing tool and static analysis (go vet, tsc, ESLint with zero warnings).