網易跟帖以「無限巢狀回覆、引用路徑完整、高併發支撐」著稱。本文從資料庫設計、資料組裝邏輯、前端組件樹、到分庫分表、快取、靜態化等工程化優化,完整拆解其核心實現原理。
CREATE TABLE `pre_comments` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`article_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '文章ID',
`quote_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '直接引用的評論ID',
`quote_path` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '引用路徑,如 100,105,110',
`user_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
`username` CHAR(15) NOT NULL DEFAULT '',
`content` VARCHAR(1024) NOT NULL,
`up` BIGINT NOT NULL DEFAULT 0,
`down` BIGINT NOT NULL DEFAULT 0,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
`deleted_at` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_article_id` (`article_id`),
KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
根評論ID,中間評論ID,...,父評論ID。100,105,110 表示 當前評論 → 引用 110 → 引用 105 → 根評論 100。// 1. 取最新 10 條主評論(根節點)
$roots = $db->query("SELECT * FROM pre_comments
WHERE article_id=? AND quote_id=0
ORDER BY created_at DESC LIMIT 10");
// 2. 收集所有 quote_path 涉及的評論 ID
$quoteIds = [];
foreach ($roots as $r) {
if ($r['quote_path']) {
$quoteIds = array_merge($quoteIds, explode(',', $r['quote_path']));
}
}
$quoteIds = array_unique($quoteIds);
// 3. 一次性撈取所有被引用評論
$quotes = [];
if ($quoteIds) {
$placeholders = implode(',', array_fill(0, count($quoteIds), '?'));
$quotes = $db->query("SELECT * FROM pre_comments WHERE id IN ($placeholders)", $quoteIds);
$quotes = array_column($quotes, null, 'id'); // 以 id 為鍵建立映射
}
// 4. 記憶體中遞歸組裝樹狀結構
function buildTree($roots, $quotes) {
foreach ($roots as &$root) {
$root['children'] = buildChildren($root, $quotes);
}
return $roots;
}
function buildChildren($comment, $quotes) {
// 這裡假設前端只需展示「引用鏈」,非完整子樹
// 實際可根據 quote_path 從 $quotes 取出鏈上每個節點
$pathIds = $comment['quote_path'] ? explode(',', $comment['quote_path']) : [];
$chain = [];
foreach ($pathIds as $pid) {
if (isset($quotes[$pid])) {
$chain[] = $quotes[$pid];
}
}
return $chain;
}
<CommentBox>
<CommentList>
<CommentItem>
<CommentQuote> {/* 引用鏈:從根到父 */}
<CommentQuote>...</CommentQuote>
<CommentToolBar>
<CommentForm /> {/* 對該引用回覆 */}
</CommentToolBar>
</CommentQuote>
<CommentToolBar>
<CommentForm /> {/* 對主評論回覆 */}
</CommentToolBar>
</CommentItem>
</CommentList>
<CommentForm /> {/* 新增主評論 */}
</CommentBox>
| 組件 | 職責 |
|---|---|
| CommentBox | 頂層容器,管理整體狀態(列表、分頁、發表框) |
| CommentList | 渲染 CommentItem 列表,處理無限滾動/分頁 |
| CommentItem | 單條主評論:內容、作者、時間、贊/踩、工具列 |
| CommentQuote | 遞歸渲染引用鏈,每層包含被引用內容 + 回覆表單 |
| CommentToolBar | 操作列:回覆、贊、踩、舉報、收藏 |
| CommentForm | 輸入框、@用戶、表情、圖片、提交驗證 |
查詢場景決定路由 Key:
comment:{id}:stats,HINCRBY 原子增減。// 熱門文章評論首頁快取(Redis String,TTL 5-10 分鐘)
$cacheKey = "comment:list:article:{$articleId}:page:1";
$cached = $redis->get($cacheKey);
if ($cached) return json_decode($cached, true);
// 組裝後存入
$redis->setex($cacheKey, 300, json_encode($result));
DEL $cacheKey 或標記版本號。@userid:username 存入內容或單獨 mentions 表。quote_id 傳目標評論 ID,quote_path = 目標.quote_path + ',' + 目標.id。網易式評論系統的核心在於:**quote_path 冗余設計消除遞歸查詢**、**兩階段查詢+記憶體組裝降低 DB 壓力**、**組件化前端支撐無限巢狀**、**分庫分表+多級快取+異步架構支撐高併發**。這套架構已驗證支撐億級評論量,是高併發業務系統的經典參考範本。