Memory Limit in Elixir

I have a game server and my problem is now how to limit the memory
I want to limit the memory so If the user wants to log in, the application will reject and will not crash .
how can i do this ? i should check memory size for each request ? is there any other way like a flag for limiting genserver and … ?

You could use something like fuse (https://github.com/jlouis/fuse). This is a “circuit breaker” (https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern) which is pretty good to use in cases like this. fuse is written in erlang but should work in elixir as well. There might be pure elixir ones

In your game server you check the “fuse” before completing the request. Then you have another process checking the memory periodically. If it gets above a specific threshold you blow the fuse and you can handle this in process dealing with the user log in. Once memory is OK again you fix the fuse in the and things continue to work.

2 Likes

You can also limit the number of processes (e.g. gen_server) using DynamicSupervisor, and / or the memory per process using the max_heap_size flag.

In my personal experience limiting the number of players / games based on benchmarks may be easier than directly controlling memory, since allocation can spike a lot, e.g. when doing expensive Ecto queries.

6 Likes

thank you . this one is easier