This article introduces a new Serverless architecture: the Serverless architecture based on the WebAsssembly virtual machine. In this article, you’ll learn about WebAssembly technology, the Serverless WebAssembly architecture, and how to develop a simple application using Serverless WebAssembly.

Let’s start with an instance of the MobileNet TensorFlow Model in Serverless Wasm. Upload a picture of food and identify what it is.

Click here to upload your lunch and it will automatically identify what you had for lunch!

Current Serverless architecture

In the Serverless architecture, developers and users do not need to pay attention to the operation and maintenance costs of the server. Instead, they focus on the code that implements the business logic and pay for the actual resource usage. Serverless significantly reduces development and operation costs without reducing the user experience.

The Serverless Cloud function is the fastest and easiest way to implement Internet applications today. Each major cloud manufacturer at home and abroad has its own Serverless business, which is either based on virtual machines of system or hardware level, or built on containers such as Docker.

Both approaches have their advantages in terms of safety and performance. According to Johann Schleier-Smith of the University of Berkeley, using virtual machines at the system or hardware level provides the best isolation and security, but is slow to run and complex to manage. The container approach is less secure, but performs much better than system-level VMS.

Emerging Serverless Wasm architecture

In addition to the above two Serverless architectures, an emerging approach is to use application-specific virtual machines, such as WebAssembly (Wasm).

In “Why WebAssembly is the Future of Software Services?” This article described the enormous potential of WebAssembly on the server side. In short, WebAssembly offers close to the performance of native code, but still maintains good security.

If we had WASM + WASI in 2008, we wouldn’t have started Docker at all. Wasm is so important! WebAssembly on the server is the future of computing. — Solomon Hykes, Docker co-founder

Serverless Wasm provides a high level of abstraction where users do not need to bind their own operating system or software stack, but simply execute compiled bytecode applications. WebAssembly provides an advanced “feature-based” security model for accessing system resources (for example, through the WASI specification) rather than coarser grained operating system-level isolation.

Let’s take a look at the Serverless Wasm architecture using Second State FaaS as an example.

The disadvantage of Serverless Wasm is that it only supports applications that can be compiled to Wasm bytecode, and currently Rust, C, C++, and AssemblyScript are the most complete languages that support Wasm. But as the Wasm ecosystem continues to evolve, Wasm will support more programming languages through the LLVM toolchain.

Second State FaaS is currently focusing on Rust support. For the fifth year in a row, Rust has been named Stack OverFlow’s most popular programming language for developers. Rust is secure, high-performance, memory safe, no GC.

The overall workflow for Serverless Wasm looks like this:

  • Compile the Rust function to WASM bytecode using the compilation tool ssVMup
  • Compile via HTTP POST.wasmUpload the file to FaaS
  • The returned WASm_ID, WASM_SHA256, SSVM_Usage_Key, SSVM_Admin_Key are used to manage the current application
  • Finally, the Rust function is called and executed through the HTTP API

The combination of Rust functions with the Wasm virtual machine is ideal for computation-intensive tasks such as AI reasoning, video transcoding, edge computing, and other scenarios.

A quick example to get started

Let’s implement a simple Hello World example using Serverless Wasm.

Tool chain Preparation

1 Use Docker image. The Docker Image below contains all the required toolchains

$ docker pull secondstate/ssvm-nodejs-starter:v2 $ docker run -p 3000:3000 --rm -it -v $(pwd):/app secondstate/ssvm-nodejs-starter:v2 (docker) # ssvmup build ... .Copy the code

Fork the Repo on Github and open the Hello World example using Github CodeSpaces. From the IDE, you can override the Rust function and run the command line in terminal Window.

3 If you have Ubuntu Linux 20.04 installed, just install Rust and SSvmup

Rust code

The source code can be viewed here

Here is the full Rust code for Hello World. This Rust function takes a string argument and returns a string value. Hello is prefixed to the input argument, and the string is returned to the caller of the function.

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn say(s: &str) -> String {
  let r = String::from("hello ");
  return r + s;
}
Copy the code

Before compiling, make sure the Cargo. Toml file declares the correct dependencies.

[dependencies] wasm - bindgen = = 0.2.61 ""Copy the code

Compile and deploy the Rust function

Use the SSvvmup tool to compile Rust functions into WebAssembly functions. Using the following named lines, the compiled WASM file will be saved in the PKG directory.

$ ssvmup build
Copy the code

Upload the.wasm file in the PKG directory to the/API /executables RPC service endpoint in Second State FaaS. Before uploading, check the file name of the.wasm file.

$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/executables' \
--header 'Content-Type: application/octet-stream' \
--header 'SSVM-Description: say hello' \
--data-binary '@pkg/hello_lib_bg.wasm'
Copy the code

After the. Wasm file is uploaded, Second State FaaS returns wasm_ID. Wasm_id can access functions in the deployed WASM file.

Call the FaaS function

The Rust function can now be called from the Web! The RPC service endpoint is in/API /run/wasm_id/function_name, where wasm_id is the ID of the Wasm file we just deployed, and function_name is the name of the function we want to call in the Wasm file.

The HTTP Request body is passed to the function as a call argument. As we can see, this function takes a string argument, so the HTTP body is converted to a text string and passed to the function. The TTP Response Body is the return string value of the function.

$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/run/161/say' \
--header 'Content-Type: text/plain' \
--data-raw 'Second State FaaS'

hello Second State FaaS
Copy the code

Web UI

One of the most compelling use cases of FaaS is for functions to act as back-end services for static web sites. Here is the JavaScript code to make the AJAX call to this FaaS function.

$.ajax({ url: "https://rpc.ssvm.secondstate.io:8081/api/run/161/say", type: "post", data : $('#input').val(), contentType: "text/plain", processData: false, success: function (data) { $('#result').html(data); }});Copy the code

Click here to access the demo with the Web UI. The Serverless function converts static functions into Web applications.

For more information

This completes a simple example of Serverless WASM. If you are interested in serverless Wasm, please visit the Serverless Wasm website to learn more serverless Wasm application examples and tutorials on AI reasoning.

  • Documentation: Binary data as input and output to a function
  • Documentation: Use a mixture of text and Binary data when calling arguments
  • Documents: webhooks
  • Document: HTTP Proxy
  • Example: Add watermark using Serverless Wasm
  • Example: Add a custom watermark using Serverless Wasm
  • Example: Sending mail using Serverless Wasm
  • Example: Using Serverless WASM for image recognition
  • Example: Using Serverless WASM for face recognition

🏆 issue 7 | all things can be Serverless technology projects