Successfully reported this slideshow.
Your SlideShare is downloading. ×

WASM! WASI! WAGI! WAT?

WASM! WASI! WAGI! WAT?

Download to read offline

WebAssembly is neither Web nor Assembly. This may sound odd at first, but makes a lot of sense if you look at some of its core features: Fast, efficient, sandboxed. Something we want from container technologies!

In this talk, we look at WASM as an alternative for your everyday container. We develop and deploy applications that run in WASM, not in the browser, but everywhere, including the cloud. Together with accompanying technologies like WASI and the experimental WAGI, we see how WebAssembly can be the future for cloud apps, including serverless.

WebAssembly is neither Web nor Assembly. This may sound odd at first, but makes a lot of sense if you look at some of its core features: Fast, efficient, sandboxed. Something we want from container technologies!

In this talk, we look at WASM as an alternative for your everyday container. We develop and deploy applications that run in WASM, not in the browser, but everywhere, including the cloud. Together with accompanying technologies like WASI and the experimental WAGI, we see how WebAssembly can be the future for cloud apps, including serverless.

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

WASM! WASI! WAGI! WAT?

  1. 1. WASM! WASI! WAGI! WAT?! fettblog.eu - oida.dev - rust-training.eu WASM outside the browser
  2. 2. @ddprrt
  3. 3. typescript-book.com
  4. 4. rust-linz.at
  5. 5. JF Bastien - Andreas Rossberg “Web Assembly. Neither web, nor assembly”
  6. 6. WAT
  7. 7. A tour on WebAssembly outside the browser
  8. 8. The Bleeding Edge
  9. 9. WASI Web Assembly Systems Interface
  10. 10. There are two important principles baked into Web Assembly
  11. 11. Portability • WebAssembly is an assembly language for a conceptual machine, not a physical one. It needs a runtime on top of a real machine to execute. This is why it can run across a variety of different machine architectures
  12. 12. Security • WebAssembly is sandboxed. This means that code can’t talk directly to the OS. The host needs to provide functions to access OS capabilities. The host can limit what the sandbox is able to do. • In the best case, exceptions are made explicit!
  13. 13. Just as WebAssembly is a language for a conceptual machine, it needs a system interface for a conceptual operating system
  14. 14. WASI
  15. 15. .rs .exe bin bin aarch64-apple-darwin x86_64-pc-windows-gnu x86_64-unknown-linux-musl https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ Win Linux OSX
  16. 16. .rs .wasm https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ WA Runtime WA Runtime WA Runtime Win Linux OSX
  17. 17. use std::{env::args, fs::File, io::Read}; fn main() { println!("Here are the contents of your file"); let mut file = File::open(args().nth(1).expect("No file provided")).expect("Couldn't open file"); let mut buf = String::new(); file.read_to_string(&mut buf) .expect("Not able to read file"); println!("{}", buf); } main.rs Compile to WASM-WASI $ rustup target add wasm32-wasi $ cargo build --release --target wasm32-wasi Execute in a WASM Runtime $ wasmtime files.wasm stairway.md $ wasmtime --dir=./lyrics/ files.wasm stairway.md
  18. 18. use std::{env::args, fs::File, io::Read}; fn main() { println!("Here are the contents of your file"); let mut file = File::open(args().nth(1).expect("No file provided")).expect("Couldn't open file"); let mut buf = String::new(); file.read_to_string(&mut buf) .expect("Not able to read file"); println!("{}", buf); } main.rs Compile to WASM-WASI $ rustup target add wasm32-wasi $ cargo build --release --target wasm32-wasi Execute in a WASM Runtime $ wasmtime files.wasm stairway.md $ wasmtime --dir=./lyrics/ files.wasm stairway.md ERROR
  19. 19. .rs .wasm https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ WA Runtime WA Runtime WA Runtime Win Linux OSX
  20. 20. .rs .wasm https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ WA Runtime WA Runtime WA Runtime Win Linux OSX THE BROWSER
  21. 21. https://blog.stackblitz.com/posts/introducing-webcontainers/
  22. 22. Security Portability ✅ ✅
  23. 23. WAIT Web Assembly Interface Types
  24. 24. WebAssembly in Host environments • WebAssembly makes native code extensions less complicated. • Think Node, Ruby, Python not compiling C/C++ Code to their native host, but rather use a WASM file • Rust, Go, C++ can use WASM to safely run extensions that don’t interfere with its own memory.
  25. 25. The problem: WASM only allows for numbers…
  26. 26. WAIT WebAssembly Interface Types (acronym made up)
  27. 27. use pulldown_cmark::{html, Parser}; use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn render(input: &str) -> String { let parser = Parser::new(input); let mut html_output = String::new(); html::push_html(&mut html_output, parser); return html_output; } main.rs A Markdown module we want to use cross env Find all demos at https://github.com/bytecodealliance/wasmtime-demos
  28. 28. import process from 'process'; import wasm_interface_types from 'wasm-interface-types'; import { promisify } from 'util'; import { readFile } from 'fs'; const baseURL = new URL(`${process.cwd()}/`, 'file://'); export async function resolve(specifier, parentModuleURL = baseURL, defaultResolver) { if (specifier.endsWith(".wasm")) { return { url: new URL(specifier, parentModuleURL).href, format: 'dynamic', }; } else { return await defaultResolver(specifier, parentModuleURL.toString()); } } const readFileAsync = promisify(readFile); loader.mjs - part 1
  29. 29. // Dynamically instantiates a wasm module with interface types included after // processing it with `wasm-interface-types`. Note that this executes // `wasm-interface-types` in Node itself (which is itself WebAssembly). export async function dynamicInstantiate(url) { const wasmBytes = await readFileAsync(new URL(url).pathname); const wasm = await wasm_interface_types.process(wasmBytes); return { exports: Object.keys(wasm), execute: exports => { for (const key in wasm) { exports[key].set(wasm[key]); } }, } } loader.mjs - part 12
  30. 30. import { render } from './markdown.wasm'; console.log(render("# Hello, node!")); main.mjs Load it like a JS file! You might know that pattern from Webpack $ $ node --experimental-wasm-mv --experimental-modules --loader ./loader.mjs ./main.mjs
  31. 31. # Import our Python extension `wasmtime_py` which loads the ability to load wasm # modules natively within Python. import wasmtime # Next up, loader our `markdown.wasm` file. This is loaded by the `wasmtime_py` # extension above and hooked up into the Python module system. import markdown # And now we can use the markdown file! print(markdown.render('# Hello, Python!'))
  32. 32. #[wasmtime_rust::wasmtime] trait WasmMarkdown { fn render(&mut self, input: &str) -> String; } fn main() -> Result<(), anyhow::Error> { let mut markdown = WasmMarkdown::load_file("markdown.wasm")?; println!("{}", markdown.render("# Hello, Rust!")); Ok(()) }
  33. 33. Interoperability Variety of hosts ✅ ✅
  34. 34. … Serverless How does WASM influence Serverless?
  35. 35. Care about servers, less Scale-out happens automatically No infrastructure management Consumption based billing Examples Google Cloud Run AWS Fargate Write less servers Focus on business logic Stateless development mindset Glue logic between services Examples AWS Lambda Azure Functions AUTOSCALING FUNCTIONS AS A SERVICE
  36. 36. Care about servers, less Scale-out happens automatically No infrastructure management Consumption based billing Examples Google Cloud Run AWS Fargate Write less servers Focus on business logic Stateless development mindset Glue logic between services Examples AWS Lambda Azure Functions AUTOSCALING FUNCTIONS AS A SERVICE "
  37. 37. {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} Virtual Machines Process overhead User Code
  38. 38. Front-End Authenticate Fetch function metadata Counting Service Check concurrency limits Worker Manager Request Worker Worker Create Sandbox Download Code Bootstrap Sandbox Run Code Always Coldstart AWS Lambda Coldstart
  39. 39. Trigger Cold Start Execution /next /response Execution /next /response Hibernation AWS Lambda Invocations
  40. 40. Trigger Cold Start Execution /next /response Execution /next /response Hibernation Trigger Cold Start Execution /next /response Hibernation AWS Lambda Invocations
  41. 41. Trigger Cold Start Execution /next /response Execution /next /response Hibernation Trigger Cold Start Execution /next /response Hibernation Trigger Cold Start Execution /next PANIC! Bootstrap Execution AWS Lambda Invocations
  42. 42. Trigger Cold Start Execution /next /response Execution /next /response Hibernation Trigger Cold Start Execution /next /response Hibernation Trigger Cold Start Execution /next PANIC! Bootstrap Execution Dispose Dispose AWS Lambda Invocations
  43. 43. {} Isolates Process overhead User Code {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
  44. 44. {} WASM Runtimes Process overhead User Code {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
  45. 45. WASM In JS Runtime
  46. 46. WASM In JS Runtime
  47. 47. WASM In JS Runtime
  48. 48. WAT
  49. 49. Fastly’s Compute@Edge • Fastly allows to execute WASM workloads on their edge infrastructure • As of recent you also can run JavaScript workloads • They run in Spidermonkey and are compiled ahead of time • How? Run JS in Spidermonkey at build time, initialise and save memory snapshot!
  50. 50. 0 1,25 2,5 3,75 5 JS Isolate WASM Snapshot https://bytecodealliance.org/articles/making-javascript-run-fast-on-webassembly
  51. 51. 0 1,25 2,5 3,75 5 JS Isolate WASM Snapshot https://bytecodealliance.org/articles/making-javascript-run-fast-on-webassembly 36 microseconds!!
  52. 52. Effectively instantaneous cold starts!
  53. 53. https://github.com/bytecodealliance/wizer
  54. 54. https://krustlet.dev
  55. 55. Isolation On demand processes ✅ ✅
  56. 56. WAGI Web Assembly Gateway Interface
  57. 57. “A common gateway interface enables web servers to execute an external program, to process user requests”
  58. 58. Server CGI Script (Perl, PHP, C) HTML/JSON Response Maybe a DB Common Gateway Interface (est. 1996) HTTP CGI Gateway Query STDOUT Per Request Execution!
  59. 59. AWS Lambda (Node, Python, …) HTML/JSON Response Dynamo DB Modern Apps with AWS/Azure HTTP API Gateway Query Response Per Request Execution!
  60. 60. Server WASM File HTML/JSON Response Maybe a DB WAGI HTTP WAGI Gateway Query STDOUT Per Request Execution!
  61. 61. fn main() { println!("Content-Type: text/plain"); println!(); println!("Hello world"); } HTTP!! Compile to WASM-WASI [[module]] route = "/" module = "examples/hello.wat" [[module]] route = "/hello/..." module = "examples/hello.wasm" [[module]] route = "/bar" module = “examples/bar.wasm" volumes = {"/path/inside/wasm" = "/path/on/host"} hello.rs modules.toml Map routes to WASM files Give access to directories on the file system
  62. 62. https://deislabs.io/posts/introducing-wagi-easiest-way-to-build-webassembly-microservices/
  63. 63. WAT Web Assembly Text File
  64. 64. The Bleeding Edge
  65. 65. Resources • https://www.fastly.com/blog/announcing-lucet-fastly-native-webassembly- compiler-runtime • https://bytecodealliance.org/articles/making-javascript-run-fast-on- webassembly • https://www.fastly.com/blog/join-the-beta-new-serverless-compute- environment-at-the-edge • https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly- system-interface/ • https://github.com/fitzgen/wasm-summit-2021
  66. 66. Resources • https://hacks.mozilla.org/2019/08/webassembly-interface-types/ • https://krustlet.dev/ • https://deislabs.io/posts/introducing-wagi-easiest-way-to-build- webassembly-microservices/ • https://medium.com/@zackbloom/isolates-are-the-future-of-cloud- computing-cf7ab91c6142 • https://github.com/bytecodealliance/wasmtime-demos • https://wasmcloud.dev/
  67. 67. WAFFLE Well, all finished, friends! Let’s enjoythebreak.
  68. 68. Thank you! @ddprrt

×