← 2026-06-22 📂 All Days 2026-06-24 →

🏗️ 系统设计 / System Design — Day 60

设计一个 AI 对话平台 (ChatGPT 规模)

Design an AI Chatbot Platform (ChatGPT-scale)


🌐 真实场景 / Real-World Scenario

想象你在设计一个每天处理 1 亿次对话的 AI 聊天平台。

每个请求都要调用巨大的 LLM(比如 GPT-4o)、保持对话历史、支持流式输出(streaming),还要保证低延迟 + 高可用。

这不是普通的 CRUD 系统——推理的成本是 I/O 的 1000 倍。

Imagine building an AI chat platform handling 100M conversations per day.

Each request calls a massive LLM, maintains conversation history, supports streaming output, all while meeting low-latency and high-availability SLAs.

This is not a normal CRUD system — inference costs 1000x more than I/O.


📐 架构图 / Architecture Diagram

User (Browser/App) │ WebSocket / SSE ▼ ┌─────────────┐ │ API Gateway │ (rate limit, auth, routing) └──────┬──────┘ │ ┌──────▼──────────────────┐ │ Conversation Service │ (session mgmt, context assembly) └──────┬──────────────────┘ │ context + prompt ┌──────▼──────────────────┐ │ LLM Inference Router │ (load balance across model replicas) └──────┬──────────────────┘ │ GPU workers ┌──────▼──────────────────────────────────┐ │ Model Serving Cluster │ │ ┌────────────┐ ┌────────────┐ │ │ │ vLLM pod │ │ vLLM pod │ ... │ │ │ (A100) │ │ (H100) │ │ │ └────────────┘ └────────────┘ │ └─────────────────────────────────────────┘ │ tokens (streaming) ┌──────▼──────┐ ┌──────────────┐ │ Stream Relay │ │ Storage │ │ (SSE push) │ │ - Redis: ctx │ └─────────────┘ │ - S3: history│ │ - Postgres: │ │ user data │ └──────────────┘

⚖️ 核心权衡 / Key Tradeoffs

1. 为什么用 vLLM + Continuous Batching?

传统服务一次处理一个请求,GPU 大部分时间在等待。

vLLM 用 PagedAttention + continuous batching,把等待时间"填满",GPU 利用率从 30% 提升到 80%+。

vLLM with PagedAttention + continuous batching fills GPU idle time. Without it, one slow streaming response blocks the entire GPU.

2. 流式输出:SSE vs WebSocket

  • SSE (Server-Sent Events): 单向、HTTP/1.1 兼容、自动重连。适合 token 流。
  • WebSocket: 双向、更复杂。聊天场景用 SSE 更简单。

Use SSE for token streaming — simpler than WebSocket for unidirectional token flow.

3. 对话上下文管理

  • 短期上下文 → Redis (TTL 30 min)
  • 长期历史 → S3 (压缩存储)
  • 上下文窗口有限 → 超长历史需要 摘要压缩 (recursive summarization)

Context window is finite. Long conversations require summarization to fit within token limits.

4. 模型路由 / Model Routing

  • 简单问题 → 小模型 (GPT-4o-mini, Haiku) — 便宜 10x
  • 复杂推理 → 大模型 (GPT-4o, Sonnet)
  • 路由决策基于 intent classification(用小模型分类)

Route cheap questions to small models. 80% of traffic often qualifies.


❌ 别踩这个坑 / Common Mistakes

坑 1: 对每个 token 都写数据库

→ 把 token 流缓存在内存,对话结束后一次性写入。

坑 2: 不限制并发请求数

→ GPU 内存是有限的。没有背压控制,会导致 OOM crash。用队列 + 请求限速。

坑 3: 上下文放全部历史

→ 100 轮对话 = context 爆炸 = 推理慢且贵。必须截断或摘要。

坑 4: 没有 prompt 注入防护

→ 用户可以通过 prompt 绕过系统指令。需要 input/output guardrails。


📚 References

  • https://vllm.ai/ — vLLM project, PagedAttention paper
  • https://platform.openai.com/docs/guides/streaming — OpenAI streaming guide
  • https://www.youtube.com/watch?v=80bIUggRJf4 — Andrej Karpathy on LLM serving

🧒 ELI5

把 AI 聊天平台想成一家饭店:用户点菜(输入问题),厨师(GPU)做菜(推理),服务员一道一道上菜(流式输出)。

vLLM 就像让厨师同时帮多张桌子备菜,而不是只等一桌做完。

