Ask HN: Does async/await exist for transient processes?
For instance, if I had a bunch of back-end servers that stored state in a DB... And as long as the back-end servers were all running the same version of my code... I could write async/await code... And all of my state could be stored in the DB... And when input came in from the user, whichever server the event got load-balanced to, could just continue processing.
I could imagine doing this with a Virtual Machine. Where I potentially store the full stack and heap into a chunk of data somewhere on disk or in a DB.
But I could also imagine that a programming language could be modified to allow me to just type async/await code, and have the state survive restarts, reboots, migrating to another server, etc. As long as I don't modify the code in the callstack of the async/await while it's in the middle of executing.
Does such a thing exist?
9 comments
[ 3.1 ms ] story [ 13.2 ms ] threadIt sounds like you're talking about having in-memory per-user state that persists between requests on the server side. Each time a request comes in, it's handled in the context of the state from previous requests?
That's a lot like Cloudflare's Durable Objects.
But it's also not much different from storing state for a user in the database and/or cache and fetching it on demand.
I seems like you're thinking of having like the ability to yield a response from code and then have a request come back in and resume at that point again. I'm not sure that makes much sense over just looking up some state from somewhere on a request.
Writeline("What's your first name?");
string First = await Readline();
Writeline($"Hello, {First}, what's your last name?");
string Last = await Readline();
Writing event-driven, database backed code is a definite skill to have.
But I see so much value in the async / await model, where you're writing code that looks very stateful, very synchronous. I think it's just much easier to reason about.
But unfortunately, that requires a long-lived process on a server somewhere.
So I'm imagining the best-of-both worlds.
I'd be willing to compile to WebAssembly, or something, that would make it easy to keep track of the memory. I think it would be awesome if .Net provided some compilation flag that enabled what I'm thinking about. I'd even be willing to switch languages, if this existed somewhere.
You could emulate this in your language and framework of choice by loading user state at the start of the request, manipulating it in memory, and save it at the end. Like with durable objects, you’d also need to gate requests and process them one at a time to keep things consistent. A simple queue could be used to do that.
It’s really not connected to async/await, you could write it synchronously or with goroutines or actors or whatever.
Being able to write my code as though it's synchronous is the most important aspect of this to me.
Writeline("What's your first name?");
string First = await Readline();
Writeline($"Hello, {First}, what's your last name?");
string Last = await Readline();
I understand what you're saying about using a state object. But then I'm responsible for keeping track of the state machine of my code. It stops looking like async/await code, and starts looking like event-driven code again.
All you have is requests and responses. They don’t come in a predictable order.
Whether you use async/await or not it’s irrelevant.
Loading and restoring the state is trivial. If you just want one state object per user, you can load and save it in your framework middleware. In real life you typically need more state than just per user state, which is why it’s uncommon to see that.
In the end you’ll find yourself back at the typical application server plus database architecture.
http://www.omg.org/bpmplus/
which is not quite the programming model you want but it is similarly breaking up "functions" into small bits and serializing the state so that this can happen over long terms.
What you want has been done on an experimental basis, but maybe not industrialized, see
https://stackoverflow.com/questions/734638/language-that-sup...
https://www.reddit.com/r/ProgrammingLanguages/comments/145du...
You stumbled upon a drawback yourself though:
>And as long as the back-end servers were all running the same version of my code
the continuation could be serialised and then picked up later with business logic that has since changed in your application, whether through changing requirements or bugfixes.
Therefore personally, (I'm not saying there aren't successful business implementations of continuations, just my own observations), in the wild, I've mostly seen long running async workflows being put together by either state machine modelling in whatever language the programmers are using to let workers run through multi-stage processes, or use of workflow engines to hoist the orchestration to a tooling layer and the regular "programming" is in the building blocks.
I believe Erlang has some tricks like this?
In my head, I call them Versioned Method Tables. A function signature, and a table of pointers that implement that function, as of different versions of your compiled code...