How Computers Synchronize Their Clocks - NTP and PTP Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
oh i wonder what the time is oh i don't have my watch hey gary do you have the time you went over time oh no sorry let me ask gary what time is it oh it's four minutes past 12 and 30 seconds hey gary gary says it's four minutes past 12 and 30 seconds okay thanks now that's how we might ask what is the time but the question is how does a computer ask what is the time but if you want to find out more please let me explain okay so most computers have clocks and everything from the clock widget on your desktop meeting reminders posts on social media online shopping they all use the time now many devices have a battery backed clock rtc like your pc for example but some devices don't for example the raspberry pi does not come with battery-backed uh real-time clock smartphones rely of course on the main battery to have enough power and even when your phone shuts down there's often enough power to keep the clock uh going but in all cases all of these devices need to check they have the correct time and synchronize it to make sure it is accurate now computer clocks tend to drift because they're not really a very expensive part in the whole kind of sum of everything so for example i switched off synchronization on a desktop pc and on a raspberry pi and they both started to lose about 0.1 seconds per day which means after a couple of weeks there'll be about two seconds out now i have a pc that i use that's not connected to the internet at all it's an audio visual setup that i have to use and during the course of the year that clock can be wrong by several minutes so it's very important that computers synchronize their time to actually get the correct date and time uh for all the things that we do the simple things we do like emails and uh buying things online there are some computers that need very very high computer accuracy for time for for example financial transactions in the stock market you know fractions of a second who got that buy order or that seller order in first becomes important the same with telecommunications power distribution factory automation all of these areas need sub one second they need very good um kind of you know accuracy for the clocks research labs you're doing particle you know physics uh research then you it's even better you need even greater definitions accuracy of time data centers need accurate time distributed databases particularly need uh accurate time so it's not only for the stuff that we do simple things like social media or buying you know a new packet of tissues online you act there are some areas where really really high accuracy is important for for the jobs that the people are doing this is no time to argue about time we don't have the time now how do any of us know what is the time now without getting philosophical or metaphysical we need a reliable time source now that's not going to be the watch on your wrist it's not going to be the kind of the clock on your kitchen wall okay we need a gold standard at the moment that's generally global navigation satellite systems gn ss which includes gps glonass galileo and there are there are several others of course there are also atomic clocks and radio clocks and these also offer high precision time keeping now these level of you know atomic clock kind of things of course gps satellites and they've got atomic clocks in them atomic clocks are generally called stratum zero devices and they broadcast the time because that's the whole way that global uh navigation satellites work they broadcast a time and you can work out your your position based on the time now servers somewhere will synchronize with a stratum zero device and they will offer synchronization services over a network and they're called stratum one devices or primary time servers so there are lots lots of these primary time servers dotted around all around the world and then you can actually have further servers that are further connected to them so what you actually get then is this layer approach stratum so stratum zero is the satellites stratum one may be these primary time servers who's kind of connected up to all these maybe connect up to a radio clocked an atomic clock and they really know the time and then you get these servers that are connected to them directly and then other servers would connect to them and you can actually get up to 15 layers of uh stratum once you get to the 15th layer you you're not going to take the time from the 15th layer one because that is the bottom layer but from 14 upwards but generally we get one two three maybe four kind of knocking about on the internet now the most common system for synchronizing clocks on servers pcs laptops smartphones tablets smart tvs etc is ntp the network time protocol ntp is one of the oldest internet protocols that's currently being used it was designed by david mills the university of delaware in the 1980s now typically an ntp client polls one or more ntp servers and requests the current time the correct time and accuracy is normally measured in tens of milliseconds so you know that the uh the clock on your pc is uh accurate within 50 milliseconds 80 milliseconds and of course if you have don't have a grasp of milliseconds microseconds nanoseconds we're going to talk about them but i do have a whole video about this which i recommend you check out now there's another uh approach to doing a time synchronization called the precision time protocol ptp now it uses special hardware to create timestamps so it doesn't use it through the software actually does it at the kind of at the network level down by the network card and it's able to get a much more accurate timing of how these packets fly about on the on the network and what happens in ptp it's a bit different to ntp there's one node which is designated to be the grand master in any particular net up network setup and the ground master assumes a leader role and it sends out synchronization messages to its followers so rather than the ntp client asking for the time with uh ptp the grand master sends out the time there are still some other messages that go back and forth trying to measure delay and we'll talk about that in a minute now accuracy with ptp is less than one microsecond so therefore it's measured in nanoseconds now the white rabbit network by cern uses ptp for sub nanosecond accuracy and picosecond precision of synchronization over distances that you measure in kilometers so that's a really really precise use of this technology so how does it work well we're going to use ntp as an example but the concepts about delays and things which talk about a minute are the same for ptp uh but we'll just use ntp so a client sends a request to the time server the time server receives the quest processes it and sends back a reply and then the client receives the reply basically what time is it uh it's uh 10 30 and 21 seconds and that's basically what happens so the client asks for the time and the server gives it back the correct time but but what about the network delays it takes time for the request to reach the server particularly if you're doing it over the internet it's gonna go down through your local network over your wi-fi even out through the uh the you know the modem or the router that you've got from your your network provider that goes out onto the internet might go across a couple of countries it might hit the uh type primary time server and then it has to process that and it's getting in lots of other requests as well your request gets processed and then it has to send it back it goes over a couple of countries goes through into your local internet provider finally it comes all the way over your wi-fi and gets back to your smartphone or to a laptop and it can say oh great i know what the time is now and also what happens at the time when your client is wrong so you know it's it's a few minutes out so the time it gets back and the time that it's sent they're different so how can it know how long that took if it's trying to calculate the delay well these are all very good questions and thankfully there is some very good answers okay so the time that the client sends its request we'll call t0 that's according to the time that the client understands now it could be the wrong time but the time it knows is the correct time it sends the request by time that then gets to the server it's uh noted down as t1 the time the request was received the server then processes it it then sends back its reply the time it sends back its reply is known as t2 it's probably the time it's also sending back and then finally when it received again at the other end that's t3 the time the reply was received so as we said t1 is a time it was uh sent t1 received t2 replies sent t3 now here are the maths the offset okay is t1 minus t0 so that's this one the time the timestamp was received minus when it was sent plus t2 minus t3 all divided by two and if you if it's the other way around the one time is ahead of the other you get minus numbers you just take the absolute value and you can also work out the round trip delay t3 minus t0 so t3 minus t0 t2 minus t1 and that will give you these two numbers offset and round trip delay well that's all very well just looking at the the mathematic symbols here but let's actually go through a working example and you actually see that it does actually work out really quite neatly so here's our algorithm at the top just so we remember those math now let's say it's 1701 exactly on the client but that time is wrong the actual time is 1701 and 30 seconds so it's 30 seconds out and on the server our primary time server that's the correct time so t0 is at 1701 the wrong time and it gets sent through to the server now let's say it takes two seconds to get to the server we're just using big nice round numbers here so t1 is 1701 and 32. notice now we've gone from 0 0 to 32 why because the actual real time of course is 1701 and 32 because it's 30 seconds wrong so t1 is 1701 32 it takes a second let's say to process that request and then a reply is sent at 1701 33 so 30 seconds different from the client and now three seconds have gone past so here's our real time 17 133 and then finally that goes all the way over the internet and it gets to our client t3 when it's 1701.05 now it's not 35 because it's got the wrong time of course so it's five seconds so from when it sent off the request when it got a reply let's say it took five seconds and it's got back a reply saying the time is 1701 33. it's okay so it knows its time is wrong but it actually that's not the time because at the moment when it gets actually 17 35 so it would be a two second wrong even if it fixed the 30 second delay it was to be wrong but if we do the math as we looked at so 32 so what's that t1 minus t0 t1 minus t0 well that's 32 seconds that's what we've got in there plus 28 what's that t2 minus t3 t2 minus 3 t3 is 28 seconds okay divided by 2 is equal to 30 seconds so there's a 30 second difference between the clock on the client on the server yes that's correct we know that brilliant what about the delay well the delay was t3 minus t0 so oh five minus that's five seconds minus t2 minus t1 is one second so that's four seconds for the trip there and back which is two seconds for the round for a one part the trip so at t3 here when this the client receives the reply it can set the correct time to whatever was t2 plus the one way delay which is two seconds so it sets it to 17 or 135 which is correct hooray or it could take its own time t3 170105 plus that 30 seconds that we calculated as it becomes 21.01 30 and that's it so there you can see a simple example by the fact that the client and the server are both able to put in these protocols their timestamps then actually we're able to work out a fairly accurate time for the client and here's an example on linux machine actually showing you it synchronizing with some primary time servers so you can see some here europe and internationally and those are the names the domain names of the devices that it's actually syncing to as you can see some of them are stratum two there's one there that's a stratum three okay so we can get these different levels of a time server and we go away and talk to them obviously you'd think something to be better but as you can see here at this end one actually there's less of a delay and that's probably because of maybe it's physical location but you can see that generally 50 30 80 there's around between 30 and 80 milliseconds uh difference between the actual correct time and the time that the um machine is able to set for itself okay so there you have it that's how the computer asks for the time we looked at all the algorithms for asking over the internet seeing the delays and how that gets calculated ptp and ntp use a very similar system for working out the delays ptp is more precise and this is another video in my series about time there's more videos coming so do stick around for those okay that's it i'll see you in the next one you
Info
Channel: Gary Explains
Views: 23,221
Rating: 4.9504814 out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, network time protocol, precision time protocol, ntp, ptp, atomic clocks, round-trip time, synchronize clocks, accurate time service, public NTP service, Stratum 0, Stratum 1, GNSS, global navigation satellite system, GPS, GLONASS, Galileo, Ntpd, Chrony
Id: WX5E8x3pYqg
Channel Id: undefined
Length: 14min 12sec (852 seconds)
Published: Thu Sep 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.