-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
315 lines (267 loc) · 11.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Batch API with REST</title>
<meta name="description" content="Codastic - a web agency offering full stack web application development">
<meta name="author" content="Mario Volke & Christian Ranz">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/github.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-state="highlight">
<p>
<img src="assets/signet_reduced.svg" width="100px" height="100px">
</p>
<h1>Batch API</h1>
<p>REST API design for the real life</p>
<p>
<small>
Mario Volke /
Munich NodeJS User Group /
13. August 2014
</small>
</p>
<p>
<small>
<a href="http://codastic.com">codastic.com</a>
</small>
</p>
</section>
<section>
<img src="assets/logo.svg" width="500px">
<p class="highlighted">
We are a <strong>web agency</strong> offering full stack <strong>web application development</strong>.
</p>
</section>
<section>
<h3>Needs in REST API design</h3>
<ul>
<li>
well-defined, separated resource definitions
<li>
The more fine-grained the resources are,
the higher the flexibility of the API.</li>
</ul>
</section>
<section>
<h3>Needs in application design</h3>
<ul>
<li>
fast responses to user input<br>
→ minimize the number of requests to the API</li>
</ul>
<hr>
<p>
What if the app needs data or has to manipulate data
from multiple resources at the same time?
</p>
<aside class="notes">
Application bootup often needs a lot of data from multiple resources before anything senseful can be displayed.
</aside>
</section>
<section>
<p>fine-grained resources <strong>VS</strong> number of requests</p>
<hr>
<p><strong>Contradiction? Tradeoff?</strong></p>
</section>
<section>
<h2>Batch API design</h2>
<p>
What if we could just use one HTTP request for multiple API requests
and let the backend handle them in parallel?<br>
</p>
</section>
<section>
<img src="assets/architecture.svg" alt="Batch API architecture">
<p>
“We call it a batch proxy”
</p>
</section>
<section>
<p>Fulfills several nice architectural properties:</p>
<ul>
<li class="fragment">Separation of concerns</li>
<li class="fragment">Scalability</li>
<li class="fragment">Reusability</li>
<li class="fragment">Cacheability</li>
<li class="fragment">Performance</li>
</ul>
<aside class="notes">
<ul>
<li>
<strong>Separation of concerns</strong><br>
The REST API doesn’t need to know anything about the existence
of the Batch API because of the layered architecture</li>
<li>
<strong>Scalability</strong><br>
REST API and Batch API can be scaled individually
</li>
<li>
<strong>Reusability</strong><br>
Batch API doesn’t need to know anything about the business logic of the REST API.
</li>
<li>
<strong>Cacheability</strong><br>
HTTP cache between REST API and Batch API is possible.
Cache between Batch API and frontend in theory only if none of the batched requests contains a modification of the data.
</li>
<li>
<strong>Performance</strong><br>
Batch API can send all requests to the REST API in parallel.
It has to wait for all responses from the REST API before it can respond to the frontend.<br>
→ response time of Batch API request = max response time of REST API requests
</li>
</ul>
</aside>
</section>
<section>
<h3>Example Request</h3>
<pre><code data-trim>POST /batch HTTP/1.1</code></pre>
<p>Request body:</p>
<pre><code data-trim class="lang-json">
{"batch": [
{"path": "/user/1234", "method": "GET"},
{"path": "/user/1234/friends", "method": "GET"}]
}
</code></pre>
</section>
<section>
<h3>Example Response</h3>
<pre><code data-trim class="lang-json">
[{
"request": {"path": "/user/1234", "method": "GET", "headers": {}},
"response": {
"headers": {
"content-type": "application/json",
"content-length": 2233
},
"body": {
"id": 1234,
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"status": 200
}
}, {
"request": {"path": "/user/1234/friends", "method": "GET", "headers": {}},
"response": {
"headers": {
"content-type": "application/json",
"content-length": 2233
},
"body": {
data: [
1235,
1236,
1237,
1238
]
},
"status": 200
}
}]
</code></pre>
</section>
<section>
<h2>Backend Implementation</h2>
<p>Easy integration into any existing Express app:</p>
<pre><code data-trim class="lang-js">
var PORT = 8080;
var express = require('express'),
bodyParser = require('body-parser'),
batchProxy = require('express-batch-proxy');
var app = express();
app.use(bodyParser.json());
/* ... other controllers */
app.post('/batch', batchProxy(PORT));
app.listen(PORT);
</code></pre>
</section>
<section>
<h2>Backend Implementation (2)</h2>
<p>Or as standalone app pointing to a different host:</p>
<pre><code data-trim class="lang-js">
var PORT = 8080;
var HOST = 'another-host.com';
var express = require('express'),
bodyParser = require('body-parser'),
batchProxy = require('express-batch-proxy');
var app = express();
app.use(bodyParser.json());
app.post('/batch', batchProxy(PORT, HOST));
app.listen(8080);
</code></pre>
</section>
<section>
<p>Backend code available at Github</p>
<h2>http://bit.ly/1q8mQiV</h2>
<p><small><a href="https://github.com/codastic/express-batch-proxy">https://github.com/codastic/express-batch-proxy</a></small></p>
</section>
<section>
<h2>Frontend Usage</h2>
<p>Example using a batch proxy jQuery plugin:</p>
<pre><code data-trim class="lang-js">
var batch = $.batch('http://host:8080/batch');
$.ajax('/user/1234', { type: 'GET', batch: batch })
.done(function(data) { ... });
$.ajax('/user/1234/friends', { type: 'GET', batch: batch })
.done(function(data) { ... });
batch.commit();
</code></pre>
<p><small>Our own implementation is currently build on top of AngularJS. But the implemented API is quite similar.</small></p>
</section>
<section data-state="highlight">
<p><strong></end></strong></p>
<h1>Questions?</h1>
<p>by Mario Volke</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: 'codastic',
transition: 'concave', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>