Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท WASI and Beyond the Browser

WebAssembly on Servers and the Edge

Learn why WebAssembly suits servers, serverless and edge platforms - fast cold starts, per-request isolation, plugins and proxies - with runnable instance-isolation and memory-limit examples.

The idea of running WebAssembly on a server sounds odd at first: we already have containers, virtual machines and native binaries. But a short description of what Wasm offers explains the interest. It is sandboxed by default, it starts in well under a millisecond with a good runtime, the same file runs on any CPU and operating system, and it can be tiny. Those properties line up closely with what edge networks, serverless platforms and plugin-based products want.

This lesson looks at where WebAssembly is used outside the browser, what properties make it a good fit, where it is not a good fit, and demonstrates two of the properties (per-request isolation and resource limits) with code you can run.

Why servers like it

Server-side advantages

Cold start Far faster than a container.
Isolation Each request in its own sandbox.
Small Kilobytes to a few megabytes.
Portable One binary, any CPU or OS.

Density: thousands of modules per host.

1 / 2

A container image starts a process inside an isolated kernel namespace: typically tens to hundreds of megabytes and anywhere from a few hundred milliseconds to seconds to become ready. A WebAssembly instance is a block of memory plus compiled code, and with ahead-of-time compilation, creating one is closer to allocating an object than booting a process. That is why platforms can run a fresh instance for every request and discard it afterward, which removes whole categories of problems with state leaking between requests.

Common use cases

Edge and serverless functions. Several content-delivery and cloud platforms (for example Cloudflare Workers, Fastly Compute and Fermyon Spin) run WebAssembly or let you deploy it. You write a handler in Rust, Go, JavaScript, C# or another language, compile it to a module, and the platform runs it near the user. Cold starts are short, so you do not need to keep warm instances.

Plugin systems. An application that wants third-party extensions has a dilemma: native plugins are fast but can do anything, scripting plugins are safe but slow and limited to one language. A Wasm plugin is sandboxed, language-agnostic and fast. Editors, databases, game engines, observability tools and e-commerce platforms use it for exactly this. See Plugin Systems with WebAssembly.

Network proxies. Envoy and other proxies load Wasm filters to inspect and modify requests (authentication, rate limiting, header rewriting) without recompiling the proxy.

Safe user code. Platforms that run customer-supplied logic (spreadsheet formulas, checkout rules, data transformations) use Wasm to bound what that logic can do.

Containers and orchestration. Docker and Kubernetes integrations (through runwasi and the containerd shims) can schedule Wasm workloads next to containers, using the same tooling.

Embedded and IoT. Interpreters such as WAMR and wasm3 run Wasm on devices with kilobytes of memory, so updates can be shipped as portable, sandboxed modules.

Cross-platform libraries. A parser or codec compiled once to Wasm can run in the browser, on the server and in a mobile app through each platform's runtime, with the same behavior everywhere.

Demonstration 1: one instance per request

A module (compiled code) can be shared and instantiated many times cheaply. Each instance has its own memory and globals. In this example the module has a bump allocator whose heap pointer is a global. Two requests each allocate memory in their own instance, and neither can see the other's state. We then create 500 instances to time instantiation once the module is compiled:

Node.js Fresh instance per request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The time per instance here (tens to a few hundred microseconds in Node, depending on memory pressure) is dominated by reserving and zeroing the memory. Production runtimes use pooling allocators that reuse memory slots, which brings instantiation down to a few microseconds.

Demonstration 2: limiting memory

A server cannot trust a module to be polite about memory. Hosts declare a maximum when they create a memory, and a request to grow beyond it fails. The same applies to a memory a module declares itself. A runtime enforces the limit; here you can see the semantics with the JavaScript API:

Node.js A memory with a hard maximum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Inside a module, memory.grow instead returns -1 when it fails, which is what C's malloc implementation checks to return NULL. Besides memory, runtimes also bound execution time (fuel or deadlines), recursion depth, table size and the number of instances.

Limits and honest trade-offs

WebAssembly is not a universal replacement for containers.

  • Ecosystem gaps. Programs that need threads, raw sockets, fork, GPUs or large native libraries may not compile or may need a platform-specific adaptation. WASI keeps growing but is not Linux.
  • Peak speed. Wasm is close to native, not equal; heavy numerical code can be 10% to 50% slower than a tuned native binary.
  • Debugging and observability. Profilers, tracers and debuggers for Wasm are improving but behind native tooling (see Debugging WebAssembly).
  • Platform differences. Each edge platform has its own APIs for HTTP, key-value storage and secrets, so "portable" code still needs a thin platform layer unless you target standard interfaces such as wasi:http.
  • Language support. Garbage-collected languages ship their runtime, enlarging modules (see Other Languages for WebAssembly).

Pick Wasm when isolation, startup time, density or portability matter, and stay with containers when you need the full Linux environment.

A typical request lifecycle

sequenceDiagram
  participant C as Client
  participant H as Host runtime
  participant M as Wasm instance
  C->>H: HTTP request
  H->>M: Instantiate precompiled module (fast)
  H->>M: Call handler(request)
  M-->>H: Response (calls host imports for I/O)
  H-->>C: HTTP response
  H->>M: Discard instance and memory

The module is compiled once; each request gets a fresh sandbox that is thrown away afterward.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a platform run a fresh WebAssembly instance for every request?

    Compile once, instantiate many: the cost per instance is small, and no state leaks from one request to the next.
  2. 2. What happens when memory.grow would exceed the declared maximum in the JavaScript API?

    The JS API throws RangeError. Inside wasm code, memory.grow returns -1 instead.
  3. 3. Which is NOT a typical reason to choose WebAssembly on the server?

    Wasm modules see only what the host grants through WASI and imports. If you need full Linux, use a container.
  4. 4. What is a Wasm filter in a proxy like Envoy used for?

    Proxies embed a runtime and run sandboxed filter code per request.

Summary

  • Server-side WebAssembly is attractive for fast cold starts, strong isolation, small size and portability.
  • Typical uses: edge and serverless functions, plugin systems, proxy filters, safe user logic, cross-platform libraries and embedded devices.
  • Compile once, instantiate per request; each instance has its own memory and globals.
  • Hosts cap memory, time, recursion and imports; growing past a memory maximum fails.
  • Wasm is not Linux: check ecosystem support, debugging tools and platform APIs before committing.

Next: The WebAssembly Toolchain