A Lightweight, Safe, Portable, and High-Performance Runtime for Dapr – InfoQ.com

Attend QCon Plus (November 1-12) and stay ahead of the tech that matters to keep your skills fresh. Register
Facilitating the spread of knowledge and innovation in professional software development


Engineering The Digital transformation leverages manufacturing’s successful track record of improving productivity and quality and organizational change management principles. It’s a training program designed to reduce the barriers to change, enable teams to understand good design patterns, and ultimately allow organizations to create a systematic approach to continuous improvement.
Charles Humble explores the topic of designing software for sustainability with Microsoft’s Asim Hussain, chair of the Green Software Foundation. They discuss techniques such as demand shifting, as well as the current challenges that the Green Software Foundation is aiming to address.
In the podcast, Rosaria Silipo talks about the emerging trends in deep learning, with focus on low code visual programming to help data scientists apply deep learning techniques without having to code the solution from scratch.
With developers becoming more important than ever, it’s critical to understand how to maximize their productivity and engagement, so in turn, businesses can create better value. The Developer Experience Engineer (DXE) clears the path to developer success by implementing a common set of principles, maintaining the right tools, and creating cohesive standards.
Liz Rice discusses how eBPF enables high-performance tools that will help connect, manage and secure applications in the cloud.
Turn advice from 64+ world-class professionals into immediate action items. Attend online on Nov 1-12.
Learn from practitioners driving innovation and change in software. Attend in-person on April 4-6, 2022.
InfoQ Homepage Articles A Lightweight, Safe, Portable, and High-Performance Runtime for Dapr
Oct 04, 2021 11 min read
by
Michael Yuan
reviewed by
Dylan Schiemann

Since its release in 2019, Dapr (Distributed Application Runtime) has quickly become a very popular open-source framework for building microservices. It provides building blocks and pre-packaged services that are commonly used in distributed applications, such as service invocation, state management, message queues, resource bindings and triggers, mTLS secure connections, and service monitoring. Distributed application developers can utilize and consume web-based APIs exposed by those building blocks at runtime. These applications are commonly known as microservices and run as sidecars. Dapr is an example of the Multi-Runtime Microservices Architecture, as described by InfoQ author Bilgin Ibryam.
Dapr’s sidecar pattern is very much like a service mesh. However, unlike traditional service mesh which aims to manage applications without any code change, Dapr applications need to integrate and actively utilize external Dapr building block services.
Join the GraphQL conference for developers and leaders at GraphQL Summit taking place virtually November 10-11, 2021.
The microservice applications in Dapr sidecars could be native client (NaCl) applications compiled from languages like Go and Rust, or managed language applications written in Python or JavaScript. In other words, the sidecar applications could have their own language runtimes. The sidecar model allows Dapr to support “any language, any framework, anywhere” for its applications.
Dapr can run sidecar applications directly on the OS or through an application container like Docker. The container offers benefits such as portability, ease of deployment, and security, but it also brings significant overheads.
In this article, we present a new approach to run Dapr sidecar applications. We use a simple NaCl written in Rust or Go to listen for API requests to the microservice. It passes the request data to a WebAssembly runtime for processing. The business logic of the microservice is a WebAssembly function created and deployed by an application developer.  
Figure 1. A Dapr microservice with a WebAssembly function.
The WebAssembly runtime is well suited to execute the business logic function. 
The table below summarizes the pros and cons of different approaches for the sidecar application.

WasmEdge is a leading cloud-native WebAssembly runtime hosted by the CNCF (Cloud Native Computing Foundation) / Linux Foundation. It is the fastest WebAssembly runtime in the market today. WasmEdge supports all standard WebAssembly extensions as well as proprietary extensions for Tensorflow inference, KV store, and image processing, etc. Its compiler toolchain supports not only WebAssembly languages such as C/C++, Rust, Swift, Kotlin, and AssemblyScript but also regular JavaScript.
A WasmEdge application can be embedded into a C program, a Go program, a Rust program, a JavaScript program, or the operating system’s CLI. The runtime can be managed by Docker tools (eg CRI-O), orchestration tools (eg K8s), serverless platforms (eg VercelNetlifyAWS LambdaTencent SCF), and data streaming frameworks (eg YoMo and Zenoh).
In this article, I will demonstrate how to use WasmEdge as a sidecar application runtime for Dapr. 
First you need to install GoRustDaprWasmEdge, and the rustwasmc compiler tool.
Next, fork or clone the demo application from Github. You can use this repo as your own application template. 
The demo has 3 Dapr sidecar applications. 
Figure 2. Dapr sidecar microservices in the demo application.
You can follow the instructions in the README to start the sidecar services. Here are commands to build the WebAssembly functions and start the 3 sidecar services.
Finally, you should be able to see the web UI in your browser.

