Ralf Zimmermann SIEGNETZ.IT GmbH

Redis

Redis as an LRU cache

Zurück zum Seitenanfang | Aktualisiert am

Redis setup hints

Es ist zu empfehlen sich die aktuelle Dokumentation von anzuschauen. Die Redis setup hints beinhalten die grundlegendsten Informationen für den Betrieb von Redis:

  • We suggest deploying Redis using the Linux operating system. Redis is also tested heavily on OS X, and tested from time to time on FreeBSD and OpenBSD systems. However Linux is where we do all the major stress testing, and where most production deployments are working.
  • Make sure to set the Linux kernel overcommit memory setting to 1. Add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect immediately.
  • Make sure to disable Linux kernel feature transparent huge pages, it will affect greatly both memory usage and latency in a negative way. This is accomplished with the following command: echo never > /sys/kernel/mm/transparent_hugepage/enabled.
  • Make sure to setup some swap in your system (we suggest as much as swap as memory). If Linux does not have swap and your Redis instance accidentally consumes too much memory, either Redis will crash for out of memory or the Linux kernel OOM killer will kill the Redis process.
  • Set an explicit maxmemory option limit in your instance in order to make sure that the instance will report errors instead of failing when the system memory limit is near to be reached.
  • If you are using Redis in a very write-heavy application, while saving an RDB file on disk or rewriting the AOF log Redis may use up to 2 times the memory normally used. The additional memory used is proportional to the number of memory pages modified by writes during the saving process, so it is often proportional to the number of keys (or aggregate types items) touched during this time. Make sure to size your memory accordingly.
  • Use daemonize no when run under daemontools.
  • Even if you have persistence disabled, Redis will need to perform RDB saves if you use replication, unless you use the new diskless replication feature, which is currently experimental.
  • If you are using replication, make sure that either your master has persistence enabled, or that it does not automatically restarts on crashes: slaves will try to be an exact copy of the master, so if a master restarts with an empty data set, slaves will be wiped as well.
  • By default Redis does not require any authentication and listens to all the network interfaces. This is a big security issue if you leave Redis exposed on the internet or other places where attackers can reach it. See for example this attack to see how dangerous it can be. Please check our security page and the quick start for information about how to secure Redis.

In der Redis Konfigurationsdatei /etc/redis/redis.conf sind folgende Konfigurationsparameter zu setzen.

Bind configuration directive


bind localhost

Maxmemory configuration directive


maxmemory 256mb

Eviction policies


maxmemory-policy volatile-lru

Approximated LRU algorithm


maxmemory-samples 5

Require clients to issue AUTH <PASSWORD>

Eine einfache Methode ein Redis Passwort zu setzen ist von einem Password die sha1sum zu erzeugen, und diese dann als requirepass zu definieren.


echo password | sha1sum 
c8fed00eb2e87f1cee8e90ebbe870c190ac3848c  -


requirepass c8fed00eb2e87f1cee8e90ebbe870c190ac3848c

PHP Session Parameter in php.ini

Damit PHP Redis als Cache Backend nutzen kann, muss die Redis Extension installiert sein.


sudo apt-get update && apt-get install php5-redis

PHP lässt sich sehr einfach als Session Cache konfigurieren. Dafür muss die php.ini nur folgerndermassen abgeändert werden.


session.save_handler = redis
session.save_path = "tcp:localhost:6379?auth=8fed00eb2e87f1cee8e90ebbe870c190ac3848c"

Redis Typo3 cache backend Beispiel

Hier ein Beispiel einer Typo3 Redis Cache Konfiguration. Dies hier ist nur ein Beispiel. Es ist empfehlendswert sich die Dokumentation des anzuschauen.


...
    'SYS' => array(
        'caching' => array(
            'cache_hash' => array(
                'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\RedisBackend',
                'options' => array(
                    'database' => 5,
                    'defaultLifetime' => 86400,
                    'password' => '8fed00eb2e87f1cee8e90ebbe870c190ac3848c',
                ),
            ),
            'cache_pages' => array(
                'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\RedisBackend',
                'options' => array(
                    'database' => 3,
                    'defaultLifetime' => 86400,
                    'password' => '8fed00eb2e87f1cee8e90ebbe870c190ac3848c',
                ),
            ),
            'cache_pagesection' => array(
                'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\RedisBackend',
                'options' => array(
                    'database' => 4,
                    'defaultLifetime' => 86400,
                    'password' => '8fed00eb2e87f1cee8e90ebbe870c190ac3848c',
                ),
            ),
            'extbase_object' => array(
                'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\RedisBackend',
                'options' => array(
                    'database' => 6,
                    'defaultLifetime' => 86400,
                    'password' => '8fed00eb2e87f1cee8e90ebbe870c190ac3848c',
                ),
            ),
            'extbase_reflection' => array(
                'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\RedisBackend',
                'options' => array(
                    'database' => 7,
                    'defaultLifetime' => 86400,
                    'password' => '8fed00eb2e87f1cee8e90ebbe870c190ac3848c',
                ),
            ),
        ),
...

Redis Magento cache backend Beispiel

Um Redis in Magento zu nutzen, muss in der Datei /app/etc/modules/Cm_RedisSession.xml der Parameter active auf true gesetzt werden.


<config>
  <modules>
    <Cm_RedisSession>
      <active>true</active>
      <codePool>community</codePool>
    </Cm_RedisSession>
  </modules>
</config>

In der Datei /app/etc/local.xml findet dann die eigentliche Redis Backend Konfiguration statt.


        <cache>
        <backend>Cm_Cache_Backend_Redis</backend>
                <backend_options>
                        <server>127.0.0.1</server>
                        <port>6379</port>
                        <persistent></persistent>
                        <database>0</database>
                        <password>8fed00eb2e87f1cee8e90ebbe870c190ac3848c</password>
                        <force_standalone>0</force_standalone>
                        <connect_retries>1</connect_retries>
                        <read_timeout>10</read_timeout>
                        <automatic_cleaning_factor>0</automatic_cleaning_factor>
                        <compress_data>1</compress_data>
                        <compress_tags>1</compress_tags>
                        <compress_threshold>20480</compress_threshold>
                        <compression_lib>gzip</compression_lib>
                        <use_lua>0</use_lua>
            <timeout>2.5</timeout>
                        <log_level>1</log_level>
                        <max_concurrency>6</max_concurrency>
                        <break_after_frontend>5</break_after_frontend>
                        <fail_after>10</fail_after>
                        <break_after_adminhtml>30</break_after_adminhtml>
                        <first_lifetime>600</first_lifetime>
                        <bot_first_lifetime>60</bot_first_lifetime> 
                        <bot_lifetime>7200</bot_lifetime> 
                        <disable_locking>0</disable_locking>
                        <min_lifetime>60</min_lifetime>    
                        <max_lifetime>2592000</max_lifetime>
                </backend_options>
        </cache>