JoeGeeky
JoeGeeky

Reputation: 3796

Synchronizing time between Roles in Azure

I recently pushed a number of new Web and Worker Roles to Azure which use the Cache infrastructure to share state during one of our business processes. In this case, Web Role A will set a DateTime field which Worker Role B will then use as the basis for various internal business processes. In most cases, Worker Role B is measuring the difference in that time from UtcNow which is obviously based on the Worker roles Host system clock.

Although its been hard to measure precisely there appears to be big differences (for our needs) between the Web Role's host clock and the Worker Role's host clock. Is there a mechanism to syncronize clocks within Azure or is there an alternative approach I should use?

Upvotes: 2

Views: 271

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

You could use a startup task + scheduled tasks that run every 5min for example to synchronize the time of your instances with a common time server:

function Get-InternetTime { 
  $TcpClient = New-Object System.Net.Sockets.TcpClient 
  [byte[]]$buffer = ,0 * 64 
  $TcpClient.Connect('time.nist.gov', 13) 
  $TcpStream = $TcpClient.GetStream() 
  $length = $TcpStream.Read($buffer, 0, $buffer.Length); 
  [void]$TcpClient.Close() 
  $raw = [Text.Encoding]::ASCII.GetString($buffer) 
  [DateTime]::ParseExact($raw.SubString(7,17), 'yy-MM-dd HH:mm:ss', $null).toLocalTime() 
} 

Set-Date (Get-InternetTime) 

Or you could get the time from a SQL Azure database and use that time as a reference for all your instances:

select getdate()

Upvotes: 1

Related Questions