pingcap / pingcap/tiproxy

Optimize CPU usage when creating short connections frequently

Open
#473 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Go
Stars
73
Forks
41
Avg merge
21h 3m
Merged PRs (30d)
21

Description

Development Task

When the client creates and destroys 300 connections per second, the CPU usage of TiProxy is up to 30%.

The main overhead is dialing the backend and forwarding messages during handshake. We may need a connection multiplex to optimize it.

However, we can do some minor optimizations without touching the architecture:

  • Reuse memory in bufio.NewReaderSize(), bufio.NewWriterSize(), and readOnePacket.
  • Save on-stack memory to avoid allocating a new stack in handshakeFirstTime.

The overall frame graph:
overall

Allocate duplicate memory:
malloc

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by profiling the short-connection path around handshakeFirstTime, readOnePacket, and the bufio.NewReaderSize()/NewWriterSize() calls, using the supplied CPU and allocation graphs as context. Measure the current allocation and CPU costs, then verify that memory reuse and reduced stack allocation improve frequent connection creation without changing handshake behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, networking, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.