Srinivas
Srinivas

Reputation: 553

Order fulfilment with Akka FSM, storing state

I am trying to build order fulfilment component with AKKA FSM. I have few basic doubts on how the state is been stored and taken further upon event from user.

Consider states

ORDER_CLEAN, ORDER_INIT, ORDER_PAYMENT_WAITING, ORDER_PAYMENT_SUCCESS, ORDER_DELIVERY, ORDER_COMPLETE

Events as

EV_CART_CHECKOUT, EV_PROCEED_PAYMENT, EV_PAYMENT_SUCCESSFUL, EV_ITEMS_PACKED, EV_DELIVERED

State changes as

(EV_CART_CHECKOUT, ORDER_CLEAN) -> ORDER_INIT
(EV_PROCEED_PAYMENT, ORDER_INIT) -> ORDER_PAYMENT_WAITING
(EV_PAYMENT_SUCCESSFUL, ORDER_PAYMENT_WAITING) -> ORDER_PAYMENT_SUCCESS
(EV_ITEMS_PACKED, ORDER_PAYMENT_SUCCESS) -> ORDER_DELIVERY 
(EV_DELIVERED, ORDER_DELIVERY) -> ORDER_COMPLETE

Questions

  1. When we create FSM actors starting at ORDER_CLEAN with event EV_CART_CHECKOUT, would this actor is alive till we bring it to ORDER_COMPLETE(assuming we stop actor at this state) state?

  2. If yes to above point, in that case as we store order status on database how do we trigger new event on that actor? Is that do we need to maintain order_id to actor mapping and trigger event? What if there are 10K unique orders are currently being processed then we maintain mapping for all 10K actors is it? If so what is best data structure for maintaining these mappings for larger number of orders?

  3. In continuation to 2nd point, what if actors go down how to bring back actors to same state? Is supervisor actor only way to solve this? Or do we need to check actor status and then send event?

  4. At any point of state, user might not trigger next event may be for days, then is it good to keep actor live for such longer time or is it good to create new actor with updated state?

What are the better approaches to address these problems with akka FSM

Upvotes: 1

Views: 200

Answers (1)

Pavel Kudinov
Pavel Kudinov

Reputation: 405

  1. If we are talking about non-persistent Actor, generally speaking, we can't assume it will be alive between events. You simply might restart or redeploy the service, so the answer to your 1. question is no.
  2. To trigger a new event to the actor, you should create this actor initialise state machine with last valid state from the DB.
  3. You could either use Akka Persistence or just read current order state from the DB and pass it to the actor
  4. Actors are very lightweight objects, but talking about 10k events I would suggest to terminate actor after each transition

Upvotes: 0

Related Questions