Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FSM vs become/unbecome in Akka

Tags:

akka

fsm

Akka provides two somewhat overlapping ways to manage actor states, Finite State Machines and unbecome/become. What are their respective benefits/drawbacks? When should one of them be chosen over the other?

like image 772
Tobias Furuholm Avatar asked Jun 14 '13 13:06

Tobias Furuholm


2 Answers

FSM is a DSL that allows you to build more sophisticated, readable state machines than would be possible using the core actor API. You could potentially show the FSM code to a business person and they could validate the business rules.

The FSM DSL allows you to compose things together more cleanly. For example transitions allow you to factor out logic that would have to be duplicated across actor become behaviors. Also you can subscribe other actors to be notified of transitions which helps with decoupling and testing.

Also timers are integrated nicely into the DSL and things like cancellation are handled cleanly. Coding timeout messages using the scheduler has a number of gotchas.

The down side to FSM is that it's a DSL and a new syntax for other team members to digest. The up side is that it's a DSL and a much higher level abstraction. I think agilesteel's threshold of 2 states is a good one. But once you get past 2 states the benefits of FSM are really compelling.

Definitely read the FSM docs and the accompanying examples contrasting become and FSM.

One note re: "popping" a behavior using unbecome - the default behavior is to not use the behavior stacking. It is only relevant in a small number of use cases (ie, usually not state machines).

like image 176
sourcedelica Avatar answered Nov 09 '22 19:11

sourcedelica


Become/Unbecome are very lightweight in contrast to FSMs. So unless you have more than 2 states (on/off for example) and/or complex state change policies I wouldn't convert Become/Unbecome to a full blown FSM. Other then that, I think there are only minor differences...Like for example FSMs give you a nice built in timer DSL:

setTimer("TimerName", msg, 5 seconds, repeat = true)
// ...
cancelTimer("TimerName")

Or for instance I'm not sure if it's possible in an FSM to "go back" to the previous state, there is only "go forward", since you have to explicitly specify which state to go to. Whereas unbecome gives you exactly that.

like image 21
agilesteel Avatar answered Nov 09 '22 20:11

agilesteel