1

In Erlang there are two behaviors that seem to be pretty similar:

-behaviour(gen_server).
-behaviour(gen_statem).

What is the practical difference? When should I use one over another?

radrow
  • 6,419
  • 4
  • 26
  • 53
  • *What is the practical difference?* -- Most people will never be able to figure out how to use gen_statem or comprehend what a finite state machine is used for. – 7stud Apr 16 '21 at 12:56

1 Answers1

2

gen_server stands for "generic server", and it's a framework/interface to build processes that handle requests and events.

gen_statem stands for "generic state machine" and it's a framework/interface to build processes that behave as state machines reacting to events.

You could build a state machine on top of a gen_server or have a single-state state machine acting as a server.

To choose between one and the other you should check the relevant chapter in the doc, but I'd narrow it down to:

If you need to model a state machine, consider the features provided by gen_statem. For simple state machines without bells and whistles gen_server works fine and it's simpler.

José M
  • 3,294
  • 1
  • 14
  • 16