Skip to main content

Redis

Redis is an open source in-memory data store that can be used as a database cache or message broker.

You can access metrics for this backing service.

Set up the service

Set up a Redis service

To set up a Redis service:

  1. Run the following in the command line to see what service plans are available for Redis:

    cf marketplace -e redis
    

    Here is an example of the output you will see (the exact service plans will vary):

    service plan         description                                                                                        free or paid
    tiny-3.2             568MB RAM, single node, no failover, daily backups (for instances created after 21/1/2019)         free
    tiny-ha-3.2          1.5GB RAM, highly-available, daily backups                                                         paid
    small-ha-3.2         3GB RAM, highly-available, daily backups                                                           paid
    medium-ha-3.2        6.37GB RAM, highly-available, daily backups                                                        paid
    

    Refer to the Redis service plans section of the documentation for more information.

    Do not use the tiny-clustered-3.2 service plan for new service instances as we have deprecated this plan.

  2. Run the following to create a service instance:

    cf create-service redis PLAN SERVICE_NAME
    

    where PLAN is the service plan you want, and SERVICE_NAME is a unique descriptive name for this service instance. For example:

    cf create-service redis tiny-3.2 my-redis-service
    
  3. It will take between 5 and 10 minutes to set up the service instance. To check its progress, run:

    cf service SERVICE_NAME
    

    for example:

    cf service my-redis-service
    

    When cf service SERVICE_NAME returns a create succeeded status, you have set up the service instance. Example output:

    name:            my-redis-service
    service:         redis
    bound apps:
    tags:
    plan:            tiny-3.2
    description:     AWS ElastiCache Redis service
    documentation:
    dashboard:
    
    Showing status of last operation from service my-redis-service...
    
    status:    create succeeded
    message:   ---
               status               : available
               cluster id           : cf-u7zpvbwzxmrvu
               engine version       : 3.2.6
               maxmemory policy     : volatile-lru
               maintenance window   : sun:23:00-mon:01:30
               daily backup window  : 02:00-05:00
    started:   2018-02-21T10:44:16Z
    updated:   2018-02-21T10:52:31Z
    
    

Bind a Redis service to your app

You must bind your app to the Redis service so you can access the cache from the app.

  1. Use the app’s manifest to bind the app to the service instance. It will bind automatically when you next deploy your app. An example manifest:

    --
    applications:
      - name: my-app
        services:
        - my-redis-service
    
  2. Deploy your app in line with your normal deployment process.

This binds your app to a service instance called my-redis-service.

Refer to the Cloud Foundry documentation on deploying with app manifests for more information.

Use the cf bind-service command

Alternatively, you can manually bind your service instance to your app.

  1. Run the following:

    cf bind-service APP_NAME SERVICE_NAME
    

    where APP_NAME is the exact name of a deployed instance of your application and SERVICE_NAME is the name of the service instance you created. For example:

    cf bind-service my-app my-redis-service
    
  2. Deploy your app in line with your normal deployment process.

Connect to a Redis service from your app

Your app must make a TLS connection to the service. Some libraries use TLS by default, but others will need to be manually configured.

Your app should parse the VCAP_SERVICES environment variable to make a secure connection to Redis.

If your app writes database connection errors to STDOUT or STDERR, you can view recent errors with cf logs APP_NAME --recent. See the section on logs for details.

Connect to a Redis service instance from your local machine

We have created the Conduit plugin to simplify the process of connecting your local machine to a Redis service.

Prerequisites

You must:

Install and configure Conduit

  1. Run the following command to install the Conduit plugin:

    cf install-plugin conduit
    
  2. Run the following command to connect to your service instance with the Redis client:

    $ cf conduit SERVICE_NAME -- redis-cli
    

    where SERVICE_NAME is a unique descriptive name for this service instance.

You have now connected your local machine to your Redis service instance using Conduit. You can test this connection with the Redis PING command:

127.0.0.1:7081> PING
PONG

Run cf conduit --help for more options, and refer to the Conduit README file for more information on how to use the plugin.

Amend the service

Upgrade Redis service plan

Warning Redis upgrades will remove all data stored in the instance.

Our service broker does not currently allow us to upgrade Redis services in place. To upgrade your Redis service, perform the following steps:

  1. cf create-service redis DESIRED_PLAN NEW_REDIS_NAME
  2. cf bind-service APP_NAME NEW_REDIS_NAME
  3. cf unbind-service APP_NAME OLD_REDIS_NAME
  4. cf restage APP_NAME
  5. cf delete-service OLD_REDIS_NAME