It's like a restaurant: customers order (input), chefs cook (GPU inference), waiters serve dish by dish (streaming tokens). vLLM lets chefs prep multiple tables at once instead of one at a time.

💻 算法 / Algorithms — Day 61

#208 Implement Trie (Medium) — 🟡 Medium


🧩 新模式 / New Pattern: 前缀树模式 (Trie Pattern)

📍 本模块共 3 道题 / This block: 3 problems

#208 Implement Trie ← TODAY

#211 Design Add and Search Words (Medium)

#212 Word Search II (Hard)

什么时候用 / When to use: 前缀搜索、自动补全、词典匹配

识别信号 / Signals: prefix search, autocomplete, word dictionary, "starts with", matching patterns across words

通用模版 / Template:

class TrieNode:
    def __init__(self):
        self.children = {}  # char -> TrieNode
        self.is_end = False  # marks end of a word

def insert(root, word):
    node = root
    for ch in word:
        if ch not in node.children:
            node.children[ch] = TrieNode()
        node = node.children[ch]
    node.is_end = True

核心洞察 / Key Insight: 共享前缀 → 节省空间 + O(L) 查询(L = 词长),不需要 O(N) 扫描所有词


🔗 Links

  • LeetCode: https://leetcode.com/problems/implement-trie-prefix-tree/
  • NeetCode: https://neetcode.io/problems/implement-prefix-tree

🌍 现实类比 / Real-World Analogy

想象一棵「词语树」:

  • 根节点是空的
  • 每一层是字符串的第 N 个字符
  • 所有以「app」开头的词共享同一条「a→p→p」的路径

这就是你手机键盘自动补全的底层结构!

Imagine a "word tree" where words sharing a prefix share the same path from the root. Your phone's autocomplete literally uses a trie underneath.


🐍 Python 解法 + 追踪 / Solution + Trace

class TrieNode:
    def __init__(self):
        self.children = {}  # maps char -> TrieNode
        self.is_end = False

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word: str) -> None:
        node = self.root
        for ch in word:
            if ch not in node.children:
                node.children[ch] = TrieNode()
            node = node.children[ch]
        node.is_end = True  # mark word end

    def search(self, word: str) -> bool:
        node = self.root
        for ch in word:
            if ch not in node.children:
                return False
            node = node.children[ch]
        return node.is_end  # must be full word

    def startsWith(self, prefix: str) -> bool:
        node = self.root
        for ch in prefix:
            if ch not in node.children:
                return False
            node = node.children[ch]
        return True  # prefix exists, don't need is_end

Trace for insert("app"), search("app"), startsWith("ap"):

