Backend Development 14 min read

Refactoring and Optimizing Tencent News Backend Service: Reducing Technical Debt, Improving Efficiency, and Enhancing Stability

This article details the challenges of Tencent News' legacy backend page service, including high code debt, low development efficiency, and poor stability, and describes the systematic refactoring, configuration-driven redesign, performance optimization, and tooling (xhprof, Xdebug, expr) employed to improve maintainability, scalability, and reliability.

High Availability Architecture
High Availability Architecture
High Availability Architecture
Refactoring and Optimizing Tencent News Backend Service: Reducing Technical Debt, Improving Efficiency, and Enhancing Stability

The Tencent News core business scenario revolves around the "bottom‑level page" service, which handles over 35,000 QPS and more than 1 billion daily requests across five front‑ends (client, WeChat/QQ plug‑in, web, share page, and mini‑program). The page displays title, summary, author, content, likes, and other fields.

Problems identified:

Historical code debt: ~100k lines of PHP code with tangled business logic, unclear layering, and functions exceeding 2,000 lines, plus many legacy, unused sections dating back over ten years.

Low development efficiency: identical features required separate implementations for each front‑end, consuming up to five person‑days per change.

Poor service stability: the original PHP service suffered from low observability and high latency (P99.9 > 3.8 s), with stability hovering around 99% (two‑nine level).

Refactoring effort (Section 03): Approximately 100k lines of PHP were targeted for cleanup. The codebase exhibited cyclomatic complexity > 1,500, with average complexity 66.25 and a maximum of 1,234. Static analysis revealed 715 errors and over 25,000 warnings, indicating severe maintainability issues.

To accelerate the rewrite, the team employed:

xhprof to generate call‑graph diagrams, exposing function dependencies and execution times.

Xdebug combined with PHP CodeCoverage to capture precise execution paths for each request.

Traffic replay via the trpc‑gateway plugin to feed real request payloads into the instrumented service, producing comprehensive coverage reports.

Configuration‑driven redesign (Section 04): To unify development across scenarios, a configuration‑centric architecture was introduced. Core output fields (title, content, video ID, author, ad controls, etc.) are defined once and reused via a four‑layer configuration hierarchy:

Global configuration applied to all scenarios and article types.

Article‑type‑specific configuration for shared fields.

Scenario‑specific configuration handling differences between the five front‑ends.

Sub‑scenario configuration for fine‑grained variations (e.g., landing‑page specifics).

The open‑source expr library powers dynamic expressions. Example expressions include:

user.Age in 18..45 and user.Name not in ["admin", "root"]
foo matches "^[A-Z].*"
tweets | filter(.Size < 280) | map(.Content) | join(" -- ")
filter(posts, {now() - .CreatedAt >= 7 * duration("24h")})

Configuration snippets (wrapped in ... ) illustrate how fields are mapped:

{
  "mapper": "ExprEngineMapper",
  "dst_path": "x",
  "source_path": "pathC",
  "desc": "描述信息",
  "ext": "news(src_path) == 1 || news('pathA') == 1 || news('pathB') == 1 ? 1 : nil ",
  "data_source": "ResourceInfo"
}
{
  "mapper": "ExprEngineMapper",
  "dst_path": "x",
  "desc": "描述信息",
  "ext": "pre(news('pathA'), news('pathB'), '腾讯新闻')"
}
{
  "mapper": "ExprEngineMapper",
  "dst_path": "x",
  "desc": "下载链接",
  "ext": "'http://xxxx'"
}
{
  "filter": "req.Apptype == 'android' && req.Appver >= 7260"
}

Configuration management is integrated with the Rainbow "七彩石" system, enabling automated testing, CI validation, and safe rollout of configuration changes.

Performance and stability improvements (Section 05): Cache layers were added both on the client side and upstream services, reducing latency and improving hit rates (up to 95% for hot articles). These caches yielded roughly a 10% performance gain.

Service design considerations (Section 06‑07): The bottom‑level page aggregates data from multiple upstream sources in configurable batches. Future directions include removing batch constraints, fully dynamic data loading, and richer hook/filter mechanisms to support diverse article types (e.g., events, links, micro‑blogs).

In conclusion, the systematic refactor, configuration‑first approach, and tooling investments have transformed a fragile PHP monolith into a maintainable, performant, and extensible backend service that can reliably support Tencent News' massive traffic.

backendPerformance Optimizationconfiguration managementphprefactoringservice stability
High Availability Architecture
Written by

High Availability Architecture

Official account for High Availability Architecture.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.