$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. What, Why?
    01

    View Slide

  3. 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

    View Slide

  4. 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

    View Slide

  5. View Slide

  6. CNCF 2022 Annual Survey

    View Slide

  7. 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

    View Slide

  8. 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

    View Slide

  9. Hardware
    VM
    Hardware
    VM
    Container
    VM
    Container
    2010
    2000 2020
    Wasm
    Is this the future?

    View Slide

  10. Use cases
    www.fermyon.com/blog/four-domains-wasm

    View Slide

  11. Wasm runtimes
    WASI
    02

    View Slide

  12. Wasm is a portable compilation target

    View Slide

  13. Wasm modules run across CPU architectures

    View Slide

  14. 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

    View Slide

  15. WebAssembly System Interface
    wasi.dev

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. Running WASI in
    Docker
    03

    View Slide

  20. 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.

    View Slide

  21. 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

    View Slide

  22. Demo: Running Wasm in Docker
    github.com/meteatamel/wasm-basics/tree/main/samples/rust-wasm-docker

    View Slide

  23. WAGI
    WASIX
    04

    View Slide

  24. 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

    View Slide

  25. WASIX – Extends WASI with most of the
    missing POSIX features
    wasix.org

    View Slide

  26. Demo: Running WASI binaries as HTTP
    handlers with WAGI
    github.com/meteatamel/wasm-basics/tree/main/samples/hello-wagi

    View Slide

  27. Frameworks
    Languages
    05

    View Slide

  28. 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

    View Slide

  29. Demo: Running Python with Spin on Wasm
    github.com/meteatamel/wasm-basics/tree/main/samples/python-spin-wasm

    View Slide

  30. 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

    View Slide

  31. 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!

    View Slide

  32. Thank you!
    Mete Atamel
    Developer Advocate at Google
    @meteatamel
    atamel.dev
    speakerdeck.com/meteatamel
    github.com/meteatamel/wasm-basics
    Feedback?
    bit.ly/atamel

    View Slide