If you are serious in running Redis, you will want to run it under HA mode (High Availability). So far for learning purpose you can run a single instance of redis under your docker environment quite easily, but what if you need to run it in production? This is where Redis Sentinel comes in to play, let’s see what the official document of Redis Sentinel have to say.
The main thing that Redis Sentinel provides is when there is a failure on master node, it will automatically choose a salve and promote it to a master. How does it do that, it basically periodically check the health and live of a Redis instance, it will also notify clients and slaves about the new master. The protocol used is gossip protocol with leader election algorithms. Sentinel also acts as a central source of authority for client discovery, clients are connected to Sentinel for the address of the master node.
Things that Sentinel doesn’t do are mainly:
- Manage client connections
- Store configurations changes to disk
Usually you would want to run your Sentinel on different servers than your Redis Server, for the very simple fact that you don’t want your monitoring software on the same server right? Can you imagine a sql server with sql monitoring on the same machine to tell you if its alive? What if the sql server machine goes down, then both goes down? One can potentially run Sentinel on client nodes and its best to not to run it on master nodes.
Here is a typical way of setting up Sentinel.
Unfortunately Redis StackExchange does not support Sentinel there are pull request that have the functionality (https://github.com/StackExchange/StackExchange.Redis/pull/692) but have not been merged into Redis StackExchange yet as we speak.
What are the alternative? You can roll your own RedisClient and have Sentinel support or manage it yourself in your code, or use Service Stack is another option.
If you are interested in rolling your own you may want to go through this code by PaulB from Stack Overflow provided
https://stackoverflow.com/questions/25385075/redis-failover-with-stackexchange-sentinel-from-c-sharp
The concepts remain the same as in ask sentinel for who the master is and then do the connection.
Note: The code is not mine its from the link above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
namespace Smartodds.Framework.Redis { public class RedisClient : IDisposable { public RedisClient(RedisEnvironmentElement environment, Int32 databaseId) { m_ConnectTimeout = environment.ConnectTimeout; m_Timeout = environment.Timeout; m_DatabaseId = databaseId; m_ReconnectTime = environment.ReconnectTime; m_CheckSubscriptionsTime = environment.CheckSubscriptions; if (environment.TestWrite == true) { m_CheckWriteTime = environment.TestWriteTime; } environment.Password.ToCharArray().ForEach((c) => m_Password.AppendChar(c)); foreach (var server in environment.Servers) { if (server.Type == ServerType.Redis) { // will be ignored if sentinel servers are used m_RedisServers.Add(new RedisConnection { Address = server.Host, Port = server.Port }); } else { m_SentinelServers.Add(new RedisConnection { Address = server.Host, Port = server.Port }); } } } public bool IsSentinel { get { return m_SentinelServers.Count > 0; } } public IDatabase Database { get { return _Redis.GetDatabase(m_DatabaseId); } } private ConnectionMultiplexer _Redis { get { if (m_Connecting == true) { throw new RedisConnectionNotReadyException(); } ConnectionMultiplexer redis = m_Redis; if (redis == null) { throw new RedisConnectionNotReadyException(); } return redis; } } private ConnectionMultiplexer _Sentinel { get { if (m_Connecting == true) { throw new RedisConnectionNotReadyException("Sentinel connection not ready"); } ConnectionMultiplexer sentinel = m_Sentinel; if (sentinel == null) { throw new RedisConnectionNotReadyException("Sentinel connection not ready"); } return sentinel; } } public void RegisterSubscription(string channel, Action<RedisChannel, RedisValue> handler, Int32 maxNoReceiveSeconds) { m_Subscriptions.Add(channel, new RedisSubscription { Channel = channel, Handler = handler, MaxNoReceiveSeconds = maxNoReceiveSeconds, LastUsed = DateTime.UtcNow, }); } public void Connect() { _Connect(true); } private void _Connect(object state) { bool throwException = (bool)state; // if a reconnect is already being attempted, don't hang around waiting if (Monitor.TryEnter(m_ConnectionLocker) == false) { return; } // we took the lock, notify everything we are connecting m_Connecting = true; try { Stopwatch sw = Stopwatch.StartNew(); LoggerQueue.Debug(">>>>>> REDIS CONNECTING... >>>>>>"); // if this is a reconnect, make absolutely sure everything is cleaned up first _KillTimers(); _KillRedisClient(); if (this.IsSentinel == true && m_Sentinel == null) { LoggerQueue.Debug(">>>>>> CONNECTING TO SENTINEL >>>>>> - " + sw.Elapsed); // we'll be getting the redis servers from sentinel ConfigurationOptions sentinelConnection = _CreateRedisConfiguration(CommandMap.Sentinel, null, m_SentinelServers); m_Sentinel = ConnectionMultiplexer.Connect(sentinelConnection); LoggerQueue.Debug(">>>>>> CONNECTED TO SENTINEL >>>>>> - " + sw.Elapsed); _OutputConfigurationFromSentinel(); // get all the redis servers from sentinel and ignore any set by caller m_RedisServers.Clear(); m_RedisServers.AddRange(_GetAllRedisServersFromSentinel()); if (m_RedisServers.Count == 0) { throw new RedisException("Sentinel found no redis servers"); } } LoggerQueue.Debug(">>>>>> CONNECTING TO REDIS >>>>>> - " + sw.Elapsed); // try to connect to all redis servers ConfigurationOptions connection = _CreateRedisConfiguration(CommandMap.Default, _SecureStringToString(m_Password), m_RedisServers); m_Redis = ConnectionMultiplexer.Connect(connection); LoggerQueue.Debug(">>>>>> CONNECTED TO REDIS >>>>>> - " + sw.Elapsed); // register subscription channels m_Subscriptions.ForEach(s => { m_Redis.GetSubscriber().Subscribe(s.Key, (channel, value) => _SubscriptionHandler(channel, value)); s.Value.LastUsed = DateTime.UtcNow; }); if (this.IsSentinel == true) { // check subscriptions have been sending messages if (m_Subscriptions.Count > 0) { m_CheckSubscriptionsTimer = new Timer(_CheckSubscriptions, null, 30000, m_CheckSubscriptionsTime); } if (m_CheckWriteTime != null) { // check that we can write to redis m_CheckWriteTimer = new Timer(_CheckWrite, null, 32000, m_CheckWriteTime.Value); } // monitor for connection status change to any redis servers m_Redis.ConnectionFailed += _ConnectionFailure; m_Redis.ConnectionRestored += _ConnectionRestored; } LoggerQueue.Debug(string.Format(">>>>>> ALL REDIS CONNECTED ({0}) >>>>>>", sw.Elapsed)); } catch (Exception ex) { LoggerQueue.Error(">>>>>> REDIS CONNECT FAILURE >>>>>>", ex); if (throwException == true) { throw; } else { // internal reconnect, the reconnect has failed so might as well clean everything and try again _KillTimers(); _KillRedisClient(); // faster than usual reconnect if failure _ReconnectTimer(1000); } } finally { // finished connection attempt, notify everything and remove lock m_Connecting = false; Monitor.Exit(m_ConnectionLocker); } } private ConfigurationOptions _CreateRedisConfiguration(CommandMap commandMap, string password, List<RedisConnection> connections) { ConfigurationOptions connection = new ConfigurationOptions { CommandMap = commandMap, AbortOnConnectFail = true, AllowAdmin = true, ConnectTimeout = m_ConnectTimeout, SyncTimeout = m_Timeout, ServiceName = "master", TieBreaker = string.Empty, Password = password, }; connections.ForEach(s => { connection.EndPoints.Add(s.Address, s.Port); }); return connection; } private void _OutputConfigurationFromSentinel() { m_SentinelServers.ForEach(s => { try { IServer server = m_Sentinel.GetServer(s.Address, s.Port); if (server.IsConnected == true) { try { IPEndPoint master = server.SentinelGetMasterAddressByName("master") as IPEndPoint; var slaves = server.SentinelSlaves("master"); StringBuilder sb = new StringBuilder(); sb.Append(">>>>>> _OutputConfigurationFromSentinel Server "); sb.Append(s.Address); sb.Append(" thinks that master is "); sb.Append(master); sb.Append(" and slaves are "); foreach (var slave in slaves) { string name = slave.Where(i => i.Key == "name").Single().Value; bool up = slave.Where(i => i.Key == "flags").Single().Value.Contains("disconnected") == false; sb.Append(name); sb.Append("("); sb.Append(up == true ? "connected" : "down"); sb.Append(") "); } sb.Append(">>>>>>"); LoggerQueue.Debug(sb.ToString()); } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> _OutputConfigurationFromSentinel Could not get configuration from sentinel server ({0}) >>>>>>", s.Address), ex); } } else { LoggerQueue.Error(string.Format(">>>>>> _OutputConfigurationFromSentinel Sentinel server {0} was not connected", s.Address)); } } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> _OutputConfigurationFromSentinel Could not get IServer from sentinel ({0}) >>>>>>", s.Address), ex); } }); } private RedisConnection[] _GetAllRedisServersFromSentinel() { // ask each sentinel server for its configuration List<RedisConnection> redisServers = new List<RedisConnection>(); m_SentinelServers.ForEach(s => { try { IServer server = m_Sentinel.GetServer(s.Address, s.Port); if (server.IsConnected == true) { try { // store master in list IPEndPoint master = server.SentinelGetMasterAddressByName("master") as IPEndPoint; redisServers.Add(new RedisConnection { Address = master.Address.ToString(), Port = master.Port }); var slaves = server.SentinelSlaves("master"); foreach (var slave in slaves) { string address = slave.Where(i => i.Key == "ip").Single().Value; string port = slave.Where(i => i.Key == "port").Single().Value; redisServers.Add(new RedisConnection { Address = address, Port = Convert.ToInt32(port) }); } } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> _GetAllRedisServersFromSentinel Could not get redis servers from sentinel server ({0}) >>>>>>", s.Address), ex); } } else { LoggerQueue.Error(string.Format(">>>>>> _GetAllRedisServersFromSentinel Sentinel server {0} was not connected", s.Address)); } } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> _GetAllRedisServersFromSentinel Could not get IServer from sentinel ({0}) >>>>>>", s.Address), ex); } }); return redisServers.Distinct().ToArray(); } private IServer _GetRedisMasterFromSentinel() { // ask each sentinel server for its configuration foreach (RedisConnection sentinel in m_SentinelServers) { IServer sentinelServer = _Sentinel.GetServer(sentinel.Address, sentinel.Port); if (sentinelServer.IsConnected == true) { try { IPEndPoint master = sentinelServer.SentinelGetMasterAddressByName("master") as IPEndPoint; return _Redis.GetServer(master); } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> Could not get redis master from sentinel server ({0}) >>>>>>", sentinel.Address), ex); } } } throw new InvalidOperationException("No sentinel server available to get master"); } private void _ReconnectTimer(Nullable<Int32> reconnectMilliseconds) { try { lock (m_ReconnectLocker) { if (m_ReconnectTimer != null) { m_ReconnectTimer.Dispose(); m_ReconnectTimer = null; } // since a reconnect will definately occur we can stop the check timers for now until reconnect succeeds (where they are recreated) _KillTimers(); LoggerQueue.Warn(">>>>>> REDIS STARTING RECONNECT TIMER >>>>>>"); m_ReconnectTimer = new Timer(_Connect, false, reconnectMilliseconds.GetValueOrDefault(m_ReconnectTime), Timeout.Infinite); } } catch (Exception ex) { LoggerQueue.Error("Error during _ReconnectTimer", ex); } } private void _CheckSubscriptions(object state) { if (Monitor.TryEnter(m_ConnectionLocker, TimeSpan.FromSeconds(1)) == false) { return; } try { DateTime now = DateTime.UtcNow; foreach (RedisSubscription subscription in m_Subscriptions.Values) { if ((now - subscription.LastUsed) > TimeSpan.FromSeconds(subscription.MaxNoReceiveSeconds)) { try { EndPoint endpoint = m_Redis.GetSubscriber().IdentifyEndpoint(subscription.Channel); EndPoint subscribedEndpoint = m_Redis.GetSubscriber().SubscribedEndpoint(subscription.Channel); LoggerQueue.Warn(string.Format(">>>>>> REDIS Channel '{0}' has not been used for longer than {1}s, IsConnected: {2}, IsConnectedChannel: {3}, EndPoint: {4}, SubscribedEndPoint: {5}, reconnecting...", subscription.Channel, subscription.MaxNoReceiveSeconds, m_Redis.GetSubscriber().IsConnected(), m_Redis.GetSubscriber().IsConnected(subscription.Channel), endpoint != null ? endpoint.ToString() : "null", subscribedEndpoint != null ? subscribedEndpoint.ToString() : "null")); } catch (Exception ex) { LoggerQueue.Error(string.Format(">>>>>> REDIS Error logging out details of Channel '{0}' reconnect", subscription.Channel), ex); } _ReconnectTimer(null); return; } } } catch (Exception ex) { LoggerQueue.Error(">>>>>> REDIS Exception ERROR during _CheckSubscriptions", ex); } finally { Monitor.Exit(m_ConnectionLocker); } } private void _CheckWrite(object state) { if (Monitor.TryEnter(m_ConnectionLocker, TimeSpan.FromSeconds(1)) == false) { return; } try { this.Database.HashSet(Environment.MachineName + "SmartoddsWriteCheck", m_CheckWriteGuid.ToString(), DateTime.UtcNow.Ticks); } catch (RedisConnectionNotReadyException) { LoggerQueue.Warn(">>>>>> REDIS RedisConnectionNotReadyException ERROR DURING _CheckWrite"); } catch (RedisServerException ex) { LoggerQueue.Warn(">>>>>> REDIS RedisServerException ERROR DURING _CheckWrite, reconnecting... - " + ex.Message); _ReconnectTimer(null); } catch (RedisConnectionException ex) { LoggerQueue.Warn(">>>>>> REDIS RedisConnectionException ERROR DURING _CheckWrite, reconnecting... - " + ex.Message); _ReconnectTimer(null); } catch (TimeoutException ex) { LoggerQueue.Warn(">>>>>> REDIS TimeoutException ERROR DURING _CheckWrite - " + ex.Message); } catch (Exception ex) { LoggerQueue.Error(">>>>>> REDIS Exception ERROR during _CheckWrite", ex); } finally { Monitor.Exit(m_ConnectionLocker); } } private void _ConnectionFailure(object sender, ConnectionFailedEventArgs e) { LoggerQueue.Warn(string.Format(">>>>>> REDIS CONNECTION FAILURE, {0}, {1}, {2} >>>>>>", e.ConnectionType, e.EndPoint.ToString(), e.FailureType)); } private void _ConnectionRestored(object sender, ConnectionFailedEventArgs e) { LoggerQueue.Warn(string.Format(">>>>>> REDIS CONNECTION RESTORED, {0}, {1}, {2} >>>>>>", e.ConnectionType, e.EndPoint.ToString(), e.FailureType)); } private void _SubscriptionHandler(string channel, RedisValue value) { // get handler lookup RedisSubscription subscription = null; if (m_Subscriptions.TryGetValue(channel, out subscription) == false || subscription == null) { return; } // update last used subscription.LastUsed = DateTime.UtcNow; // call handler subscription.Handler(channel, value); } public Int64 Publish(string channel, RedisValue message) { try { return _Redis.GetSubscriber().Publish(channel, message); } catch (RedisConnectionNotReadyException) { LoggerQueue.Error("REDIS RedisConnectionNotReadyException ERROR DURING Publish"); throw; } catch (RedisServerException ex) { LoggerQueue.Error("REDIS RedisServerException ERROR DURING Publish - " + ex.Message); throw; } catch (RedisConnectionException ex) { LoggerQueue.Error("REDIS RedisConnectionException ERROR DURING Publish - " + ex.Message); throw; } catch (TimeoutException ex) { LoggerQueue.Error("REDIS TimeoutException ERROR DURING Publish - " + ex.Message); throw; } catch (Exception ex) { LoggerQueue.Error("REDIS Exception ERROR DURING Publish", ex); throw; } } public bool LockTake(RedisKey key, RedisValue value, TimeSpan expiry) { return _Execute(() => this.Database.LockTake(key, value, expiry)); } public bool LockExtend(RedisKey key, RedisValue value, TimeSpan extension) { return _Execute(() => this.Database.LockExtend(key, value, extension)); } public bool LockRelease(RedisKey key, RedisValue value) { return _Execute(() => this.Database.LockRelease(key, value)); } private void _Execute(Action action) { try { action.Invoke(); } catch (RedisServerException ex) { LoggerQueue.Error("REDIS RedisServerException ERROR DURING _Execute - " + ex.Message); throw; } catch (RedisConnectionException ex) { LoggerQueue.Error("REDIS RedisConnectionException ERROR DURING _Execute - " + ex.Message); throw; } catch (TimeoutException ex) { LoggerQueue.Error("REDIS TimeoutException ERROR DURING _Execute - " + ex.Message); throw; } catch (Exception ex) { LoggerQueue.Error("REDIS Exception ERROR DURING _Execute", ex); throw; } } private TResult _Execute<TResult>(Func<TResult> function) { try { return function.Invoke(); } catch (RedisServerException ex) { LoggerQueue.Error("REDIS RedisServerException ERROR DURING _Execute - " + ex.Message); throw; } catch (RedisConnectionException ex) { LoggerQueue.Error("REDIS RedisConnectionException ERROR DURING _Execute - " + ex.Message); throw; } catch (TimeoutException ex) { LoggerQueue.Error("REDIS TimeoutException ERROR DURING _Execute - " + ex.Message); throw; } catch (Exception ex) { LoggerQueue.Error("REDIS ERROR DURING _Execute", ex); throw; } } public string[] GetAllKeys(string pattern) { if (m_Sentinel != null) { return _GetAnyRedisSlaveFromSentinel().Keys(m_DatabaseId, pattern).Select(k => (string)k).ToArray(); } else { return _Redis.GetServer(_Redis.GetEndPoints().First()).Keys(m_DatabaseId, pattern).Select(k => (string)k).ToArray(); } } private void _KillSentinelClient() { try { if (m_Sentinel != null) { LoggerQueue.Debug(">>>>>> KILLING SENTINEL CONNECTION >>>>>>"); ConnectionMultiplexer sentinel = m_Sentinel; m_Sentinel = null; sentinel.Close(false); sentinel.Dispose(); } } catch (Exception ex) { LoggerQueue.Error(">>>>>> Error during _KillSentinelClient", ex); } } private void _KillRedisClient() { try { if (m_Redis != null) { Stopwatch sw = Stopwatch.StartNew(); LoggerQueue.Debug(">>>>>> KILLING REDIS CONNECTION >>>>>>"); ConnectionMultiplexer redis = m_Redis; m_Redis = null; if (this.IsSentinel == true) { redis.ConnectionFailed -= _ConnectionFailure; redis.ConnectionRestored -= _ConnectionRestored; } redis.Close(false); redis.Dispose(); LoggerQueue.Debug(">>>>>> KILLED REDIS CONNECTION >>>>>> " + sw.Elapsed); } } catch (Exception ex) { LoggerQueue.Error(">>>>>> Error during _KillRedisClient", ex); } } private void _KillClients() { lock (m_ConnectionLocker) { _KillSentinelClient(); _KillRedisClient(); } } private void _KillTimers() { if (m_CheckSubscriptionsTimer != null) { m_CheckSubscriptionsTimer.Dispose(); m_CheckSubscriptionsTimer = null; } if (m_CheckWriteTimer != null) { m_CheckWriteTimer.Dispose(); m_CheckWriteTimer = null; } } public void Dispose() { _KillClients(); _KillTimers(); } } } |
In the next post we will go through redis cluster, I wanted to talk about sentinel first such that readers tend to mix them up and by first explaining sentinel we can talk about clusters which are different concepts.
Leave A Comment