Lesson 18 / 32

Compute Near Users or Near Data

Estimate when an edge function will be slower than a regional one and split logic between edge and region accordingly.

By · 15 years in web development

Count the round trips

Moving code to the edge only helps if the code does not have to travel back to a distant origin for every step. Suppose a student in London is served by an edge function in a London PoP, but your Postgres database lives in a Mumbai region and the function makes four sequential queries. Each query is a round trip between London and Mumbai, so the user waits for four long trips instead of one. Running the whole function in Mumbai, next to the database, costs the user a single long trip plus four very short ones. Rules of thumb: put stateless or cache-served logic at the edge (auth checks, redirects, personalisation from a token, cached reads); keep chatty or write-heavy logic next to the primary database; and when you must cross the distance, batch queries into one round trip. Some platforms can automatically place a function near the backend it talks to, precisely because of this effect.

A back-of-the-envelope latency model

The round-trip times are assumptions, but the shape of the result holds whenever queries are sequential and the database is far from the edge.

# Rough latency model in ms. The round-trip times are illustrative assumptions.
RTT_USER_TO_EDGE = 10      # user to a nearby PoP
RTT_EDGE_TO_DB = 150       # that PoP to a database on another continent
RTT_USER_TO_REGION = 160   # user straight to the database's region
RTT_IN_REGION = 1          # function to database inside one region
QUERIES = 4                # sequential queries per request

edge_first = RTT_USER_TO_EDGE + QUERIES * RTT_EDGE_TO_DB
region_first = RTT_USER_TO_REGION + QUERIES * RTT_IN_REGION

print(f"function at the edge  : {edge_first} ms")
print(f"function in the region: {region_first} ms")

Output:

function at the edge  : 610 ms
function in the region: 164 ms

A common split that works

Serve the page shell and cached reads from the edge, verify the session token there, and forward writes and complex queries to a regional API next to the database. Users get fast first bytes, and the database sees one nearby client instead of many distant ones.

Quick check: An edge function makes five sequential queries to a database on another continent. What is the most likely result?

  • It is always faster because edge code runs closer to the user
  • It is slower than the same function deployed next to the database
  • The queries automatically run in parallel at the edge
  • The database replicates itself to every PoP
Answer

It is slower than the same function deployed next to the database — Each sequential query pays the long round trip. Co-locating the function with the database turns five long trips into one.