Erlang like fault tolerance with Swift actors

Hi all, is it feasible to implement erlang like supervision trees and process registries with actors?

Swift's actors kinda feel like erlang's gen_servers

3 Likes

Yes and no, so since Swift actor's lifetimes are managed by ARC, and not completely memory isolated, we can't "let it crash" the same way as one can on the beam vm.

On the other hand, if you do cross a process boundary one can express the link / monitor style semantics, and we have done so in the cluster runtime implementation: https://swiftpackageindex.com/apple/swift-distributed-actors/main/documentation/distributedcluster/lifecycle:

distributed actor Juliet: LifecycleWatch {
    deinit {
        print("\(Self.self) terminated!")
    }


    distributed func watch(_ romeo: Romeo) {
        watchTermination(of: romeo)
    }


    func terminated(actor id: ActorID) async {
        print("Oh no! \(id) is dead!")
        // *Stabs through heart*
    }
}

So across nodes, if Romeo was on another node which crashes - we do indeed get notified about it. The distributed cluster's lifecycle monitoring pattern also works with local actors but only as much as "the watched actor was deinitialized" since within the same process we cannot crash individual actors.

This is separate from the language feature though -- it's a library solution on top of the language.

12 Likes

A registry is basically what we have in the cluster known as the receptionist. It is also well-typed so you discover distributed actors of known types.

So all these things are implementable on top of the existing language features, yes.

2 Likes

wow, that's awesome. I'll go play around with distributed actors :)

2 Likes

If you encounter any issues with the cluster lib let's chat over here: Distributed Actors - Swift Forums

It's not gotten as much maintanance recently as I would have wished but it should still be functional. Thanks for your interest!

3 Likes

Hey @ktoso just an FYI - your link here is broken due to the trailing colon :wink: should just be https://swiftpackageindex.com/apple/swift-distributed-actors/main/documentation/distributedcluster/lifecycle

1 Like

I have been exploring this implementation @unwrapped_monad did you see it? GitHub - otp-interop/swift-erlang-actor-system: Erlang/Elixir integration for Swift Distributed Actors

2 Likes

It does not do anything regarding supervisors, registry and so on that still the work of distributed actors that ktoso mentioned and which the library expands upon implementing erl_interface

You could see this library as the protocol implementation of which erlang/elixir nodes communicate/connect with each other

1 Like

Thanks for clarifying that @wolfdan. That's very helpful. I'm very interested in these distributed systems. I think they can solve many scaling problems. I am wondering if this would be a library that I could contribute to. As I will have 3-4 months off and looking for open source projects that I can learn from and hopefully contribute to.
Would you know of such a thing? Or maybe other libraries to explore?

Are there maybe any topics I could contribute to? I'm currently looking to do some open source work but maybe would need some guidance in which topic to pick up. I have fairly solid Swift knowledge but most of all enthusiasm.

I'm thinking to kick off a community syncup for Distributed enthusiasts, so that would slot in nicely as well...

Overall updating the cluster system and getting rid of all the legacy implementation stuff inside it and giving it a fresh start could be interesting. Here's a branch to which I'd be happy to accept any "start fresh" implementations: GitHub - apple/swift-distributed-actors at fresh-start-da-first @jaleel wanted to poke at it but I personally do not have the time. The internals all predate actors as well as distributed actors in the language so there's a lot we could get rid of... Probably easiest by just writing a "fresh" ClusterSystem next to the current one and just replicating the functionality it offers, without reusing the Behavior types it relied on.

We could also just lean into using the erlang cluster and make it a "proper package" that's well documented how to use :slight_smile: That would be quite fun as well!

Overall, it'd be great to have a cluster solution with some nice examples to show and the erl one is quite exciting as well, it's all about polishing it all up I think! :-)

4 Likes

Allow me to cite from another post of you:

2 Likes

Yeah polishing that up to be a complete plug and play "actually use this" system would be fantastic as well!

2 Likes

There is also a ticket explaining migration steps.

Cluster system is working properly though, so there are lots of things in distributed systems world to built on top or alongside, like finishing Event Sourcing plugin, thinking about consensus, replication and etc., so yeah:

this would be nice to have. :slightly_smiling_face:

2 Likes

Will have a look next week. Thanks!

1 Like