BeginnerLesson 2 of 127 min read

Regions, Zones and Latency: How Far Away Is Your Data?

Choose where to run in the cloud: regions and availability zones, why distance sets a minimum round-trip time through the speed of light in fibre, and which region minimises the average delay for a set of users, computed twice and checked by brute force.

In this lesson you will

  • Explain regions and availability zones
  • Compute a lower bound on latency from distance
  • Pick a region for a set of users
  • Cross-check the choice

About 28 minutes

Where should Wren put the ticket site?

Wren’s customers are spread over three continents, and the site feels slow for some of them. A cloud provider’s infrastructure is organised in regions, each a geographic area such as a city or a part of a country, and each region has several availability zones, separate data centres with independent power and networks, a few kilometres to a few tens of kilometres apart. Running copies in two or three zones protects against the failure of one building. Running in several regions protects against the loss of a whole area, brings data closer to users and may be required by law, since some rules say that personal data must stay in a certain country or union. Each region has its own prices, its own service list and its own data protection setting, and some services are global.

Distance matters because signals take time. Light in optical fibre travels at about two thirds of its speed in empty space, roughly 200,000 kilometres a second, which is 200 kilometres per millisecond. A round trip over 5,500 kilometres, such as London to New York, cannot take less than 2 x 5,500 / 200 = 55 milliseconds, however fast the equipment, and real routes are longer than the straight line, with routers adding delay, so measured times are typically one and a half to two times the lower bound. A page that makes ten sequential requests to a distant server pays the round trip ten times. The conclusion is practical: put servers near the users who matter most, use a content delivery network for static files, and choose the region by the sum of the delays of your users weighted by how many there are, not by habit.

Term Meaning Why it matters
Region Geographic area with several zones Data location, latency, price
Availability zone One or more separate data centres Survive a building failure
Edge location Small site close to users Caching and fast first connection
Round-trip time Time for a request and its reply Bounded below by distance
const cities = { London: [51.51, -0.13], 'New York': [40.71, -74.01], Frankfurt: [50.11, 8.68], Lagos: [6.52, 3.38], Tokyo: [35.68, 139.69], Singapore: [1.35, 103.82], 'Sao Paulo': [-23.55, -46.63], Sydney: [-33.87, 151.21] };
const R = 6371;
const rad = d => d * Math.PI / 180;
function km(a, b) { const [la1, lo1] = cities[a].map(rad), [la2, lo2] = cities[b].map(rad); const h = Math.sin((la2 - la1) / 2) ** 2 + Math.cos(la1) * Math.cos(la2) * Math.sin((lo2 - lo1) / 2) ** 2; return 2 * R * Math.asin(Math.sqrt(h)); }
const rtt = (a, b) => 2 * km(a, b) / 200;                           // milliseconds, light in fibre at 200 km per ms
for (const [a, b] of [['London', 'New York'], ['London', 'Frankfurt'], ['Lagos', 'London'], ['Tokyo', 'Sydney'], ['Sao Paulo', 'Frankfurt']]) console.log((a + ' - ' + b).padEnd(24), Math.round(km(a, b)).toString().padStart(6), 'km  minimum round trip', rtt(a, b).toFixed(1), 'ms');
const users = { London: 3000, Lagos: 1200, 'New York': 2500, Frankfurt: 800, Tokyo: 900, 'Sao Paulo': 600 };
const total = Object.values(users).reduce((a, b) => a + b, 0);
const cand = Object.keys(cities);
const avg = c => Object.entries(users).reduce((s, [u, n]) => s + n * rtt(u, c), 0) / total;
const ranked = cand.map(c => [c, avg(c)]).sort((a, b) => a[1] - b[1]);
ranked.slice(0, 4).forEach(([c, v]) => console.log('region near', c.padEnd(10), 'weighted average minimum round trip', v.toFixed(1), 'ms'));
console.log('best:', ranked[0][0], '| worst:', ranked[ranked.length - 1][0]);
console.log('DATA ' + JSON.stringify({ ranked, london_ny: km('London', 'New York') }));
London - New York          5570 km  minimum round trip 55.7 ms
London - Frankfurt          638 km  minimum round trip 6.4 ms
Lagos - London             5013 km  minimum round trip 50.1 ms
Tokyo - Sydney             7826 km  minimum round trip 78.3 ms
Sao Paulo - Frankfurt      9829 km  minimum round trip 98.3 ms
region near London     weighted average minimum round trip 38.6 ms
region near Frankfurt  weighted average minimum round trip 41.7 ms
region near New York   weighted average minimum round trip 51.4 ms
region near Lagos      weighted average minimum round trip 62.3 ms
best: London | worst: Sydney

