Troubleshooting and best practices for actors
Even though we are using some of the newer features of Akka, Akka actors themselves are still the core building block on our new stack. As such, it's important to know some of the most common gotchas when using actors, as well as some best practices. I'll use my experiences with Akka to present that information to you in this section in an effort to help you write good actors and avoid some common pitfalls in the process.
Avoid closing over mutable state with Futures
This one is sort of the cardinal sin of actors, and I'm sure it's been discussed a million times over on the Internet, but I'm including it anyway because it can cause all sorts of problems if not properly respected. You should never use or modify some sort of mutable state that is part of an actor within the body of any high-order function-based call on a Future
, such as map
, flatMap
, onComplete
, and so on. Consider the following example:
object UnsafeActor{ def props = Props...