kjw0188
kjw0188

Reputation: 3685

What is the idiomatic way of mapping IDs to processes in OTP/Erlang?

I have a game server which hosts multiple instances of a game. Players can enter an ID to join a currently running game. In order to pair the client with the game instance they want, I have an ETS table which maps the game IDs to the PID of the Erlang process handling that particular game instance. While this works, I don't really like having to use ETS, since it creates global data that isn't under the control of any one process.

What I want to know is: Is there an idiomatic way in Erlang/OTP to map identifiers to processes? Is using ETS ok in this case, since the data is inherently global? Or should I have one process which stores a dictionary of all the ID to process mappings? I feel that is more Erlang style, but may not be as performant or concurrent. I also came across gproc while looking at other SO answers, which seems to be an improved process dictionary. However, I was under the impression that using the process dictionary is frowned upon (according to the erlang.org web page).

Upvotes: 2

Views: 595

Answers (1)

Muzaaya Joshua
Muzaaya Joshua

Reputation: 7836

ETS tables can be controlled by a given process/gen_server and their ownership can be transferred from process to process during times of trouble through the ets:give_away/3 or the heir option during the table creation.

Many game developers rely on something like memcached which does not differ (purposely) from ETS. However, mnesia is a very stable OTP Application and can handle much more concurrent updates compared to ETS. We have used mnesia in very busy applications and it has never gone down, unless the entire VM goes down due to other problems. Actually, most available systems built in erlang do depend on either mnesia or ETS for sharing state between redundant components so that as one fails over to another, in-memory data is available.

gproc is a very handy tool especially for things like Gaming. This is because, a process depends on its own easily accessible data. No other process can look up another process's data. So , instead of keeping game state in ETS, you can as well usegproc.

Anyways, if u have time, you could try each option and do some load tests to see which one performs better.

Upvotes: 6

Related Questions