Building fast APIs at the edge

Article

A sample technical post showing code blocks, headings, the table of contents, and inline code.

This sample post shows what a technical article looks like: syntax highlighting, a sticky table of contents, and copy buttons on code blocks.

The problem

Every request that has to travel back to a single origin region adds latency. For users far away from that region, it can easily add 100–300 ms.

Moving logic to the edge

With Cloudflare Workers, your code runs in the data centre closest to the user:

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);

    if (url.pathname === '/api/hello') {
      return Response.json({ message: 'Hello from the edge!', colo: request.cf?.colo });
    }

    return new Response('Not found', { status: 404 });
  },
};

Caching responses

You can cache expensive responses with the Cache API:

const cache = caches.default;
let response = await cache.match(request);
if (!response) {
  response = await fetch(request);
  ctx.waitUntil(cache.put(request, response.clone()));
}

Measuring the result

Run curl -w "%{time_total}" before and after the change and compare the numbers.

curl -s -o /dev/null -w "%{time_total}\n" https://example.com/api/hello

Wrapping up

Edge compute isn’t a silver bullet, but for read-heavy, latency-sensitive endpoints it’s a big win.