← 回到 Reading
ByteByteGo 2026-11-03

How Netflix Uses AI Agents for Performance Engineering

Netflix 傳統的 performance engineering 高度依賴反覆且大部分為手動的 profiling 流程。開發者在 production server 上觸發 profiler、在本地分析複雜的 flame graph、將發現對應回 application code,並在重新部署前測試 patch。這種耗費人力的 workflow 無法 scale,因為它依賴具備底層 execution stack 深入知識的稀缺領域專家。因此,團隊只能優先處理 major incident 或明顯的 slowdown,導致較小、漸進式的 resource drain 未被偵測到。AI coding assistant 的廣泛採用導致軟體 performance 問題增加,主要由「speed trap」和「context gap」所驅動。雖然開發者生成程式碼的速度可提高達 10 倍,但這種加速往往會導致 code bloat、regression 以及長期的架構維護問題。此外,雖然 large language models (LLM) 擅長處理局部邏輯和語法,但它們缺乏對 runtime 環境、call stack 和 peak-load 資源限制的關鍵 visibility。因此,孤立來看看似正確的程式碼,在規模化執行時可能導致嚴重的資源低效。Netflix 進行了一項實驗,評估 large language models (LLM) 是否可以直接解析並解讀跨多種程式語言 runtime 的 raw profiling data。Java、Python 和 Go 等語言的 profiler 共享核心 paradigm,即將執行過程拆解為 call stack 並追蹤 inclusive time 與 self (CPU) time 等 metric。因為 LLM 已經攝取了大量的軟體 repository,它們對標準 anti-pattern(如 quadratic loop 和過度的 allocation)擁有天生的知識。因此,Netflix 得出結論:統一的分析 workflow 可以利用 LLM 將 call stack 與已知的 performance 問題進行 pattern-match。

閱讀原文 ↗
目錄 9 段
  1. 01傳統 Performance 工作的 Bottleneck
  2. 02AI Code 的問題
  3. 03LLM 能否處理 Profile Data
  4. 04Agentic Profiling Pipeline
  5. 05Netflix 在 Production 環境中發現的問題
  6. 06安全、驗證與信任架構
  7. 07Shifting Left
  8. 08Netflix 如何及早發現問題
  9. 09結論

傳統 Performance 工作的 Bottleneck

Netflix 傳統的 performance engineering 高度依賴反覆且大部分為手動的 profiling 流程。開發者在 production server 上觸發 profiler、在本地分析複雜的 flame graph、將發現對應回 application code,並在重新部署前測試 patch。這種耗費人力的 workflow 無法 scale,因為它依賴具備底層 execution stack 深入知識的稀缺領域專家。因此,團隊只能優先處理 major incident 或明顯的 slowdown,導致較小、漸進式的 resource drain 未被偵測到。

  • Netflix 傳統的 performance engineering 主要是手動且反覆的 loop。
  • 該流程需要開發者在線上的 production server 上觸發 profiler、下載 trace data,並手動分析 flame graph 以找出高 CPU 或 memory 消耗。
  • 開發者必須手動檢查 codebase 邏輯中晦澀難懂的 method 名稱、定位低效的 operation,並提出 patch。
  • 傳統的 workflow 無法 scale,因為它需要具備深厚底層 execution stack 知識的稀缺領域專家。
  • 由於人力注意力有限,調查僅限於 major incident 或明顯的 slowdown,導致較小且持續的 resource leak 被忽略。

AI Code 的問題

AI coding assistant 的廣泛採用導致軟體 performance 問題增加,主要由「speed trap」和「context gap」所驅動。雖然開發者生成程式碼的速度可提高達 10 倍,但這種加速往往會導致 code bloat、regression 以及長期的架構維護問題。此外,雖然 large language models (LLM) 擅長處理局部邏輯和語法,但它們缺乏對 runtime 環境、call stack 和 peak-load 資源限制的關鍵 visibility。因此,孤立來看看似正確的程式碼,在規模化執行時可能導致嚴重的資源低效。

  • AI coding assistant 主要透過「speed trap」和「context gap」引發軟體 performance 問題。
  • 開發者使用 AI 工具可以將程式碼生成與 ship 的速度提升達 10 倍,但快速生成並不保證 runtime performance。
  • 快速的 AI 輔助程式碼生成經常會引入 technical debt,例如 debugging regression 和 code bloat。
  • LLM 生成的程式碼在語法上正確且能通過 unit test,但缺乏對 host runtime 環境和 critical execution path 的感知。
  • 孤立環境下由 AI 生成的程式碼,在 platform scale 運行時可能會嚴重浪費系統資源。

LLM 能否處理 Profile Data

