Search code examples
redishaproxyredis-sentinel

Running Redis Sentinel, HAProxy and Apache/w PHP on the same servers


I'm trying to introduce more redundancy and reliability to my server architecture.

Currently I have a variable number of web servers (minimum two) running apache with PHP and Memcached for sessions, which means that if one of the server goes down, the user will have to log in again.

After much research, I've decided to give Redis a go to store sessions, Redis Sentinel to manage Redis instance failure, and HAProxy to pass the sessions to the nominated Redis Master.

I'm a bit unclear about where all these things fit together despite days of reading. Let's say I have a minimum of two front end servers and two Redis servers, could this look like this:

 Load balanced connection
           |
     -------------
     |           |
----------  ----------       
| Apache |  | Apache |
|Sentinel|  |Sentinel|
| HAProxy|  | HAProxy|
----------  ---------- 
     |          |
     |----------
     ?          ?           - Managed by HAProxy,
----------  ----------        by talking to Sentinel
|        |  |        |
| Redis  |  |  Redis |
|        |  |        |
----------  ----------

And then scale up, by adding more of the Apache/Sentinel/HAProxy severs? If so would this work on small (1gb memory) instances, or does Sentinel/HAProxy take up a lot of resources?

Secondly, how does HAProxy reliably talk to Sentinel? There appears to be lots of conflicting advice about how this works.

Many thanks for any help you can offer!


Solution

  • In the end, the simplest way to get this to work was to avoid HAProxy, and use Sentinel to tell the web server which Redis server was the Master.

    To achieve this, I made a masterupdate.sh script that contained the following

    #!/bin/bash
    echo $6 > /location/master.txt
    

    And edited redis/sentinel.conf to contain the following line

    sentinel client-reconfig-script cache-master /location/masterupdate.sh
    

    This meant that every time the master switched, Sentinel wrote the IP address to the master.txt text file, and I could read that text file from PHP to set up the sessions. Simple!