Tech, ExplainedCloud platforms and developer tools
Part

Edge functions

Code that runs in the city next door.

INSIDE: CONTENT DELIVERY NETWORK (CDN)HOW IT WORKS · A TAMIL LESSON LIST SERVED FROM CHENNAIA learner in Chennaiopens the app; herrequest reaches theCDN's Chennai serverin about 8 msThe edge functionreads the request'slocation: Tamil NaduIt checks thesignature on herlogin token in about2 msIt sends the storedTamil lesson list;the Mumbai server isnot askedWhen she finishes alesson, it passes herscore on to theMumbai serverALSO TALKS TODeveloper cloudINSIDE: CONTENT DELIVERY NETWORK (CDN)HOW IT WORKS · A TAMIL LESSONLIST SERVED FROM CHENNAIA learner in Chennaiopens the app; herrequest reachesthe CDN's Chennaiserver in about 8 msThe edge function readsthe request's location:Tamil NaduIt checks the signatureon her login token inabout 2 msIt sends the stored Tamillesson list; the Mumbaiserver is not askedWhen she finishesa lesson, it passesher score on tothe Mumbai serverALSO TALKS TODeveloper cloudINSIDE: CONTENT DELIVERY NETWORK (CDN) · ATAMIL LESSON LIST SERVED FROM CHENNAIA learner in Chennai opens the app; herrequest reaches the CDN's Chennai serverin about 8 ms↓The edge function reads the request'slocation: Tamil Nadu↓It checks the signature on her logintoken in about 2 ms↓It sends the stored Tamil lesson list;the Mumbai server is not asked↓When she finishes a lesson, it passes herscore on to the Mumbai serverALSO TALKS TODeveloper cloud
A learner's request reaches the CDN's Chennai server, where an edge function checks her login and sends the Tamil lesson list from stored copies; only her lesson score goes on to the main server in Mumbai.
opens a page inside this one a page in the same scene a page in another scene a concept page a label only, with no page

Edge functions are small pieces of an app's code that run on a content delivery network's servers, in cities near the users. So some requests are answered close by, and never travel to the app's main server far away. They are also called edge workers.

A content delivery network (CDN) first kept only copies of files, such as images. A copy is the same for every visitor. But many answers depend on who is asking: the visitor's language, the visitor's city, or whether the visitor is logged in. Before edge functions, every such request had to go to the app's own server. An edge function lets the developer write a few lines of code that work out the answer at the nearby server.

Here is a typical case. A language-learning app from Bengaluru teaches English to speakers of nine Indian languages. Its main server, called the origin, is in Mumbai. A learner in Chennai opens the app. Her request reaches the CDN's server in Chennai in about 8 milliseconds. There, an edge function runs. It reads the location that the CDN attaches to each request, which says Tamil Nadu. It also reads her login token, a signed piece of text that proves who she is, and checks the signature. This check takes about 2 milliseconds. The function then picks the Tamil version of today's lesson list from the CDN's stored copies, and sends it back. The Mumbai server is never asked.

Some requests must still go to Mumbai. For example, when she finishes a lesson, her score must be saved in the app's main database. So the edge function passes that request on to the origin. Out of every ten requests, perhaps one goes all the way back.

Why does this matter? A round trip from Chennai to Mumbai takes about 30 milliseconds, as the page on bandwidth and latency explains. Saving that time on every screen makes the app feel faster. It also means the main server handles far fewer requests. So the team can rent a smaller server from its developer cloud.

Edge functions have limits. Each one may use only a few milliseconds of computing and a small amount of memory for each request. They cannot run long jobs. They keep no data of their own from one request to the next, unless they use the CDN's storage. So the heavy part of the backend, such as the main database, stays on the main server.

How are edge functions different from a virtual private server? A virtual server runs all the time, in one place, and the team pays for every hour. An edge function runs only when a request arrives, in hundreds of cities at once, and the team pays for each request. Edge functions are one form of edge computing.

Tech, Explained is a part of ingrid.fyi. Every page explains one piece of technology in plain words.