SkillSprout
Chapter 5 · Lesson 3
Intermediate
4 min read

Techniques to Reduce Latency

Overview

Once you know where the time goes, you can attack it. There is a small toolbox of techniques, and each removes a different kind of delay. This lesson gives you the toolbox and the cost of each tool.

Detailed Notes

What is latency reduction

It is choosing the right fix for the right source of delay. The golden rule: the fastest operation is the one you never do. Before adding machines, ask whether you can avoid a call, answer from memory, send less data, or do the work later.

The problem it solves

Speed is a feature: slow pages convert worse and feel unreliable. But "make it faster" is vague. Matching a technique to a measured cause is what makes it work.

How it works

1. Caching. Keep a copy of data in a faster place so later requests skip the slow work. A request that finds the data is a hit; otherwise it is a miss and goes to the source. With a 90% hit rate, 1 ms cache and 50 ms source:

text
0.9 x 1 ms + 0.1 x 50 ms = 5.9 ms   (versus 50 ms with no cache)

Cost: cached copies can go stale when the source changes. Caching is covered in detail later.

2. Locality. Put data and computation near the user. A CDN (content delivery network) is a set of servers around the world serving files such as images from a nearby location; running the application in a region close to users works the same way. Both are covered later. Keeping related data on one machine also means a request touches fewer machines.

3. Asynchronous work. Reply to the user without waiting for slow, non-essential work, and finish it in the background (usually via a message queue, covered later). After checkout, answer "order received" at once and let a background worker send the email. Cost: results arrive later, and you must handle failures of background jobs.

4. Batching. Group many small operations into one to share fixed costs such as a round trip. Inserting 1,000 rows in one request beats 1,000 requests. Cost: early items wait for the batch to fill. A common rule: send when the batch is full or after a short time limit.

5. Parallelism. Run independent tasks at the same time. Three independent 100 ms calls take 300 ms one after another but about 100 ms together. Cost: more simultaneous load, and the total is now set by the slowest call (the tail again).

6. Reuse connections. Opening a new connection needs handshakes (see the network chapter), costing several round trips. Connection pooling and keep-alive reuse open connections instead.

7. Send fewer bytes. Compress text responses (gzip, Brotli), return only the fields needed, and paginate big lists. Cost: a little CPU; files already compressed (video, JPEG) gain little.

8. Efficient data access. Avoid the N+1 query problem: one query to fetch a list, then one more query per item in a loop, causing many round trips. Fetch it in one query instead. Choosing a better algorithm often beats any infrastructure fix.

A simple example

Running a pizza shop: caching is keeping popular pizzas pre-made under a warmer; locality is opening a branch near customers; async is handing out a buzzer so you can start the next order; batching is baking several pizzas per oven load; parallelism is having several ovens.

System design example

A product page takes 1.8 s. A trace shows 300 ms of network to the user's region, 600 ms for four sequential database queries, 400 ms calling a recommendation service and 300 ms sending a confirmation email before replying. The fixes: serve nearby through a CDN, run the four queries in parallel, cache the recommendations, and move the email to a background queue. The page drops to a few hundred milliseconds.

Always measure percentiles before and after each change, and watch for new costs: stale data, extra load and complexity.

Summary

  • Trace first, then match the technique to the cause.
  • Cache for repeated reads; CDN and regions for distance.
  • Async removes non-essential work from the user's wait.
  • Batch, parallelise, reuse connections and send fewer bytes.
  • Every technique has a cost: staleness, delay for early items, extra load or CPU.

Architecture Diagram

Latency-reduction pipeline
Architecture
Users
CDNlocality
App Serverparallel + batch
Cache
Database

Each layer removes a different source of delay.

  1. 1Static content is served from a CDN edge close to the user.
  2. 2Dynamic requests reach the app server, which checks the cache first.
  3. 3Independent calls run in parallel; small ones are batched.
  4. 4Only cache misses reach the database.

Common Interview Questions

Chapter quiz

Latency and Performance · 5 questions

  1. 1.Bandwidth is:

  2. 2.By Little's law, 500 RPS with 0.2 s latency gives about how many requests in flight?

  3. 3.A request calls 100 services; each is slow 1% of the time. Roughly how likely is at least one slow call?

  4. 4.Why is the average a poor latency summary?

  5. 5.Which technique moves slow, non-essential work out of the user's wait?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course