-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.html
719 lines (459 loc) · 14.5 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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
---
layout: presentation
title: Server-side Caching
---
class: center, middle
# {{page.title}}
## CS291A: Scalable Internet Services
---
# Caching HTTP Responses: Motivation
A single web server process repeatedly responds to HTTP requests from a variety
of clients.
--
Responding to each request requires computation and I/O to be performed, both
of which can be expensive.
--
In practice, there is a significant amount of similarity between HTTP
responses.
--
With client-side caching we looked at optimizing scenarios where
requests for the same resource would result in an identical response.
--
In this lecture we will look at optimizing scenarios where repeated responses
are not identical, but similar.
---
# Caching HTTP Responses
> Which parts of an HTTP response are similar to responses for other resources?
--
* View fragments
--
* Rarely modified ORM objects
--
* Expensive to compute data
---
# View Fragments
View fragments are items that are similar across pages such as the header,
footer, sidebar, or even an item listing that may appear in multiple locations
(product listings, search results, product suggestions).
Sidebar Fragment (shown on all cs291.com pages)
```html
<div class="col-md-3 hidden-xs">
<div class="sidebar well">
Scalable Internet Services, CS291A, Fall 2017
</div>
<div class="sidebar well">
<h1 id="course-information">Course Information</h1>
<h2 id="instructor">Instructor</h2>
<p><a href="https://cs.ucsb.edu/~bboe">Dr. Bryce Boe</a>
<h2 id="lecture">Lecture</h2>
<p>Tuesday and Thursday<br />
3:00pm – 4:50pm</p>
...
</div>
</div>
```
---
# Rarely Modified ORM Objects
* User permissions
* Configuration options
* Product details (on a shopping site)
* In general, any persisted data (via a database, file store, external server, etc) that seldom changes
---
# Expensive to Compute Data
Expensive to compute data is exactly what the name suggests. Expensive could
mean:
* it takes a significant amount of time to compute
* it is best suited for a background worker due to hardware constraints on the
application servers.
--
## Examples
* The complete diff between two commits on GitHub
* The list of suggested contacts on LinkedIn
---
# Semi-Expensive Operations
View fragments are produced by extensive string manipulation. In ruby, many
string manipulations with portions loaded from disk can result in a significant
amount of time.
Every query to the database utilizes the database's resources. Optimizing for
similar queries can minimize the chance of a database bottleneck.
--
> Assuming we want to keep previously computed results around between requests,
> how can we do it?
--
> Where can we store the cached results?
---
# Storing Cached Results
Option 1: In memory on the application server
--
Option 2: On the file system
--
Option 3: In memory on another machine
--
Now, let's look into each of these options. But first a look at latency.
---
# Latency Numbers (ns and μs)
![Latency Numbers Every Programmer Should Know](latency_1_2019.png)
Source: <https://colin-scott.github.io/personal_website/research/interactive_latency.html>
---
# Latency Numbers (μs and ms)
![Latency Numbers Every Programmer Should Know](latency_2_2019.png)
Source: <https://colin-scott.github.io/personal_website/research/interactive_latency.html>
---
# Latency Numbers for Caching
* Storing in memory and reading later is fast
* Random read: `0.1μs`
* Reading 1MB: `4μs`
--
* Storing on disk is slow without SSD (solid state drive):
* Disk seek: `3000μs`
* Reading 1MB: `947μs`
--
* Storing on disk with SSD is much more reasonable
* Random read: `16μs`
* Reading 1MB: `62μs`
--
* Storing on another machine is reasonable
* Round trip within data center is `500μs`
---
# Latency Numbers Summary
## Summary
* In memory: single digit `μs`
* On SSD: tens of `μs`
* On magnetic disk: thousands of `μs`
* On remote machine: __add__ hundreds of `μs`
## Conclusions
* Prefer SSD over magnetic storage for speed
* Memory > SSD > Remote
> Are these conclusions always true?
---
# Caching Locations
> What effect on the cache hit rate does each of these designs have?
* In memory: cache per process
--
* On SSD: cache per machine
--
* On remote machine: cache per application server pool (cluster)
---
# Caching Location Trade-offs
* In memory
* highest performance
* lowest hit rate (if applicable to only one process)
* On SSD
* lower performance
* higher hit rate
* On remote machine
* lowest performance
* highest hit rate
There is no silver bullet.
???
* In memory: cache per process
* If the cache is per process, then the cache hit rate is low. This is
because each process has its own cache and the cache is not shared
between processes.
* This could be a consideration in using a single process multi threaded
server.
* On SSD: cache per machine
* If the cache is per machine, then the cache hit rate is higher. This is
because the cache is shared between all processes on the machine.
* This could be a consideration in using a multi process server vs a single
process multi threaded server.
* On remote machine: cache per application server pool (cluster)
* If the cache is per application server pool, then the cache hit rate is
highest. This is because the cache is shared between all processes on all
machines in the pool.
* This could be a consideration if you plan to load balance many servers.
---
# Memcached
__memcached__ is a commonly used remote cache server. It...
* keeps a cache in memory
* provides a simple TCP protocol to return responses to look up requests
* is a distributed key-value store
* keys can be up to 250 bytes
* values can be up to 1MB
* scales horizontally
* uses a simple LRU (least recently used) to make space for new items when full
* performs all operations in constant time
???
* Memcached vs Redis
* Memcached is a simple key-value store
* Redis is a more complex data structure store
* Redis is more feature rich
* Memcached is slightly faster
* Redis
* Features is a key value store with additional features
* Persistence to disk
* Clustering
* Data structures (lists, sets, sorted sets, hashes, ...)
* Lua scripting to extend functionality
---
# Server-side Caching in Rails
Rails provides excellent support for server-side caching.
The three primary interfaces to caching in rails are:
* HTTP caching
* Fragment caching
* Low level caching
We already covered HTTP caching. Today we'll discuss the latter two.
---
# Enabling Caching in Rails
By default, caching is disabled in development and test modes, and enabled only
in production mode.
If you want to enable caching in development mode you must make the following
change to your Rails environment:
```ruby
config.action_controller.perform_caching = true
```
Rails can be configured to store cached data in a few different places:
* In memory
* On a _local_ file system
* In a remote in-memory store
<https://edgeguides.rubyonrails.org/caching_with_rails.html>
---
# Configuring Rails Cache Stores
* ActiveSupport::Cache::MemoryStore
* ActiveSupport::Cache::FileStore
* ActiveSupport::Cache::MemcacheStore
---
# ActiveSupport::Cache::MemoryStore
* Cached data is stored in memory, in the same address space as the ruby
process.
* Defaults to `32MB`, configurable
---
# ActiveSupport::Cache::FileStore
* Cached data is stored on the _local_ file system.
* Can configure the location of the storage in the Rails environment:
```ruby
config.cache_store = :file_store, '/path/to/cache/'
```
---
# ActiveSupport::Cache::MemcacheStore
* Cached data is stored in memory on another machine (could also be local).
* Can configure the location of the server in the Rails environment:
```ruby
config.cache_store = :mem_cache_store, 'cache-1.example.com'
```
---
# Fragment Caching in Rails
Fragment caching caches a portion of a rendered view for reuse with future
requests.
Let's look at fragment caching in the context of the demo app.
---
# Fragment Caching Submission Partials
.left-column30[
We can cache each submission listing.
With that done, then regardless of any other changes on the page, we can
re-render the submission view for all submissions that haven't been updated.
]
.right-column70[
![Submissions Index View](demo_submissions_index.png)
]
???
* Contrast with client-side caching where the entire page is cached.
* Caching on client side only benefits the client that requested the page.
* Caching on the server side benefits all clients that request the page.
---
# Submission Listing (without cache)
```erb
<% @submissions.each do |submission| %>
<tr>
<td><%= link_to(submission.title, submission.url) %></td>
<td><%= submission.url %></td>
<td><%= submission.community.name %></td>
<td>
<%= link_to("#{submission.comments.size} comments",
submission, class: 'btn btn-primary btn-xs') %>
</td>
</tr>
<% end %>
```
---
# Submission Listing (with cache)
```erb
<% @submissions.each do |submission| %>
* <% cache(cache_key_for_submission(submission)) do %>
<tr>
<td><%= link_to(submission.title, submission.url) %></td>
<td><%= submission.url %></td>
<td><%= submission.community.name %></td>
<td>
<%= link_to("#{submission.comments.size} comments",
submission, class: 'btn btn-primary btn-xs') %>
</td>
</tr>
* <% end %>
<% end %>
```
---
# Choosing a Cache Key
> How should we choose a cache key for a submission?
--
```ruby
module SubmissionHelper
def cache_key_for_submission(submission)
"submission/#{submission.id}"
end
end
```
> What are the weaknesses with the above approach?
--
* Invalidation will need to be explicit as the submission ID typically will
never be updated for an object.
* Explicitly invalidating cache items can easily make a mess of the code.
---
# Choosing a Better Cache Key
```ruby
module SubmissionHelper
def cache_key_for_submission(sub)
"submission/#{sub.id}/#{sub.updated_at}/#{sub.comments.count}"
end
end
```
With the above, a submission's fragment cache is invalidated anytime the
submission is updated (`updated_at` is automatically updated on change), or
when the number of comments associated with the submission changes.
_Note_: An Active Record model can be used directly as the key. It calls an
over-writable method `cache_key` on the model. By default that method returns a
key that "includes the model name, the id and finally the updated_at
timestamp".
Reference:
<https://guides.rubyonrails.org/caching_with_rails.html#fragment-caching>
???
* Creating a cache key and checking the cache may be more expensive than
rendering the view. This is especially true if the cache key is complex.
---
# More Fragment Caching
.left-column30[
Now we are caching a fragment for each submission.
> What else can we cache?
]
.right-column70[
![Submissions Index View](demo_submissions_index.png)
]
--
We can cache the entire submission listing!
---
# Community Listing (without cache)
```erb
<h3>Submissions</h3>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Url</th>
<th>Community</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @submissions.each do |submission| %>
<% cache(...) do %> ... <% end %>
<% end %>
</tbody>
</table>
...
```
---
# Community Listing (with cache)
```erb
*<% cache(cache_key_for_submission_table) do %>
<h3>Submissions</h3>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Url</th>
<th>Community</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @submissions.each do |submission| %>
<% cache(...) do %> ... <% end %>
<% end %>
</tbody>
</table>
...
*<% end %>
```
---
# cache_key_for_submission_table
```ruby
module SubmissionHelper
def cache_key_for_submission(sub)
"submission/#{sub.id}/#{sub.updated_at}/#{sub.comments.count}"
end
* def cache_key_for_submission_table
* ("submission_table/#{Submission.maximum(:updated_at)}/"
* + Comment.maximum(:updated_at).to_s)
* end
end
```
---
# Russian Doll Caching
This technique of nesting cache fragments is known as __Russian Doll Caching__.
.center[![Imperial Russian Dolls](russian_dolls.jpg)]
---
# Low-level Rails Caching
You can use the same built-in mechanisms to manually cache anything:
```ruby
class Product < ActiveRecord::Base
def competing_price
Rails.cache.fetch("#{cache_key}/competing_price",
expires_in: 12.hours) do
Competitor::API.find_price(id)
end
end
end
```
---
# Performance Comparison
Let's compare the performance of the demo app with and without caching.
The subsequent graphs were generated using Tsung against a deployment of the
`main` branch (without caching) and against a deployment of the
`server_side_caching` branch (with server-side caching) using the default rails
caching mechanism (memory).
The `main` branch intentionally includes no optimizations.
---
# Caching Test: Simulated Users
We will use an m3-medium instance with the usual workload deployed using
passenger as an NGINX module.
Using Tsung (erlang-based test framework) we will simulate multiple users
visiting the Demo App web service. Each user will:
```
Visit the homepage (/)
Wait randomly between 0 and 2 seconds
Request community creation form
Wait randomly between 0 and 2 seconds
Submit new community form
Request new link submission form
Wait randomly between 0 and 2 seconds
Submit new link submission form
Wait randomly between 0 and 2 seconds
Delete the link
Wait randomly between 0 and 2 seconds
Delete the community
```
---
# Caching Test: Phases
This time we have six phases of testing each lasting 60 seconds:
```
(0-59s) Every second a new simulated user arrives
(60-119s) Every second 1.5 new simulated users arrive
(120-179s) Every second 2 new simulated users arrive
(180-239s) Every second 4 new simulated users arrive
(240-299s) Every second 6 new simulated users arrive
(300-359s) Every second 10 new simulated users arrive
```
---
class: center middle
# Performance without Caching
![Performance without caching](performance_no_caching.png)
---
class: center middle
# Performance with Caching
![Performance with caching](performance_server_cache.png)
---
# Performance Comparison
Without caching the server struggled to handle a single new arrival each
second.
With server-side caching the server can easily handle up to two
new arrivals a second.