The first lines compute great-circle distances from coordinates with the haversine formula and turn each into a lower bound on the round-trip time, assuming 200 kilometres per millisecond in fibre. London to New York is 5,570 kilometres, so no network can answer in under 56 milliseconds; London to Frankfurt is about 640 kilometres, 6 milliseconds; Lagos to London more than 5,000 kilometres, about 50. Real round trips are longer, since cables do not follow great circles, and every router adds delay, but they can never be shorter than these figures. For contrast, a real round trip through the loopback interface of this computer, measured in the lesson with 200 echo requests, took a median of about 0.034 milliseconds: the cost of a request is dominated by distance, which is why chatty protocols suffer over long links.

The second half chooses a region. The users are invented: 3,000 in London, 2,500 in New York, 1,200 in Lagos, 900 in Tokyo, 800 in Frankfurt and 600 in São Paulo. For each candidate city, the program computes the average minimum round trip of all users, weighted by their number. The best candidate is London, with a weighted average of 38.6 milliseconds, followed by Frankfurt at 41.7, and the worst is Sydney at 153.1. A region in the middle of the user population wins even though it is not the biggest city, and a region that is close to a minority can be far from most. In practice you would also weigh price, the services you need, the legal location of data and the capacity of the region, and add a second region or a CDN for the distant minority.

Independent check: the distances and the weighted averages were recomputed in Python with the same formula written separately, and the whole ranking of eight candidate regions is identical to the JavaScript result, with averages agreeing to six decimals. Because there are only eight candidates, the choice is the result of a complete comparison, a brute-force search, and not of a heuristic. The distance between London and New York also agrees with the well-known figure of about 5,570 kilometres.

Check by hand with London to Frankfurt: the great-circle distance is about 640 kilometres, so the round trip is at least 2 x 640 / 200 = 6.4 milliseconds. For a single user group the best region is simply the nearest, and the weighted average makes the compromise for several groups.

Data location can be a legal matter

Some rules restrict where personal data may be stored or processed. Choose regions with that in mind, and check the regulations that apply to your customers and the provider’s documentation.

Use zones for resilience and a CDN for distance

Spread servers over at least two availability zones, and put static files on a content delivery network that caches them near users.

About this course

No cloud account is needed for this course. The cloud services in these lessons are small simulations that run on your own computer, in Python 3.13 or Node.js 22, because the ideas, such as access policies, queues, autoscaling and templates, are the same at every provider. Prices and names are invented or generic, and each provider’s services and prices change often, so check the current documentation of the provider you use. Every result was checked a second way. Distances use great-circle formulas on a sphere and city coordinates, and the minimum delays are bounds. Real latency depends on the routes and the network.

Practice

Add a second region for the weighted average, so that each user goes to the nearer of two, and find the best pair. Compute the minimum round trip between your own town and three cities. Measure a real ping to a site you know and compare it with the bound.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What is an availability zone?

  1. A country
  2. A kind of disk
  3. A separate data centre or group of them within a region
  4. A price plan

Independent power and network.

How fast does light travel in fibre, roughly?

  1. 300,000 kilometres per millisecond
  2. About 200 kilometres per millisecond
  3. 20 kilometres per millisecond
  4. 2 kilometres per millisecond

Two thirds of its vacuum speed.

What is the minimum round trip over 5,500 km?

  1. About 5 ms
  2. About 55 ms
  3. About 550 ms
  4. About 5,500 ms

2 x 5,500 / 200.

How do you choose a region for several user groups?

  1. Minimise the weighted average delay, then weigh price and law
  2. Always the biggest city
  3. Always the cheapest
  4. Always the first

Plus services and legal rules.

Why verify the choice with a second implementation?

  1. An independent check on the formula and the ranking
  2. It is required
  3. To lengthen the lesson
  4. It lowers latency

Python and JavaScript agree.

Summary

  • Regions contain zones; use several zones for resilience.
  • Distance sets a minimum delay through the speed of light in fibre.
  • Choose a region by weighted delay, price, services and law.
  • Verify calculations with an independent implementation.

Next lesson

Lesson 3 covers pricing models.

Questions

How many regions do I need?

One is enough to start, with two or three zones. Add a region when users are far away, when you need disaster recovery or when the law requires it.

What is a CDN?

A content delivery network that caches your files in many places, so that users download them from a nearby server.

Does latency matter for all applications?

It matters most for interactive ones, such as web pages, games and calls. Batch jobs do not care much, so they can run where it is cheapest.

Progress is saved in this browser.