WEBVTT

00:00.320 --> 00:00.980
Hey, everyone.

00:00.980 --> 00:02.300
And welcome back.

00:02.300 --> 00:08.510
Now, in the earlier lectures we were discussing about the Http cache control headers and we looked

00:08.510 --> 00:11.150
into the store header in detail.

00:11.150 --> 00:17.240
So continuing our journey with the cache control headers today we'll be speaking about if modified since

00:17.240 --> 00:17.770
header.

00:17.780 --> 00:25.130
Now this is quite important header and this specific header is sent quite a lot by the browsers.

00:25.130 --> 00:29.480
So let's go ahead and understand more about this header.

00:29.720 --> 00:36.890
Now, we had looked into the scenarios where there can be multiple same requests by the browser, so

00:36.890 --> 00:45.590
if a user refreshes the page 5 or 10 times, then what Ideally a cache does is it sends the response

00:45.590 --> 00:49.190
from the cache itself instead of connecting to the web server.

00:49.190 --> 00:51.500
So let's look into the scenario.

00:52.350 --> 00:57.450
So let's assume there is a file which is already cached in the.

00:58.690 --> 00:59.830
Cash server.

00:59.830 --> 01:05.170
So what happens is a client sends a request of get my C dot PNG.

01:05.740 --> 01:14.320
Now since the cache already has this file in its local storage, what it will do is it will send the

01:14.320 --> 01:17.460
file from the local store itself.

01:18.160 --> 01:20.440
Now, this is quite good enough.

01:20.440 --> 01:27.400
But the problem is what happens if the file in the web server side is updated?

01:27.520 --> 01:33.640
Now, in that case, the caching server has sent a older response.

01:33.640 --> 01:42.960
That means the new updated file is not sent because of the cache servers misconfiguration related settings.

01:42.970 --> 01:52.840
So in order to solve this kind of a issue where a older response might be sent, a new header is generally

01:52.840 --> 01:55.240
used as if modified since header.

01:55.390 --> 02:04.510
So what really happens is let's assume you have a this is a my C dot PNG file which is there in the

02:04.540 --> 02:09.220
web server side and it is also there in the cache server side.

02:09.520 --> 02:19.910
Now when a client sends a request, a get request on my c dot PNG, although the cache server has this

02:19.910 --> 02:28.130
file, it will still verify whether the file which is there in its local cache is the latest updated

02:28.130 --> 02:28.640
one.

02:29.420 --> 02:35.900
So how can a cache server know how this file is updated or not?

02:35.900 --> 02:39.680
So what it can do is the cache server will send.

02:41.670 --> 02:51.510
A get request on my PNG asking whether this file is modified since this specific amount of time.

02:51.510 --> 03:01.830
So this time is basically the time associated with the last modified header of the specified PNG file.

03:02.070 --> 03:08.580
So the cache server will send a get request asking if this file is modified or not.

03:08.580 --> 03:10.350
After this specific time.

03:10.500 --> 03:22.050
If it is not modified then the web server will send a response saying 304 not modified and then the

03:22.050 --> 03:27.390
cache server will serve the file from the local cache itself.

03:27.600 --> 03:34.880
However, if this file is modified, then the web server will not send 304.

03:34.890 --> 03:37.410
It will send a 200 with a new file.

03:38.490 --> 03:39.120
Perfect.

03:39.120 --> 03:40.840
So let's do one thing.

03:40.840 --> 03:45.730
Let's go into the practical and let's understand this in great detail.

03:46.880 --> 03:54.470
So we have been looking into the practical and we'll try and keep the link same.

03:54.470 --> 03:58.790
So you have Dexter Labs dot in slash JPG.

04:00.710 --> 04:07.520
Now if you do a curl hyphen I on this specific URL

04:10.220 --> 04:20.120
you will see there is one specific header called as last modified and it has a modified or it has the

04:20.120 --> 04:24.320
time at which this file was specifically updated.

04:24.470 --> 04:25.880
My c dot jpg.

04:26.150 --> 04:28.520
Now let's.

04:29.790 --> 04:35.250
Take the example of a Docker instance so that it will become much more easier for us to do practical.

04:35.460 --> 04:41.170
I'll do a curl hyphen I and it contains the last modified time of this specific file.

04:41.190 --> 04:52.110
Now if you look into the I'll copy this key and in my Chrome browser if you look into the about cache.

04:55.370 --> 04:58.580
You see, this specific file is already cached.

04:58.700 --> 05:07.220
Now, whenever I enter this in my URL, the entry should be retrieved from the cache itself.