Figure 3. The demo application in action. 
We have two functions written in Rust and compiled into WebAssembly. They are deployed in the sidecar microservices to perform the actual work of image processing and classification. 
While our example WebAssembly functions are written in Rust, you can compile functions written in C/C++, Swift, Kotlin, and AssemblyScript to WebAssembly. WasmEdge also provides support for functions written in JavaScript and DSLs.
The grayscale function is a Rust program that reads image data from STDIN and writes the grayscale image into STDOUT.
We use rustwasmc to build it and then copy it to the image-api-rs sidecar.
The classify function is a Rust function that takes a byte array for image data as input and returns a string for the classification. It uses the WasmEdge TensorFlow API.
We use rustwasmc to build it and then copy it to the image-api-go sidecar.
In the next three sections, we will look into those three sidecar services.
The image-api-rs sidecar application is written in Rust. It should already have the WebAssembly function lib/grayscale.wasm installed from the previous step. Please refer to the functions/bin/install.sh script to install the WasmEdge Runtime binary lib/wasmedge-tensorflow-lite and its dependencies.
The sidecar microservice runs a Tokio-based event loop that listens for incoming HTTP requests at the path /api/image
Once it receives an image file in the HTTP POST request, it invokes a WebAssembly function in WasmEdge to perform the image processing task. It creates a WasmEdge instance to interact with the WebAssembly program. 
The following Dapr CLI command starts the microservice in the Dapr runtime environment.
The image-api-go sidecar application is written in Go. It should already have the WebAssembly function lib/classify_bg.wasm installed from the previous step. Please refer to the functions/bin/install.sh script to install the WasmEdge Runtime Go SDK.
The sidecar microservice runs an event loop that listens for incoming HTTP requests at the path /api/image
 
Once it receives an image file in the HTTP POST request, it invokes a WebAssembly function in WasmEdge to perform the Tensorflow-based image classification task. It utilizes the Go API for WasmEdge to interact with the WebAssembly program. 
The following Dapr CLI command starts the microservice in the Dapr runtime environment.
The web UI service web-port is a simple web server written in Go. It serves static HTML and JavaScript files from the static folder and sends images uploaded to /api/hello to the grayscale or classify sidecars’ /api/image endpoints.
The JavaScript in page.js simply uploads images to the web-port sidecar’s /api/hello endpoint and the web-port will request the classify or grayscale microservice based on the request header api
The following Dapr CLI command starts the web service for the static UI files.
That's it. You now have a three part distributed application written in two languages!
As we have demonstrated, there is a lot of synergy between Dapr’s distributed network runtime and WasmEdge’s universal language runtime. This approach can be generalized and applied to other service mesh or distributed application frameworks. Unlike Dapr, many service meshes can only operate in Kubernetes as their control plane and hence are dependent on the Kubernetes API. WasmEdge is a Kubernetes compatible runtime and could play an important role as a lightweight container alternative to run microservices. Stay tuned!
Dr. Michael Yuan is the author of five books on software engineering. His latest book,Building Blockchain Apps, was published by Addison-Wesley in Dec 2019. Dr. Yuan is the co-founder of Second State, a startup that brings WebAssembly and Rust technologies to cloud, blockchain, and AI applications. Second State enables developers to deploy fast, safe, portable, and serverless Rust functions on Node.js. Stay in touch by subscribing to the WebAssembly.Today newsletter.

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.
You need to Register an InfoQ account or or login to post comments. But there’s so much more behind being registered.
Get the most out of the InfoQ experience.
Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.
Focus on the topics that matter in software development right now.
Deep-dive with 64+ world-class software leaders. Discover how they are applying emerging trends. Learn their use cases and best practices.
Stay ahead of the adoption curve and shape your roadmap with QCon Plus online software development conference.
InfoQ.com and all content copyright © 2006-2021 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we’ve ever worked with.
Privacy Notice, Terms And Conditions, Cookie Policy

source