Trying out Unison, part 4: from the edge all the way to the cloud
The final installment of the "Trying out Unison" series. Discover how distributed programming could become far more accessible than it is today – and genuinely enjoyable to write
Earlier parts of the “Trying out Unison” series began by looking at the language’s central feature, content-addressed code, which makes a function’s identity depend on what it does rather than on its name (names serve merely as labels). Next, we investigated how this affects the way you can organize your code and handle dependencies using namespaces. Lastly, we studied Unison’s abilities, an algebraic effect system offering capabilities like error handling or dependency injection.
On their own, these characteristics already let Unison move beyond what mainstream programming languages offer today: effortless rename refactorings, no dependency hell, and the ability to restrict effectful computations without hurting readability, just to list a few. Unison takes a different path while at the same time putting the “programmer experience” first, and that makes it a fascinating thing to explore.
Unison has one more trick up its sleeve, though: Unison Cloud. It’s not open-source and can be used only as part of a closed beta, yet thanks to the features described above it could make distributed programming far more accessible than it is today – and genuinely enjoyable to write!
That’s not all: performance gains can be expected too, thanks to the model of “bring computation to the data”, in contrast to the “bring data to the computation” approach commonly found in practice.
Could this be the cloud we were promised?
Going remote
Unison’s support for distributed computing – and networked computing in general – consists of three pieces. The first is the programming interface exposed to users, found in the distributed namespace. Its central abstraction is the Remote ability, which lets you launch computations on remote nodes and wait for their results.
The second piece is a Unison Cloud client; besides setting up access to the cloud service, it offers the run function. This function handles the Remote ability by interpreting it as IO + Exception, which can then be handled locally:
run : '{Remote, Http, Atomic, Channels, Scratch} a ->{IO, Exception} a
The last piece is the Unison Cloud server implementation: time-sharing, communication, keeping track of cluster state, and so on. Both the client and the server include the serialization and networking logic – all the dull yet essential things you’d rather not have to think about. The server is what we cannot use yet – but we can get a glimpse of what’s coming.
When you write distributed systems in Unison, the most elementary operation you’ll deal with is forking a computation so that it runs on a remote node, using forkAt:
forkAt : Location g -> '{g, Remote, Exception} t ->{Remote} Task t
The function accepts a Location – an abstract description of either one server or a group of servers (a region). This is where the computation will ultimately execute.
The second argument is the computation itself. It gets transferred to the remote node with the very mechanism we’ve already encountered: whenever we fork a library via Unison Share, Unison code travels across the network. The process relies on function hashes and, more broadly, on the idea of content-addressed code.
The remote node has to get hold of the function whose hash matches whatever was passed as an argument to forkAt, together with all of its dependencies (once again, just like when forking). This can take several network round trips, since the target server may already hold the whole code graph locally, or just some of it.
The computation may itself use the Remote ability (to talk to other nodes), throw exceptions, and use whichever abilities the location supports (we’ll get to that later). All of this is captured by the '{g, Remote, Exception} t type. The ' quoting is essential here: it marks a lazily evaluated piece of code, which after desugaring is just a () ->{g, Remote, Exception} t thunk.
Notice as well that conflicts, eviction errors, or name clashes cannot occur, because everything works at the level of hashes. A single supervisor can execute code relying on different versions of the base Unison library without caring at all.
A task, but not a monad
Calling forkAt gives us a computation that uses the Remote ability and returns a Task, representing a computation in progress on some node (remote or local).
A Task may remind you of a Future or Promise from other languages. In certain respects, that’s accurate. For instance, Remote.empty! lets you create a fresh task, which you can complete later with either a value or an error via Remote.tryComplete.
Since Unison provides lightweight threads and direct-style programming (rather than the monadic style), though, you won’t find any flatMaps or other operations for chaining several tasks. What you do instead is await a task and, once its result is available, use it to calculate the next value:
await : Task a ->{Remote} a
Where’s my task
A key property of distributed programming in Unison is precise control over where computations run. And although the overall idea is to “bring the computation to the data” rather than “bringing the data to the computation”, it remains possible (with little effort) to do things less efficiently than you could.
Consider a simple example taken directly from the introductory documentation of the distributed namespace:
example1 loc =
t1 = forkAt loc '(1 + 1)
t2 = forkAt loc '(2 + 2)
await t1 + await t2
This code launches two computations at the provided location loc (most likely in parallel) and then does an await for both results. Keep in mind that the await happens locally – that is, on whichever node executes the example1 function. Consequently, the results of the t1 and t2 tasks are sent from the remote loc back to the calling location. In this case the data and the reduction of results (+) are trivial, so it doesn’t matter, but in general it could become a problem.
Here’s a slightly altered example that attempts to reduce how much data travels between nodes:
example2 loc =
use Nat +
t1 = forkAt loc '(1 + 1)
t2 = forkAt loc '(2 + 2)
t3 = forkAt (task.location t1) '(await t1 + await t2)
await t3
You may ask why t3 is forked at task.location t1 – meaning wherever t1 ran – rather than simply at loc. The loc may denote a broad region where computations can be forked, not one particular node. In that situation, we ensure that t3 at least runs on the same node as t1. t2, however, may end up on another one. In any case, less data has to travel between nodes.
It’s all about what’s not there
There’s no magic involved – deciding where each part of a computation runs and how the results get combined may well be the essential complexity of the distributed programming problem domain – but it’s easy to miss what is absent from the code snippets above.
Look: to run a function on a remote node, we simply handed it to forkAt. And to get its result, we simply called await. No serialization, no tough decisions between JSON or Protobuf (those are made on our behalf), no setting up connections, and so on. Still, a remote call remains different from a local one, since it needs the Remote ability. That’s why we don’t fall into the RPC fallacy. Even so, this is likely as close as one can get to making a remote call act like a local one while staying safe. Most (perhaps all) of the accidental complexity has disappeared.
I can guess what’s on your mind: errors can occur, but they can be dealt with properly. Indeed, retries can be added through an ability handler for Remote that produces another Remote computation (this time with network retries turned on, see Remote.retrying). In the same way, you can introduce timeouts, bulkheading, circuit breakers, and so on.
And consider this: performing a rolling upgrade comes down to using a new function hash in a service exposed to the outside world – there’s no need to redeploy binaries across our server farms. Unison handles distributing the right function hashes upon the first invocation. Meanwhile, the old ones that are already running continue without change.
Spark-like datasets
On top of Remote and Task, Unison provides a few higher-level constructs. One of these is Seq – a lazily-evaluated, distributed data structure. It can model data sets spread across many nodes, where the overall data size is larger than the memory of any single node.
A great series of blog posts explains how this data structure was designed, so I’ll simply suggest going through it!
Scoped locations
It’s worth stressing that locations are not all the same. Rather, each is parameterized by the abilities available to code executing at that location.
In the present (closed beta) Unison Cloud, for instance, computations can be run with these abilities:
{Remote, Http, Atomic, Channels, Scratch}
In practice this means, for example, that arbitrary I/O is off the table, because the IO ability is not included. We can, however, talk to other nodes in the cloud, make HTTP requests, keep some mutable state in memory, exchange messages through channels, and memoize requests in a cache.
These abilities let you accomplish a great deal, while the platform can guarantee security and complete isolation of the functions it executes.
Naturally, other locations may offer access to a different set of abilities. Those could grant more power, but possibly at an extra cost.
The Unison computer
Taken together, all of this makes the nodes of a Unison Cloud deployment act as a single, big computer. Sounds too good to be true? For the most part – yes. Unison exists only as a closed beta, and as everyone knows, the devil is in the details.
Writing distributed programs in Unison is elegant and friendly to programmers: it’s exactly the kind of computing you’d like to be doing. Sooner or later, though, you’ll need to integrate with the outside world. And getting all those integrations in place is going to take plenty of work.
One crucial point to remember: Unison is a closed, walled-off system. Part of its elegance comes from everything being written in Unison – and that’s also a weakness, because everything must be written in Unison. Every utility library you love will therefore need a Unison counterpart.
Still, Unison may well be prepared to deal with that issue. Eventually a Foreign Function Interface (FFI) will be needed, so that we can invoke, for example, a native numerical algorithm on data stored locally on a node. An ability with its own handler is a perfect fit for that kind of integration.
Cloud at the edge
Besides “the cloud”, there’s one more trend: edge computing. The idea is to move computations close to the user – either onto the user’s own device (like a smartphone) or somewhere in their vicinity (for instance, a cell phone base station or an edge server belonging to a content delivery network). If edge computing interests you, you’ve likely come across “bringing the computation to the data”: precisely the concept Unison Cloud implements.
This is no accident; Unison and its model fit this use case particularly well. Shipping computations to the edge is easy – it’s yet again the same mechanism – and all it takes is the hash of the function to execute plus some peers from which that function can be fetched. What’s more, Unison computations run in a sandbox. That resembles what WebAssembly provides, where it’s regarded as a key security feature.
Sandboxing is built on Unison’s abilities. Say we declare that a given edge location may only execute functions using particular abilities. Then we can be certain that no other operations get executed (provided the Unison language exposes no unsafe “backdoors” – which, so far, is the case).
Above all, the entire process is very lightweight and invisible to the programmer. Only the code (hashes) required to run a given function is sent: exactly that, no more and no less! On top of that, we get a functional, garbage-collected programming language with a direct syntax for effectful computations. That looks promising!
What’s next?
With this, our Unison mini-series comes to an end. Until an open beta of Unison cloud arrives, I can only suggest getting to know the language in its single-node form. For me, it has definitely been a very instructive and inspiring experience.
I’m eager to see Unison gain more momentum, and I hope to write some open-source Unison code someday!