05:07.250 --> 05:17.990
However, in order to make sure that the older file is not sent, what browser will do, what the browser

05:17.990 --> 05:25.970
cache will do is it will send a get request to the server with a if modified since header.

05:26.750 --> 05:28.430
So let's try this out.

05:29.510 --> 05:36.650
Just remember that this file was modified on Sunday 18th October 2015.

05:37.040 --> 05:38.300
Quite old file.

05:39.650 --> 05:42.730
So this is basically a PNG file.

05:42.740 --> 05:46.640
We are actually recording this lecture at the end of 2017.

05:46.850 --> 05:51.410
But anyways, so let's do one thing.

05:51.980 --> 05:56.150
I'll do a tail on var log nginx access dot log.

05:59.350 --> 06:09.250
And now let's open this file up and let's refresh.

06:09.340 --> 06:11.920
Okay, Now, if we look into the

06:14.380 --> 06:16.580
Enginex access log, what do you see?

06:16.600 --> 06:18.330
You see A304.

06:18.340 --> 06:27.280
So the reason why you're seeing A304 is because my browser cache, my browser cache asked It sent a

06:27.310 --> 06:32.440
get request on the file name asking whether it was modified.

06:32.440 --> 06:39.520
Since now since the file was not modified, the web server sent A304 not modified response back.

06:40.540 --> 06:47.940
So this is the basic let let me show you this output in tcpdump.

06:47.950 --> 06:54.040
So I do a tcpdump hyphen, a hyphen I zero and on port 80.

06:54.910 --> 07:02.630
Now this will give you a much more clear idea just remember, this is the last modified time.

07:02.850 --> 07:07.310
Now I'll start my tcpdump capture.

07:11.090 --> 07:12.980
Okay, so the capture has started.

07:14.390 --> 07:15.380
I'll refresh.

07:15.380 --> 07:17.960
I'll refresh once and again.

07:17.960 --> 07:21.560
The browser must have sent a if modified since header.

07:22.070 --> 07:23.210
Let's see.

07:24.620 --> 07:30.830
And here if you see this is basically the let me go up.

07:36.350 --> 07:37.910
Let's go.

07:38.120 --> 07:38.540
Perfect.

07:38.540 --> 07:42.140
So this is basically the file.

07:42.140 --> 07:50.420
So now you see, this is the if modified since header where the browser is asking whether this file

07:51.380 --> 08:00.470
you see the get request is get Docker dot jpg because we actually requested for Docker dot jpg and now

08:00.950 --> 08:10.100
there is a header which browser has added saying whether this file has been modified since this specific

08:10.100 --> 08:10.610
time.

08:10.610 --> 08:17.480
So whenever a browser or a cache server stores a file, this specific file is stored in the cache.

08:17.480 --> 08:27.080
It will note the last modified date, so it will note this specific date and whenever the cache server

08:27.080 --> 08:35.280
will send a request to the web server in the if modified, it will append the date of the last modified.

08:35.280 --> 08:45.270
So since the last modified is 18th October 2015, it has appended that specific date and said whether

08:45.270 --> 08:48.780
this file is modified since this specific time.

08:48.780 --> 08:54.690
Now, if it is not modified, then the web server.

08:54.690 --> 09:02.310
If you look into the next response, the web server has sent that this specific file is not modified

09:02.310 --> 09:04.440
after this specific date.

09:04.440 --> 09:14.130
And now since my cache or a web browser now received a 304 response, what it will do is it will serve

09:14.160 --> 09:17.120
the file from the cache itself.

09:17.130 --> 09:23.340
Now, one important thing to remember is that whenever a web server sends a 304 not modified, it will

09:23.340 --> 09:24.300
not send a file.

09:24.300 --> 09:28.530
It will just send a response without a file.

09:29.130 --> 09:34.590
So this is the basics about the if modified since header.

09:34.590 --> 09:40.020
I hope you got the basic understanding on the importance of if modified since header.

09:40.200 --> 09:43.200
I hope this lecture has been informative for you.

09:43.200 --> 09:49.650
Again, go ahead and try this out because practical is very important and if you have any issues you

09:49.650 --> 09:51.810
can let us know in our forums.

09:51.810 --> 09:52.920
So this is it.

09:52.920 --> 09:58.560
If you have any questions, doubts, suggestions, feel free to connect us at Twitter, Facebook or

09:58.560 --> 10:02.760
LinkedIn or email us at instructors at the Rate Labs dot n.

10:02.790 --> 10:04.020
Thanks for watching.
