Fast volatile objects in Erlang

What is the best approach to storing and managing high-performance mutable objects in Erlang? Suppose I want to write a really simple online game server with real-time gameplay. Somehow I need to imagine the state of the player in Erlang's memory. For example, it could just be a simple tuple, for example

{name, "Bob", health, 100, ammo, 50, score, 0}

These objects must be stored for a long time (at least as long as the user session is alive), they must be mutable (since players can interact with each other, for example, shoot at each other, kill, heal, etc.), and also high-performance (because the game is in real time, but not step by step). Therefore, I do not want to store this data in SQL or Mnesia. Which data structures are best suited?

+4
source share
1 answer

You should take a look at the ETS module (Erlang Term Storage). This is a keystore for storage in memory and, as the name implies, it can store erlang-terms in hashmap / tree-structure. I recommend reading this article , it has nice examples and is written for beginners.

With ETS, you can have 4 types of storage:

  • set - no warrant, no duplication, permanent access
  • ordered_set - ordered, no duplication, O (log N) -time access
  • bag - no order, duplicate keys (but not values), O (log N) -time access
  • duplicate_bag - no order, duplicate keys and values, O (log N) -time access

If you need to have permanent long-term storage, try the DETS-Module .

+7
source

All Articles