Minh Kha
Minh Kha

Reputation: 1112

What is the usage of set Receive timeout in Akka?

From Akka's documentation about ReceiveTimeout. I still cannot get the idea of it, why need to set it? Could anyone teach me the use case of that in reality?

Upvotes: 1

Views: 445

Answers (1)

Levi Ramsey
Levi Ramsey

Reputation: 20611

Each actor consumes some amount of memory (Akka itself has an overhead in the hundreds of bytes per actor on most JVMs; additionally each actor can hold an arbitrarily large amount of in-memory state). There's thus a benefit to stopping an actor when it's no longer needed: if nothing else, greater memory consumption tends to mean longer garbage collection cycles, which can lead to things like missed cluster heartbeats or latency spikes.

In some cases, the point where an actor is no longer needed can be easily determined in its interactions with other actors. An example of this is spawning an actor to coordinate a particular task: once the task is done, the actor can be stopped.

In other cases, however, there's no apparent point where the actor's work is done. In some situations, one might infer from the fact that no messages have been sent to the actor for some time that the actor is no longer needed. This sort of thing might be most common in actors which encapsulate the state of a domain object. An example of this might be an actor representing a shopping cart: if the cart hasn't been checked out or had an item added/removed in, say, 2 hours, one could say that the customer left the site and the cart isn't serving any useful purpose. An especially common instance of this sort of thing is where the actor's state has been durably persisted elsewhere and the actor in memory is a cache: we can stop the actor and reload its state if it turns out we guessed wrong (the cost of improperly stopping the actor becomes extra latency when we have to read from a datastore rather than hit much faster memory).

So you don't ever need to set a receive timeout, but doing so can be useful in many cases.

Upvotes: 3

Related Questions