Asia/Dhaka
Posts

Introduction to Edge Computing and Workers

October 16, 2024
For the past decade, the dominant model for web applications has been centralizing computing in the cloud. You host your Node.js server, database, and assets in a data center (e.g., AWS us-east-1 in Virginia). While cloud computing provides incredible scale, it has a physical limitation: the speed of light. If a user in Tokyo requests a page from your server in Virginia, the data has to travel halfway across the globe. This physical distance introduces unavoidable latency (often hundreds of milliseconds) known as the "Round Trip Time" (RTT). Edge computing solves this by decentralizing execution. Instead of running your code in a single central data center, you deploy it to a global network of "edge nodes" (often operated by CDNs like Cloudflare, Vercel, or Fastly). When a user makes a request, it is routed to the edge node geographically closest to them. Your code executes mere miles away from the user, drastically reducing latency to just a few milliseconds. The magic behind edge computing relies on Edge Workers (like Cloudflare Workers or Vercel Edge Functions). These are not traditional Node.js servers running Express. Instead, they use the V8 JavaScript Engine (the same engine powering Google Chrome) to run lightweight scripts. They conform to standard Web APIs (like fetch, Request, Response) rather than Node.js specific APIs. Because they don't have the overhead of spinning up a full Node process or an entire container, Edge Workers boast cold boot times of zero milliseconds.
  1. A/B Testing and Personalization: You can modify incoming requests or outgoing HTML at the edge based on the user's cookies or location without hitting the origin server.
  2. Authentication and Authorization: Validate JWT tokens at the edge and reject unauthorized requests immediately, saving your origin server from unnecessary load.
  3. Dynamic Rendering: Frameworks like Next.js now support Edge Rendering, allowing you to server-side render pages at the edge node closest to the user.
  4. Geolocation: Edge providers automatically inject headers containing the user's city, country, and timezone, making it trivial to localize content.
While edge computing offers blazing speed, it's not a silver bullet.
  • Database Proximity: If your Edge Worker in Tokyo has to connect back to your database in Virginia, you've reintroduced the latency. You need distributed databases (like PlanetScale or Turso) to truly benefit from the edge.
  • API Limitations: You cannot use Node.js specific packages (like fs or path) inside an Edge Worker.
By carefully deciding what logic to move to the edge, you can create applications that feel incredibly fast and responsive for users anywhere in the world.
On this page
Hey! Got questions?Ask me anything 👋