Search code examples
erlangelixirbeam

Is gen_server restart strategy copy state?


Erlang world not use try-catch as usual. I'm want to know how about performance when restart a process vs try-catch in mainstream language.

A Erlang process has it's small stack and heap concept which actually allocate in OS heap. Why it's effective to restart it?

Hope someone give me a deep in sight about Beam what to do when invoke a restart operation on a process.

Besides, how about use gen_server which maintain state in it's process. Will cause a copy state operate when gen_server restart?

Thanks


Solution

  • I recommend having a read of https://ferd.ca/the-zen-of-erlang.html

    Here's my understanding: restart is effective for fixing "Heisenbug" which only happens when the (Erlang) process is in some weird state and/or trying to handle a "weird" message.

    The presumption is that you revert to a known good state (by restarting), which should handle all normal messages correctly. Restart is not meant to "fix all the problems", and certainly not for things like bad configuration or missing internet connection. By this definition we can see it's very dangerous to copy the state when crash happened and try to recover from that, because this is defeating the whole point of going back to a known state.

    The second point is, say this process only crashes when handling an action that only 0.001% (or whatever percentage is considered negligible) of all your users actually use, and it's not really important (e.g. a minor UI detail) then it's totally fine to just let it crash and restart, and don't need to fix it. I think it can be a productivity enabler for these cases.


    Regarding your questions in the OP comment: yes just whatever your init callback returns, you can either build the entire starting state there or source from other places, totally depend on the use case.