Why is the actor “ask” pattern considered an anti-pattern or “code smell?”

后端 未结 1 1391
無奈伤痛
無奈伤痛 2021-02-14 06:52

From what I\'ve gathered, the \"ask\" pattern is considered a bad practice and should be avoided. Instead, the recommended pattern is the \"actor per request\" model. However, t

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 07:21

    From Akka docs:

    "There are performance implications of using ask since something needs to keep track of when it times out, there needs to be something that bridges a Promise into an ActorRef and it also needs to be reachable through remoting. So always prefer tell for performance, and only ask if you must."

    But sometimes you want to send a message from outside of an actor in which case you can use ask. Using ask will guarantee that you get a response within the specified timeout and sometimes that's what you want. However, when you use ask pattern you should ask yourself a question whether you could just use Futures instead.

    There is a place for ask but it should have very limited use due to the aforementioned reasons.

    You don't have to use actor per request. Some actors are meant to be long lived and some not. If an actor performs a potentially dangerous or blocking operation you might want to create one per request. Whatever fits your application logic.

    0 讨论(0)
提交回复
热议问题