Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Spray be asynchronous when HTTP is not?

It is said on the Spray site:

spray is an open-source toolkit for building REST/HTTP-based integration layers on top of Scala and Akka. Being asynchronous, actor-based, fast, lightweight, modular and testable it's a great way to connect your Scala applications to the world.

That asynchronous part confuses me because HTTP is a synchronous protocol.

Can you explain how Spray can be asynchronous when HTTP is not?

like image 627
Ivan Voroshilin Avatar asked Dec 07 '14 18:12

Ivan Voroshilin


1 Answers

Spray uses Akka underneath the hood as well as Futures. It is asynchronous in the sense that incoming requests are handed off to Akka Actors which in turn route the request and eventually generate a response.

In overly simplified terms, a request comes into the request handling actor and is asynchronously handed off to another actor that can deal with routing. The request handling actor is free to process additional requests without blocking to handle the response. The routing actor can hand off to a specific route handler in the same fashion. The actor with the finished response can send it.

Actors can have different types of dispatchers that define how they interact with threads to process messages, but essentially, the goal is to write code that never or rarely blocks, which is what the folks working on Spray have done.

like image 186
jxstanford Avatar answered Oct 11 '22 07:10

jxstanford