Given the following Spray code:
object Main extends App with SimpleRoutingApp {
implicit val system = ActorSystem(\"my-system\")
val pipeline: HttpRequ
detach
is usually needed because routing runs synchronously in an actor. This means that while an HttpRequest
is routed, the actor cannot process any other messages at the same time.
However, routing bits that are asynchronous like completing with a Future
or using one of the FutureDirectives
will also free the original routing actor for new requests.
So, in cases where routing itself is the bottleneck or you complete a request synchronously, adding detach
may help. In your case above, you already complete with a Future
and have a relatively simple routing structure in which case adding detach
won't help much (or may even introduce a tiny bit of latency).
Also, detach
comes with some inconsistencies you can read about here:
An alternative to using detach
is using per-request-actors.
In akka-http, routing is implemented on top of Futures to be as asynchronous as possible and not confined to an actor any more so that detach
isn't needed and was removed therefore.
Without detach spray will process all requests one by one, while with detach it'll process them parallel. If you can process this requests in parallel, you'd better use detach for better performance.