How Shopify Uses WebAssembly Outside of the Browser

At Shopify, we’re keeping the flexibility of untrusted Partner code, but executing it on our own infrastructure with WebAssembly.

Overview

The article discusses how Shopify utilizes WebAssembly (Wasm) outside of the browser to enhance its platform's extensibility and performance. It outlines the benefits of Wasm, including security, performance, and flexibility, while detailing the architecture of Shopify's code execution service using Lucet.

What You'll Learn

1

How to execute untrusted code securely using WebAssembly

2

Why WebAssembly is suitable for performance-critical applications

3

How to leverage AssemblyScript for WebAssembly module development

Prerequisites & Requirements

  • Familiarity with WebAssembly concepts and its ecosystem
  • Basic understanding of using command line tools for development(optional)

Key Questions Answered

What is WebAssembly and how does Shopify use it?
WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine, designed for portable compilation across programming languages. Shopify uses Wasm to execute untrusted Partner code securely on its infrastructure, enhancing performance and flexibility while minimizing network latency.
What are the security features of WebAssembly?
WebAssembly executes code in a sandboxed environment, preventing malicious actions by relying on explicit imports for communication. It includes features like protected call stacks and runtime type checking to enhance security against buggy code.
How does Shopify ensure performance with WebAssembly?
Shopify leverages WebAssembly's ability to utilize common hardware capabilities, achieving near-native performance. During tests, module execution times averaged around 100 μs, with total execution times under 5 ms, demonstrating negligible performance impact.
What is Lucet and how does it relate to Shopify's WebAssembly execution?
Lucet is an open-source runtime and compiler for WebAssembly developed by Fastly. Shopify wraps Lucet within a Rust web service to manage I/O and module storage, allowing for efficient execution of untrusted code in response to web requests.
Why did Shopify choose AssemblyScript for WebAssembly module development?
Shopify selected AssemblyScript due to its TypeScript-like syntax, which aligns with the familiarity of developers in their ecosystem. This choice allows for effective compilation to WebAssembly while avoiding the performance penalties associated with dynamic languages.

Key Statistics & Figures

Lucet module execution time
around 100 μs
This represents the average time taken to execute a module in Lucet during performance tests.
Total execution time
around 4 ms
This is the total time for module execution including I/O, indicating the efficiency of the Lucet execution process.
Lucet container startup time
35 μs
This is the impressive startup time for Lucet containers, showcasing their efficiency.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Backend
Webassembly
Used for executing untrusted Partner code securely and efficiently.
Backend
Lucet
Acts as a runtime and compiler for WebAssembly modules.
Backend
Assemblyscript
A language used for developing WebAssembly modules with a familiar syntax.
Backend
Rust
Used to wrap Lucet for managing I/O and module storage.

Key Actionable Insights

1
Utilizing WebAssembly can significantly enhance the performance of applications by executing code closer to the user, reducing latency.
This is particularly important for e-commerce platforms where speed directly impacts sales. By adopting WebAssembly, developers can ensure that features are delivered quickly and efficiently.
2
Adopting a community-driven approach to technology like WebAssembly can lead to better tools and support for developers.
Engaging with the WebAssembly community allows Shopify to contribute to and benefit from ongoing improvements, ensuring that their implementation remains robust and up-to-date.
3
Using a statically compiled language like AssemblyScript can mitigate performance issues associated with dynamic languages in WebAssembly.
This approach allows developers to leverage familiar syntax while ensuring that the modules perform efficiently within the Shopify ecosystem.

Common Pitfalls

1
Relying on dynamic languages for WebAssembly can lead to performance issues and complexity in execution.
Dynamic languages like Ruby and JavaScript are not suitable for direct compilation to WebAssembly due to their runtime requirements, which can hinder performance and increase latency.

Related Concepts

Webassembly Security Models
Performance Optimization Techniques In E-commerce
Community Engagement In Open-source Projects