SlideShare a Scribd company logo
1 of 19
Download to read offline
WebAssembly and Web
Awareness With NGINX Unit
Dave McAllister
2
WebAssembly (abbreviated Wasm) is a binary
instruction format for a stack-based virtual
machine.
Wasm is designed as a portable compilation
target for programming languages, enabling
deployment on the web for client and server
applications.
• orders of magnitude smaller than container images
• entirely portable across OS and CPU architectures
• with incredibly low startup latency
• capable of running tens of thousands of requests per second
• capable of running anywhere
• from tiny devices such as Raspberry Pis, in Docker
Desktop, Kubernetes, your desktop or cloud
00 Section Header
What is WebAssembly https://webassembly.org/
🤔
3
What is a Stack-Based-Virtual Machine
Set of Instruction-Types
Instructions: The What and
How
The Program
4
WAT
Rust
C
C++
Go
Python
TypeScript
…
00 Section Header
What can be compiled to
WebAssembly?
Who is using it now
WebApps
Decoupling FTW! Encourages
choice
Fosters
innovation
Separates
concerns
Frontend
Flash
AJAX
JQuery
Front-end frameworks
WebAssembly
Backend
CGI + POSIX
LAMP stack
Web-native languages
Backend frameworks
Cloud-native languages
What developers need
Client Server
POST /api/calculator/v1/add HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: api.example.com
{
"operands": [
0.1,
0.2
]
}
HTTP/1.1 200 OK
Content-Length: 32
Content-Type: application/json
Date: Sat, 02 Sep 2023 15:48:31 GMT
{
"result": 0.30000000000000004
}
What developers need
Client Server
POST /api/calculator/v1/add HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: api.example.com
{
"operands": [
0.1,
0.2
]
}
HTTP/1.1 200 OK
Content-Length: 32
Content-Type: application/json
Date: Sat, 02 Sep 2023 15:48:31 GMT
{
"result": 0.30000000000000004
}
require "json"
app = Proc.new do |env|
result = nil
body = JSON.parse(env["rack.input"].read)
body["operands"].each { |operand|
result.nil? ? result = operand : result += operand
}
["200", {
"Content-Type" => "application/json; charset=utf-8",
}, [JSON.pretty_generate({'result' => result})]]
end;
run app
WebAssembly is a slam dunk for executing web
applicationsClient Server
POST /api/calculator/v1/add HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: api.example.com
{
"operands": [
0.1,
0.2
]
}
HTTP/1.1 200 OK
Content-Length: 32
Content-Type: application/json
Date: Sat, 02 Sep 2023 15:48:31 GMT
{
"result": 0.30000000000000004
}
BYTE
STREAM
B
S
How to build a WebAssembly web app server?
Server
Client
Network
layer
TLS layer
HTTP
layer
Request
routing
Code execution
🤔
Separating concerns FTW
Server
Client
Network
layer
TLS layer
HTTP
layer
Request
routing
Code execution
MAGIC
🙌
Introducing NGINX Unit
Server
Client
Network
layer
TLS layer
HTTP
layer
Request
routing
unitd: router unitd: app
NGINX Unit
HTTP context passed
over
shared memory
13
00 Section Header
WebAssembly on NGINX Unit
unitd: app
NGINX Unit
unitd: router
shared memory
HTTP context HTTP context
Wasmtime
.wasm module
Initialize & communicate over
linear memory
14
Linear Memory in a Nutshell for non trivial data types
00 Section Header
Host Wasm-Module
Init
Act
Response
Requesting allocation of Memory for X bytes!
Returning a pointer to the begin of the Memory Page
Put rawBytes in Linear Memory
Call the exported Module Function (e.g. http_request_handler)
Returning a Pointer to the Memory Page with the Response
data and its length
Free
Finished Processing. Free Linear memory
🎮
Control over
its’ linear memory
15
NGINX Unit System Architecture
What developers need
Client Server
POST /api/calculator/v1/add HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: api.example.com
{
"operands": [
0.1,
0.2
]
}
HTTP/1.1 200 OK
Content-Length: 32
Content-Type: application/json
Date: Sat, 02 Sep 2023 15:48:31 GMT
{
"result": 0.30000000000000004
}
require "json"
app = Proc.new do |env|
result = nil
body = JSON.parse(env["rack.input"].read)
body["operands"].each { |operand|
result.nil? ? result = operand : result += operand
}
["200", {
"Content-Type" => "application/json; charset=utf-8",
}, [JSON.pretty_generate({'result' => result})]]
end;
run app
What developers need (unit-wasm edition)
Client Server
GET /hello HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Host: www.example.com
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 14
Server: Unit/1.31.0
Date: Tue, 05 Sep 2023 21:57:03 GMT
Hello, world!
use unit_wasm::rusty::*;
#[no_mangle]
pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32
{
let ctx = &mut UWR_CTX_INITIALIZER();
let mut request_buf: *mut u8 = std::ptr::null_mut();
uwr_init_ctx(ctx, addr, 4096);
uwr_set_req_buf(ctx, &mut request_buf, LUW_SRB_ALLOC);
uwr_write_str!(ctx, "Hello, world!n");
uwr_http_init_headers(ctx, 2, 0);
uwr_http_add_header_content_type(ctx, "text/plain");
uwr_http_add_header_content_len(ctx);
uwr_http_send_headers(ctx);
uwr_http_send_response(ctx);
uwr_http_response_end();
uwr_free(request_buf);
return 0;
}
Developer experience is king, but…
This only works if we have universal portability
WASI-HTTP must ensure separation of concerns
The innovation will follow
This is just the beginning
🤓
fin
• unit.nginx.org
• github.com/nginx/unit
• github.com/nginx/unit-wasm
NGINX Unit
• Linkedin : in/davemc
• 𝕏 @dwmcallister
Dave McAllister
NGINX Unit
On GitHub