You can find available plans on the marketplace using cf marketplace -e redis.

Remove the service

Unbind a Redis service from your app

You must unbind the Redis service before you can delete your service instance. To unbind the Redis service, run the following code in the command line:

cf unbind-service APPLICATION SERVICE_NAME

where APPLICATION is the name of a deployed instance of your application (exactly as specified in your app’s manifest.yml file or push command) and SERVICE_NAME is a unique descriptive name for this service instance, for example:

cf unbind-service my-app my-redis-service

If you unbind your services from your app but do not delete them, these services will persist even after your app is deleted. You can re-bind or reconnect to them in future.

Delete a Redis service

Once the Redis service has been unbound from your app, you can delete your service instance. Run the following in the command line:

cf delete-service SERVICE_NAME

where SERVICE_NAME is a unique descriptive name for this service instance.

Type yes when asked for confirmation.

Maintaining the service

Data classification

You can store data classified up to ‘official’ on the GOV.UK PaaS. Refer to the data security classification documentation for more information.

Redis service plans

Each service in the marketplace has multiple plans that vary by availability and storage capacity.

Some service plans are paid and we will bill you based on your service usage.

New organisations cannot access paid plans by default. Enabling this access is controlled by an organisation’s quota settings.

If paid plans are not enabled, when you try to use a paid service you will receive an error stating “service instance cannot be created because paid service plans are not allowed”. One of your org managers must contact us at gov-uk-paas-support@digital.cabinet-office.gov.uk to request that we enable paid services.

High availability plans - Redis

If you use a high availability service plan, Amazon ElastiCache for Redis provides a hot standby service for failover.

If you do not have a high availability service plan, you will lose data during a service instance failure. Before deciding which service plan to use, you should assess your data and what type of plan you need.

Redis maintenance

Redis maintenance times

Every Redis service instance has a maintenance window pre-configured. You can retrieve the day and time of this maintenance window with the Cloud Foundry CLI (version 8 and above) by running:

cf service --params SERVICE_NAME
{
  "daily_backup_window": "02:00-05:00",
  "preferred_maintenance_window": "sun:23:00-mon:01:30"
}

You can set your own maintenance window by running cf update-service in the command line and setting the preferred_maintenance_window custom parameter:

cf update-service SERVICE_NAME -c '{"preferred_maintenance_window": "START_DAY:START_TIME-END_DAY:END_TIME"}'

where SERVICE_NAME is a unique, descriptive name for this service instance, for example:

cf update-service my-redis-service -c '{"preferred_maintenance_window": "Tue:04:00-Tue:04:30"}'

For more information on maintenance times, refer to the Amazon ElastiCache maintenance window documentation.

Force a failover

If you have a highly available service instance, you can force a failover for that service instance. You can use this to test how your app behaves when a failover happens.

Run the following to force a failover of your highly-available service:

cf update-service SERVICE_NAME -c '{"test_failover": true}'

When you force a failover, your Redis service’s IP address will change. The service’s hostname will not change. You must configure your app to close all Redis connections to the previous IP address when a failover happens.

Redis key eviction policy

The eviction policy is the behaviour Redis follows when you reach your service plan’s maximum memory limit. The eviction policy can take the following values:

 
Eviction policy Definition
volatile-lru evict keys by trying to remove the least recently used (LRU) keys first, but only among keys that have an expire set, in order to make space for the new data added
allkeys-lru evict keys by trying to remove the least recently used (LRU) keys first, in order to make space for the new data added
allkeys-random evict keys randomly in order to make space for the new data added
volatile-random evict keys randomly in order to make space for the new data added, but only evict keys with an expire set
volatile-ttl evict keys with an expire set, and try to evict keys with a shorter time to live (TTL) first, in order to make space for the new data added
noeviction return errors when the memory limit was reached and the client is trying to execute commands that could result in more memory to be used
 

Your Redis instance is set to use the volatile-lru eviction policy by default. You can check this by running the cf service SERVICE_NAME command.

You can set the eviction policy for an existing service instance by running:

cf update-service SERVICE_NAME -c '{"maxmemory_policy": "EVICTION_POLICY"}'

where SERVICE_NAME is a unique descriptive name for this service instance and EVICTION_POLICY is the eviction policy you want, for example:

cf update-service my-redis-service -c '{"maxmemory_policy": "volatile-ttl"}'

Refer to the Redis LRU cache documentation for more information.

Further information

Refer to the Amazon ElastiCache for Redis page [external link] for more information.