title: Akka Classic Ask pattern. How does it match asks with responses?
body: <p>I'm a newbie with Akka Actors, and I am learning about the Ask pattern.  I am looking at the following example <a href="https://alvinalexander.com/scala/scala-akka-actors-ask-examples-future-await-timeout-result/" rel="nofollow noreferrer">from alvin alexander</a>:</p>
  <pre class="lang-scala prettyprint-override"><code>class TestActor extends Actor {
    def receive = {
      case AskNameMessage =&gt; // respond to the &quot;ask&quot; request
                             sender ! &quot;Fred&quot;
      case _ =&gt; println(&quot;that was unexpected&quot;)
    }
  }
  
  ...
  
    val myActor = system.actorOf(Props[TestActor], name = &quot;myActor&quot;)
  
    // (1) this is one way to &quot;ask&quot; another actor
    implicit val timeout = Timeout(5 seconds)
    val future = myActor ? AskNameMessage
    val result = Await.result(future, timeout.duration).asInstanceOf[String]
    println(result)
  </code></pre>
  <p>(Yes, I know that <code>Await.result</code> isn't generally the best practice, but this is just a simple example.)</p>
  <p>So from what I can tell, the only thing you need to do to implement the &quot;askee&quot; actor to service an Ask operation is to send a message back to the &quot;asker&quot; via the Tell operator, and that will be turned into a future on the &quot;asker&quot; side as a response to the Ask.  Seems simple enough.</p>
  <p>My question is this:</p>
  <p>When the response comes back, how does Akka know that this particular message is the response to a certain Ask message?</p>
  <p>In the example above, the &quot;Fred&quot; message doesn't contain any specific routing information that specifies that it's the response to a particular Ask operation.  Does it just assume that the next message that the asker receives from that askee is the answer to the Ask? If that's the case, then what if one actor sends multiple Ask operations to the same askee?  Wouldn't the responses likely get jumbled, causing random responses to be mapped to the wrong Asks?</p>
  <p>Or, what if the asker is also receiving other types of messages from the same askee actor that are unrelated to these Ask messages?  Couldn't the Asks receive response messages of the wrong type?</p>
  <p>Just to be clear, I'm asking about Akka Classic, not Typed.</p>