[erlang-questions] gen_server vs gen_fsm

Chandru chandrashekhar.mullaparthi@REDACTED
Mon Oct 8 12:31:20 CEST 2007


Hi,

On 08/10/2007, YC <yinso.chen@REDACTED> wrote:
> Hi -
>
> Thanks for the quick response, please see inline.
>
> On 10/8/07, Chandru <chandrashekhar.mullaparthi@REDACTED >
> wrote:
> > > A gen_fsm helps you implement the logic for a finite state machine.
> > > You can implement a FSM using gen_server but gen_fsm makes it much
> > > more easier and your code will be more structured and easier to read.
> >
> > > > * when should one use gen_fsm?  and when should one use a gen_server?
> >
> > > Use a gen_server when you want a long lived process which reacts to
> > > events. Use a gen_fsm to implement state machines. See
> > > http://en.wikipedia.org/wiki/Finite_state_machine
>
> When I think of state machine two primary types of example comes to mind:
>
> 1) the machine will generate the end result in one call as the input stream
> contains enough information to allow the machine to finish its processing;
> for example, a regular expression match
>
> 2) the machine will reach its end state only after multiple calls as each
> call changes the state; for example, an e-commerce website.
>
> Which type is gen_fsm suited for?  To me, the 1st type is not a server,
> where the second type is, and hence my confusion about its suitability.  As
> you say that gen_server is better suited for long lived processes, is
> gen_fsm not suited to implement the logic behind an e-commerce site?
>

gen_fsm can certainly be used to implement the logic behind an
e-commerce site where you'd have one instance of the gen_fsm for each
customer you are handling. You'd somehow have to map (Cookie to
process-id) your HTTP request to a particular instance of the state
machine. If you wanted to keep the state machine persistent, you'd
have to save it to some database and resurrect it when your next
request comes in and your state machine isn't around for whatever
reason.

> > > A web server's worker process doesn't have to be a gen_server or a
> > > gen_fsm. It just needs to be a module which given the HTTP request,
> > > does some processing and returns some content to the HTTP client.
>
> I see.  Are there disadvantages to use gen_server or gen_fsm in this case?
> Overkill?

If it is a HTTP request where the processing of one request is not
related to the next, it is definitely overkill to use a
gen_server/gen_fsm. But if you are talking about a ecommerce site, a
gen_fsm makes sense. But you'd have to take care to store the FSM
state in some persistent database, so that the user can be served even
if  your webserver suffered a restart in between requests.

>
> > > > * what's a good way to test the spawning to make it that it works?  I
> mean,
> > > > are there available tools to simplify the process?
> >
> > > Sorry - I don't understand your question.
>
>
> Sorry - I simply meant to ask if there are tools within Erlang that would
> help with simulating load testings, such as the ab tool.
>

I see. There is Tsung (previously tsunami) though I've never used it.
http://tsung.erlang-projects.org/

cheers
Chandru



More information about the erlang-questions mailing list