Netflix 進行了一項實驗,評估 large language models (LLM) 是否可以直接解析並解讀跨多種程式語言 runtime 的 raw profiling data。Java、Python 和 Go 等語言的 profiler 共享核心 paradigm,即將執行過程拆解為 call stack 並追蹤 inclusive time 與 self (CPU) time 等 metric。因為 LLM 已經攝取了大量的軟體 repository,它們對標準 anti-pattern(如 quadratic loop 和過度的 allocation)擁有天生的知識。因此,Netflix 得出結論:統一的分析 workflow 可以利用 LLM 將 call stack 與已知的 performance 問題進行 pattern-match。

  • Netflix 評估了 LLMs 是否能自動化分析原始的 runtime profiling 資料。
  • 不同語言的 profilers(例如 JVM 的 async-profiler、Python 的 py-spy 與 Go 的 pprof)擁有相似的核心結構,都是將執行過程拆解為包含 inclusive 與 self CPU time 指標的 call stacks。
  • 得益於這些共通的 profiler 基礎,單一分析 pipeline 理論上可以處理來自幾乎任何程式語言 runtime 的 profiling 資料。
  • Pretrained LLMs 已經具備理解經典程式設計 anti-patterns 的能力,包含 loop invariants、quadratic complexity,以及 hot paths 上的過度記憶體分配。
  • 工程工作流程可以直接 prompt LLMs,將已知的效能 bugs 與觀測到的 call stacks 進行 pattern-match,而不用從零開始瞎猜效能瓶頸。

Agentic Profiling Pipeline

Netflix 開發了一套自動化效能 profiling 系統,將大型語言模型整合至既定的 pipeline 中。在實作過程中,工程師發現直接提供未受限制的原始 stack traces 會導致模型浪費 reasoning tokens 在低階常式(routines)上,例如 JVM garbage collection pauses、Linux kernel syscalls 以及第三方 library overhead。由於應用程式開發者無法控制這些 platform 層級的操作,系統強制執行嚴格的 package prefix 規則。這種限制範圍的策略讓 agent 能夠過濾掉背景 platform noise,專注在可採取行動的 application code 上。

  • Netflix 實作了一套封裝了 LLM 的自動化效能 profiling pipeline。
  • 提供原始、未設限的 stack traces 會導致 LLMs 浪費 reasoning tokens 去分析低階的 platform noise。
  • 未經篩選的 traces 會因 JVM garbage collection pauses、Linux kernel syscalls 以及第三方 framework overhead 而分散模型的注意力。
  • 開發者通常無法修改底層 library internals 或作業系統常式(routines)。
  • 套用嚴格的 package prefix 規則可引導 agent 僅專注在使用者 application code 上。

Netflix 在 Production 環境中發現的問題

當 Netflix 將自動化 profiling agent 部署到其 production 服務時,它辨識出了四大類的效能 bugs。這些瓶頸包括 Java Streams reduction 中的二次方演算法複雜度(quadratic algorithmic complexity)、hot paths 上的過度正規表示式(regular expression)評估,以及導致 out-of-memory 風險的冗餘物件分配。此外,agent 還發現了 metric registry lookup 誤用問題,該問題因 synchronization locks 消耗了高達 66% 的 CPU 時間。在所有案例中,agent 透過 single-pass collections、lookup tables 和 cached references 等方式對程式碼進行了最佳化。

  • 在 Java Stream reduction 中重複複製 ImmutableMap 導致了 O(N^2) 複雜度,並消耗了 8.8% 的 CPU 資源。
  • 在高流量下執行動態 regex 字串清理(sanitization)消耗了服務 46% 的 CPU;改用預先計算的 lookup table 則快了 22 倍。
  • 在每個連入請求(incoming request)中建立新的 HashSet 佔了總 heap collections 的 47%,並促成了 out-of-memory crashes。
  • 重複從 registry 查找 metric counters 產生了 dictionary lookups 與 synchronization locks,消耗了 66% 的 CPU,而實際的 business logic 僅佔 0.3%。
  • 在 class 初始化期間快取 metric counter reference,解決了三個獨立服務中的 telemetry overhead。

安全、驗證與信任架構

