-
Notifications
You must be signed in to change notification settings - Fork 2
/
lecture6.html
623 lines (575 loc) · 22.1 KB
/
lecture6.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<!DOCTYPE html>
<!--
Middleware and Web Services, CTU course slides
(cc) 2010-2023 Tomas Vitvar, http://vitvar.com
written for Humla, an open source HTML5 presentation environment
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="course" content="Middleware Architectures 1" />
<meta name="lecture" content="Lecture 7" />
<meta name="keywords" content="SOA, service concepts, service technologies, SOAP, REST, WSDL" />
<link type="text/css" rel="stylesheet" href="css/meta.css">
</link>
<link type="text/css" rel="stylesheet" href="css/ctu-fit.css">
</link>
<link type="text/css" rel="stylesheet" href="humla/lib/core/humla.css">
</link>
<script type="text/javascript" src="humla/lib/humla.js"></script>
<title>HATEOAS, Caching and Concurrency</title>
</head>
<body>
<footer>
<p><b>#META_LECTURE#: #TITLE#</b>, <span class="meta_semester" />,
<span class="meta_twitter" />
</p>
<p><b>‒ #SLIDE_NO# ‒</b></p>
</footer>
<div class="slide intro">
<hgroup>
<h1><span class="meta_course" /></h1>
<h2>#META_LECTURE#: #TITLE#</h2>
</hgroup>
<div class="author">
<p class="meta_author" />
<p><span class="meta_email" /> • <span class="meta_twitter" /> •
<span class="meta_web" />
</p>
</div>
<center>
<div class="meta_logo"></div>
</center>
<div class="org">
<p class="meta_org" />
<p><span class="meta_orgfac" /> • <span class="meta_field" />
• <span class="meta_orgweb" /></p>
</div>
<div class="etc">
<div class="text-info">
Modified: #LAST_MODIFIED#<br />
Humla v#HUMLA_VERSION#
</div>
<a href="http://creativecommons.org/licenses/by-sa/3.0/">
<div class="license"></div>
</a>
<div class="oppa"></div>
</div>
</div>
<div class="slide outline"></div>
<section>
<header>HATEOAS</header>
<div class="slide">
<hgroup>
<h1>HATEOAS</h1>
</hgroup>
<ul>
<li>HATEOAS = Hypertext as the Engine for Application State</li>
<ul>
<li>The REST core principle</li>
<li><b>Hypertext</b></li>
<ul>
<li>Hypertext is a representation of a resource state with <b>links</b></li>
<li>A link is an URI of a resource</li>
<li>Applying an access (PUT, POST, DELETE) to a resource via its link = state transition</li>
</ul>
</ul>
<li>Statelessness</li>
<ul>
<li>A service does not use a session memory to remember a state</li>
<li>HATEOAS enables stateless implementation of services</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Stateful server</h1>
</hgroup>
<ul class="x-small">
<li>Sessions to store the application state</li>
<ul>
<li>The app uses a server memory to remember the state</li>
<li>When the server restarts, the app state is lost</li>
</ul>
</ul>
<img src="/img/stateful-server.png" width="500px" style="padding-left: 150px; padding-top: 15px" />
</div>
<div class="slide">
<hgroup>
<h1>Stateless server</h1>
</hgroup>
<ul class="x-small">
<li>HTTP and hypermedia to transfer the app state</li>
<ul>
<li>Does not use a server memory to remember the app state</li>
<li>State transferred between a client and a service via HTTP metadata and resources' representations</li>
</ul>
</ul>
<img src="/img/stateless-server.png" width="500px"
style="padding-left: 150px; padding-top: 0px; margin-top: -10px" />
</div>
<div class="slide">
<hgroup>
<h1>Persistent Storage and Session Memory</h1>
</hgroup>
<ul class="small">
<li>Persistent Storage</li>
<ul>
<li>Contains the app data</li>
<li>Data is serialized into resource representation formats</li>
<li>All sessions may access the data via resource IDs</li>
</ul>
<li>Session Memory</li>
<ul>
<li>Server memory that contains a state of the app</li>
<li>A session may only access its session memory</li>
<li>Access through cookies</li>
<li>Note</li>
<ul>
<li>A session memory may be implemented via a persistent storage (such as in Google AppEngine)</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Link</h1>
</hgroup>
<ul class="small">
<li>Service operation</li>
<ul>
<li>Applying an access to a link (GET, PUT, POST, DELETE)</li>
<li>Link: HTTP method + resource URI + optional link semantics</li>
</ul>
<li>Example: <code>getOrder</code>, <code>addOrder</code>, and <code>updateItem</code></li>
</ul>
<div class="h-drawing" id="16vcb0AGdT3N5Jd4FFw9YADqYoF_QQlP46UsYngjfTKs" style="height: 320px"></div>
</div>
<div class="slide">
<hgroup>
<h1>Atom Links</h1>
</hgroup>
<ul class="small">
<li>Atom Syndication Format</li>
<ul>
<li>XML-based document format; Atom feeds</li>
<li>Atom links becoming popular for RESTful applications</li>
<pre class="brush: xml; highlight : [2,3,4,5]">
<order a:xmlns="http://www.w3.org/2005/Atom" xmlns="...">
<a:link
rel="next"
href="http://company.com/orders/5543"
type="application/xml"/>
<customer>Tomas</customer>
<items>...</items>
</order>
</pre>
<li>Link structure</li>
<ul class="no-bullet">
<li><code>rel</code> – name of the link<br />
~ semantics of an operation behind the link</li>
<li><code>href</code> – URI to the resource described by the link</li>
<li><code>type</code> – media type of the resource the link points to</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Link Semantics</h1>
</hgroup>
<ul class="small">
<li>Standard <code>rel</code> values</li>
<ul>
<li>Navigation: <code>next</code>, <code>previous</code>, <code>self</code></li>
<li>Does not reflect a HTTP method you can use</li>
</ul>
<li>Extension <code>rel</code> values</li>
<ul>
<li>You can use <code>rel</code> to indicate a semantics of an operation</li>
<li>Example: add item, delete order, update order, etc.</li>
<li>A client associates this semantics with an operation it may apply at a particular state</li>
<li>The semantics should be defined by using an URI</li>
<pre class="brush: xml;">
<order a:xmlns="http://www.w3.org/2005/Atom" xmlns="...">
<id>2324</id>
<a:link rel="http://company.com/op/addItem"
href="http://company.com/orders/2324"/>
<a:link rel="http://company.com/op/deleteOrder"
href="http://company.com/orders/2324"/>
</order>
</pre>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Link Headers</h1>
</hgroup>
<ul class="small">
<li>An alternative to Atom links in resource representations</li>
<ul>
<li>links defined in HTTP Link header, <span class="h-ref" id="rfc-5988">Web Linking IETF spec</a></li>
<li>They have the same semantics as Atom Links</li>
<li>Example:</li>
<pre class="brush: plain; gutter: 'false'">
> HEAD /orders HTTP/1.1
< Content-Type: application/xml
< Link: <http://company.com/orders/?page=2&size=10>; rel="next"
< Link: <http://company.com/orders/?page=10&size=10>; rel="last"
</pre>
</ul>
<li>Advantages</li>
<ul>
<li>no need to get the entire document</li>
<li>no need to parse the document to retrieve links</li>
<li>use HTTP HEAD only</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Preconditions and HATEOAS</li>
</hgroup>
<ul class="x-small">
<li>Preconditions in HATEOAS</li>
<ul>
<li>Service in a current state generates only valid transitions
that it includes in the representation of the resource.</li>
<li>Transition logic is realized at the server-side</li>
</ul>
</ul>
<img src="/img/preconditions-HATEOAS.png" width="700px" style="padding-left: 50px; padding-top: 15px" />
</div>
<div class="slide">
<hgroup>
<h1>Advantages</h1>
</hgroup>
<ul class="x-small">
<li>Location transparency</li>
<ul>
<li>only "entry-level" links published to the World</li>
<li>other links within documents can change without changing client's logic</li>
<li>Hypertext represents the current user's view, i.e. rights or other context</li>
</ul>
<li>Loose coupling</li>
<ul>
<li>no need for a logic to construct the links</li>
<li>Clients know to which states they can move via links</li>
</ul>
<li>Statelessness and Cloud</li>
<ul>
<li>Better implementation of scalability</li>
</ul>
</ul>
</div>
</section>
<div class="slide outline"></div>
<section>
<header>Caching, Revalidation, Concurrency Control</header>
<div class="slide">
<hgroup>
<h1>Scalability</h1>
</hgroup>
<ul>
<li>Need for scalability</li>
<ul>
<li>Huge amount of requests on the Web every day</li>
<li>Huge amount of data downloaded</li>
</ul>
<li>Some examples</li>
<ul>
<li>Google, Facebook: 5 billion API calls/day</li>
<li>Twitter: 3 billions of API calls/day (75% of all the traffic)<br />
→ 50 million tweets a day</li>
<li>eBay: 8 billion API calls/month</li>
<li>Bing: 3 billion API calls/month</li>
<li>Amazon WS: over 100 billion objects stored in S3</li>
</ul>
<li>Scalability in REST</li>
<ul>
<li>Caching and revalidation</li>
<li>Concurrency control</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Caching</h1>
</hgroup>
<div class="h-drawing" id="1nc-u-R3NUvur4WBAB1yxSqtj3PfmsSWF8iMo0aLT-Nc" style="height: 200px"></div>
<ul class="x-small">
<li>Your service should cache:</li>
<ul>
<li>anytime there is a static resource</li>
<li>even there is a dynamic resource</li>
<ul>
<li>with chances it updates often</li>
<li>you can force clients to always revalidate</li>
</ul>
</ul>
<li>three steps:</li>
<ul>
<li>client GETs the resource representation</li>
<li>server controls how it should cache through <code>Cache-Control</code>
header</li>
<li>client revalidates the content via conditional GET</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Cache Headers</h1>
</hgroup>
<ul class="x-small">
<li><code>Cache-Control</code> response header</li>
<ul>
<li>controls over local and proxy caches</li>
<li><code>private</code> – no proxy should cache, only clients can</li>
<li><code>public</code> – any intermediary can cache (proxies and clients)</li>
<li><code>no-cache</code> – the response should not be cached.
If it is cached, the content should always be revalidated.</li>
<li><code>no-store</code> – must not store persistently (this turns off caching)</li>
<li><code>no-transform</code> – no transformation of cached data; e.g. compressions</li>
<li><code>max-age</code>, <code>s-maxage</code> a time in seconds how long the cache is valid;
<code>s-maxage</code> for proxies
</li>
</ul>
<li><code>Last-Modified</code> and <code>ETag</code> response headers</li>
<ul>
<li>Content last modified date and a content entity tag</li>
</ul>
<li><code>If-Modified-Since</code> and <code>If-None-Match</code> request headers</li>
<ul>
<li>Content revalidation (conditional GET)</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Example Date Revalidation</h1>
</hgroup>
<ul class="x-small">
<li>Cache control example:</li>
<ul>
<pre class="brush: plain; gutter: 'false'">
> GET /orders HTTP/1.1
> ...
< HTTP/1.1 200 OK
< Content-Type: application/xml
< Cache-Control: private, max-age=200
< Last-Modified: Sun, 7 Nov 2011, 09:40 CET
<
< ...data...
</pre>
<li>only client can cache, the cache is valid for 200 seconds.</li>
</ul>
<li>Revalidation (conditional GET) example:</li>
<ul>
<li>A client revalidates the cache after <code>200</code> seconds.</li>
<pre class="brush: plain; gutter: 'false'">
> GET /orders HTTP/1.1
> If-Modified-Since: Sun, 7 Nov 2011, 09:40 CET
< HTTP/1.1 304 Not Modified
< Cache-Control: private, max-age=200
< Last-Modified: Sun, 7 Nov 2011, 09:40 CET
</pre>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Entity Tags</h1>
</hgroup>
<ul class="x-small">
<li>Signature of the response body</li>
<ul>
<li>A hash such as MD5</li>
<li>A sequence number that changes with any modification of the content</li>
</ul>
<li>Types of tag</li>
<ul>
<li>Strong ETag: reflects the content bit by bit</li>
<li>Weak ETag: reflects the content "semantically"</li>
<ul>
<li>The app defines the meaning of its weak tags</li>
</ul>
</ul>
<li>Example content revalidation with <code>ETag</code></li>
<pre class="brush: plain; gutter: 'false'; class-name: 'tight'">
< HTTP/1.1 200 OK
< Cache-Control: private, max-age=200
< Last-Modified: Sun, 7 Nov 2011, 09:40 CET
< ETag: "4354a5f6423b43a54d"
> GET /orders HTTP/1.1
> If-None-Match: "4354a5f6423b43a54d"
< HTTP/1.1 304 Not Modified
< Cache-Control: private, max-age=200
< Last-Modified: Sun, 7 Nov 2011, 09:40 CET
< ETag: "4354a5f6423b43a54d"
</pre>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Design Suggestions</h1>
</hgroup>
<ul class="small">
<li>Composed resources use weak ETags</li>
<ul>
<li>For example <code>/orders</code></li>
<ul>
<li>a composed resource that contains a summary information</li>
<li>changes to an order's items will not change semantics of <code>/orders</code></li>
</ul>
<li>It is usually not possible to perform updates on these resources</li>
</ul>
<li>Non-composed resources use strong ETags</li>
<ul>
<li>For example <code>/orders/{order-id}</code></li>
<li>They can be updated</li>
</ul>
<li>Further notes</li>
<ul class="small">
<li>Server should send both <code>Last-Modified</code> and
<code>ETag</code> headers
</li>
<li>If client sends both <code>If-Modified-Since</code> and <code>If-None-Match</code>,<br />
<code>ETag</code> validation takes preference
</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Weak ETag Example</h1>
</hgroup>
<ul class="x-small">
<li>App specific, <code>/orders</code> resource example</li>
<pre class="brush: js; class-name: 'tight'">
{
"orders" :
[
{ "id" : 2245,
"customer" : "Tomas",
"descr" : "Stuff to build a house.",
"items" : [...] },
{ "id" : 5546,
"customer" : "Peter",
"descr" : "Things to build a pipeline.",
"items" : [...] }
]
}
</pre>
<li>Weak ETag compute function example</li>
<ul>
<li>Any modification to an order's items is not significant for <code>/orders</code>:</li>
</ul>
<pre class="brush: js; class-name: 'tight'">
var crypto = require("crypto");
function computeWeakETag(orders) {
var content = "";
for (var i = 0; i < orders.length; i++)
content += orders[i].id + orders[i].customer + orders[i].descr;
return crypto.createHash('md5').update(content).digest("hex");
}
</pre>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Weak ETag Revalidation</h1>
</hgroup>
<ul class="x-small">
<li>Updating <code>/orders</code> resource</li>
<ul>
<li><code>POST /orders/{order-id}</code> inserts a new item to an order</li>
<li>Any changes to orders' items will not change the Weak ETag</li>
</ul>
</ul>
<div class="h-drawing" id="1n1jGatYjOc_kgMhWSuXyReptynOFn1CEDELB75Qj_3A" style="height: 380px"></div>
</div>
<div class="slide">
<hgroup>
<h1>Concurrency</h1>
</hgroup>
<ul class="x-small">
<li>Two clients may update the same resource</li>
<ul class="no-bullet">
<li>1) a client GETs a resource <code>GET /orders/5545</code></li>
<li>2) the client modifies the resource</li>
<li>3) the client updates the resource via <code>PUT /orders/5545 HTTP/1.1</code></li>
<li class="space-before">What happens if another client updates the
resource between 1) and 3) ?</li>
</ul>
<li>Concurrency control</li>
<ul>
<li>Conditional <code>PUT</code></li>
<ul>
<li>Update the resource only if it has not changed since a specified date or
a specified ETag matches the resource content</li>
</ul>
<li><code>If-Unmodified-Since</code> and <code>If-Match</code> headers</li>
<li>Response to conditional <code>PUT</code>:</li>
<ul>
<li><code>200 OK</code> if the <code>PUT</code> was successful</li>
<li><code>412 Precondition Failed</code> if the resource was updated in the meantime.</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Concurrency Control Protocol</h1>
</hgroup>
<div class="h-drawing" id="1D9w_NKq3AqIl14fn0vGFBmVlbNMrAQqeYljjOYLcqCo" style="height: 380px"></div>
<ul class="x-small">
<li>Conditional PUT and ETags</li>
<ul>
<li>Conditional PUT must always use strong entity tags or date validation</li>
</ul>
</ul>
</div>
</section>
<div class="slide outline"></div>
<section>
<header>Richardson Maturiy Model</header>
<div class="slide">
<hgroup>
<h1>Steps towards REST</h1>
</hgroup>
<img src="img/maturity-model.png" style="margin-top: 40px; margin-left: 50px"></img>
<ul class="xx-small">
<li class="no-bullet">See <span class="h-ref" id="rmm">Richardson Maturiy Model</span> details.</li>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Levels</h1>
</hgroup>
<ul class="small">
<li>LEVEL 0 – POX (Plain Old XML)</li>
<ul>
<li>HTTP as a tunneling mechanism</li>
<li>URL defines a service endpoint</li>
<li>No Web principles</li>
</ul>
<li>LEVEL 1 – Resources</li>
<ul>
<li>Take advantages of resources and URIs</li>
</ul>
<li>LEVEL 2 – HTTP Verbs</li>
<ul>
<li>Use HTTP methods and respect their semantics</li>
</ul>
<li>LEVEL 3 – Hypermedia Controls</li>
<ul>
<li>HATEOAS</li>
</ul>
</ul>
</div>
</section>
</section>
</body>
</html>