1. 17
  1.  

  2. 11

    I was hoping for a real discussion of using Elixir at significant scale, alas. This is an intro article from an agency that, while employing a number of Elixir heavy hitters, mostly deals in agency-scale projects.

    1. 1

      I was hoping for the same. I could not really find content about how to scale up a gen_server. If somebody has any good resources it would be great to see it here.

      1. 4

        There’s no “scaling up” a genserver, a genserver is one process just like any other one on the VM. To scale past a bottleneck where one genserver can’t process its messages fast enough (a common Elixir/Erlang scaling problem), you need to figure out how to use more than one genserver to do the work. That means you can’t call the genservers by name anymore, but the process Registry is good for that

        I’ve been using a pattern lately where, in the genserver’s init(args) function, I register the process under a name that is passed in the args parameter. Then I launch the genserver under a supervisor. If the process dies, its entry will be removed from the registry, and when the supervisor launches another genserver to replace it, that server will register itself automatically. I like it so far.

        1. 2

          You’ve just posted a very short introduction to the concept of how to scale up the use of gen_server(s, plural) 😸

          1. 2

            If your gen_server does not need a lot of state to start it up (or side steppable with persistent_term), it may be easier to use simple_one_for_one and spawn a new process for each unit of work and have the source of the request monitor the process to handle retries. This works especially well when the inputs are binaries as then a copy of that data is not produced so starting up becomes cheaper and GC is now just the process destruction.

            Routing is difficult and sometimes it is best just to skip it altogether, especially when spawning is really really cheap.

            timer:tc(fun() -> Self = self(), Pid = spawn(fun() -> Self ! ok end), receive ok -> ok end end).
            
      2. 1

        So what are the best patterns for scaling horizontally with Erlang/Elixir?

        1. 1

          I’m not aware of any rules like “you must write elixir/Erlang exactly this way to scale horizontally”. It scales automatically, part of BEAM. Just write code in these languages and don’t try to go outside the box (NIFs, for example) and you’ll be fine is my understanding.

          1. 1

            My impulse is to load balance the way I would any python code.

            1. 2

              this works, although as with any sharded thing, it’s helpful if you design to permit sharded stuff to stay in-shard as much as possible. I mention this because erlang has “free” distributed systems built in, so reaching across shards is trivial, even accidentally. There’s an old anecdote of a company that moved a bunch of servers from London to another continent, booted them up, and the app was mysteriously slow. Come to find out, they had accidentally left the database server behind, and erlang was seamlessly accessing it across the internet with appropriate speed penalty. Made for confusing debugging.

              1. 1

                That’s a nice story. Would make a good post of it’s own.

              2. 1

                What do you mean? Run multiple python processes and use haproxy or something?

                edit: this is probably what you’d want to know https://www.poeticoding.com/the-primitives-of-elixir-concurrency-full-example/

                1. 1

                  Yea basically.