som

som

Is updating a map frequently and saving in cache is alright?

We are developing a game, using elixir and phoenix channels as backend, it is a turn based game.

Player joins to lobby channel after starting the game, when a new game starts a genserver is created and players got joined to a new game channel.

We are using Nebulex for caching our game state, while genserver is used for keeping track of timing for single game instance. If genserver crashed for any reason, it will be restarted by supervisor but restore its state from Nebulex cache.

The state in cache is stored as map as value and game id as key :
game_state = %{
playing_players_ids: [1,2,3],
players_on_table_ids: [1,2,3,4],
numbers_of_players: 3,
round_scores: %{ 1 => %{1: 2, 2: 1, 3: 5}, 2 => %{1: 2, 2: 1, 3: 5}},
players_turn: %{1 => 2, 2 => 3, 3 => 1},
players_local_to_global_ids: %{1 => 51, 2 => 41, 3 => 56},
players_global_to_local_ids: %{51 => 1, 41 => 2, 56 => 3},
next_turn_id: 2,
rounds_played: 1,
first_card_suit: “”,
last_turn_winner: 2,
number_of_cards_played: 1,
last_turn_cards: %{1: “H5”, 2: “H1”, 3: “H10”},
bet_info: %{single_bet_amount: 30, collected_amount: 90}
genserver_pid: #PID<0.104.0>
}
Cache.set(game_id, game_state)

After every player turn, we have to update the state. Get state from cache, update one or more value and save it again in cache. The problem is: to update a single value, an entire map is recreated because of elixir immutability when saved in cache.

We want to know, is it ok? or is there any other way we can implement this?

Also let us know, if this application structure can be improved.

Thanks

Marked As Solved

fuelen

fuelen

When I wrote a game I didn’t save intermediary results to persistent DB. If something fails then system sends report to Sentry or other similar service using terminate callback, game ends and that’s it. Why? Usually when GenServer fails while working only with internal data and pure functions (no calls to other services which may fail) it means that you have a state and some action which you can’t handle. Saving state and applying the same action won’t help you in reliability. Server will be failed again and again. And even if you restore state and force player to perform another action - this game is not fair, because player has fewer decisions to perform and this has some impact on statistics.
If you really need to have persistent state I’d recommend storing a sequence of actions and initial state. By applying these actions to initial state you’ll get the final one. This approach allows you to store actions asynchronously. Also, it provides better info for debugging and gathering statistics.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

We're in Beta

About us Mission Statement