More Related Content

Similar to OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Unit by Dave McAllister

The Lies We Tell Our Code (#seascale 2015 04-22)
The Lies We Tell Our Code (#seascale 2015 04-22)The Lies We Tell Our Code (#seascale 2015 04-22)
The Lies We Tell Our Code (#seascale 2015 04-22)Casey Bisson
 
Vert.x keynote for EclipseCon 2013
Vert.x keynote for EclipseCon 2013Vert.x keynote for EclipseCon 2013
Vert.x keynote for EclipseCon 2013timfox111
 
Web Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris UriarteWeb Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris Uriartewebhostingguy
 
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWS
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWSArquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWS
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWSAmazon Web Services LATAM
 
DevOps and Hybrid Applications: What You Need to Know
DevOps and Hybrid Applications: What You Need to KnowDevOps and Hybrid Applications: What You Need to Know
DevOps and Hybrid Applications: What You Need to KnowDevOps.com
 
Web scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelWeb scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelpurpleocean
 
Com day how to bring windows azure portal to your datacenter
Com day   how to bring windows azure portal to your datacenterCom day   how to bring windows azure portal to your datacenter
Com day how to bring windows azure portal to your datacenterChristopher Keyaert
 
Scalable Web Architectures and Infrastructure
Scalable Web Architectures and InfrastructureScalable Web Architectures and Infrastructure
Scalable Web Architectures and Infrastructuregeorge.james
 
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver VMworld
 
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)QAware GmbH
 
.NET Cloud-Native Bootcamp- Los Angeles
.NET Cloud-Native Bootcamp- Los Angeles.NET Cloud-Native Bootcamp- Los Angeles
.NET Cloud-Native Bootcamp- Los AngelesVMware Tanzu
 
Web server hardware and software
Web server hardware and softwareWeb server hardware and software
Web server hardware and softwareVikram g b
 

Similar to OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Unit by Dave McAllister (20)

The Lies We Tell Our Code (#seascale 2015 04-22)
The Lies We Tell Our Code (#seascale 2015 04-22)The Lies We Tell Our Code (#seascale 2015 04-22)
The Lies We Tell Our Code (#seascale 2015 04-22)
 
Vert.x keynote for EclipseCon 2013
Vert.x keynote for EclipseCon 2013Vert.x keynote for EclipseCon 2013
Vert.x keynote for EclipseCon 2013
 
Web Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris UriarteWeb Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris Uriarte
 
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWS
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWSArquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWS
Arquitetura Hibrida - Integrando seu Data Center com a Nuvem da AWS
 
Apache web service
Apache web serviceApache web service
Apache web service
 
About netcore2
About netcore2About netcore2
About netcore2
 
DevOps and Hybrid Applications: What You Need to Know
DevOps and Hybrid Applications: What You Need to KnowDevOps and Hybrid Applications: What You Need to Know
DevOps and Hybrid Applications: What You Need to Know
 
Node js
Node jsNode js
Node js
 
Web scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelWeb scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannel
 
Com day how to bring windows azure portal to your datacenter
Com day   how to bring windows azure portal to your datacenterCom day   how to bring windows azure portal to your datacenter
Com day how to bring windows azure portal to your datacenter
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Scalable Web Architectures and Infrastructure
Scalable Web Architectures and InfrastructureScalable Web Architectures and Infrastructure
Scalable Web Architectures and Infrastructure
 
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver
VMworld 2013: How to Replace Websphere Application Server (WAS) with TCserver
 
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
 
.NET Cloud-Native Bootcamp- Los Angeles
.NET Cloud-Native Bootcamp- Los Angeles.NET Cloud-Native Bootcamp- Los Angeles
.NET Cloud-Native Bootcamp- Los Angeles
 
Web server hardware and software
Web server hardware and softwareWeb server hardware and software
Web server hardware and software
 
Ecom 1
Ecom 1Ecom 1
Ecom 1
 
Where should I run my code? Serverless, Containers, Virtual Machines and more
Where should I run my code? Serverless, Containers, Virtual Machines and moreWhere should I run my code? Serverless, Containers, Virtual Machines and more
Where should I run my code? Serverless, Containers, Virtual Machines and more
 
Webdevelopment
WebdevelopmentWebdevelopment
Webdevelopment
 
ansible_rhel_90.pdf
ansible_rhel_90.pdfansible_rhel_90.pdf
ansible_rhel_90.pdf
 

Recently uploaded

Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. Mumbai
Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. MumbaiCall Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. Mumbai
Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. MumbaiPriya Reddy
 
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityUnlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityHung Le
 
Ready Set Go Children Sermon about Mark 16:15-20
Ready Set Go Children Sermon about Mark 16:15-20Ready Set Go Children Sermon about Mark 16:15-20
Ready Set Go Children Sermon about Mark 16:15-20rejz122017
 
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfMahamudul Hasan
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatmentnswingard
 
Zone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxZone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxlionnarsimharajumjf
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoKayode Fayemi
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...David Celestin
 
Introduction to Artificial intelligence.
Introduction to Artificial intelligence.Introduction to Artificial intelligence.
Introduction to Artificial intelligence.thamaeteboho94
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Baileyhlharris
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lodhisaajjda
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
Lions New Portal from Narsimha Raju Dichpally 320D.pptx
Lions New Portal from Narsimha Raju Dichpally 320D.pptxLions New Portal from Narsimha Raju Dichpally 320D.pptx
Lions New Portal from Narsimha Raju Dichpally 320D.pptxlionnarsimharajumjf
 
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORNLITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORNtntlai16
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalFabian de Rijk
 
History of Morena Moshoeshoe birth death
History of Morena Moshoeshoe birth deathHistory of Morena Moshoeshoe birth death
History of Morena Moshoeshoe birth deathphntsoaki
 
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINES
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINESBIG DEVELOPMENTS IN LESOTHO(DAMS & MINES
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINESfuthumetsaneliswa
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIINhPhngng3
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfSkillCertProExams
 
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptx
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptxBEAUTIFUL PLACES TO VISIT IN LESOTHO.pptx
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptxthusosetemere
 

Recently uploaded (20)

Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. Mumbai
Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. MumbaiCall Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. Mumbai
Call Girls Near The Byke Suraj Plaza Mumbai »¡¡ 07506202331¡¡« R.K. Mumbai
 
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityUnlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
 
Ready Set Go Children Sermon about Mark 16:15-20
Ready Set Go Children Sermon about Mark 16:15-20Ready Set Go Children Sermon about Mark 16:15-20
Ready Set Go Children Sermon about Mark 16:15-20
 
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 
Zone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxZone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptx
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
 
Introduction to Artificial intelligence.
Introduction to Artificial intelligence.Introduction to Artificial intelligence.
Introduction to Artificial intelligence.
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Lions New Portal from Narsimha Raju Dichpally 320D.pptx
Lions New Portal from Narsimha Raju Dichpally 320D.pptxLions New Portal from Narsimha Raju Dichpally 320D.pptx
Lions New Portal from Narsimha Raju Dichpally 320D.pptx
 
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORNLITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of Drupal
 
History of Morena Moshoeshoe birth death
History of Morena Moshoeshoe birth deathHistory of Morena Moshoeshoe birth death
History of Morena Moshoeshoe birth death
 
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINES
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINESBIG DEVELOPMENTS IN LESOTHO(DAMS & MINES
BIG DEVELOPMENTS IN LESOTHO(DAMS & MINES
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptx
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptxBEAUTIFUL PLACES TO VISIT IN LESOTHO.pptx
BEAUTIFUL PLACES TO VISIT IN LESOTHO.pptx
 

OSMC 2023 | IGNITE: Serving Server-Side WASM with Web Awareness with NGINX Unit by Dave McAllister

  • 1. WebAssembly and Web Awareness With NGINX Unit Dave McAllister
  • 2. 2 WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications. • orders of magnitude smaller than container images • entirely portable across OS and CPU architectures • with incredibly low startup latency • capable of running tens of thousands of requests per second • capable of running anywhere • from tiny devices such as Raspberry Pis, in Docker Desktop, Kubernetes, your desktop or cloud 00 Section Header What is WebAssembly https://webassembly.org/ 🤔
  • 3. 3 What is a Stack-Based-Virtual Machine Set of Instruction-Types Instructions: The What and How The Program
  • 5. Who is using it now
  • 6. WebApps Decoupling FTW! Encourages choice Fosters innovation Separates concerns Frontend Flash AJAX JQuery Front-end frameworks WebAssembly Backend CGI + POSIX LAMP stack Web-native languages Backend frameworks Cloud-native languages
  • 7. What developers need Client Server POST /api/calculator/v1/add HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 24 Content-Type: application/json Host: api.example.com { "operands": [ 0.1, 0.2 ] } HTTP/1.1 200 OK Content-Length: 32 Content-Type: application/json Date: Sat, 02 Sep 2023 15:48:31 GMT { "result": 0.30000000000000004 }
  • 8. What developers need Client Server POST /api/calculator/v1/add HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 24 Content-Type: application/json Host: api.example.com { "operands": [ 0.1, 0.2 ] } HTTP/1.1 200 OK Content-Length: 32 Content-Type: application/json Date: Sat, 02 Sep 2023 15:48:31 GMT { "result": 0.30000000000000004 } require "json" app = Proc.new do |env| result = nil body = JSON.parse(env["rack.input"].read) body["operands"].each { |operand| result.nil? ? result = operand : result += operand } ["200", { "Content-Type" => "application/json; charset=utf-8", }, [JSON.pretty_generate({'result' => result})]] end; run app
  • 9. WebAssembly is a slam dunk for executing web applicationsClient Server POST /api/calculator/v1/add HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 24 Content-Type: application/json Host: api.example.com { "operands": [ 0.1, 0.2 ] } HTTP/1.1 200 OK Content-Length: 32 Content-Type: application/json Date: Sat, 02 Sep 2023 15:48:31 GMT { "result": 0.30000000000000004 } BYTE STREAM B S
  • 10. How to build a WebAssembly web app server? Server Client Network layer TLS layer HTTP layer Request routing Code execution 🤔
  • 11. Separating concerns FTW Server Client Network layer TLS layer HTTP layer Request routing Code execution MAGIC 🙌
  • 12. Introducing NGINX Unit Server Client Network layer TLS layer HTTP layer Request routing unitd: router unitd: app NGINX Unit HTTP context passed over shared memory
  • 13. 13 00 Section Header WebAssembly on NGINX Unit unitd: app NGINX Unit unitd: router shared memory HTTP context HTTP context Wasmtime .wasm module Initialize & communicate over linear memory
  • 14. 14 Linear Memory in a Nutshell for non trivial data types 00 Section Header Host Wasm-Module Init Act Response Requesting allocation of Memory for X bytes! Returning a pointer to the begin of the Memory Page Put rawBytes in Linear Memory Call the exported Module Function (e.g. http_request_handler) Returning a Pointer to the Memory Page with the Response data and its length Free Finished Processing. Free Linear memory 🎮 Control over its’ linear memory
  • 15. 15 NGINX Unit System Architecture
  • 16. What developers need Client Server POST /api/calculator/v1/add HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 24 Content-Type: application/json Host: api.example.com { "operands": [ 0.1, 0.2 ] } HTTP/1.1 200 OK Content-Length: 32 Content-Type: application/json Date: Sat, 02 Sep 2023 15:48:31 GMT { "result": 0.30000000000000004 } require "json" app = Proc.new do |env| result = nil body = JSON.parse(env["rack.input"].read) body["operands"].each { |operand| result.nil? ? result = operand : result += operand } ["200", { "Content-Type" => "application/json; charset=utf-8", }, [JSON.pretty_generate({'result' => result})]] end; run app
  • 17. What developers need (unit-wasm edition) Client Server GET /hello HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Content-Type: application/json Host: www.example.com HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 14 Server: Unit/1.31.0 Date: Tue, 05 Sep 2023 21:57:03 GMT Hello, world! use unit_wasm::rusty::*; #[no_mangle] pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 { let ctx = &mut UWR_CTX_INITIALIZER(); let mut request_buf: *mut u8 = std::ptr::null_mut(); uwr_init_ctx(ctx, addr, 4096); uwr_set_req_buf(ctx, &mut request_buf, LUW_SRB_ALLOC); uwr_write_str!(ctx, "Hello, world!n"); uwr_http_init_headers(ctx, 2, 0); uwr_http_add_header_content_type(ctx, "text/plain"); uwr_http_add_header_content_len(ctx); uwr_http_send_headers(ctx); uwr_http_send_response(ctx); uwr_http_response_end(); uwr_free(request_buf); return 0; }
  • 18. Developer experience is king, but… This only works if we have universal portability WASI-HTTP must ensure separation of concerns The innovation will follow This is just the beginning 🤓
  • 19. fin • unit.nginx.org • github.com/nginx/unit • github.com/nginx/unit-wasm NGINX Unit • Linkedin : in/davemc • 𝕏 @dwmcallister Dave McAllister NGINX Unit On GitHub