Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Containers vs Web Assembly: Comparing Two Powerful Technologies for Building Applications

3 min read

In the world of modern application development, two popular technologies have emerged that have revolutionized the way we build and deploy applications – Containers and Web Assembly. Containers have been around for some time now, and have become the de-facto standard for deploying microservices-based architectures. On the other hand, Web Assembly is a relatively new technology that promises to bring native performance to the web. In this article, we will compare Containers and Web Assembly, highlighting their similarities and differences, and explore how they can be used together.

What are Containers?

Containers are lightweight, standalone executables that package everything an application needs to run, including code, libraries, system tools, and runtime. Containers provide a consistent, reproducible environment that can be deployed across different platforms, making it easy to develop, test, and deploy applications at scale. Containers are typically managed by container orchestration platforms like Kubernetes, which provide automated deployment, scaling, and management of containerized applications.

What is Web Assembly?

Web Assembly is a binary format that allows developers to run native code in the browser. Unlike JavaScript, which is an interpreted language, Web Assembly is a low-level language that is designed to be compiled from other programming languages like C and C++. This makes it possible to run high-performance code in the browser, which was previously not possible. Web Assembly is supported by all major browsers, including Chrome, Firefox, Safari, and Edge.

Similarities between Containers and Web Assembly:

  • Portable:  Both Containers and Web Assembly are portable, meaning they can be run on different platforms and environments without modification.

  • Isolation:  Both Containers and Web Assembly provide isolation, meaning they run in a sandboxed environment and do not interfere with the host system.

  • Performance:  Both Containers and Web Assembly are designed for high-performance, making them ideal for building and deploying applications that require low-latency and high-throughput.

Differences between Containers and Web Assembly:

  • Purpose:  Containers are designed for deploying applications, while Web Assembly is designed for running native code in the browser.

  • Resource Utilization:  Containers are more resource-intensive than Web Assembly, as they need to spin up a new operating system instance for each container. Web Assembly, on the other hand, runs directly in the browser, without the need for a separate runtime environment.

  • Security:  Containers provide a more secure environment than Web Assembly, as they are sandboxed at the operating system level. Web Assembly is sandboxed at the browser level, which may not provide the same level of security as an operating system-level sandbox.

Using Containers and Web Assembly Together:

While Containers and Web Assembly serve different purposes, they can be used together to build powerful applications. For example, you can use Containers to deploy a microservices-based architecture, and use Web Assembly to run high-performance code in the browser, like computer vision algorithms or machine learning models. This allows you to build applications that leverage the strengths of both technologies, providing a seamless and performant experience for end-users.

Let’s take a look at some code examples to see how Containers and Web Assembly can be used together.

Deploying a Containerized Application with a Web Assembly Frontend

In this example, we will deploy a containerized application using Kubernetes, and use Web Assembly to build a performant frontend.

First, we will create a Dockerfile for our backend application:

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]
 

This Dockerfile uses the official Node.js image, installs the required dependencies, and copies the application code into the container. The CMD instruction specifies the command to run the application.

Next, we will create a Kubernetes deployment file for our backend application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
        - name: backend
          image: <backend_image>
          ports:
            - containerPort: 8080
 

This deployment file creates a single replica of our backend application, using the Docker image we built in the previous step. It also exposes port 8080 for incoming traffic.

Now, let’s build a Web Assembly frontend for our application. We will use Rust to write our frontend code, and the wasm-pack tool to build the Web Assembly binary.

First, we will create a new Rust project:

$ cargo new frontend
 

Next, we will add the required dependencies to our Cargo.toml file:

[package]
name = "frontend"
version = "0.1.0"
edition = "2018"

[dependencies]
wasm-bindgen = "0.2"
 

The wasm-bindgen dependency provides the tools we need to interface with JavaScript from our Rust code.

Next, we will write some Rust code to create a simple “Hello, World!” Web Assembly module:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn hello() -> String {
    "Hello, World!".to_string()
}
 

This code defines a hello function that returns the string “Hello, World!”. The #[wasm_bindgen] macro tells Rust to generate JavaScript bindings for this function.

Now, let’s build our Web Assembly binary:

$ wasm-pack build --target web --out-name wasm --out-dir ./dist
 

This command uses the wasm-pack tool to build our Web Assembly binary, targeting the web platform. The –out-name and –out-dir flags specify the output filename and directory.

Finally, we will create a simple HTML file that loads our Web Assembly module and calls the hello function:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Web Assembly Example</title>
</head>
<body>
    <script type="module">
        import init, { hello } from './dist/wasm.js';
        init().then(() => {
            console.log(hello());
        });
    </script>
</body>
</html>

This HTML file uses JavaScript to load our Web Assembly module and call the hello function. The init function is a JavaScript function generated by the wasm-pack tool that initializes the Web Assembly module.

Conclusion

Containers and Web Assembly are two powerful technologies that can be used together to build performant and scalable applications. While Containers are designed for deploying applications, and Web Assembly is designed for running native code in the browser, they can be used together to provide a seamless and performant experience for end-users. By leveraging the strengths of both technologies, developers can build applications that are faster, more reliable, and more secure than ever before.

 

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index