cannam@147
|
1 ---
|
cannam@147
|
2 layout: post
|
cannam@147
|
3 title: "Promise Pipelining and Dependent Calls: Cap'n Proto vs. Thrift vs. Ice"
|
cannam@147
|
4 author: kentonv
|
cannam@147
|
5 ---
|
cannam@147
|
6
|
cannam@147
|
7 _UPDATED: Added Thrift to the comparison._
|
cannam@147
|
8
|
cannam@147
|
9 So, I totally botched the 0.4 release announcement yesterday. I was excited about promise
|
cannam@147
|
10 pipelining, but I wasn't sure how to describe it in headline form. I decided to be a bit
|
cannam@147
|
11 silly and call it "time travel", tongue-in-cheek. My hope was that people would then be
|
cannam@147
|
12 curious, read the docs, find out that this is actually a really cool feature, and start doing
|
cannam@147
|
13 stuff with it.
|
cannam@147
|
14
|
cannam@147
|
15 Unfortunately, [my post](2013-12-12-capnproto-0.4-time-travel.html) only contained a link to
|
cannam@147
|
16 the full explanation and then confusingly followed the "time travel" section with a separate section
|
cannam@147
|
17 describing the fact that I had implemented a promise API in C++. Half the readers clicked through
|
cannam@147
|
18 to the documentation and understood. The other half thought I was claiming that promises alone
|
cannam@147
|
19 constituted "time travel", and thought I was ridiculously over-hyping an already-well-known
|
cannam@147
|
20 technique. My HN post was subsequently flagged into oblivion.
|
cannam@147
|
21
|
cannam@147
|
22 Let me be clear:
|
cannam@147
|
23
|
cannam@147
|
24 **Promises alone are _not_ what I meant by "time travel"!**
|
cannam@147
|
25
|
cannam@147
|
26 <img src='{{ site.baseurl }}images/capnp-vs-thrift-vs-ice.png' style='width:350px; height:275px; float: right;'>
|
cannam@147
|
27
|
cannam@147
|
28 So what did I mean? Perhaps [this benchmark](https://github.com/kentonv/capnp-vs-ice) will
|
cannam@147
|
29 make things clearer. Here, I've defined a server that exports a simple four-function calculator
|
cannam@147
|
30 interface, with `add()`, `sub()`, `mult()`, and `div()` calls, each taking two integers and\
|
cannam@147
|
31 returning a result.
|
cannam@147
|
32
|
cannam@147
|
33 You are probably already thinking: That's a ridiculously bad way to define an RPC interface!
|
cannam@147
|
34 You want to have _one_ method `eval()` that takes an expression tree (or graph, even), otherwise
|
cannam@147
|
35 you will have ridiculous latency. But this is exactly the point. **With promise pipelining, simple,
|
cannam@147
|
36 composable methods work fine.**
|
cannam@147
|
37
|
cannam@147
|
38 To prove the point, I've implemented servers in Cap'n Proto, [Apache Thrift](http://thrift.apache.org/),
|
cannam@147
|
39 and [ZeroC Ice](http://www.zeroc.com/). I then implemented clients against each one, where the
|
cannam@147
|
40 client attempts to evaluate the expression:
|
cannam@147
|
41
|
cannam@147
|
42 ((5 * 2) + ((7 - 3) * 10)) / (6 - 4)
|
cannam@147
|
43
|
cannam@147
|
44 All three frameworks support asynchronous calls with a promise/future-like interface, and all of my
|
cannam@147
|
45 clients use these interfaces to parallelize calls. However, notice that even with parallelization,
|
cannam@147
|
46 it takes four steps to compute the result:
|
cannam@147
|
47
|
cannam@147
|
48 # Even with parallelization, this takes four steps!
|
cannam@147
|
49 ((5 * 2) + ((7 - 3) * 10)) / (6 - 4)
|
cannam@147
|
50 (10 + ( 4 * 10)) / 2 # 1
|
cannam@147
|
51 (10 + 40) / 2 # 2
|
cannam@147
|
52 50 / 2 # 3
|
cannam@147
|
53 25 # 4
|
cannam@147
|
54
|
cannam@147
|
55 As such, the Thrift and Ice clients take four network round trips. Cap'n Proto, however, takes
|
cannam@147
|
56 only one.
|
cannam@147
|
57
|
cannam@147
|
58 Cap'n Proto, you see, sends all six calls from the client to the server at one time. For the
|
cannam@147
|
59 latter calls, it simply tells the server to substitute the former calls' results into the new
|
cannam@147
|
60 requests, once those dependency calls finish. Typical RPC systems can only send three calls to
|
cannam@147
|
61 start, then must wait for some to finish before it can continue with the remaining calls. Over
|
cannam@147
|
62 a high-latency connection, this means they take 4x longer than Cap'n Proto to do their work in
|
cannam@147
|
63 this test.
|
cannam@147
|
64
|
cannam@147
|
65 So, does this matter outside of a contrived example case? Yes, it does, because it allows you to
|
cannam@147
|
66 write cleaner interfaces with simple, composable methods, rather than monster do-everything-at-once
|
cannam@147
|
67 methods. The four-method calculator interface is much simpler than one involving sending an
|
cannam@147
|
68 expression graph to the server in one batch. Moreover, pipelining allows you to define
|
cannam@147
|
69 object-oriented interfaces where you might otherwise be tempted to settle for singletons. See
|
cannam@147
|
70 [my extended argument]({{ site.baseurl }}rpc.html#introduction) (this is what I was trying to get
|
cannam@147
|
71 people to click on yesterday :) ).
|
cannam@147
|
72
|
cannam@147
|
73 Hopefully now it is clearer what I was trying to illustrate with this diagram, and what I meant
|
cannam@147
|
74 by "time travel"!
|
cannam@147
|
75
|
cannam@147
|
76 <img src='{{ site.baseurl }}images/time-travel.png' style='max-width:639px'>
|