简体   繁体   中英

Sync time between servers

I have a web server, that process events from several sources / machines. For each event I give a timestamp, and store it in the DB. This way, even if not all of the clocks in the source machines has the exact time - it doesn't matter. the order of the events will be preserved, because the server is responsible to the timestamp.

Now, I want to add one more server that can handle these events, and store them in the same DB. What is the best way to synch the clock between the servers? the servers are deployed on costumer site, and although i can instruct him that the clocks must be synch- i want to ensure that.

Is there any way to solve it without communicating between the servers? Is there any way to use the DB machine clock, in SQL statement? (I should support mysql, sqlserver and oracle. My O/R mapping is hibernate).

  1. Synchronizing application servers times (NTP etc) isn't going to 'ensure' perfect ordering of events across them. The two servers have different timelines, and there is no way they can independently decide the 'order' in which events in both occur. This is quite well known: http://en.wikipedia.org/wiki/Lamport_timestamps

  2. You said 'same' DB => This is a good place to decide the 'order' of events. But the 'event' will be 'arrival of INSERT request at DB'. Is this acceptable to you?

  3. If #1 is acceptable, and the 'order' of occurance matters, you're better off using an auto increment field: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

You can still keep the timestamp, just don't use it to decide 'order'.

It sounds to me like you are trying to load balance the process so that you have two servers that can process the events. As mentioned in the comments, using NTP on the server would be the best way.

If you can't guarantee this though, why not make your code call out to a NTP server of your choosing? If all the applicable servers call out to the same NTP server, you can be assured that the times are consistent. The time from the NTP server can then be stored in the DB or whatever else you want to do with it.

NTP Utilities have 2 types of time synchronization.

  1. Get Time and stamp it now. (ntpdate)
  2. Get Time and gradually sync the time between your time provider and consumer. (xntpd)

ntpdate needs a command line parameter, the ip address of the time sever. xntpd needs you to configure (the same ip address) in ntp.conf. xntpd then needs to be run as a daemon. It communicates with the time provider and gradually corrects the time and keeps your time consumer in sync with the server at all times, to as good as nanoseconds.

It is a good practice to configure and use xntpd. xntpd also calls ntpdate when it starts to get as close as to the server at the first time on.

So depending on your requirement, you can either call ntpdate at regular intervals (using cron, may be 1 time a day, since time does not drift more than a few milli seconds) or just configure xntpd and be done with it.

If you choose xntpd, you may need a way to know if you are synchronized. For that you need to run a command line interactive tool called ntpq. This will help you get the status of the synchronization.

To understand ntpq in more detail refer to http://www.novell.com/coolsolutions/trench/5730.html (Note, in this technote, rvi command is specific to netware, unix only has a rv command)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM