04.5.7 · Skill in Performance engineering

Backend performance

Fast and efficient servers, queries and services.

Backend performance comes from good queries and indexes, fewer round trips, efficient algorithms and doing work asynchronously. Most problems are a few slow queries. Fixing them often cuts infrastructure costs too.

Your status · Backend performance

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Most backend slowness comes from a few places: a query without the right index, a loop that calls the database once per item, a new connection for every request, or work that could happen later. Fixing them makes the product faster and the servers cheaper at the same time. A product engineer who knows the code and the data can find these places quickly.

How to do it

  1. Find the requests with the most total time, from logs or traces. Total time is how often a request runs multiplied by how long it takes.
  2. For each one, count the round trips: database queries, calls to other services, new connections. Remove what you can by batching, joining or caching.
  3. Read the query plan of every slow query, and add or fix the index.
  4. Reuse connections from a pool. A new TCP and TLS connection costs several round trips before any work starts.
  5. Move work the person does not wait for, such as emails and reports, to a background job.
  6. Measure p95 latency and CPU before and after, under real traffic.

Common mistakes

  • N+1 queries: one query for a list, then one more query for each item in it.
  • A cache that hides a slow query instead of a fix for it.
  • Bigger servers before a profile.
  • Measuring with an empty development database.

Example

Cloudflare’s old proxy was NGINX. Each NGINX worker process kept its own pool of connections to customers’ servers, so adding workers made connection reuse worse, and more requests paid for a new TCP and TLS handshake. Cloudflare built Pingora in Rust, and its threads share one set of connections. For one large customer, connection reuse went from 87.1% to 99.92%: 160 times fewer new connections to its servers.

Numbers

Cloudflare's proxy after the move from NGINX to Pingora

80ms
Less time to first byte at p95
5 ms less at the median
1/3
As many new connections per second
70%
Less CPU for the same traffic
67% less memory

Fewer new connections meant fewer handshakes. The slow tail improved most, and the servers did less work for the same traffic.

ReportedYuchen Wu and Andrew Hauck, Cloudflare, 14 Sep 2022

Sources: How we built Pingora, the proxy that connects Cloudflare to the Internet (Cloudflare) ↗ · Use the index, Luke (Markus Winand) ↗

Resources

2
  • bookGo deeper
    Use the index, Luke ↗
    Markus Winand · free online book

    SQL indexing explained for developers, across the major databases. It prevents most slow queries.

  • articleGo deeper
    Flame graphs ↗
    Brendan Gregg · 20 min

    From the person who invented them: how to read a flame graph and find where CPU time really goes.

Tools

2
Tools for Performance engineering