Edit

Supporting Services

LearnHouse relies on several supporting services that run alongside the main applications.

  • In development (learnhouse dev), PostgreSQL and Redis run in Docker via .learnhouse/docker-compose.dev.yml while the API, web, and collab processes run locally on the host.
  • In self-hosted production, the LearnHouse CLI generates a docker-compose.yml from templates in apps/cli/src/templates/ that runs PostgreSQL, Redis, a single learnhouse-app container (which bundles web + API + collab), and either nginx or Caddy as a reverse proxy.

PostgreSQL

pgvector/pgvector:pg16 (PostgreSQL 16 with the pgvector extension) is the primary database for LearnHouse. It stores all persistent application data including:

  • Organizations and their configurations
  • User accounts and authentication records
  • Courses, chapters, and activities
  • Library folders, trails, and progress tracking
  • AI vector embeddings (via the pgvector extension)

The backend connects to PostgreSQL through SQLModel (asyncpg for the application by default, or psycopg 3 with LEARNHOUSE_DB_DRIVER=psycopg; psycopg2 for Alembic migrations). PgBouncer and Supavisor poolers are detected from the connection string, or forced with LEARNHOUSE_PGBOUNCER=true.

Default port: 5432

Redis

Redis is the caching, session, queue and collaboration persistence layer:

  • Caching: Reduces database load by storing frequently accessed data in memory.
  • Session storage: Manages user session data for fast access.
  • Rate limiting: Counters for login, signup and other throttled endpoints.
  • Job queues: Background work run by the API (AI search indexing, HLS transcoding, AI captions).
  • Collaboration state: Stores Yjs document state from the Collab server with a 1-hour TTL before it is persisted to PostgreSQL.

The API reads its connection from LEARNHOUSE_REDIS_CONNECTION_STRING and the collaboration server from LEARNHOUSE_REDIS_URL. The dev compose uses redis:8.6.1-alpine; the CLI-generated self-host production compose uses redis:7.4-alpine.

Default port: 6379

Hocuspocus (Collaboration Server)

The Hocuspocus server, located at apps/collab, provides real-time collaboration on boards. Each board is one shared document. It is a Node.js + TypeScript application (run with tsx watch in dev, and from its compiled build in the app image) built on Hocuspocus 4, which uses the Yjs CRDT library under the hood. It connects to Redis via ioredis.

Default port: 4000 (configurable via COLLAB_PORT env var)

VariablePurposeDefault
COLLAB_PORTListening port4000
LEARNHOUSE_API_URLAPI base URLhttp://localhost:8000
LEARNHOUSE_AUTH_JWT_SECRET_KEYMust match the APInone (required)
COLLAB_INTERNAL_KEYShared key for calls to the API's internal endpoints; must match the APInone (required)
LEARNHOUSE_REDIS_URLRedis connectionredis://localhost:6379
COLLAB_TRUST_PROXYSet to true behind a reverse proxy so client IPs come from X-Forwarded-Forfalse

The server refuses to start when LEARNHOUSE_AUTH_JWT_SECRET_KEY or COLLAB_INTERNAL_KEY is missing or still an example placeholder, and warns when one is short.

Authentication

The Collab server performs JWT verification using the same secret as the API backend. On connection, it verifies board membership by making a request to the API. People who may only view a board (for example on a public board) get a read-only connection. Open connections re-check their access every minute and are closed when it is gone.

Persistence

Document state follows a two-tier persistence strategy:

  1. Redis: Yjs state is first stored in Redis with a 1-hour TTL (REDIS_YDOC_TTL = 3600) for fast access.
  2. PostgreSQL: State is persisted to the database via the API with a 5-second debounce (DB_FLUSH_DELAY = 5000) to avoid excessive writes.

Rate Limiting and Capacity

  • Seats per board: 10 editors and 10 read-only viewers at once (MAX_BOARD_EDITORS, MAX_BOARD_VIEWERS), and at most 2 sessions per board on one connection
  • HTTP rate limiting of 30 plain HTTP requests per IP per minute (RATE_LIMIT_MAX, RATE_LIMIT_WINDOW_MS)
  • WebSocket limits: 300 new sockets per IP per minute, 200 open sockets per IP, and 2,000 unauthenticated sockets in total. A socket must authenticate within 10 seconds.
  • Size limits: 2 MB per message and 5 MB per document (MAX_MESSAGE_BYTES, MAX_YDOC_BYTES)

Key Features

  • Real-time document synchronization: Multiple users can edit the same board simultaneously.
  • Conflict resolution: Yjs CRDTs handle concurrent edits without conflicts.
  • Durable persistence: Document state survives server restarts through Redis and PostgreSQL.

The frontend connects to Hocuspocus directly over WebSocket using the @hocuspocus/provider client for low-latency collaborative editing.

Reverse Proxy (nginx or Caddy)

The CLI-generated self-host stack uses one of two reverse proxies depending on the SSL choice made during setup:

  • Caddy (caddy:2.11-alpine): selected when "Automatic SSL" is enabled; handles Let's Encrypt certificate issuance and renewal automatically.
  • nginx (nginx:1.30-alpine): selected for manual SSL or HTTP-only setups.

Both route traffic to port 80 of the single learnhouse-app container, where an internal nginx sends each path to the Next.js frontend, the FastAPI backend, or the Hocuspocus collaboration server.

Service Communication

Logically, a request flows like this:

Client
  └── nginx / caddy (reverse proxy)
        └── learnhouse-app (single container, internal nginx on port 80)
              ├── Next.js (web, port 8000)
              ├── FastAPI (api, port 9000)
              │     ├── PostgreSQL (port 5432)
              │     └── Redis (port 6379)
              └── Hocuspocus (collab, port 4000)
                    ├── Redis (port 6379)
                    └── FastAPI (for persistence & JWT-based auth)

The Hocuspocus server authenticates connections via JWT (shared secret with the API), verifies board membership through the API, and persists document state to both Redis and PostgreSQL (via the API). PostgreSQL is accessed directly only by the backend.