The Network Time Protocol (NTP) synchronizes computer clocks across networks by having clients exchange timestamped packets with servers to calculate and correct time offsets, using a hierarchical stratum system where stratum 0 represents atomic/GPS clocks, stratum 1 synchronizes to those, and subsequent strata propagate synchronization, with clients adjusting their clocks gradually using clock skew algorithms to avoid perceptible jumps while achieving millisecond-level accuracy.
Network Time Protocol (NTP) Explained: Synchronizing Internet Clocks
Added:today we're looking at Network time protocol and uh this is how you synchronize couple of computers well well in fact the whole world uh potentially I got into this um sort of by accident I met Dave Mills who is the Godfather or something of ntp he did all the early work on it and I think he's still involved so I happened to meet him at a conference and uh he he was telling me a few anecdotes about it and so when I got back home I thought all right I have to have a play with this CU at the time we only had a few computers slightly networked and it didn't really matter too much whether they were running at the same time everybody you know just look at your wristwatch type in the time that's that's good enough uh but there are some applications where you really need the time to be uh a lot closer particularly when you share file systems so if one computer writes a file and puts a time stamp on it and the other one looks at it it's not too bad if this one is behind that one but if it's ahead of it this computer gets a bit confused and says well this time hasn't happened uh you some will make crazy decisions based on that particularly with the make program that uh looks at time stamps a lot so Dave Mills came up with this um process to synchronize computers and I sort of I remember chatting with him and thinking that's an interesting problem how do you synchronize because if you going talk to that computer and say what time have you got and it comes across and it says you know the time is 10:32 and 57 seconds or something like that you think well is that now or how long has it taken to get from here to there if I do it again it may well be different because it may come over quicker or it may come over slower so um anyway that's that's kind of where we got started with this and uh sorry had a beer with him or something I I forget it was a long time ago but uh anyway so I thought it would be interesting just to to show how this this all works so it's all based on an ntp packet so there's a this is 32 bits I think across here so there's a whole lot of little flags and bits uh which you often get in protocols and in the very first bite that tell you I think this is the version and this this one actually tells you whether there's a leap second happening today because it even cers for that and there's a stratum here and I think that's the version I forgot what goes in there but uh it's it's not quite so important then there's some other stuff but the key bit are some time stamps that come in so there's a t T1 T2 and there's also a T3 but that doesn't actually appear in it and then there's some other stuff down here which is to do with authentication and authorization so if you need to make sure that you're not uh getting bogus packets from places so this didn't used to be in the first version because everybody trusted everybody back in the uh back in the day but uh since then everybody's got much more um focused on security so what you do to synchronize clocks is we have a client on the server and uh the client wants to synchronize to the server so it sends out a packet with its t0 which is uh this is my time this is what I think my time is and as soon as it arrives into the server it stamps it with T1 that's service time so this would then T1 minus t0 is is how long it took to get across there that would be if the clocks were right yes but but at this point they're not right we're almost always the case so then it does some processing internally because it it might be handling lots of things and then eventually it's going to send back a packet and it will put T2 in which is my time when I have actually sent this out and when it comes back in you do record when it comes back in T3 and you don't need to fill it into the uh the protocol packet because um you you're finished with that so you can see probably that these two times are on the same machine so they are relatively the same you know if you subtract T3 minus t0 then that's how long this whole thing has taken but you can improve that by also um removing the t2 minus T1 because that's how long it took to process that this gives you a fairly good estimate of the round trip minus the servers processing uh then the uh the difference in clocks is uh T1 minus t0 you know this is server's clock and my clock and you know if that that equals zero then they're synchronized uh it it never does but you want to get closer and closer to that so if it's uh guess if it's negative it's one way and if it's positive it's the other way you then have to adjust your clock and say oh we're out by three or 4 seconds so that's usually why you you apply this clock skew algorithm to say right keep uh we're we're a bit ahead so keep uh taking instead of every second moving on a second we'll move on you know 0 N9 of a second and eventually we'll get closer and closer to the real thing uh there's actually now a system call to do that that uh in certain Unix is anyway I believe Windows has something similar so that you don't get these huge jumps in time that uh time moves slowly and imp percept onwards but to get it even better you do this a number of times and you also do it to a number of uh servers if you can so you get a sense of um different times from different servers and they should all be fairly close if they're all uh sort of playing the game but some of them you will get much better uh roundtrip times uh but more importantly some of them you'll get a very stable roundtrip time so it doesn't vary you know if you if one takes a second and then the next one is 5 Seconds and you never quite know where you are so it has within the uh the protocol the protocol State machine a way to work out which is the best and say well this this one is a very good source of time but it's um it's moving around too much so I'm I'm not going to trust that uh the communication links are just too bad it's perhaps in South America or somewhere like that uh but this one is not quite as good but uh at least it's stable this is where the stratum comes in this St so you can have 16 stratums I think what does stratum mean then so a zero stratum is a is a really good clock so it's like an atomic clock or a GPS clock or something like that and a stratum one is a computer that's using one of those to synchronize itself so you never actually talk to AUM zero and asum 2 is something that synchronized to Aon one and ason 3 to kind of hops away from theultimate sources yeah so that all sort of feeds into it it would prefer to synchronize to a stratum one because you're closer to it but this Jitter and the fact that um quite a lot of the stratum one clocks are kind of a bit locked down and don't trust anyone because otherwise everybody would synchronize to that so you you go backwards and forwards uh between them how often is this happening uh well it happens quite quickly initially I think it's every minute uh but once it settled down and decide where it is it it slowly backs off until it's about every 10 minutes it sends a packet out and says um are you still right because by now it synchronized it but not originally we had connections across the internet to just one place that had one of these St one or two things so we were setting it from that and then we were Distributing it all around the department because we only had a very thin wire at that point which was great because you so You' see this stratum one come in and we would all be stratum 2 because we were connected to that but when you lose that connection it it's quite interesting what happens so each of the computers looks around and says oh I've lost that one but there's a whole load of stratum twos around here so I'll synchronize to one of those and they're all saying oh yes there's a I lost that but there's a whole load of stratum twos so I'll synchronize to that so that makes me a three and this one says oh that was a two minute ago it's now a three so I'm now a four and this one says well that that's a four now so I'm a five so they they move up until they get to 16 and then they sort of they kind of tap out at that point and say well yeah there doesn't seem to be anything uh anything reliable anymore so that that's kind of where where they give up on that and how accurate can it be it can get very very accurate you can certainly get to milliseconds or or even better now I think I think the protocol has it down to microsc one of the things in my late night conversation with Dave Mills is he spent half his life I think watching clocks and seeing how they drifted so when uh the UK first came online and uh started started synchronizing he was watching uh some of the early traffic across that and he noticed at sort of 5:00 all the clocks started to drift and then they sort of got back into sink at about 7:00 only by small amounts and uh this was because you that is when everybody gets thrown from work puts the kettle on the power grid suits up and the 50 HZ signal slowly moves slightly as they try and maintain the grid level but that influences the clocks so the clock all sort of drift and then when the load diminishes they they put it up a bit so all the clocks move back a bit but he said I could tell you know I can tell when coronation streets on and I can tell when the uh the adverts are on because I can see see this Gap here so for for those not in the UK these are kind of popular so popularion with a break halfway through that everybody rushed out put the kettle on to have a cup of tea you know being British so he could detect that he reckoned um in one case there was an earthquake in um San Francisco I think U and he said the clocks went a bit spooky beforehand I could see them drifting around I wonder if I've got an earthquake detection device I don't think he ever followed that up but uh there's a few cases like that where he said you know I could see strange things happening because I got this high level of precision fun for Robotics and for problem solving which is your by choosing the car 10% of your Journeys take you 71 four d d a b c and d and I tell you that D A has a value four how much did you learn about the data set
Up Next

Async Rust with Tokio: A Comprehensive Starter Guide
@dreamsofcode
116.1K views•2023-03-01

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

Monads in Haskell Explained: Maybe Monad & Effects
@Computerphile
647K views•2017-11-24

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science
















![Day 1: Cisco CCNA 200-301 || Building Your Networking Foundation, Networking Basics & More [English]](https://i.ytimg.com/vi/Xc8cN1LqT7M/maxresdefault.jpg)








![[#SemanaCap 3] Curso “NTP e NTS”](https://i.ytimg.com/vi/UmkZQc6rF2k/maxresdefault.jpg)









![CMU Database Systems - 26 Systems Potpourri [CockroachDB, Spanner, MongoDB] (Fall 2018)](https://i.ytimg.com/vi_webp/n75Mcv5iF7s/maxresdefault.webp)



