Designing a Q&A Forum (Quora / StackOverflow)

Difficulty: Intermediate · Prerequisites: Caching, Database Indexing, and Fan-Out


TL;DR

A Q&A forum lets users post questions, write answers, and vote on both. Reads dominate writes 100:1, and most visitors arrive cold from a search engine to read one question and its top answers. The hard parts: serving that read volume cheaply, ranking answers so the best one lands first, and full-text search over 10M questions.

The shape of the answer: Postgres as the source of truth, Redis in front of the read path, Elasticsearch beside it for search, and rankings computed asynchronously rather than per request.
💡 Elasticsearch is a search engine: it stores an inverted index, which maps each word to the documents containing it, so a keyword query is a lookup rather than a scan. Redis is an in-memory store used here purely as a cache. Learn more →


Understanding the Problem

A Q&A forum lets users post questions, write answers, and vote on both. Readers vastly outnumber writers — most traffic is people reading a question and its top answers, often arriving from a search engine. The hard parts: serving reads cheaply at scale (100:1 read-to-write ratio), ranking answers so the best one appears first, and providing full-text search across millions of questions.

The read-heavy skew is the single most important fact about this system. It means a cache is not an optimisation you add later — it is load-bearing from day one. It also means the write path can afford to be slower and do more work (recompute rankings, fan out to a search index) because writes are 1% of traffic.


Naive First Cut

flowchart LR
    USER["User"]
    API["Monolith API"]
    DB[("Single SQL DB")]

    USER --> API
    API --> DB

New components:

  • Monolith API: One service handling everything - posting, reading, voting and searching - in the user’s request.
  • Single SQL DB: Questions, answers, votes and the search all in one database. Nothing is cached, so every page view is reassembled from scratch.

Why this breaks:

  • Hot questions (viral content) get 100K reads/sec — single DB can’t handle the load
  • Full-text search with LIKE '%keyword%' does a full table scan, no relevance ranking
  • Computing “top answers” on every read (sort by votes) is expensive when there are 500 answers
  • Feed generation for 10M users requires scanning all followed topics per request
  • No caching — same popular question page is recomputed on every request

The rest of the doc evolves this into a design that survives a front-page traffic spike.


Prior Art We’re Drawing From

  • Stack Overflow — famously runs on a remarkably small server footprint: a handful of SQL Server boxes plus aggressive caching, because an immutable-ish question page is close to a perfect cache candidate. Proof that read-heavy Q&A is a caching problem before it is a sharding problem. (Stack Overflow architecture)
  • Quora — separates the write store from the read/serving path and computes ranking and feed material asynchronously, keeping page loads free of ranking work. (Quora Engineering)
  • Reddit — its comment ranking uses time-decayed and confidence-based scoring (Wilson score for “best”) rather than raw vote counts, which is exactly the ranking problem in Deep Dive 2. (Reddit ranking algorithms)
    💡 Time decay means an older vote counts for less than a recent one. The Wilson score adds confidence: 8 upvotes out of 10 is a weaker signal than 800 out of 1000, even though the ratio is identical, and raw counts cannot tell the two apart.

Functional Requirements

Core (top 3)

  1. Post and read Q&A — users post questions, others write answers, anyone can read
  2. Vote on answers — upvote/downvote; answers ranked by score
  3. Search questions — full-text search across all questions by keyword and tags

Below the Line

  • User profiles, comments on answers, tag system, personalized feed, notifications, moderation tools

Non-Functional Requirements

  • Read latency — <100ms for question page loads (including top answers)
  • Scale — 10M questions, 50M answers, 100:1 read-to-write ratio
  • Search — full-text results in <200ms with relevance ranking
  • Availability — 99.9%; read path must stay up even if write path degrades

Below the Line