Redis (Object cache)

Redis is a high-performance in-memory object store, well-suited for application level caching.

See the Redis documentation for more information.

Platform.sh supports two different Redis configurations: One persistent (useful for key-value application data) and one ephemeral (in-memory only, useful for application caching). Aside from that distinction they are identical.

Supported versions

  • 2.8
  • 3.0
  • 3.2

note Versions 3.0 and higher support up to 64 different databases per instance of the service, but Redis 2.8 is configured to support only a single database

Ephemeral Redis

The redis service type is configured to serve as a LRU cache with the eviction policy allkeys-lru - its storage is not persistent. It is not suitable for use except as a disposable cache.

To add an Ephemeral Redis service, specify it in your .platform/services.yaml file like so:

rediscache:
    type: redis:3.2

Data in an Ephemeral Redis instance is stored only in memory, and thus requires no disk space. When the service hits its memory limit it will automatically evict old cache items to make room for new ones.

Persistent Redis

The redis-persistent service type is configured for persistent storage. That makes it a good choice for fast application-level key-value storage.

To add a Persistent Redis service, specify it in your .platform/services.yaml file like so:

redisdata:
    type: redis-persistent:3.2
    disk: 1024

The disk key is required for redis-persistent to tell Platform.sh how much disk space to reserve for Redis' persistent data.

Relationship

The format exposed in the $PLATFORM_RELATIONSHIPS environment variable:

{
    "redis": [
        {
            "host": "248.0.65.198",
            "scheme": "redis",
            "port": 6379
        }
    ]
}

The format is identical regardless of whether it's a persistent or ephemeral service.

Usage example

In your .platform/services.yaml:

rediscache:
    type: redis:3.2

If you are using PHP, configure a relationship and enable the PHP redis extension in your .platform.app.yaml.

runtime:
    extensions:
        - redis

relationships:
    applicationcache: "rediscache:redis"

You can then use the service in a configuration file of your application with something like:

<?php
// This assumes a fictional application with an array named $settings.
if (getenv('PLATFORM_RELATIONSHIPS')) {
    $relationships = json_decode(base64_decode($relationships), TRUE);

    // For a relationship named 'applicationcache' referring to one endpoint.
    if (!empty($relationships['applicationcache'])) {
        foreach ($relationships['applicationcache'] as $endpoint) {
            $settings['redis_host'] = $endpoint['host'];
            $settings['redis_port'] = $endpoint['port'];
            break;
        }
    }
}

Using redis-cli to access your Redis service

With a Redis relationship named applicationcache.

# .platform/services.yaml
rediscache:
    type: redis:3.2
# .platform.app.yaml
relationships:
    applicationcache: "rediscache:redis"

The host name and port number obtained from PLATFORM_RELATIONSHIPS would be applicationcache.internal and 6379. Open an SSH session and access the Redis server using the redis-cli tool as follows:

redis-cli -h applicationcache.internal -p 6379

Using Redis as handler for native PHP sessions

Using the same configuration but with your Redis relationship named sessionstorage:

# .platform/services.yaml
rediscache:
    type: redis:3.2
# .platform.app.yaml
relationships:
    sessionstorage: "rediscache:redis"
# .platform.app.yaml
variables:
    php:
        session.save_handler: redis
        session.save_path: "tcp://sessionstorage.internal:6379"