Launch HN: Lunatic (YC W21) – An Erlang Inspired WebAssembly Platform
The two of us met in high school, studied computer science together, but then went separate ways working as backend engineers. Bernard worked at CERN and Hrvoje co-founded Amodo, an insurance tech startup. Bernard, being a huge fan of Erlang/Elixir, started working on a similar open-source runtime for WebAssembly which he called Lunatic (https://github.com/lunatic-solutions/lunatic).
Lunatic runs Wasm modules as lightweight processes with its own heap/stack and preemptively schedules them on a multi-threaded executor. You can spawn those processes using a library we provide (currently for Rust and AssemblyScript) to enable actor-based architectures with message passing. Scheduling is implemented by modifying a Wasm module and adding “reduction counts” (similar to Erlang). You can write seemingly blocking code but it won’t actually block the underlying OS thread as our implementation of WebAssembly System Interface (WASI, think of it as POSIX syscalls) will be implemented with async Rust and a bit of magic [0] :) The code is JIT’ed and we build on top of existing Wasm runtimes Wasmtime/Wasmer for this part (codegen is done by LLVM or Cranelift).
To step back from technical details, working on Lunatic for the past few months, we have started to form a bigger picture about server-side applications. Over the years we have witnessed many trends: Docker & containers became popular, asynchronous programming and green threads are ubiquitous for IO intense work, polyglot codebases are always present, microservice architecture became popular, and distributed is being used both for scale and to bring computation closer to clients to lower latency. Two important driving forces are hardware capabilities and how we develop software. Those have changed dramatically from the time operating systems were created.
For example, to maximize resource usage of a single machine, we started running virtual machines and then moved to more lightweight containers, both to give isolation and sandboxing to different applications. Serverless is pushing this even further. Lunatic builds on top of WebAssembly security principles [1] to give sandboxing and isolation to enable even more lightweight environments.
Servers also needed to handle more and more network connections and spawning an OS thread per connection became problematic so developers used different programming patterns, async implementations, or user-space threads/processes to tackle this problem. Lunatic solves this by using Erlang’s proven approach [2].
How we develop software has also changed. Today most of our application consists of third-party libraries and it’s common to have hundreds or even thousands of dependencies and obviously it’s impossible to audit them all. When you compile and run your application, the whole code has the same privileges, so a malicious dependency could easily steal your private keys.[3,4,5] WebAssembly is trying to standardise ideas like Interface Types and dynamic linking between Wasm modules. We could isolate libraries into different modules based on capabilities they require (which “system calls” they use) and let developers decide which parts of their app have what capabilities.
Other use-cases that Lunatic and Wasm enable are plugin architecture to run third-party code, sharing code between frontend and backend, polyglot codebases that use Wasm interface types to call each other functions, etc....
39 comments
[ 6.1 ms ] story [ 98.9 ms ] threadBy the way, if it sounds vague, that's important info for us, we have to improve that!
My takeaway after building that is that the build tooling is the major pain point. If you're trying to onboard someone onto this platform from their favorite language the hard part is getting from code to the .wasm file. wasm-bindgen (as an example) has put so much effort into build tooling, I wonder if that's a necessary path for success here. (edit: this also might be out of date, maybe wasi solves a lot of this)
It's also great that WASI exists now, if I had to do embly again I'd just use wasi and then implement all of my "platform" features as filesystem features, not syscalls. If your API interface is the filesystem then you could provide interoperability between environments. Let's say you want to include a key-value store in the wasi runtime, you just make the keys files and the values file contents. Then you could so something like ship a FUSE filesystem to interact with the filesystem in the same way from a traditional VM or on a personal computer. I got really bogged down in custom syscalls and this path seems potentially more elegant.
Have you also thought about live process migration? I got really excited about this from a technical standpoint. Since you completely control the runtime you could set up a clustered wasm solution that moves long running processes from VM to VM by sending their live memory state to another machine. Not sure if that's actually useful, but cool that it's not bogged down by the usual complexities of doing the same in a full OS environment.
Anyway, so glad to see this. Please make a cloud platform and let me pay for it. Also happy to chat more if any of this is useful, I've joined your discord as "max".
wasm-bindgen is necessary only because it’s really hard right now to merge the wasm world and the JS one in the browser. We have the advantage here of staying out of the browser.
Also it'd be fantastic to be able to compile Nim as well. Maybe a weekend project. :-)
We have been thinking about live process migration. I’m planning to do a demo where we run a lot of board games (e.g. chess) on authoritative servers using Lunatic. Then we give a developer ability to move a live game to a different machine without players noticing. We are still working out the details, but that area also excites us!
They had lots of desyncs at first, but nowadays Factorio is fantastically stable in this respect. One of the tools they developed for themselves to achieve this (and the reason why I'm bringing up Factorio here) is called "Heavy Mode" [2], which is a game option that saves & reloads the full game state on every tick (the game targets 60 ticks/s), and compares the before/after states for inconsistencies.
Perhaps a similar "Heavy Mode" for Wasm live migrations could aid in hardening such a feature. E.g. spawn two instances of the same process on different hosts, and migrate both to new hosts every, say, 100k instructions, comparing a hash of their serialized representations at each migration.
[1]: https://wiki.factorio.com/Desynchronization
[2]: https://www.factorio.com/blog/post/fff-63
https://news.ycombinator.com/item?id=25160474
https://news.ycombinator.com/item?id=25253070
https://getlumen.org/ https://github.com/lumen/lumen
1) lightweight isolated computation - VMs, containers and the lot.
2) asynchronous io - user space green threads, epoll, event loops and the likes to have single OS process handle 1000s of connections
3) distributed - scale up compute + have it run close to user. Cloudflare workers, edge@lambda, fly.io. They’re all moving to this direction.
4) polyglot micro services - rather than one big thing written in a single language, many small things that talk to each other via HTTP & grpc. They can be independently changed and scaled up without having to restart the whole system.
Erlang + webassembly are a great fit for this paradigm.
Seeing the success of fly.io, Cloudflare workers and other cloud providers, this definitely has legs and I would love to try it out.
Sounds good, as long as we can have structural sharing between processes on the same CPU, for performance.
Programming is an infuriatingly obtuse and, well, stupid discipline.
It's probably early, but how are you planning on making money? If I build on top of this, I don't want the rug to be pulled out from under me.
I'm not for or against any of them. I'm just wondering what criteria there was in choosing those two rather than any others, since I only have a cursory overview of the different WASM runtimes.
We’ve created a crate “uptown_funk” which enables us to write Rust code which easily integrates with both Wasmer and Wasmtime. It was very helpful in implementing WASI and it takes a little bit different approach than Wasmer and Wasmtime. Regarding, Wasmer vs. Wasmtime, we don’t have a preference yet, both are great!
Sounds completely unsafe to use without.
I saw a bunch of stuff on SSVM and their impressive WASM VM performance [0]. Wondering if this can be of any benefit to Lunatic?
Furthermore, would be really interested in seeing getting started guides / examples / maybe even some tutorial articles on an example or two. This could really help adoption if thats the goal.
[0] https://www.secondstate.io/articles/ssvm-performance/
"How can Lunatic help with security? WebAssebmly" ...
Looks super cool, I believe it would be cool to make one app which works as a CLI, front-end web client, and back-end server, just by passing different environment vars... can this accomplish that?