insert("app"): root → 'a' → 'p' → 'p' (is_end=True) search("app"): root → 'a' ✓ → 'p' ✓ → 'p' ✓, is_end=True → True search("ap"): root → 'a' ✓ → 'p' ✓, is_end=False → False startsWith("ap"): root → 'a' ✓ → 'p' ✓ → return True (don't care about is_end)

复杂度 / Complexity:

  • Time: O(L) per operation (L = word length)
  • Space: O(N × L) where N = number of words

🔄 举一反三 / Pattern Connections

这只是 Trie 模块的第 1/3 题——打好基础:

  • #211 (下一题): 支持 . 通配符 → DFS 遍历所有子节点
  • #212 (终极题): 在 2D 棋盘上搜索单词列表 → Trie 剪枝优化暴力 DFS

关键变化点:

  1. 只是实现数据结构 → 下题是变形搜索 → 最后是组合应用
  2. 每次都用同一个 TrieNode 模板,只改搜索逻辑

📚 References

  • https://leetcode.com/problems/implement-trie-prefix-tree/
  • https://en.wikipedia.org/wiki/Trie
  • https://neetcode.io/courses/dsa-for-beginners/26

🧒 ELI5

Trie 就像一棵字母树:每层是一个字母,想找「apple」就沿着 a→p→p→l→e 走下去。

比每次扫描整个词典快多了!

A Trie is like a letter tree: each level is one character, so finding "apple" means walking a→p→p→l→e. Way faster than scanning the whole dictionary each time!

🗣️ 软技能 / Soft Skills — Day 60

Leadership — Staff Level

Q: Describe how you've contributed to hiring and growing a team.

描述你在招聘和团队成长方面的贡献。


为什么这个问题重要 / Why This Matters

Staff+ 工程师的影响力不只是代码——而是通过他人放大成果。

招聘 = 最高杠杆的投资。一个招聘决策的影响可以持续多年。

Staff+ engineers multiply impact through others. Hiring is the highest-leverage investment you make — one decision can shape a team for years.


STAR 拆解 / STAR Breakdown

Situation (背景):

"我们团队在快速扩张,从 5 人增长到 12 人,但没有标准化的面试流程。每次 Debrief 都是各说各的,录取决策不一致。"
"Our team was scaling from 5 to 12 engineers with no standardized interview process. Every debrief was ad hoc, leading to inconsistent hiring decisions."

Task (任务):

"我主动接手了重设计面试流程这件事,并且亲自参与每次系统设计面试,成为团队面试文化的负责人。"
"I took ownership of redesigning our interview process and became the go-to system design interviewer, owning our hiring bar."

Action (行动):

"我做了三件事:
1. 建立了评分矩阵(rubric),把「感觉不错」变成可量化的维度——技术深度、设计直觉、沟通清晰度
2. 与 Recruiting 合作,定义了 JD 和 level calibration 标准
3. 建立了 shadow/reverse-shadow 制度:新面试官先观摩两场再独立面试"
"Three actions: (1) Built a scoring rubric converting gut feel into measurable dimensions — technical depth, design intuition, communication clarity. (2) Partnered with Recruiting to define JD and level calibration. (3) Created shadow/reverse-shadow system: new interviewers observe two sessions before flying solo."

Result (结果):

"面试一致性显著提升——pass rate 方差降低了 40%。我们在 6 个月内成功 onboard 了 5 名工程师,其中 3 名已经独立负责项目。另外,我写的面试指南被公司其他团队复用。"
"Hiring consistency improved — pass-rate variance dropped 40%. We onboarded 5 engineers in 6 months; 3 are already owning projects. My interview guide was adopted by two other teams."

❌ 不好的回答 vs ✅ 好的回答

❌ Bad: "我参加了很多面试,面了很多人。"

Vague. No ownership, no impact.

✅ Good: 上面的 STAR 回答 — 有具体问题、有主动 ownership、有量化结果。

❌ Bad: "我会给候选人打分,然后参加 debrief。"

This is just describing the process, not your contribution.

✅ Good: 描述你改变了什么,而不是参与了什么。


👑 Senior/Staff Tips

  • Staff 面试官应该能 define the bar,不只是 execute it
  • 提到你如何帮助 calibrate others(帮同事理解好坏候选人的区别)
  • 如果你参与过 sourcing(主动挖人)、JD 设计、offer 谈判——这些都是加分项
  • 成长贡献不只是招聘:还包括 onboarding、mentoring、performance review

Staff engineers define the bar, don't just apply it. Mention calibrating peers, sourcing, and growing people post-hire.


🔑 Key Takeaways / 要点

  1. 招聘是杠杆 — 一个 hire 的影响比你一个人写代码大 10 倍
  2. 流程 > 直觉 — 用 rubric 减少偏见,增加一致性
  3. 言传身教 — shadow/reverse-shadow 是传递面试文化最好的方式
  4. 量化影响 — 能说出 "reduced variance by X%" 比泛泛而谈更有说服力

📚 References

  • https://www.lethain.com/staff-engineer/ — Will Larson on Staff Engineering
  • https://interviewing.io/guides/hiring-bar — How top companies set bars
  • https://betterprogramming.pub/how-to-run-a-structured-technical-interview-9acb19e3d877

🧒 ELI5

招聘就像选队友打游戏:你不只是要赢一把,你要建立一套「怎么选队友」的标准,让每次选人都更靠谱。

Hiring is like picking teammates for a game. You don't just want to win one round — you want to build a system for choosing well every time.

🤖 AI — Day 74

AGI 争论与新兴架构 / The Future — AGI Debates, Emerging Architectures


💡 AGI 到底是什么?/ What Even Is AGI?

AGI(通用人工智能)没有统一定义,不同人的理解天差地别:

定义 / Definition谁这么说 / Proponent
能做任何人类可以做的认知任务OpenAI 官方定义
能自主完成大多数经济任务OpenAI 内部操作定义
能自我改进的系统DeepMind 等
图灵测试通过经典定义(已过时)
AGI doesn't have a universally accepted definition. OpenAI's operational definition is "a system that can autonomously complete most economically valuable tasks." Others require self-improvement loops.

🏛️ 两大阵营 / Two Camps

阵营 1:Scaling Wins(缩放定律派)

核心论点: 更大的模型 + 更多数据 + 更多算力 → 涌现能力持续出现

代表: Sam Altman, Demis Hassabis

**证据:** GPT-2 → GPT-3 → GPT-4 的质变跨越

Bigger models keep surprising us with emergent capabilities. Just keep scaling.

阵营 2:Architecture Change Needed(架构变革派)

核心论点: Transformer 有根本缺陷(无记忆、推理弱),需要新架构

代表: Yann LeCun (世界模型论), Gary Marcus

证据: LLM 仍然会算错 9.11 > 9.9,hallucinate 事实

Transformers lack persistent memory and true reasoning. We need something fundamentally different.


🔭 新兴架构 / Emerging Architectures

1. Mamba / State Space Models (SSM)

  • 线性时间序列处理,不需要 Attention
  • 长序列效率远超 Transformer (O(n) vs O(n²))
  • 2024 年崛起,2025 年混合模型(Attention + SSM)开始流行

Linear-time sequence modeling without attention. Efficient for very long contexts.

2. Mixture of Experts (MoE) 演化

  • GPT-4 据报道是 8 个专家的 MoE
  • 每次 token 只激活部分专家 → 大模型,小推理成本
  • 趋势:更细粒度的 MoE,每层 1000+ 专家

Only activate a subset of experts per token — big model capacity, small inference cost.

3. Retrieval-Augmented Everything

  • 不只是 RAG for QA,而是主动记忆检索
  • 模型训练时学会「何时去查、查什么」
  • GraphRAG(微软):用知识图谱结构化检索,而非纯向量相似度

Not just retrieval at query time — models learning when and what to retrieve during reasoning.

4. Test-Time Compute (推理时计算)

  • OpenAI o3, DeepSeek R1 的核心:推理时多想几步
  • 不是训练更大模型,而是推理时生成更多「思考链」
  • 趋势:动态分配推理算力(简单问题快速回答,复杂问题深思熟虑)

Spending more compute at inference time beats just training bigger models for reasoning tasks.


🧮 可运行代码 / Runnable Python Snippet

探索现有 SSM 实现(Mamba 风格):

# pip install mamba-ssm  (requires CUDA)
# 或者用纯 Python 模拟 SSM 的核心思想

import numpy as np

def ssm_step(A, B, C, x, h):
    """
    Simplified State Space Model step.
    h: hidden state (memory)
    x: input token
    Returns: output y, new hidden state
    """
    h_new = A @ h + B * x    # update state
    y = C @ h_new             # project to output
    return y, h_new

# 模拟 3 个 tokens 的处理
A = np.array([[0.9, 0.1], [0.0, 0.8]])  # state transition
B = np.array([[1.0], [0.5]])             # input projection
C = np.array([[1.0, 0.0]])               # output projection

h = np.zeros((2, 1))  # initial hidden state
tokens = [1.0, 2.0, 3.0]

for i, x in enumerate(tokens):
    y, h = ssm_step(A, B, C, x, h)
    print(f"Token {i}: input={x:.1f}, output={y[0,0]:.3f}, state={h.flatten()}")

🤔 我们离 AGI 还有多远?/ How Far Are We?

乐观派(2-5年): OpenAI, Anthropic 内部时间线

谨慎派(10-20年): 大多数学术研究者

怀疑派(不确定): Yann LeCun — 认为当前路径根本上错误

没有人真正知道。但这个问题本身就说明:我们正处于 AI 历史上最有趣的时刻。

No one really knows. But the fact that serious people disagree so dramatically tells you we're at a genuinely inflection point.


📚 References

  • https://openai.com/research/planning-for-agi-and-beyond — OpenAI AGI roadmap
  • https://arxiv.org/abs/2312.00752 — Mamba paper (Gu & Dao, 2023)
  • https://yann.lecun.com/ex/pubs/index.html — LeCun's world model papers

🧒 ELI5

AGI 就是「什么都会的 AI」。现在的 AI 很擅长特定任务(下棋、写文章、画画),但遇到从没见过的新任务就会卡壳。科学家们在争论:是「继续加大模型」就能解决,还是需要从头发明新方法?

AGI is "AI that can do anything." Current AI excels at specific tasks but struggles with genuinely novel ones. Scientists debate whether we just need bigger models, or a fundamentally new approach.

Attention Heatmap

When the model processes it, it attends most strongly to animal.

Theanimaldidntcrossthestreetbecauseitwastootired
it →