Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebAssembly beyond the browser

WebAssembly beyond the browser

Mete Atamel

June 19, 2023
Tweet

More Decks by Mete Atamel

Other Decks in Programming

Transcript

  1. WebAssembly beyond the browser Mete Atamel Developer Advocate at Google

    @meteatamel atamel.dev speakerdeck.com/meteatamel
  2. What is WebAssembly? WebAssembly (Wasm) is a binary instruction format

    for a stack-based virtual machine Wasm is designed as a portable compilation target for programming languages webassembly.org
  3. What is WebAssembly? The original use cases were focused on

    augmenting Javascript in the browser to run native code in a fast, portable, and secure way for games, 3D graphics, etc. Supported by all major browsers webassembly.org
  4. Wasm outside the browser There’s an attempt to get WebAssembly

    run outside the browser with the WASI project Instead of running apps as VMs or containers, you can run them as faster, smaller, more secure, and more portable WebAssembly modules
  5. Faster ⇒ Wasm apps start faster than containers with no

    cold-start Smaller ⇒ A HelloWorld Rust app in Wasm is smaller than in an OCI container More secure ⇒ Containers execute in an allow-by-default model whereas Wasm apps execute in a deny-by-default sandbox More portable ⇒ A container built for linux/amd64 won’t work on linux/arm64 whereas a wasm32/wasi module can run on any host with a Wasm runtime
  6. Wasm runtimes Many runtimes exist, some of the popular ones

    are: • wasmtime: A Bytecode Alliance project • wasmedge: A CNCF project • wasmer: From a startup github.com/appcypher/awesome-wasm-runtimes
  7. POSIX-inspired Preview1 with a limited number of functions • Read

    and write files (sandboxed) • Program arguments • Environment variables • Hardware clock • Random • Sockets (limited) WebAssembly System Interface wasi.dev
  8. But… A lot of WASI is still in proposals and

    limited ⇒ e.g. no socket support yet Some runtimes implement their own sockets ⇒ e.g. wasmedge has extensions for sockets in Rust, JavaScript, C Some projects try to enhance WASI ⇒ e.g. WAGI adds HTTP handlers around WASI ⇒ e.g. WASIX adds full POSIX but only supported on wasmer runtime No Garbage collection yet but there’s a proposal ⇒ github.com/WebAssembly/gc wasi.dev
  9. Demo: Compile code to Wasm+Wasi and run on a Wasm

    runtime github.com/meteatamel/wasm-basics/tree/main/samples/rust-wasm github.com/meteatamel/wasm-basics/tree/main/samples/dotnet8-wasm github.com/meteatamel/wasm-basics/tree/main/samples/go-wasm github.com/jamesward/hello-kotlin-wasm/tree/wasi
  10. Why take something smaller, faster, and more portable than a

    container and run it inside Docker? To combine the benefits of Wasm with the benefits of existing ecosystem with Docker, Kubernetes, etc.
  11. runwasi Project to facilitate running Wasm managed by containerd Starting

    in 4.15, Docker uses runwasi to run Wasm with wasmedge, wasmtime, spin, and slight Azure AKS is also previewing Wasm node pools using runwasi with spin and slight on wasmtime WebAssembly and containerd: How it works by Nigel Poulton
  12. WAGI allows you to run WASI binaries as HTTP handlers

    Write a command line application that prints a few headers, and compile it to wasm32/wasi Add an entry to the modules.toml matching URL to the Wasm module and that’s it WebAssembly Gateway Interface github.com/deislabs/wagi
  13. Frameworks Lots of Wasm frameworks out there with different capabilities

    Spin by Fermyon seems to be the most comprehensive and relevant for Wasm services Handles the HTTP request/response using WAGI. Spin SDKs are available in Rust, Go and .NET. and all Wasi-compatible languages are supported github.com/fermyon/spin
  14. Language support for Wasm + WASI Rust Good support (but

    no Tokio or async) C, C++ Good support (but no C++ exceptions or threads) Go Support via TinyGo (but reflection is not supported) and Go 1.21 RC JavaScript Support via Shopify’s Javy on QuickJS? C#/.NET Support via experimental WASI SDK in .NET 7, wasi-experimental workload in .NET 8 preview Java Support via a fork by Fermyon of TeaVM (Java to Javascript compiler)? Kotlin Support via kowasm and Node experimental WASI API Python Support via CPython on wasm32-wasi Ruby Support via ruby.wasm www.fermyon.com/wasm-languages/webassembly-language-support enarx.dev/docs/WebAssembly/Introduction
  15. Conclusions Wasm outside the browser is a fast changing space

    Standards and language support need to mature for Wasm microservices Will it replace containers? Possibly Exciting time to learn this new technology!
  16. Thank you! Mete Atamel Developer Advocate at Google @meteatamel atamel.dev

    speakerdeck.com/meteatamel github.com/meteatamel/wasm-basics Feedback? bit.ly/atamel