Redis Server

Redis is one of the fastest key-value store databases. It’s an open source project written in ANSI C. It’s usually used as a sessions store, but it can be used as cache or different types of data store.

Data stored in Redis can be persisted. The server itself can be clustered.


We’ll install Redis server on Debian 8.1:

PHP Example Application

We’ll use PHP as a language for our example.

We’ll create one website which will first add a record in Redis, and second then one which will fetch it .


  • we need to have a webserver running wigh php support (i.e. apache2 with libapache2-mod-php5  – apt-get install libapache2-mod-php5 apache2).
  • we need a previously installed Redis server on our localhost.

We’ll put in var/www/html/ two files a.php and b.php.

a.php (for adding a value to a key in Redis):

b.php (for reading a value set to key from Redis)


You can test it by visiting:

  • http://{YOUR_IP_SERVER_HERE}/a.php
  • http://{YOUR_IP_SERVER_HERE}/b.php

Redis bind and auth

By default Redis is listenning on localhost and doesn’t require any authorization. If we want to use Redis in our network we’ll have to change redis configuration.

In /etc/redis/redis.conf change the line with “bind″ parameter to listen on all IPs:

To turn on the autorization add the following line in SECURITY section:

Finally, we have to restart the redis service:

We’re now ready for connections from remote hosts.


  • from now on you have to run auth(‘DOSatFMCS’) method, before sending any other requests to Redis.

The default connection method is covered in the following code:


Redis unix socket

Usually it’s good to use tcp/ip socket for redis connection,  but redis, as most of the modern server applications, allows to connect via unix socket.

In this case there’s no tcp/ip stack overhead, so it’s a little faster (but as I measured the differences are usually 0.0001 to 0.0002 seconds).

I’ve prepared some tests on my own (maybe I’ll publish it the future), but you can use redis-benchmark with -s option to test unix socket, and -h to test tcp/ip stack on localhost.

The main litmiations of unix sockets is number of open files (which is not extendable for unix socket and can be easily get around with listening on multiple IPs with tcp/ip).

I’ll show how to set up redis to listen on unix socket (it can listen on both unix socket and IP address)

In /etc/redis/redis.conf uncomment or add the lines with unixsocket and unixsocketperm parameters:


  • redis server runs as a “redis” user in the system
  • Unix socket has default permission set to 755
  • In order to let other users use the socket we need to either change the default permission from 755 to 777 (as we presented above in the redis.conf file), or add the user to “redis” group in the system (running usermod kalkos -a -G redis)

Finally, we have to restart the redis service:

We’re now ready for connections from remote hosts.

Here is the example with Redis unix socket connection:


  • don’t run auth(”) for unix sockets – you’ll get errors and the script is likely to crash.

Library documentation

Complete reference on Redis commands can be found here and php library here. Nevertheless we should mentions the following commands from redis php library:

  • $redisClient->setex(‘key’, 3600, ‘value’ ); – adds a key and set its value with expiration to 3600 seconds,
  • $redisClient->exists(‘key’); – returns a value assigned to a key,
  • $redisClient->exists(‘key’); – returns true if key exists and false otherwise,
  • $redisClient->del(‘key’); – deleting the key,
  • $redisClient->auth(‘password’); – if authorization is required, we have to run auth method right after setting up the connection.

If we want to run simple performance test, we can run the redis-benchmark command distributed with Redis server (write the output to a file or pipe it to less command, so you can read everything).

Hint: you can run a redis-cli command which allows you to test some commands manually.

The Checklist

  1. If php responds there is no Redis class:
    1. check if you have installed php5-redis (or newer version of this package – dpkg -l |grep -i redis |grep -i php)
    2. restart the apache serwer (service apache2 restart)
  2. If you don’t see the result of get in b.php, test Redis server using redis-cli and then:
    1. get 123
    2. set 123 MyValue
    3. get 123