Engineering Evolution and Optimization of Tencent Docs Microservice Gateway

This article analyzes the existing issues of the Tencent Docs web‑gateway, explains why dependency versions and monorepo structure caused resource exhaustion, and details a series of engineering improvements using pnpm workspace, custom Docker contexts, lock‑file hooks, and soft‑link strategies to achieve a clean, reproducible build pipeline.

Architect
Architect
Architect
Engineering Evolution and Optimization of Tencent Docs Microservice Gateway

The web‑gateway of Tencent Docs serves as the traffic entry for the document front‑end and has evolved from a monolithic service to a microservice architecture, but the lack of lock‑file usage caused uncontrolled upgrades of indirect dependencies such as @grpc/grpc-js, leading to excessive CPU usage and service outages.

Initial analysis identified three core problems: (1) the gateway could not lock specific package versions, (2) indirect dependencies were upgraded unintentionally, and (3) the monorepo workspace generated soft/hard links that conflicted with Docker image construction.

To address these issues, the team adopted pnpm workspace for unified package management, which keeps all packages in a single repository while preserving clear boundaries. The workspace also provides hooks ( readPackage and afterAllResolved) to enforce exact versions of critical packages.

Key code snippets used in the solution:

<span>@svr/[email protected] /app</span></code>
<code><span>`-- @wgw/[email protected]</span></code>
<code><span>+-- @opentelemetry/[email protected]</span></code>
<code><span>`-- @grpc/[email protected]</span>

A custom Docker build process was introduced to copy only the necessary microservice files and its dependencies, avoiding the pollution caused by copying the entire repository. The Dockerfile now uses a multi‑stage build with a tailored context:

FROM base AS topdep</code>
<code>WORKDIR /</code>
<code>RUN mkdir node_modules packages</code>
<code>COPY node_modules/ node_modules</code>
<code>COPY packages/ packages</code>
<code>FROM topdep as svr</code>
<code>WORKDIR /usr/app</code>
<code>COPY ["pnpm-*.yaml", ".npmrc", "./"]</code>
<code>COPY "servers/${APP}/" .

The pnpm‑context.mjs script generates a minimal Docker context by selecting only the files required for a specific service:

async function main(cli) {</code>
<code>  const projectPath = dirname(cli.dockerFile);</code>
<code>  const [dependencyFiles, packageFiles, metaFiles] = await Promise.all([</code>
<code>    getFilesFromPnpmSelector(`{${projectPath}}^...`, cli.root, {extraPatterns: cli.extraPatterns}),</code>
<code>    getFilesFromPnpmSelector(`{${projectPath}}`, cli.root, {extraPatterns: cli.extraPatterns.concat([`!${cli.dockerFile}`])}),</code>
<code>    getMetafilesFromPnpmSelector(`{${projectPath}}...`, cli.root, {extraPatterns: cli.extraPatterns})</code>
<code>  ]);</code>
<code>  // copy files into a temporary directory and stream as tar</code>
<code>}

To keep the runtime environment consistent, the project disables the node-linker=hoist option and sets package-import-method=copy, ensuring that Docker images contain real files instead of symlinks.

Soft‑link and hard‑link concepts were leveraged to map files without changing their logical paths, reducing the verification effort after the restructuring.

The final outcome includes fully locked dependency versions, elimination of ghost dependencies, a clean Docker image per microservice, and a simplified, reproducible build pipeline that aligns development and production environments.

In conclusion, systematic engineering improvements—especially adopting pnpm workspace, custom Docker contexts, and lock‑file hooks—significantly reduce maintenance overhead and increase the stability of the microservice gateway.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MicroservicesBuild OptimizationMonorepopnpm
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

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.