WebAssembly on the server-side

Michael Yuan
Wasm
Published in
6 min readNov 23, 2019

WebAssembly (Wasm), originally developed by Mozilla, Google, Microsoft, Apple, and others at W3C, is a secure, cross-platform, and high-performance application runtime. It serves as the execution engine for compiled C/C++ applications in modern browsers.

As Wasm gains popularity in the browser, developers found that the same attributes that made Wasm successful on the client side are also important to server-side applications. Modern server-side applications in the multi-tenancy cloud need a secure, cross-platform, and high-performance runtime to support modern compiled languages such as Rust, C/C++, and Go. Furthermore, by supporting C/C++, Wasm runtimes can support high-level languages such as JavaScript, Ruby, Python, Kotlin, and PHP.

Before Wasm, Linux containers are commonly used to run these compiled applications in the cloud — eg a Rust or C++ app is compiled to x86_64 machine code and runs inside a Linux container. Wasm provides a more secure, much lighter, faster, and more portable alternative to Linux containers for this type of performance-minded server-side application.

Value propositions

Wasm’s move from client-side to server-side follows well-traveled paths of previous generations of technologies such as Java and JavaScript. Compared with existing solutions, Wasm offers some compelling value propositions on the server side.

Compared with the JVM and JavaScript engines, Wasm supports 20+ programming languages via the LLVM toolchain. In particular, it supports emerging memory-safe programming languages, such as Rust. At the same time, Wasm provides near-native performance without sacrificing safety.

Compared with the traditional Linux containers, Wasm runtimes are safer since they have much-reduced attack surfaces (no OS libraries) and much simpler software supply chains. Wasm applications are portable across OSes and CPUs. They can be AOT compiled to native machine code at deployment time achieving near-native performance. Furthermore, Wasm consumes much less memory and resources than Linux containers.

Wasm as containers

Last year, Docker announced its support for Wasm in partnership with WasmEdge (an open-source project under the CNCF).

The demo-microservices example is a fully featured microservice written in Rust. It provides a built-in HTTP server and a MySQL database client. It is compiled into Wasm and then deployed by Docker Compose in a WasmEdge container. In Docker Desktop, we see the size of the demo-microservices Wasm container image is only 3.05 MB, which is a fraction of traditional Linux images.

The Wasm container image size is much smaller than Linux containers.

Wasm, and WasmEdge in particular, are supported by many mainstream container tools.

Use cases for server-side WebAssembly

Cloud-native platforms need to support untrusted and high-performance applications. Wasm containers are great choices for these use cases.

Microservices

As microservices grow popular, the infrastructure costs associated with their management are exploding. That is especially problematic for a new generation of microservice applications that are computationally intensive (i.e., AI) and performance critical (i.e., edge cloud applications).

Serverless functions are publicly accessible microservices. They are the fastest-growing product sector in public clouds.

Wasm is a secure sandbox for compiled applications. It is lightweight and portable, runs applications at near-native performance, and works seamlessly with container ecosystem tools like K8s and Docker. Developers can use the same workflow to build, deploy, and manage Wasm Apps. Wasm is a great choice for the next-generation edge cloud-based microservices. See how to create database backed microservices in Rust and WasmEdge.

SaaS/UDF

As SaaS proliferates, the demands to automate, customize, or integrate SaaS products are also increasing. Traditional webhook-based SaaS APIs are slow, bloated, complex, and not secure enough for many automation use cases.

An emerging approach is for users to simply upload customization code onto the SaaS platform, which can be triggered by SaaS events. Examples include AWS Lambda, which allows you to customize AWS workflows through functions, and GitHub Actions, which allows users to automatically respond to events in GitHub using apps running on GitHub infra.

Wasm runtimes can be easily integrated into SaaS or database platforms. They are secure, portable, and lightweight. Once integrated, Wasm runtimes can run functions (or guest apps) written in a variety of different programming languages, supporting SaaS SDKs in all popular languages.

See a database UDF example here from Nebular Graph.

Streaming data

Modern data-intensive applications often need to filter, map, and transform data in streaming or messaging pipelines. That requires user-defined functions to be embedded into the data streaming platform.

For the same reason why Wasm is a great runtime for SaaS UDFs, it is a great choice for data streaming functions too. Furthermore, Wasm runtimes such as WasmEdge, provide high-performance APIs for AI/ML processing, which makes them ideal for running compute-intensive AI/ML workloads in many streaming analytics use cases.

See a database ETL function example based on WasmEdge here.

Blockchain

Large public blockchain networks, such as Ethereum, EOS, Polkadot, NEAR, FileCoin, Cosmos, Ripple, have all committed to Wasm as their smart contract execution engines. In our view, smart contracts on public blockchain networks are decentralized serverless functions. Blockchains are ideal use cases for Wasm.

Show me the code

WasmEdge is a WebAssembly runtime that is specifically optimized for server-side and cloud-native applications. It is an open-source project managed by the Linux Foundation / CNCF. Its key features include the following.

To see some code examples, check out the following tutorials. You could also refer to WasmEdge book.

The road ahead

The road ahead for server-side Wasm is very promising. WasmEdge has brought the cloud-native toolchain to WebAssembly, enabling developers to use their familiar workflow in Wasm application development. Companies such as Second State, Fermyon, Cosmonic, Suborbital, VMWare, Cloudflare, Fastly, SingleStore, Shopify, InfinyOn and Microsoft, have also launched/pre-launched their own WebAssembly-based public cloud services.

The Wasm community will continue to innovate in areas such as developer tooling and component model support. We expect more exciting news, tools, use cases, and partnerships to emerge in server-side Wasm.

Join our discord server or follow us on Twitter to stay updated! See you in the community and happy coding!

https://www.secondstate.io/

--

--