-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaching.html
More file actions
1110 lines (1093 loc) · 70.3 KB
/
caching.html
File metadata and controls
1110 lines (1093 loc) · 70.3 KB
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Caching — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="Caching — Ruby on Rails Guides" />
<meta name="description" content="CachingWhat is even better than having a really fast web server, framework, programming language that creates your web page? Not having to create and load the page at all because it's already there in a cache.After working through this guide: you will know that many different caches influence your web app: HTTP caching Fragment caching ActiveRecord QueryCache Caches inside the Database you will be able to configure rails for caching you will be able to measure if a change you made improved the performance of your rails app" />
<meta property="og:description" content="CachingWhat is even better than having a really fast web server, framework, programming language that creates your web page? Not having to create and load the page at all because it's already there in a cache.After working through this guide: you will know that many different caches influence your web app: HTTP caching Fragment caching ActiveRecord QueryCache Caches inside the Database you will be able to configure rails for caching you will be able to measure if a change you made improved the performance of your rails app" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<h1>Caching</h1><p>What is even better than having a really fast web
server, framework, programming language that creates
your web page? Not having to create and load the page at all
because it's already there in a cache.</p><p>After working through this guide:</p>
<ul>
<li>you will know that many different caches influence your web app:
<ul>
<li>HTTP caching</li>
<li>Fragment caching</li>
<li>ActiveRecord QueryCache</li>
<li>Caches inside the Database</li>
</ul></li>
<li>you will be able to configure rails for caching</li>
<li>you will be able to measure if a change you made improved the performance of your rails app</li>
</ul>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#what-is-caching">What is Caching</a></li>
<li><a href="#why-cache-questionmark">Why Cache?</a></li>
<li><a href="#where-are-caches-used-questionmark">Where are caches used?</a>
<ul>
<li><a href="#browser-http-caching">Browser + HTTP Caching</a></li>
</ul></li>
<li><a href="#measuring-performance">Measuring Performance</a>
<ul>
<li><a href="#rack-mini-profiler">rack-mini-profiler</a></li>
<li><a href="#example-app">Example App</a></li>
</ul></li>
<li><a href="#fragment-caching">Fragment Caching</a>
<ul>
<li><a href="#configure-caching">Configure Caching</a></li>
<li><a href="#caching-a-view">Caching a View</a></li>
<li><a href="#caching-smaller-fragements">Caching smaller fragements</a></li>
<li><a href="#russian-doll-caching">Russian Doll Caching</a></li>
<li><a href="#caching-in-api-only-rails-projects">Caching in API-only Rails Projects</a></li>
<li><a href="#the-limits-of-fragment-caching">The limits of fragment caching</a></li>
<li><a href="#caching-in-other-frameworks">Caching in other Frameworks</a></li>
<li><a href="#final-thought-on-caching-in-rails">Final Thought on Caching in Rails</a></li>
</ul></li>
<li><a href="#activerecord-and-db">ActiveRecord and DB</a>
<ul>
<li><a href="#ignore-this">Ignore this</a></li>
<li><a href="#querycache">QueryCache</a></li>
<li><a href="#indexes-in-the-db">indexes in the db</a></li>
<li><a href="#n-1-queries">n+1 queries</a></li>
<li><a href="#view-in-the-database">view in the database</a></li>
<li><a href="#final-thoughts">final thoughts</a></li>
<li><a href="#orms-in-other-frameworks">ORMs in other Frameworks</a></li>
</ul></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_caching.html#/0'>◻</a></p>
<h2 id="what-is-caching"><a class="anchorlink" href="#what-is-caching"><span>1</span> What is Caching</a></h2><p>In general english usage a cache is <a href="https://en.wiktionary.org/wiki/cache#Noun">A store of things that may be required in the future, which can be retrieved rapidly, protected or hidden in some way</a>.
But in computing the cache is not proteced or hidden, the important part
it that data stored in a cache can be retrieved rapidly.</p><p><img src="images/caching.svg" alt="what is caching"></p><p>The basic algorithm is very simple: is the data I need already in the cache?
then retrieve it from there. If not, retrieve the original data and store
it in the cache.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_caching.html#/1'>◻</a></p>
<h2 id="why-cache-questionmark"><a class="anchorlink" href="#why-cache-questionmark"><span>2</span> Why Cache?</a></h2><p>In computing we are faced with vastly different access speeds for different media:</p>
<ul>
<li>reading a megabyte of data from another host on the internet might take seconds</li>
<li>loading the same data from a local ssd takes may take a few hundred µs</li>
<li>reading the data from main memory may take a few µs.</li>
</ul>
<p>See <a href="https://gist.github.com/hellerbarde/2843375#file-latency_humanized-markdown">Latency numbers every programmer should know</a>.</p><p>Given these numbers it makes sense to keep a local copy of data that
we might use again soon. Better to read it from ssd or memory the second
time we need it!</p><p>Caches have finite storage so items are periodically removed from storage.
This process is called cache eviction.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_caching.html#/2'>◻</a></p>
<h2 id="where-are-caches-used-questionmark"><a class="anchorlink" href="#where-are-caches-used-questionmark"><span>3</span> Where are caches used?</a></h2><p>When you load a webpage into your browser there are many levels
of caches involved:</p>
<ul>
<li>Caching in the Browser, configured by HTTP Headers</li>
<li>Caching Proxies</li>
<li>Load Balancer + Caches, for example <a href="https://dev.to/satrobit/how-to-set-up-an-nginx-reverse-proxy-cluster-with-a-shared-cache-38eh">nginx</a>, or <a href="https://varnish-cache.org/">varnish</a></li>
<li>Inside the Backend Framework (see below)</li>
<li>Inside the Database (see below)</li>
</ul>
<p>If you are using a separate frontend you will have another caching layer
in the frontend for storing the result of api requests.</p><p>And in all the computers involved in this process:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/CPU_cache">CPU Cache</a> reading a cache line from memory, even if the CPU only needs one byte</li>
<li><a href="https://en.wikipedia.org/wiki/File_system">File Systems</a> reading (+caching) whole blocks of data, when only a byte is requested</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_caching.html#/3'>◻</a></p>
<h3 id="browser-http-caching"><a class="anchorlink" href="#browser-http-caching"><span>3.1</span> Browser + HTTP Caching</a></h3><p>HTTP Caching is built into the HTTP protocol. There are several
headers in both the HTTP-request and HTTP-response that influence
if the browser will cache a resource for later and how long
it will keep the resource in the cache.</p><p>The Server can send the <code>Cache-Control:</code> header:</p>
<ul>
<li><code>Cache-Control: no-store</code> - The cache should not store anything about the client request or server response. A request is sent to the server and a full response is downloaded each and every time.</li>
<li><code>Cache-Control: no-cache</code> - The cache will send the request to the origin server for validation before releasing a cached copy.</li>
<li><code>Cache-Control: public</code> - that the response may be cached by any cache.</li>
<li><code>Cache-Control: private</code> - the response is intended for a single user only and must not be stored by a shared cache. A private browser cache may store the response.</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-4' href='slides_caching.html#/4'>◻</a></p>
<h2 id="measuring-performance"><a class="anchorlink" href="#measuring-performance"><span>4</span> Measuring Performance</a></h2><p>As we already discussed in <a href="asset_pipeline.html">the chapter on the asset pipeline</a>
it is important to measure the performance of your app before you try to optimize anyhing.</p><p>In this chapter we will learn about new tools for measuring what happens on the server.</p><p>Let's start with a very general rule of thumb for performance:</p><p>We want the whole web page to load within a second. We expect to need about half of that (500ms) for loading extra assets like javascript files, css, images. We will set aside another 200ms for shipping data across the network, which leaves us with 300ms time to render out the first HTML document from our Rails App.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-5' href='slides_caching.html#/5'>◻</a></p>
<h3 id="rack-mini-profiler"><a class="anchorlink" href="#rack-mini-profiler"><span>4.1</span> rack-mini-profiler</a></h3><p>This gem helps you analyze where your Rails App spends time.</p><p><img src="images/rack-mini-profiler.png" alt="https://github.com/MiniProfiler/rack-mini-profiler"></p><p><a href="https://github.com/MiniProfiler/rack-mini-profiler">https://github.com/MiniProfiler/rack-mini-profiler</a></p><p>See <a href="https://archive.org/details/podcast_railscasts_368-miniprofiler_1000118526275">RailsCast #368</a> for a good introduction.</p><p>The Mini Profiler only measures the server side: the time spent in the rails app to generate
the webpage. So we need to compare the numbers Mini Profiler gives us to the
300ms threshold defined above.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-6' href='slides_caching.html#/6'>◻</a></p>
<h3 id="example-app"><a class="anchorlink" href="#example-app"><span>4.2</span> Example App</a></h3><p>We will use a portfolio site as an example app. All the screenshots
above already show this example app. You can study <a href="https://portfolio.fh-salzburg.ac.at/">the original</a>,
where all the caching is already implemented.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-7' href='slides_caching.html#/7'>◻</a></p>
<h2 id="fragment-caching"><a class="anchorlink" href="#fragment-caching"><span>5</span> Fragment Caching</a></h2><p>Fragement caching is a feature of the Rails Framework. Looking at
the views and partials that are rendered, you can mark some fragments
of the output to be cached for later.</p><p>To decide which fragments can be cached and which parts of
the view have to be computed anew every time you need to know
about the application.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-8' href='slides_caching.html#/8'>◻</a></p>
<h3 id="configure-caching"><a class="anchorlink" href="#configure-caching"><span>5.1</span> Configure Caching</a></h3><p>Fragment Caching is deactivated by default in the development environment.
You have to activate it if you want to try this out in development:</p><div class="interstitial code">
<pre><code class="highlight plaintext"># on the command line
$ rails dev:cache
Development mode is now being cached.
</code></pre>
<button class="clipboard-button" data-clipboard-text="# on the command line
$ rails dev:cache
Development mode is now being cached.
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-9' href='slides_caching.html#/9'>◻</a></p>
<p>You have to decide on a <strong>cache store</strong>. This store can be
any "key-value" store.
For production the simplest
method when using just one web server is in-memory.</p><div class="interstitial code">
<pre><code class="highlight plaintext"># in the file config/environments/production.rb
require 'active_support/core_ext/numeric/bytes'
config.cache_store = :memory_store, { size: 64.megabytes }
</code></pre>
<button class="clipboard-button" data-clipboard-text="# in the file config/environments/production.rb
require 'active_support/core_ext/numeric/bytes'
config.cache_store = :memory_store, { size: 64.megabytes }
">Copy</button>
</div>
<p>When using
several web servers you need a cache store that can be shared between them
like <a href="https://en.wikipedia.org/wiki/Memcached">memcached</a> or <a href="https://en.wikipedia.org/wiki/Redis">redis</a>.</p><p>In development, to get a quick impression of what is saved to the cache
it is helpful to use the file_store:</p><div class="interstitial code">
<pre><code class="highlight plaintext"># in the file config/environments/development.rb
# config.cache_store = :memory_store
config.cache_store = :file_store, "#{Rails.root}/tmp/file_store"
</code></pre>
<button class="clipboard-button" data-clipboard-text="# in the file config/environments/development.rb
# config.cache_store = :memory_store
config.cache_store = :file_store, "#{Rails.root}/tmp/file_store"
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-10' href='slides_caching.html#/10'>◻</a></p>
<h3 id="caching-a-view"><a class="anchorlink" href="#caching-a-view"><span>5.2</span> Caching a View</a></h3><p>The first image of miniprofiler above showed
the rendering of the show-action in the project controller.</p><p>At first glance rendering
the view takes too long: 450ms. We could dig
into the details, but let's try a simple approach first: cache
the whole view.</p><p>add this around the whole project view:</p><div class="interstitial code">
<pre><code class="highlight plaintext"><% cache @project do %>
...
<% end %>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<% cache @project do %>
...
<% end %>
">Copy</button>
</div>
<p>The result is stunning: from 450ms down to 45ms:</p><p><img src="images/rack-mini-profiler-faster.png" alt="https://github.com/MiniProfiler/rack-mini-profiler"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-11' href='slides_caching.html#/11'>◻</a></p>
<h4 id="how-caching-works"><a class="anchorlink" href="#how-caching-works"><span>5.2.1</span> How caching works</a></h4><p>So what happens here? When the view is rendered for the <strong>first time</strong>,
it will be rendered normally and still take around 450ms.
In the log file you will see a message like this:</p><div class="interstitial code">
<pre><code class="highlight plaintext">Write fragment views/projects/show:0db0955317bafa37cc34ffcb7567a8/projects/741679-20140722193808000000000 (1.1ms)
</code></pre>
<button class="clipboard-button" data-clipboard-text="Write fragment views/projects/show:0db0955317bafa37cc34ffcb7567a8/projects/741679-20140722193808000000000 (1.1ms)
">Copy</button>
</div>
<p>This shows the <strong>key</strong> that is used for the fragment.</p><p>The key depends
on both the object we specified (here <code>@project</code>), and on the view fragment.
The first part of the key the name of the view plus a hash of the view fragment inside the <code>cache</code> block.
After the colon you can see the class of the model, the id '741679', and the value of the <code>updated_at</code> attribute - in this example '20140722193808000000000'.</p><p>So if either the object or the view changes, a new key will be generated,
and nothing will be found in the cache. The view will be rendered from scratch.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-12' href='slides_caching.html#/12'>◻</a></p>
<p>When the view is rendered for the <strong>second time</strong>,
you find the following message in the log file:</p><div class="interstitial code">
<pre><code class="highlight plaintext">Read fragment views/projects/show:0db0955317bafa37cc34ffcb7567a8/projects/741679-20140722193808000000000 (0.9ms)
</code></pre>
<button class="clipboard-button" data-clipboard-text="Read fragment views/projects/show:0db0955317bafa37cc34ffcb7567a8/projects/741679-20140722193808000000000 (0.9ms)
">Copy</button>
</div>
<p>Here the cache is read out, which is a lot faster then rendering.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-13' href='slides_caching.html#/13'>◻</a></p>
<p>So which parts of the Rail Stack have we skipped by using the
fragment cache?</p><p>Really only part of the view. The whole stack was traversed
from Routing to Controller to View.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-14' href='slides_caching.html#/14'>◻</a></p>
<h4 id="peeking-into-the-cache"><a class="anchorlink" href="#peeking-into-the-cache"><span>5.2.2</span> Peeking into the cache</a></h4><p>You can also read from the cache in the rails console:</p><div class="interstitial code">
<pre><code class="highlight plaintext">irb(main):002:0> Rails.cache.read('views/projects/1679-20140722193808000000000/0db0955317bafa37cc34ffcb7567a874')
</code></pre>
<button class="clipboard-button" data-clipboard-text="irb(main):002:0> Rails.cache.read('views/projects/1679-20140722193808000000000/0db0955317bafa37cc34ffcb7567a874')
">Copy</button>
</div>
<p>The result is a string with 14716 bytes of html (too long to show here).</p><p>When using file_store you can also find the cache in the directory you specified.
A two level directory structure will be generated for the cache files,
for example:</p><div class="interstitial code">
<pre><code class="highlight plaintext">$ ls tmp/file_store/*/*/*
tmp/file_store/5CD/A81/views%2Fprojects%2F2205-20170312162506000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5D0/6A1/views%2Fprojects%2F2182-20170313205317000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5D7/A81/views%2Fprojects%2F2208-20170321190926000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5E6/091/views%2Fprojects%2F1679-20140722193808000000000%2F0db0955317bafa37cc34ffcb7567a874
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ ls tmp/file_store/*/*/*
tmp/file_store/5CD/A81/views%2Fprojects%2F2205-20170312162506000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5D0/6A1/views%2Fprojects%2F2182-20170313205317000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5D7/A81/views%2Fprojects%2F2208-20170321190926000000000%2F0db0955317bafa37cc34ffcb7567a874
tmp/file_store/5E6/091/views%2Fprojects%2F1679-20140722193808000000000%2F0db0955317bafa37cc34ffcb7567a874
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-15' href='slides_caching.html#/15'>◻</a></p>
<h4 id="changing-the-model"><a class="anchorlink" href="#changing-the-model"><span>5.2.3</span> Changing the model</a></h4><p>Now let's check if the cache is really invalidated when the underlying model
changes. Load the project "Origin" in your web browser: <a href="http://localhost:3000/projects/2015-origin">http://localhost:3000/projects/2015-origin</a></p><p>In the rails console you can find the corresponding model, and change an attribute:</p><div class="interstitial code">
<pre><code class="highlight plaintext">project = Project.find_by_title('Origin')
project.description = project.description + " and some new information"
project.save
</code></pre>
<button class="clipboard-button" data-clipboard-text="project = Project.find_by_title('Origin')
project.description = project.description + " and some new information"
project.save
">Copy</button>
</div>
<p>Now reload the browser to make sure that a new version of the page is rendered.
Reload again to check if the new version is cached.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-16' href='slides_caching.html#/16'>◻</a></p>
<h3 id="caching-smaller-fragements"><a class="anchorlink" href="#caching-smaller-fragements"><span>5.3</span> Caching smaller fragements</a></h3><p>If you look at <a href="https://portfolio.fh-salzburg.ac.at/">the original homepage</a>
you’ll see that each department displays a "current featured project".</p><p>Caching the entire homepage won’t work if we want to keep this feature dynamic.
For example, when a department updates its featured project,
the homepage will still show the old version if the whole page is cached.</p><p>A better approach is to cache only the individual project display
instead of the whole homepage. This means caching at the level of
the <code>projects/_project.html.erb</code> partial, so updates are reflected dynamically.</p><p>This strategy works well, especially when the partial is reused in multiple places.</p><p>To generalize, think about an "activity stream" on a social media site.
The page looks different for every user and updates with each reload.
However, smaller fragments — like individual posts, photos, or videos —
can still be cached and reused efficiently across pages.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-17' href='slides_caching.html#/17'>◻</a></p>
<h4 id="caching-a-partial"><a class="anchorlink" href="#caching-a-partial"><span>5.3.1</span> Caching a partial</a></h4><p>In the file <code>projects/_project.html.erb</code> we switch on caching.</p><p>When you add the code,
make sure that you specify the correct object. If not, you might up
loading the same partial again and again:</p><p><img src="images/caching-error.png" alt="problem with fragement caching"></p><p>If you implement it correctly each rendering of the partial should
be faster now:</p><p><img src="images/caching-compare.png" alt="successful fragement caching"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-18' href='slides_caching.html#/18'>◻</a></p>
<p>You can speed up the rendering even more.
If you look at the <code>fronts/show</code> view you can see that the project partial
is rendered through a collection:</p><div class="interstitial code">
<pre><code class="highlight plaintext"><%= render :partial => "projects/project", :collection => @sample %>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<%= render :partial => "projects/project", :collection => @sample %>
">Copy</button>
</div>
<p>You can add caching here:</p><div class="interstitial code">
<pre><code class="highlight plaintext"><%= render :partial => "projects/project", :collection => @sample, :cached => true %>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<%= render :partial => "projects/project", :collection => @sample, :cached => true %>
">Copy</button>
</div>
<p>Now instead of fetching each partial from the cache one by one
rails will do a multi-fetch, which is faster.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-19' href='slides_caching.html#/19'>◻</a></p>
<h4 id="side-effects"><a class="anchorlink" href="#side-effects"><span>5.3.2</span> Side Effects</a></h4><p>An unexpected side effect of caching the partial can be seen in the
<a href="https://portfolio.fh-salzburg.ac.at/editions/master-projects-2024">edition view</a>:
this view also uses the <code>projects/_project</code> partial, so it too will
profit from the caching.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-20' href='slides_caching.html#/20'>◻</a></p>
<h3 id="russian-doll-caching"><a class="anchorlink" href="#russian-doll-caching"><span>5.4</span> Russian Doll Caching</a></h3><p>In the previous step we implemented caching for the <code>projects/_project</code> partial,
which is also used in the <code>editions/show</code> view. Now let's add caching to this
view also:</p><div class="interstitial code">
<pre><code class="highlight plaintext"><% cache @edition do %>
...
<% end %>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<% cache @edition do %>
...
<% end %>
">Copy</button>
</div>
<p>This change will again speed up the display of the page:</p><p><img src="images/russian.png" alt="russian doll caching"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-21' href='slides_caching.html#/21'>◻</a></p>
<p>But now we have problem: if we change one of the projects
<strong>inside</strong> this edition, the cache for the partial would be recreated.
But this never gets triggered, because the cache for the
whole edition is still valid:</p><div class="interstitial code">
<pre><code class="highlight plaintext">project = Project.find_by_title('Origin')
project.title = 'Orange'
project.save
</code></pre>
<button class="clipboard-button" data-clipboard-text="project = Project.find_by_title('Origin')
project.title = 'Orange'
project.save
">Copy</button>
</div>
<p>If you reload the page now, you can still see the project named "Origin", not
"Orange".</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-22' href='slides_caching.html#/22'>◻</a></p>
<p>The problem here is a <strong>missing dependency</strong>: our cache entry only depends
on the edition, not on the projects contained in the edition.</p><p>We can declare the full dependency by supplying an array of objects to
the cache helper method:</p><div class="interstitial code">
<pre><code class="highlight plaintext"><% cache [@edition,@edition.projects] do %>
...
<% end %>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<% cache [@edition,@edition.projects] do %>
...
<% end %>
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-23' href='slides_caching.html#/23'>◻</a></p>
<p>If you reload the page now, you can see that a much longer cache key is
generated:</p><div class="interstitial code">
<pre><code class="highlight plaintext">Write fragment views/editions/16-20160202125058000000000/projec
ts/1622-20141216101932000000000 /projects/1658-2015060105552300
0000000/projects/1773-20170420014824000000000/projects/1835-201
50604061050000000000/projects/1864-20150611174811000000000/proj
ects/1872-20150603140238000000000/projects/1873-201506030846480
00000000/projects/1879-20150606174629000000000/projects/2044-20
161010124545000000000/e212725e51fc97160af625b6651e38b8
</code></pre>
<button class="clipboard-button" data-clipboard-text="Write fragment views/editions/16-20160202125058000000000/projec
ts/1622-20141216101932000000000 /projects/1658-2015060105552300
0000000/projects/1773-20170420014824000000000/projects/1835-201
50604061050000000000/projects/1864-20150611174811000000000/proj
ects/1872-20150603140238000000000/projects/1873-201506030846480
00000000/projects/1879-20150606174629000000000/projects/2044-20
161010124545000000000/e212725e51fc97160af625b6651e38b8
">Copy</button>
</div>
<p>This key works for all changes in an edition:</p>
<ul>
<li>changing an attribute of the <strong>edition</strong> will change the <code>updated_at</code> attribute also, and will change the key</li>
<li>changing an attribute of one of the <strong>projects</strong> will change the corresponding <code>updated_at</code> attribute also, and will change the key</li>
<li>adding a <strong>new project</strong> to the edition will make the key longer</li>
<li><strong>removing a project</strong> from the edition will make the key shorter</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-24' href='slides_caching.html#/24'>◻</a></p>
<p>In the example below the title of one of the projects was changed:
You can see in rack-mini-profiler that only one of the
partials was recreated, all the other partials were loaded from cache.
The next time the same page was rendered the edition cache was reused.</p><p><img src="images/russian-change.png" alt="russian doll caching at work: changes when a project changes"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-25' href='slides_caching.html#/25'>◻</a></p>
<h3 id="caching-in-api-only-rails-projects"><a class="anchorlink" href="#caching-in-api-only-rails-projects"><span>5.5</span> Caching in API-only Rails Projects</a></h3><p>jbuilder has built in caching support:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">json</span><span class="p">.</span><span class="nf">cache!</span> <span class="p">[</span><span class="s1">'v1'</span><span class="p">,</span> <span class="vi">@person</span><span class="p">],</span> <span class="ss">expires_in: </span><span class="mi">10</span><span class="p">.</span><span class="nf">minutes</span> <span class="k">do</span>
<span class="n">json</span><span class="p">.</span><span class="nf">extract!</span> <span class="vi">@person</span><span class="p">,</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:age</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="json.cache! ['v1', @person], expires_in: 10.minutes do
json.extract! @person, :name, :age
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-26' href='slides_caching.html#/26'>◻</a></p>
<h3 id="the-limits-of-fragment-caching"><a class="anchorlink" href="#the-limits-of-fragment-caching"><span>5.6</span> The limits of fragment caching</a></h3><p>Caching is really helpful for pages that are accessed a lot.
In our example app this might be true for the homepage and
maybe the editions. But there are hundreds of projects in the portfolio.
Each individual project page will only get very few hits.
Which means that chances are high that the page will not
already be in the cache when it is requested.</p><p>You can "warm up the cache" by automatically loading the most
important pages of your app after each deployment.</p><p>But caching cannot be the solution to all performance problems.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-27' href='slides_caching.html#/27'>◻</a></p>
<h3 id="caching-in-other-frameworks"><a class="anchorlink" href="#caching-in-other-frameworks"><span>5.7</span> Caching in other Frameworks</a></h3><p>Backend Frameworks:</p>
<ul>
<li>laravel(php): <a href="https://laravel.com/docs/11.x/cache">Cache</a></li>
<li>nest.js(javascript): <a href="https://docs.nestjs.com/techniques/caching#auto-caching-responses">Caching</a></li>
<li>django(python): <a href="https://docs.djangoproject.com/en/5.1/topics/cache/#template-fragment-caching">Cache</a></li>
<li>ASP.NET(c#): <a href="https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/perform-fragment-caching">Fragment Caching</a></li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-28' href='slides_caching.html#/28'>◻</a></p>
<h3 id="final-thought-on-caching-in-rails"><a class="anchorlink" href="#final-thought-on-caching-in-rails"><span>5.8</span> Final Thought on Caching in Rails</a></h3><p>If you find that the backend framework causes a performance problem
you should be able to narrow down the problem and fix it using
different methods. You should be able to:</p>
<ul>
<li>configure caching in development and production</li>
<li>use caching for fragments that depend on one or several objects</li>
<li>use caching with partials and collections</li>
<li>recognize russion doll caching and debug it if necessary</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-29' href='slides_caching.html#/29'>◻</a></p>
<h2 id="activerecord-and-db"><a class="anchorlink" href="#activerecord-and-db"><span>6</span> ActiveRecord and DB</a></h2><p>Accessing the database is significantly slower compared to the computations performed in Ruby code. Therefore, examining the database and the ORM used to interact with it could be a valuable step toward performance optimization.</p><p>Before you start working on the Database,
make sure to switch off caching in development:</p><div class="interstitial code">
<pre><code class="highlight plaintext"># on the command line
$ rails dev:cache
Development mode is no longer being cached.
</code></pre>
<button class="clipboard-button" data-clipboard-text="# on the command line
$ rails dev:cache
Development mode is no longer being cached.
">Copy</button>
</div>
<p>In <code>config/environments/development.rb</code> you can
set cache_classes to true, to get rid of extra sql requests.
But beware: now you have to restart the rails server after changing the source code!</p><div class="interstitial code">
<pre><code class="highlight plaintext"> # In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
# config.cache_classes = false
config.cache_classes = true
</code></pre>
<button class="clipboard-button" data-clipboard-text=" # In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
# config.cache_classes = false
config.cache_classes = true
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-30' href='slides_caching.html#/30'>◻</a></p>
<h3 id="ignore-this"><a class="anchorlink" href="#ignore-this"><span>6.1</span> Ignore this</a></h3><p>If you find <code>SHOW FULL FIELDS</code> queries in your log file or in rack-mini-profiler,
you can ignore them. These
queries are used by activerecord to find out which attributes an
object has. In production these will only occur when the first
object of a type is loaded, so you can savely ignore them.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-31' href='slides_caching.html#/31'>◻</a></p>
<h3 id="querycache"><a class="anchorlink" href="#querycache"><span>6.2</span> QueryCache</a></h3><p>If you look into the log file <code>logs/development.log</code> you will
see all the SQL queries made to the database, and also some that
are not really sent to the database.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-32' href='slides_caching.html#/32'>◻</a></p>
<p>Here are some lines from a log file:</p><div class="interstitial code">
<pre><code class="highlight plaintext">Started GET "/projects/2014-yokaisho" for ::1 at 2017-04-20 04:40:10 +0200
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"2014-yokaisho"}
...
User Load (6.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1
...
CACHE (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
...
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
...
CACHE (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
</code></pre>
<button class="clipboard-button" data-clipboard-text="Started GET "/projects/2014-yokaisho" for ::1 at 2017-04-20 04:40:10 +0200
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"2014-yokaisho"}
...
User Load (6.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1
...
CACHE (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
...
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
...
CACHE (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 953 LIMIT 1 [["id", 953]]
">Copy</button>
</div>
<p>What we can see here is that the Data for user 953 was loaded four times.
Somewhere in our rails app we call <code>User.find(953)</code> or similar ActiveRecord
methods four times.</p><p>But only the first time a SQL requests is really sent to the database. Loading
the data from the database took 6.9 ms here.</p><p>The next three times the same user was loaded, it was loaded from the
ActiveRecord QueryCache, which only took 0.1ms or less.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-33' href='slides_caching.html#/33'>◻</a></p>
<p>The default behaviour is that rails loads each model only once for each
HTTP request.
For the next HTTP request the QueryCache is cleard. So one request-responce
cycle is the lifespan of the cached object.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-34' href='slides_caching.html#/34'>◻</a></p>
<p>If you ever run into problems with the QueryCache, you can always
reload a model explicitly:</p><div class="interstitial code">
<pre><code class="highlight plaintext">user = User.find(953)
# will do SQL request
user = User.find(953)
# will use the query cache
user.reload
# bust the query cache, do a real SQL query
</code></pre>
<button class="clipboard-button" data-clipboard-text="user = User.find(953)
# will do SQL request
user = User.find(953)
# will use the query cache
user.reload
# bust the query cache, do a real SQL query
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-35' href='slides_caching.html#/35'>◻</a></p>
<h3 id="indexes-in-the-db"><a class="anchorlink" href="#indexes-in-the-db"><span>6.3</span> indexes in the db</a></h3><p>When we query the database by <code>id</code> we will get a rapid response:
the primary key is always accessed through an index.</p><p>But in this app the main way of identifying a resource is
through a "friendly url". For example the project show action
is not accessed through the conventional route</p><div class="interstitial code">
<pre><code class="highlight plaintext">/projects/1679
</code></pre>
<button class="clipboard-button" data-clipboard-text="/projects/1679
">Copy</button>
</div>
<p>but through</p><div class="interstitial code">
<pre><code class="highlight plaintext">/projects/2014-anton-eine-multimediale-inszenierung
</code></pre>
<button class="clipboard-button" data-clipboard-text="/projects/2014-anton-eine-multimediale-inszenierung
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-36' href='slides_caching.html#/36'>◻</a></p>
<p>In the miniprofiler we can see, that this translates to the SQL query</p><div class="interstitial code">
<pre><code class="highlight plaintext">SELECT * FROM projects WHERE slug='2014-anton-eine-multimediale-inszenierung'
</code></pre>
<button class="clipboard-button" data-clipboard-text="SELECT * FROM projects WHERE slug='2014-anton-eine-multimediale-inszenierung'
">Copy</button>
</div>
<p>There should be an index on columns <code>slug</code>!</p><p>We can check if this is the case in the database console:</p><p><img src="images/describe-1.png" alt=""></p><p>Yes, there is an index that is used for this query. Contrast this with the output
of <code>DESCRIBE</code> when there is no index:</p><p><img src="images/describe-2.png" alt=""></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-37' href='slides_caching.html#/37'>◻</a></p>
<h3 id="n-1-queries"><a class="anchorlink" href="#n-1-queries"><span>6.4</span> n+1 queries</a></h3><p>When analyzing the SQL queries generated by a Rails project, you’ll often encounter this common scenario: a one-to-many relationship, such as a project having many users. When displaying a project along with all its associated users, you may notice the <strong>n+1 query problem</strong>. This issue is present in our example app:</p><div class="interstitial code">
<pre><code class="highlight plaintext">SELECT * FROM `projects` WHERE `slug` = '2014-yokaisho' ORDER BY `projects`.`id` ASC LIMIT 1
SELECT * FROM `projects_roles_users` WHERE `project_id` IN (1622)
SELECT * FROM `users` WHERE `id` = 1033 LIMIT 1
SELECT * FROM `users` WHERE `id` = 1018 LIMIT 1
SELECT * FROM `users` WHERE `id` = 901 LIMIT 1
SELECT * FROM `users` WHERE `id` = 938 LIMIT 1
SELECT * FROM `users` WHERE `id` = 945 LIMIT 1
SELECT * FROM `users` WHERE `id` = 977 LIMIT 1
SELECT * FROM `users` WHERE `id` = 953 LIMIT 1
SELECT * FROM `users` WHERE `id` = 652 LIMIT 1
SELECT * FROM `users` WHERE `id` = 940 LIMIT 1
</code></pre>
<button class="clipboard-button" data-clipboard-text="SELECT * FROM `projects` WHERE `slug` = '2014-yokaisho' ORDER BY `projects`.`id` ASC LIMIT 1
SELECT * FROM `projects_roles_users` WHERE `project_id` IN (1622)
SELECT * FROM `users` WHERE `id` = 1033 LIMIT 1
SELECT * FROM `users` WHERE `id` = 1018 LIMIT 1
SELECT * FROM `users` WHERE `id` = 901 LIMIT 1
SELECT * FROM `users` WHERE `id` = 938 LIMIT 1
SELECT * FROM `users` WHERE `id` = 945 LIMIT 1
SELECT * FROM `users` WHERE `id` = 977 LIMIT 1
SELECT * FROM `users` WHERE `id` = 953 LIMIT 1
SELECT * FROM `users` WHERE `id` = 652 LIMIT 1
SELECT * FROM `users` WHERE `id` = 940 LIMIT 1
">Copy</button>
</div>
<p>Here 9 users belong to the project. They are loaded using 9 requests.
This is inefficient! If we were coding SQL by hand,
we could get the same data using one query with a join.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-38' href='slides_caching.html#/38'>◻</a></p>
<p>We can use rack-mini-profiler to find the code line that generated
the request:</p><p><img src="images/sql-project.png" alt="finding the source code for a sql request"></p><p>In this example, the ActiveRecord method that generate
the first request is in project_controller.rb, line 26</p><div class="interstitial code">
<pre><code class="highlight plaintext">@project = Project.friendly.find(params[:id])
</code></pre>
<button class="clipboard-button" data-clipboard-text="@project = Project.friendly.find(params[:id])
">Copy</button>
</div>
<p>Later, in the view and partials, the relationships from
@project to users is accessed.</p><div class="interstitial code">
<pre><code class="highlight plaintext">@project.users.each do |user| ...
</code></pre>
<button class="clipboard-button" data-clipboard-text="@project.users.each do |user| ...
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-39' href='slides_caching.html#/39'>◻</a></p>
<p>To get ActiveRecord to automatically load <strong>all the users</strong> for the project at once
we can change the line where the project is first loaded:</p><div class="interstitial code">
<pre><code class="highlight plaintext"># @project = Project.friendly.find(params[:id])
@project = Project.includes(:users).friendly.find(params[:id])
</code></pre>
<button class="clipboard-button" data-clipboard-text="# @project = Project.friendly.find(params[:id])
@project = Project.includes(:users).friendly.find(params[:id])
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-40' href='slides_caching.html#/40'>◻</a></p>
<p>After this change we find a lot less SQL requests:</p><div class="interstitial code">
<pre><code class="highlight plaintext">SELECT * FROM `projects` WHERE `slug` = '2014-yokaisho' ORDER BY `projects`.`id` ASC LIMIT 1
SELECT * FROM `projects_roles_users` WHERE `project_id` IN (1622)
SELECT * FROM `users` WHERE `id` IN (1033, 1018, 901, 938, 945, 977, 953, 652, 940)
</code></pre>
<button class="clipboard-button" data-clipboard-text="SELECT * FROM `projects` WHERE `slug` = '2014-yokaisho' ORDER BY `projects`.`id` ASC LIMIT 1
SELECT * FROM `projects_roles_users` WHERE `project_id` IN (1622)
SELECT * FROM `users` WHERE `id` IN (1033, 1018, 901, 938, 945, 977, 953, 652, 940)
">Copy</button>
</div>
<p>This makes a measurable difference:</p><p><img src="images/sql-include.png" alt="compare render times with and withoud include"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-41' href='slides_caching.html#/41'>◻</a></p>
<p>The project model has associations not only
with the user model, but with many other models too.
If we include them all, we end up with a sizable reduction in SQL queries:</p><div class="interstitial code">
<pre><code class="highlight plaintext">@project = Project.includes(:users, :roles, :assets, :urls, :tags).friendly.find(params[:id])
</code></pre>
<button class="clipboard-button" data-clipboard-text="@project = Project.includes(:users, :roles, :assets, :urls, :tags).friendly.find(params[:id])
">Copy</button>
</div>
<p><img src="images/sql-include-more.png" alt="compare render times with many includes"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-42' href='slides_caching.html#/42'>◻</a></p>
<h3 id="view-in-the-database"><a class="anchorlink" href="#view-in-the-database"><span>6.5</span> view in the database</a></h3><p>The last method of speeding up the database access is called a <strong>view</strong>. The word
view here has nothing to do with MVC in Rails, but is a technical term used in databases.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-43' href='slides_caching.html#/43'>◻</a></p>
<p>Let's look at a problem where a database view might be a solution:
in a previous version of the portfolio the display of a team member
was more elaborate:</p><p><img src="images/collaborator.png" alt="collaborator partial"></p><p>The collaborator partial showed information
about one team member: the thumbnail, the name, their degree program(s)
and the role(s) they had in the project.</p><p>For the <code>collaborators/_show</code> partial a lot of SQL queries are created.</p><p>The information about the degree programs is found in 2 different tables:</p>
<ul>
<li>studycourses</li>
<li>agegroups_studycourses_departments_users</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-44' href='slides_caching.html#/44'>◻</a></p>
<p>To display "MMT Bachelor 2010, MMT Master 2014"
for Mr. Huber the helper method <code>print_studycourses</code> is used. We can try out this
helper method in the rails console:</p><div class="interstitial code">
<pre><code class="highlight plaintext">> user = User.find(901)
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 901 LIMIT 1
> ApplicationController.helpers.print_studycourses(user)
Enrollment Load (0.5ms) SELECT `agegroups_studycourses_departments_users`.* FROM `agegroups_studycourses_departments_users` WHERE `agegroups_studycourses_departments_users`.`user_id` = 901
Studycourse Load (0.3ms) SELECT `studycourses`.* FROM `studycourses` WHERE `studycourses`.`id` = 3 LIMIT 1
Agegroup Load (0.4ms) SELECT `agegroups`.* FROM `agegroups` WHERE `agegroups`.`id` = 3 LIMIT 1
Studycourse Load (0.4ms) SELECT `studycourses`.* FROM `studycourses` WHERE `studycourses`.`id` = 5 LIMIT 1
Agegroup Load (0.4ms) SELECT `agegroups`.* FROM `agegroups` WHERE `agegroups`.`id` = 19 LIMIT 1
</code></pre>
<button class="clipboard-button" data-clipboard-text="> user = User.find(901)
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 901 LIMIT 1
> ApplicationController.helpers.print_studycourses(user)
Enrollment Load (0.5ms) SELECT `agegroups_studycourses_departments_users`.* FROM `agegroups_studycourses_departments_users` WHERE `agegroups_studycourses_departments_users`.`user_id` = 901
Studycourse Load (0.3ms) SELECT `studycourses`.* FROM `studycourses` WHERE `studycourses`.`id` = 3 LIMIT 1
Agegroup Load (0.4ms) SELECT `agegroups`.* FROM `agegroups` WHERE `agegroups`.`id` = 3 LIMIT 1
Studycourse Load (0.4ms) SELECT `studycourses`.* FROM `studycourses` WHERE `studycourses`.`id` = 5 LIMIT 1
Agegroup Load (0.4ms) SELECT `agegroups`.* FROM `agegroups` WHERE `agegroups`.`id` = 19 LIMIT 1
">Copy</button>
</div>
<p>Here, data from three database tables is combined.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-45' href='slides_caching.html#/45'>◻</a></p>
<h4 id="creating-a-database-view"><a class="anchorlink" href="#creating-a-database-view"><span>6.5.1</span> creating a database view</a></h4><p>In the database console we can build a simple select statement with two
joins to get the same information:</p><div class="interstitial code">
<pre><code class="highlight plaintext">mysql> SELECT user_id, concat(studycourses.name, ' ', year) AS name
FROM agegroups_studycourses_departments_users x
LEFT JOIN studycourses ON (x.studycourse_id=studycourses.id)
LEFT JOIN agegroups ON (x.agegroup_id=agegroups.id)
WHERE user_id=901;
+---------+-------------------+
| user_id | name |
+---------+-------------------+
| 901 | MMT Bachelor 2010 |
| 901 | MMT Master 2014 |
+---------+-------------------+
2 rows in set (0,01 sec)
</code></pre>
<button class="clipboard-button" data-clipboard-text="mysql> SELECT user_id, concat(studycourses.name, ' ', year) AS name
FROM agegroups_studycourses_departments_users x
LEFT JOIN studycourses ON (x.studycourse_id=studycourses.id)
LEFT JOIN agegroups ON (x.agegroup_id=agegroups.id)
WHERE user_id=901;
+---------+-------------------+
| user_id | name |
+---------+-------------------+
| 901 | MMT Bachelor 2010 |
| 901 | MMT Master 2014 |
+---------+-------------------+
2 rows in set (0,01 sec)
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-46' href='slides_caching.html#/46'>◻</a></p>
<p>Any Query can turned into a <strong>view</strong> by prepending it with <code>CREATE VIEW ... AS</code>.</p><div class="interstitial code">
<pre><code class="highlight plaintext">mysql> CREATE VIEW degree_programs AS
SELECT user_id, concat(studycourses.name, ' ', year) AS name
FROM agegroups_studycourses_departments_users x
LEFT JOIN studycourses ON (x.studycourse_id=studycourses.id)
LEFT JOIN agegroups ON (x.agegroup_id=agegroups.id);
Query OK, 0 rows affected (0,06 sec)
</code></pre>
<button class="clipboard-button" data-clipboard-text="mysql> CREATE VIEW degree_programs AS
SELECT user_id, concat(studycourses.name, ' ', year) AS name
FROM agegroups_studycourses_departments_users x
LEFT JOIN studycourses ON (x.studycourse_id=studycourses.id)
LEFT JOIN agegroups ON (x.agegroup_id=agegroups.id);
Query OK, 0 rows affected (0,06 sec)
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-47' href='slides_caching.html#/47'>◻</a></p>
<p>After the view has been created, we can use <code>degree_programs</code> like a
table in the database. In fact, Ruby on Rails does not know that
this is not a "normal" table.</p><div class="interstitial code">
<pre><code class="highlight plaintext">mysql> SELECT * from degree_programs WHERE user_id=901 ;
+---------+-------------------+
| user_id | name |
+---------+-------------------+
| 901 | MMT Bachelor 2010 |
| 901 | MMT Master 2014 |
+---------+-------------------+
2 rows in set (0,00 sec)
</code></pre>
<button class="clipboard-button" data-clipboard-text="mysql> SELECT * from degree_programs WHERE user_id=901 ;
+---------+-------------------+
| user_id | name |
+---------+-------------------+
| 901 | MMT Bachelor 2010 |
| 901 | MMT Master 2014 |
+---------+-------------------+
2 rows in set (0,00 sec)
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-48' href='slides_caching.html#/48'>◻</a></p>
<h4 id="model-and-relationships-for-the-view"><a class="anchorlink" href="#model-and-relationships-for-the-view"><span>6.5.2</span> model and relationships for the view</a></h4><p>In Rails we can define a model for the database view, just like for any table:</p><div class="interstitial code">
<pre><code class="highlight plaintext">app/models/degree_program.rb
class DegreeProgram < ApplicationRecord
belongs_to :user
def to_s
name
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="app/models/degree_program.rb
class DegreeProgram < ApplicationRecord
belongs_to :user
def to_s
name
end
end
">Copy</button>
</div>
<p>And add a relationship from user:</p><div class="interstitial code">
<pre><code class="highlight plaintext">class User < ApplicationRecord
...
has_many :degree_programs
</code></pre>
<button class="clipboard-button" data-clipboard-text="class User < ApplicationRecord
...
has_many :degree_programs
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-49' href='slides_caching.html#/49'>◻</a></p>
<p>back in the rails console we can now use this new model:</p><div class="interstitial code">
<pre><code class="highlight plaintext">> user = User.find(901)
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 901 LIMIT 1
> user.degree_programs.join(', ')
DegreeProgram Load (0.6ms) SELECT `degree_programs`.* FROM `degree_programs` WHERE `degree_programs`.`user_id` = 901
=> "MMT Bachelor 2010, MMT Master 2014"
</code></pre>
<button class="clipboard-button" data-clipboard-text="> user = User.find(901)
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 901 LIMIT 1
> user.degree_programs.join(', ')
DegreeProgram Load (0.6ms) SELECT `degree_programs`.* FROM `degree_programs` WHERE `degree_programs`.`user_id` = 901
=> "MMT Bachelor 2010, MMT Master 2014"
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-50' href='slides_caching.html#/50'>◻</a></p>
<p>And in a final step we can refactor the helper method print_studycourses</p><div class="interstitial code">
<pre><code class="highlight plaintext"> def print_studycourses(student)
student.degree_programs.join(', ')
end
</code></pre>
<button class="clipboard-button" data-clipboard-text=" def print_studycourses(student)
student.degree_programs.join(', ')
end
">Copy</button>
</div>
<p>This reduces the number of SQL statements to one per collaborator partial:</p><p><img src="images/view.png" alt="view"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-51' href='slides_caching.html#/51'>◻</a></p>
<p>If we add a relationship from projects to degree_program (actually: three <code>has_many through:</code> steps to get from projects to collaborators,
and from collaborators to users, and from users to degree_programs), we can also include degree_programs in our
includes statement when loading the project:</p><div class="interstitial code">
<pre><code class="highlight plaintext">@project = Project.includes(:users, :roles, :assets, :urls, :tags, :degree_programs).friendly.find(params[:id])
</code></pre>
<button class="clipboard-button" data-clipboard-text="@project = Project.includes(:users, :roles, :assets, :urls, :tags, :degree_programs).friendly.find(params[:id])
">Copy</button>
</div>
<p>This way we end up with only very few sql queries, and a big performance improvement:</p><p><img src="images/before-after.png" alt="final state of the app"></p></div>
<div class='slide'>