Wasm Builders 🧱

Cover image for WebAssembly Beyond the WebBrowser: Serverless WebAssembly
Deepanshu Arora
Deepanshu Arora

Posted on • Updated on

WebAssembly Beyond the WebBrowser: Serverless WebAssembly

Hey Everyone,

In My Last Post, I gave an 101 Introduction To WebAssembly,but if you noticed, I only talked about WebAssembly in context of the Web.

Today, I'll dive into the whole new realm of using WebAssembly outside of the Web Browser.

So, Let's begin

Image description

Let us have a Recap first:

So, What is WebAssembly?
Browsers, as of now, only have support for rendering JavaScript and we were constrained to using JavaScript as the de-facto.

Then, in 2017 something phenomenal came into picture which is
WebAssembly. At a very superficial level, you can think of
WebAssembly as an another language which can be executed inside our Web Browser.

Now, Let's jump onto our Main topic of the day, which is
Making Use of WebAssembly outside of Web Browsers

Let us try to understand it with the help of an Example:

Let us Assume that we want to build an Application X and the main purpose of this Application is to perform some sort Of Data Analysis.

Now, Let's have a look at it's architecture:

Architecture of the Tool

For the UI/FrontEnd Part, we are making use of JavaScript and the Analysis Part in the Backend is done using a Library in C Language.

Now, This approach of constructing our application works but there is an issue. Every time when you want to perform analysis on some of your data, you will have to manually push your data to the Server, which consumes a lot of time and also does heavy processing at the Backend/(Server Side would be a more relevant technical term for this)

Why, not switch to some another approach where instead of relying on any external infrastructure services for our server, we can do it in our browser itself.

Optimal Architecture -2

For this approach, let's assume that we know nothing about WebAssembly.
Now under this approach, working on the Analysis Part becomes a pain in the butt since now we will have to entirely rewrite our C Library and convert it into JavaScript Code so that our Web Browser can interpret it.

Pros and Cons Of Approach2

Now, this is where our Saviour WebAssembly comes into Picture

Approach-3

This is now where we can save on our time, and instead of rewriting our Whole C Library and then transitioning it into JavaScript Code, we can directly compile that C Code into WebAssembly which in turn then can be interpreted by the Browser.

Pros and Cons of Approach 3

Now, the advantage over here is that now you have a much faster interface and you can reuse existing code and essentially just every time that library updates, you can compile it and then use it.

The downside though is that, anytime that you are making use of WebAssembly, there will be some nuance of complexity that will get introduced to your Application.

Now, let's explore what it would look like to have a Serverless Architecture

Serverless WebAssembly : Approach 4

Now, under this approach, Working on the Frontend Side remains the same as previous 3 approaches, the only change here is that for the analysis part we are adopting a Serverless based approach.

Now, for implementing this Serverless approach, we can make use of Fastly

Implementing Approach Number 4

Fastly makes use of WebAssembly RunTime, meaning now we can deploy our C Library code to it, and it will get transpiled to WebAssembly binary and then thus can be interpreted.

You might also think that, Why not make use of any Traditional Cloud Service Provider like AWS, Azure or Google Cloud Platform(GCP) for deploying our code.

Well, there are 2 reasons for not using any Cloud Service Provider:

1) Initialisation Time and Compatibility issues:
First of all, if we deploy our function to any traditional Cloud based service provider, it will take a lot of time for our function to spin up and concurrently accept API Requests from Users.
Since most of the Cloud Based Service providers make use of Containers, it kind of becomes messy to use them for building out our Application.

Secondly, the WASM Runtime is currently undergoing a lot of R&D for the traditional cloud based service providers, so using them here will not make sense for our purpose.

Now, if you look at our demonstration picture as well for Approach Number 4, it only takes 35 Nano Seconds for our Function to spin up and start accepting API Requests from Users.

Thus, it is very time effective to go for this Serverless Approach.

So, as you see the Use Of WebAssembly is not limited to using it in our WebBrowser only, it extends much beyond that as I explained you in Today's post with the help of an Example.

See you in the Next Post :)

Latest comments (0)