Live data from Hacker News

Gleam OTP – Fault Tolerant Multicore Programs with Actors

github.com

51–60 of 89 posts

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#51
post #2

I just started a small project using gleam / lustre, and so far I’m loving it. Worth trying if you’re on the fence, especially if you’re into static types, no nulls, functional, ML type languages. Plus beam of course.

I like all of the above but have no understanding of the BEAM or OTP. Can you recommend a good place to start learning about that?

In general, I found starting with a Erlang/Elixir framework tutorial helps. Phoenix includes a generic wrapper on top of PostgreSQL (Ecto provides data mapping and language integrated query), and hit a surprising number of users per host with trivial code (common game engine back-end.)

https://www.phoenixframework.org/

https://www.amazon.com/Programming-Phoenix-Productive-Reliab...

If you don't run away from a framework intro, then dive into the details of the OTP:

https://www.amazon.com/Designing-Elixir-Systems-OTP-Self-hea...

https://www.amazon.com/Elixir-Action-Third-Sa%C5%A1a-Juric/d...

The only foot-gun I would initially avoid, is a fussy fault-tolerant multi-host cluster deployment. Check out RabbitMQ package maintainers, as those guys certainly offer a fantastic resource for students ( https://www.rabbitmq.com/docs/which-erlang .)

Best of luck =3

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#52
post #26
post #2

I just started a small project using gleam / lustre, and so far I’m loving it. Worth trying if you’re on the fence, especially if you’re into static types, no nulls, functional, ML type languages. Plus beam of course.

For someone who hasn’t worked with either, is it better to learn gleam/lustre better or elixir/phoenix?

I love Gleam, but I would start with Elixir if you're interested in learning about how powerful the BEAM & OTP are.

There's not much documentation/resources around OTP in Gleam. When I was playing around with it I often found myself referring to the Elixir docs and then 'translating' that knowledge to Gleam's OTP implementation.

Gleam is still very new so this is totally understandable, and both are great languages so you'll likely have a lot of fun learning either of them.

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#53
post #26

Earlier quoted context omitted.

For someone who hasn’t worked with either, is it better to learn gleam/lustre better or elixir/phoenix?

In my opinion, Elixir and Phoenix will give you a better experience with BEAM and OTP, excellent tooling, a more mature ecosystem, and one of the best web frameworks ever to exist. I think Gleam is cool, but I can't see trading these benefits in for static typing. To be fair, I can't think of anything I care less about than static typing, so please keep that in mind when entertaining my opinion.

I also preferred dynamic typing, until my complex rails app grew to the point I didn't dare to do any refactoring. But I didn't switch opinion until I discovered ML type systems, which really allow for fearless refactoring. At occasion there's some battling to satisfy the typesystem, but even with that I'm more productive once the app grows in complexity.

I thought I'd share my experience, not trying to convince anyone ; - )

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#55
post #54

I’m fascinated by the sound of Erlang/BEAM but I’ve never found the time to actually try it. How are people using it in production? Do you write all your service logic using it or delegate specific parts to it?

I've seen folks doing both. Certainly, once you understand the OTP well enough, it makes sense to build all your systems on it. I've been doing that for seven years now; the only real issue was trying to train juniors to be productive on those systems. It did take longer to get them going I found.

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#56
post #43

Earlier quoted context omitted.

Having Erlang-style OTP support (for the most part) is very doable, I've written my own OTP layer instead of the pretty shoddy stuff Gleam ships with. It's not really that challenging of a problem and you can get stuff like typed processes (`Pid(message_type)`, i.e. we can only send `message_type` messages to this process), etc. out of it very easily. This idea that static typing is such a massive issue for OTP style…

I wholeheartedly agree with you that gleam_otp is janky. Still, actor message passing is only part of the picture. Here are some issues that make static typing difficult in OTP: • OTP processes communicate via the actor model by sending messages of any type . Each actor is responsible for pattern-matching the incoming message and handling it (or not) based on its type. To implement static typing, you need to know at…

I suppose I was unclear. It is OTP-style `gen_server` processes that I'm talking about.

> OTP processes communicate via the actor model by sending messages of any type. Each actor is responsible for pattern-matching the incoming message and handling it (or not) based on its type. To implement static typing, you need to know at compile time what type of message an actor can receive, what type it will send back, and how to verify this at compile time.

This is trivial, your `start` function can simply take a function that says which type of message you can receive. Better yet, you split it up in `handle_cast` (which has a well known set of valid return values, you type that as `incomingCastType -> gen_server.CastReturn`) and deal with the rest with interface functions just as you would in normal Erlang usage (i.e. `get_user_preferences(user_preference_process_pid) -> UserPreferences` at the top level of the server).

Here is an example of a process I threw together having never used Gleam before. The underlying `gen_server` library is my own as well, as well as the FFI code (Erlang code) that backs it. My point with posting this is mostly that all of the parts of the server, i.e. what you define what you define a server, are type safe in the type of way that people claim is somehow hard:

    import gleam/option
    import otp/gen_server
    import otp/types.{type Pid}
    
    pub type State {
      State(count: Int, initial_count: Int)
    }
    
    pub type Message {
      Increment(Int)
      Decrement(Int)
      Reset
    }
    
    pub fn start(initial_count: Int) -> Result(Pid(Message), Nil) {
      let spec =
        gen_server.GenServerStartSpec(
          handle_cast:,
          init:,
          name: option.Some(gen_server.GlobalName("counter")),
        )
      gen_server.start(spec, initial_count)
    }
    
    fn name() -> gen_server.ProcessReference(Message, String) {
      gen_server.ByGlobal("counter")
    }
    
    pub fn count() -> Result(Int, Nil) {
      gen_server.call(name(), fn(state: State) -> gen_server.CallResult(State, Int) {
        gen_server.CallOk(new_state: state, reply: state.count)
      })
    }
    
    pub fn increment(count: Int) -> Nil {
      gen_server.cast(name(), Increment(count))
    }
    
    pub fn decrement(count: Int) -> Nil {
      gen_server.cast(name(), Decrement(count))
    }
    
    pub fn reset() -> Nil {
      gen_server.cast(name(), Reset)
    }
    
    pub fn init(initial_count: Int) -> State {
      State(count: initial_count, initial_count:)
    }
    
    pub fn handle_cast(
      message: Message,
      state: State,
    ) -> gen_server.CastResult(State) {
      case message {
        Increment(count) ->
          gen_server.CastOk(State(..state, count: state.count + count))
    
        Decrement(count) ->
          gen_server.CastOk(State(..state, count: state.count - count))
    
        Reset -> gen_server.CastOk(State(..state, count: state.initial_count))
      }
    }

It's not nearly as big of an issue as people make it out to be; most of the expected behaviors are exactly that: `behaviour`s, and they're not nearly as dynamic as people make them seem. Gleam itself maps custom types very cleanly to tagged tuples (`ThingHere("hello")` maps to `{thing_here, >}`, and so on) so there is no real big issue with mapping a lot of the known and useful return types and so on.

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#57
post #52
post #26

Earlier quoted context omitted.

For someone who hasn’t worked with either, is it better to learn gleam/lustre better or elixir/phoenix?

I love Gleam, but I would start with Elixir if you're interested in learning about how powerful the BEAM & OTP are. There's not much documentation/resources around OTP in Gleam. When I was playing around with it I often found myself referring to the Elixir docs and then 'translating' that knowledge to Gleam's OTP implementation. Gleam is still very new so this is totally understandable, and both are great languages s…

Erlang is a much better language to learn if you're interested in learning about the BEAM and OTP, and the book "Programming Erlang"[0] is an excellent resource for learning it.

0 - https://pragprog.com/titles/jaerlang2/programming-erlang-2nd...

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#58
post #54

I’m fascinated by the sound of Erlang/BEAM but I’ve never found the time to actually try it. How are people using it in production? Do you write all your service logic using it or delegate specific parts to it?

We use Elixir at TV Labs to build our web services, a realtime matching engine, execute sandboxed Lua code, talk to microcontrollers over binary protocols, machine learning, and much more.

It is an excellent general purpose language that succeeds in a lot of domains.

Check out my conversation from Developer Voices for more info

https://youtu.be/_MwXbHADT-A?si=2lRqjwAY9dsODyhW

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#59
post #57
post #52

Earlier quoted context omitted.

I love Gleam, but I would start with Elixir if you're interested in learning about how powerful the BEAM & OTP are. There's not much documentation/resources around OTP in Gleam. When I was playing around with it I often found myself referring to the Elixir docs and then 'translating' that knowledge to Gleam's OTP implementation. Gleam is still very new so this is totally understandable, and both are great languages s…

Erlang is a much better language to learn if you're interested in learning about the BEAM and OTP, and the book "Programming Erlang"[0] is an excellent resource for learning it. 0 - https://pragprog.com/titles/jaerlang2/programming-erlang-2nd...

I disagree. I started with Elixir and its OTP resources are really good. Books like Elixir in Action do a great job.

I read Programming Erlang later, but it was just for fun, and I knew most things already at that point.

Re: Gleam OTP – Fault Tolerant Multicore Programs with Actors

#60
post #59
post #57

Earlier quoted context omitted.

Erlang is a much better language to learn if you're interested in learning about the BEAM and OTP, and the book "Programming Erlang"[0] is an excellent resource for learning it. 0 - https://pragprog.com/titles/jaerlang2/programming-erlang-2nd...

I disagree. I started with Elixir and its OTP resources are really good. Books like Elixir in Action do a great job. I read Programming Erlang later, but it was just for fun, and I knew most things already at that point.

I've used Elixir since 2015 and in fact learned it first. I still think "Programming Erlang" is a much better book than any other for actually learning Erlang and BEAM/OTP principles. Erlang as a language is simpler, leaving more time and energy for learning the actual important bits about OTP.
Post reply on HN