zero-copy reverse proxy http server with spray?

677 views
Skip to first unread message

Scott Clasen

unread,
May 13, 2013, 2:34:43 PM5/13/13
to spray...@googlegroups.com
Is it possible to build such a thing with spray?

Looking at the HttpEntity / HttpBody, it seems not.  The response bytes look to be read into the HttpBody.

I would think this would be a desirable thing to be able to do with spray (at least for me :)

Mathias

unread,
May 14, 2013, 8:18:57 AM5/14/13
to spray...@googlegroups.com
Scott,

AFAIK zero-copy would entail that the HTTP message bytes be copied directly from one socket to another without having to pass through the JVM heap.
This is not currently supported by the underlying IO layer (the new Akka IO).

However, it's an interesting use case that we'll keep in mind in our work on the IO layer going forward.
Thanks for bringing it up!

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Roland Kuhn

unread,
May 14, 2013, 8:28:21 AM5/14/13
to spray...@googlegroups.com
Mathias (and Scott),

actual zero-copy is not something which is supported on the JVM at all; it is not even supported on all O/S and network cards AFAIK. What is called zero-copy—incorrectly—is actually “one less copy” (namely within the JVM), and we are using that already via DirectByteBuffers. But handing these around between actors is not safe (as they are mutable) and also quite expensive (as their GC process is rather painful), so it is not immediately clear that it would be a win.

Regards,

Roland
Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Empowering professional developers to build amazing apps.
twitter: @rolandkuhn

See you at Scala Days 2013 in NYC!
June 10th - June 12th
www.scaladays.org

Mathias

unread,
May 14, 2013, 8:48:26 AM5/14/13
to spray...@googlegroups.com
Roland,

yes, right.
Without analyzing deeper I was thinking that it might be possible to "short-circuit" two channels directly on the IO layer, for example to immediately forward all bytes coming in on one socket to another socket. The data would have to be copied from the kernels receive buffers into an (off-heap) DirectByteBuffer (e.g. from the pool) and from there into the send buffer of the target socket.

So, even though the data would not have to enter the JVM heap this doesn't really constitute the "zero-copy" approach which is possible between file channels and socket channels (and which is actually also more of a "one-copy").

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Scott Clasen

unread,
May 14, 2013, 2:21:51 PM5/14/13
to spray...@googlegroups.com
Right on guys, I think some kind of "as few copies as possible" functionality would be great for building akka.io based proxies. In the http case, most often you need to look at request or response headers, and do something like route the request to the right backend server, or look at the response code and based on it either return the response to the client or both return it to the client and put it in a cache, for instance.

Especially when proxying requests that can have large responses, the less copies the better.

For instance, how could I build a maven proxy with spray server + client? Seems like that would currently involve a lot of copying.
Reply all
Reply to author
Forward
0 new messages