將 AI agents 產生的程式碼最佳化直接部署至 production 會帶來 business logic regression 的嚴重風險。為了緩解此問題,agentic pipeline 納入了四階段標準軟體開發生命週期(SDLC)安全架構。該流程始於透過 unit、integration 和 contract testing 進行自動化邏輯驗證,接著進行並行(side-by-side)的 production canary deployment。嚴格收集 latency 與 CPU utilization 等 telemetry 指標且不由 AI 評估,最後由人類開發者透過 pull request 做出最終的 merge 決策。

  • AI 驅動的程式碼最佳化需要嚴格的自動化驗證,以防止破壞核心 business logic。
  • Stage 1 要求 agent 的 branch 在繼續進行之前,必須通過所有既有的 unit、integration 與 contract tests。
  • Stage 2 使用 production canary deployment,在接收部分流量的同時,與運行原始程式碼的相同 baseline 並行運作。
  • Stage 3 收集客觀的 telemetry 指標(CPU utilization、median/P99 latency、memory allocation),且不使用 AI 模型來評估 canary 結果。
  • Stage 4 需要由人類開發者審查包含 code、原始 flame graph 與 canary report 的 pull request,以做出最終的 deployment 決策。

Shifting Left

僅依賴 automated production profiling 會帶來浪費 compute costs、開發者對修改運行中服務猶豫不決,以及跨團隊重複 debugging 等問題。為了解決這個問題,Netflix 將 performance checks 左移至更早的開發階段,並在其 agents 間實作了 shared memory 機制。由於 LLM 本質上是 stateless 的,Netflix 使用包含純 Markdown 檔案的簡單 Git repository 建立了 Pattern Catalog,以維持組織層級的 persistence。這個 catalog 作為集中式、可供人類審查的 source of truth,能輕鬆 inject 到多種環境中的 LLM prompts,而無需複雜的 vector databases。

  • 僅在 production 環境中偵測 performance anti-patterns 會導致浪費 compute costs,並使開發者不願修改穩定運行中的服務。
  • Netflix 將 performance profiling 左移至早期開發週期,以防止跨不同團隊出現重複的問題。
  • 由於 LLM 是 stateless 的,Netflix 將它們與 stateful 的 Pattern Catalog 搭配使用,以提供跨服務的 shared memory。
  • Pattern Catalog 的設計目標為集中化、跨環境相容(CI/CD、local、offline)、可輕鬆 inject 到 system prompts 中,且可供人類審查。
  • 受 Andrej Karpathy 的「LLM-wiki」概念啟發,Netflix 並未採用複雜的 vector databases,而是透過包含 Markdown 檔案的標準 Git repository 來實作 Pattern Catalog。

Netflix 如何及早發現問題

Netflix 透過由集中式 Pattern Catalog 驅動的兩個互補 workflow 來管理 performance engineering:reactive path 與 proactive path。在 reactive path 中,automated agents 會對 production 服務進行 profiling、在 canary instances 上測試候選修正方案,並使用新的 bottleneck patterns 更新 catalog。在 proactive path 中,CI 工具利用 regex 與 symbol matching 對照 catalog 掃描 pull requests,以提早識別 anti-patterns。當比對出問題時,LLM 會評估 pull request diff 並留下 inline comments,在 merge 之前解釋低效之處、計算對資源的影響並提出修正方案。

  • Netflix 將 performance engineering 劃分為 reactive profiling pipeline 與 proactive pre-merge detection pipeline。
  • 這兩個 performance workflows 都依賴一個儲存已知問題的共用 repository,稱為 Pattern Catalog。
  • reactive pipeline 會先在 canary instances 上測試自動修正方案,再以驗證過的發現更新 Pattern Catalog。
  • proactive pipeline 在 CI 中透過快速的 regex 或 symbol indexing,對照已編入 catalog 的 anti-patterns 檢查 pull requests。
  • 當標記出 anti-patterns 時,LLM 會分析 PR diff,針對低效能、資源成本與 remediation 提供 inline feedback。

結論

正如 Netflix 的工程實踐所示,自動化 performance engineering 並非必須全有或全無(all-or-nothing)。組織可以在四個等級的成熟度光譜中循序漸進,從純手動 performance analysis 到完全自主的 AI agents。Netflix 目前運作於 Level 2,採用 orchestration、proactive scanning 以及 Pattern Catalog。他們的長期願景是 Level 3,讓 autonomous agents 能獨立制定最佳化策略並執行 automated benchmarks。

  • Performance engineering 自動化遵循四個等級的成熟度光譜,而非全有或全無的方法。
  • Level 0 完全依賴開發者手動擷取 traces 並解讀 flame graphs,不涉及 machine learning。
  • Level 1 使用 LLM 作為分析用 copilot,從開發者提供的 stack dumps 中識別 hot execution paths。
  • Level 2 代表 Netflix 目前的現況,涵蓋 orchestration、proactive scanning 以及 Pattern Catalog。
  • Level 3 是 Netflix 的未來目標,旨在打造能制定最佳化策略、探索替代實作並執行 benchmarks 的 autonomous agents。