Doex Elixir handle multiple core utilization automatically?

So basically, the entire reason why I am moving to Elixir is because on node, we have to utilize “multiple instances”, and then utilize Redis to share memory (and also for the Pub Sub System). Which is fine! I had no problem doing that, and it’s a fairly easy way to scale.

But, having to not utilize Redis and manage multiple instances is a huge turn on for me! And obviously the extra performance Elixir has along with its sexy syntax (IMO).

But I’m worried after I read this: http://elixir-lang.org/getting-started/processes.html

For example, our game is instance based (Similar to Diablo 2, but not P2P).

Let’s say 1000 Websocket connections are active on an Xeon E3. 500 Players are in 83 games (6 players each). Each player is moving around the map and sending socket data to the other 5 players in their respective game. [This is not including Player Skills, Dropping an Item, Chatting, Monster Loot drops, etc, etc].

With nodejs, I would have to split these players up on multiple nodes and then either use Redis` Pub Sub to notify each time a player moves [since they will be on different node instances] – (which is way too much overhead). OR, just simply transfer that user to that instance before joining the game. (Which is what I do currently, and WHY I am switching to Elixir). (I don’t like doing that and I feel like it’s unintuitive)

With that said, after reading the processing doc, does this mean I would need to incorporate communication between processes manually in my code? For example, a function that notifies all friends that a player just logged in.

The reason I ask is because I made a similar topic in the Reddit golang sub weeks ago. And they said using “goroutines and channels” would be necessary. Then, I got a message from someone saying “Elixir would do all that stuff for you”.

But I really have no idea what I’m talking about honestly, I just don’t want to have to incorporate a system in Elixir that I am trying to get away from. (This entire Pub/Sub, or sending messages between stuff)

4 Likes

There is no magic going on in Elixir or Erlang. Elixir is a language that runs on top of Erlang VM, and you actually will benefit a lot from Erlang VM and it’s standard library, known as OTP.

So what you can do differently in Elixir/Erlang is that you don’t need no Redis or external PubSub, usually. You can spawn your players, monsters, even items as Erlang processes. They would communicate using sending and receiving messages. It’s a natural way of modeling problems like yours.

This stuff scales well, if designed appropriately too. You can add nodes to your cluster, and send/receive messages natively, without any third party solutions, as the processes you are sending messages to were local.

I encourage you to get an Elixir book or Erlang book. Good recommendations are here on the forum (search for books). Read a few chapters, figure out if that’s the right thing to you and your domain problem. I can say it probably is

5 Likes

Yes, you need to send messages to communicate between processes and yes the erlang VM handles core utilisation automatically. The BEAM, the erlang VM, automatically uses all the cores it is allowed to use and spreads the processes over them. It will also actively load balance over the cores. It does this automatically so you never to think about this, your program will run effectively on any number of cores without you having to specifically handle this.

Robert

5 Likes

Hey Hubert, and rvirding, I will definitely grab a book.

I saw this thread: http://stackoverflow.com/questions/30448588/does-spawning-new-processes-use-all-cpu-cores-in-elixir?rq=1 and it’s awesome how Elixir does this for us.

I guess now I’m just kind of stumped as in when to use them. For example, sending 5 messages to 20 players is not really a computational intensive function. But, when you got hundreds players doing it, it probably is? So it’s safe to just spawn the new processes for the function regardless?

But then, playing devils advocate, why not just run spawn on every function (since when the amount of players increase, the chance of that function being cpu intensive will rise)… Now I’m probably over thinking things :smiley:

2 Likes

Once you understand that on a typical developer-caliber laptop you can spawn on the order of 100K lightweight beam processes, your use cases for asynchronous behavior will increase.

6 Likes

Basically, once you learn to model your system in terms of processes, the Erlang VM handles distribution of work across the different cores. It would probably be extremely difficult if not impossible to build what you’re asking for without using processes, so you may consider you get it for “free”. You could also learn to write Go in a way that takes advantage of its concurrency features. However, I think it’s probably easier to write Go code that doesn’t take advantage of concurrency features than Elixir.

4 Likes

Dont worry, Elixir has what you need and lots more. One tip could be to check out the videos from ElixirConf Berlin 2016 (when they are posted) as there are some talks abour games and also wbsockets. That will give you an idea of what Erlang/Elixir could do for you. My impresion is also that Erlang has a lot more built into the system, such as process discovery, linked tasks and supervisors that you will probably be needing down the line (compared to gorutines and channels)

2 Likes