radrow
radrow

Reputation: 7139

What's the difference between gen_server and gen_statem?

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?

Upvotes: 1

Views: 1106

Answers (1)

José M
José M

Reputation: 3509

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.

Upvotes: 3

Related Questions