Ur/Web: A Simple Model for Programming the Web Adam Chlipala MIT CSAIL POPL 2015 January 15, 2015
Ur / Web Ur A new general-purpose typed functional language λ Web Tools for implementing modern three-tier web applications Motto: the compiler understands the structure of your web app. Focuses on: Programmer productivity Security Performance (especially server-side, for scaling) 2
There are at least several users whom I have never met! The first commercial Ur/Web application: BazQux Reader, by Vladimir Shabanov Feed reader with comments http://www.bazqux.com/ On the order of 1000 paying users daily 3
TechEmpower Web Framework Benchmarks <http://www.techempower.com/benchmarks/> 3rd-party benchmarking initiative 93 frameworks in test to the right Ur/Web's performance: 4th best throughput (~100 krps) Best latency (2.1 ms) At the top of the chart in the preview results for the next round 4
This is reasonably real. You can download it and build some nifty Web apps. Open source at: http://www.impredicative.com/ur/ Bonus talk content: 1) Background: a general model for programming distributed applications 2) Background: the mess that is today's mainstream Web development techniques 3) Ur/Web's simpler model for programming Web apps, with 2 big ideas: a) Encapsulation that crosses nodes of a distributed system b) A simple concurrency model for distributed systems 4) Code demo 5
Distributed Systems (Hard) Node Message Message (look of mistrust) Message Node Message Node Message Node Message Node 6
Why Distributed Systems? Physics forces distribution in space to scale performance. Mutually distrusting users controlling distinct resources Challenges: Mutable state Concurrency Security 7
The Restricted Model of Many Web Apps Simple communication topology: only edges are between server and clients Server Most optimization work focused on server, especially for scaling Client Thin client : most client Centralized security: server state is ephemeral. under tight control; clients can do whatever they want. We don't care if troublemakers corrupt their own state, and server state is behind an API. Client... Client flashy UI flashy UI flashy UI 8
What Web programmers expect today Web Server SQL Database HTTP, HTML, URLs HTTP, HTTP, URLs XML/ JSON HTTP, XML/JSON Browser All represented as normal strings in most frameworks! Hello, code injection attacks! Browser Browser... Browser AJAX! JavaScript The DOM (a big old mutable global variable, modified by looking up nodes by string ID) Comet (long polling) or WebSockets 9
The Ur/Web model An application is written entirely in Ur/Web source code, exposing this view: Typed message-passing channels stored in DB and passed to clients. Strongly typed SQL syntax tree Web Server Database write Browser... List of records of native Ur values Key point: semantics says First-class Strongly typed First-class Native Ur Key value point 1: threads are spawned requests are contains run one at function a time to Native Ur value in Ur/Web code and themselves document (the tree standard transaction function to run on server run Ur/Web code. abstraction). run on server Browser Browser (marked as AJAXstyle) read Thread Thread... Thread Key point 2: client-side threading Document is cooperative, with thread... Mutable switches cells only at well-defined points (a natural choice in function for rendering based on contents JavaScript). (functional-reactive GUI style) 10
Big Language Design Idea #1: Encapsulation What should we encapsulate? Server Module encapsulating bits of state across the system [On server:] Database tables RPC endpoints Message-passing channels [On client:] Subtrees of a client-side page Cookies (persistent store) Client Client... Client flashy UI flashy UI flashy UI 11
Threads Big Language Design Idea #2: Concurrency Server may also send Later, server runs it messages RPC goes to other into clients, a queue atomically and places and all deliveries on the server. re: the result back on client. same RPC appear to happen atomically. Semantics A thread ends is that, its cooperative any time, slice just with one certain thread actions, is running like across an RPC the to whole the server. system! Client f(a) f(a) Server Big Idea: Semantics presents the illusion that just one thread is running at a time, across the whole system! c User interactions may trigger creation of new threads. Client... Client b flashy UI flashy UI flashy UI 12
Demo time! 13
λ Open source at: http://www.impredicative.com/ur/ 14