From Bauman National Library
This page was last modified on 24 December 2016, at 10:23.
Licence BSD
Web site

Redis is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. The development of Redis has been sponsored by Redis Labs since June 2015[1]. Before that, it was sponsored by Pivotal Software[2] and by VMware.[3][4] According to the monthly ranking by, Redis is the most popular key-value database.[5] Redis has also been ranked the #1 NoSQL (and #4 database) in User Satisfaction and Market Presence based on user reviews,[6] the most popular NoSQL database in containers,[7] and the #2 NoSQL among Top 50 Developer Tools & Services.[8] The name Redis means REmote DIctionary Server.[9]


It keeps a database in RAM, equipped with mechanisms of pictures and journaling to provide permanent storage (disk space). Also provides operations to implement the messaging engine in the pattern publish - subscribe [en]. With it, the application can create channels, subscribe to them and put into the channels of communication that will be received by all subscribers (as IRC-Chat). It supports data replication from the main units several subordinate (English master -. Slave replication). Redis also supports transactions (sequential execution of all operations, or audio), and batch processing commands (command execution packet, obtaining the results package).

Redis runs on most POSIX systems, such as Linux, * BSD, Mac OS X without any additions. Linux and Mac OS X - the two operating systems, which has been designed and tested to a greater degree wide Redis, so VMware recommends using them for deployment. Official support for Windows builds there, but some of the available options to ensure Redis work on this OS Microsoft is actively working on the transfer of Redis on Windows.

As of July 15, 2011 Redis development sponsored by VMware . Since May 2013, it is sponsored by Pivotal Software.

Version 2.6.0 added support for Lua, allows you to perform queries on the server. Lua allows atomically commit processing arbitrary data on a server and is intended for use when it is impossible to achieve the same result by using the standard commands.

Supported languages

Many languages have Redis bindings, including:[10] ActionScript, C, C++, C#, Chicken Scheme, Clojure, Common Lisp, D, Dart, Erlang, Go, Haskell, Haxe, Io, Java, JavaScript (Node.js), Julia, Lua, Objective-C, Perl, PHP, Pure Data, Python, R,[11] Racket, Ruby, Rust, Scala, Smalltalk and Tcl.

Data types

Redis maps keys to types of values. A key difference between Redis and other structured storage systems is that Redis supports not only strings, but also abstract data types:

  • Lists of strings
  • Sets of strings (collections of non-repeating unsorted elements)
  • Sorted sets of strings (collections of non-repeating elements ordered by a floating-point number called score)
  • Hash tables where keys and values are strings
  • HyperLogLogs used for approximated set cardinality size estimation.

The type of a value determines what operations (called commands) are available for the value itself. Redis supports high-level, atomic, server-side operations like intersection, union, and difference between sets and sorting of lists, sets and sorted sets.


a couple of nuances should be considered before installing Redis. To begin with apt-get update packages:

sudo apt-get update 

On completion of the process, set the compiler package dependencies, by means of which we establish Redis from source code:

 sudo apt-get install build-essential

Finally, download the tcl:

sudo apt-get install tcl8.5 

Installation of Redis

After the necessary preparations, we are ready to proceed with the installation from source Redis:

tar xzf redis-2.8.9.tar.gz 
cd redis-2.8.9 

Then run make command:


It is recommended to test the assembly:

make test 

We finish by running make install, with the result that the program is installed globally:

`` `Bash

 sudo make install 

Upon completion of the installation, we get Redis with built-in script to start the server as a service. To access the script, go to utils directory:

 cd utils 

Run the script from this directory:

 sudo ./ 

During the execution of the script, you can select the default settings by pressing enter. At the end of the script Redis server will run in the background. Start and interrupt the server with the following command (the number depends on the port you selected during installation):

`` `Bash

 sudo service redis_6379 start 
 sudo service redis_6379 stop 

Get access to the database Redis is possible with the command:


Your Redis server is ready and running, proved by the line:


To automatically start the server at boot, run:

 sudo update-rc.d redis_6379 defaults 

Lest you get confused and it was more clear, I attach the video installation Redis on Ubuntu Server 14.04.4 LTS, and also shows an example of recording and obtaining a plurality of sorting.

Operations Redis

The simplest command to add the string data (underlying data type) might look like this:

    > SET users: GeorgeWashington "job: President, born: 1732, dislikes: cherry trees

In this case, for the SET command to be key (users: GeorgeWashington), followed by the value (string itself). Colon in Redis has no effect on the team. Nevertheless, it is useful to describe the use of a key.

Extract data can GET command:

    GET users: GeorgeWashington
    "Job: President, born: 1732, dislikes: cherry trees"


When unloading data, you can specify the range by using two parameters: the first and last elements (to accept 0 as the first element). If the last parameter is set to -1, then you will get all the elements to the end. For example, if the list includes 6 colors of the rainbow (sorted on the basis of ROYGBV), you get the following results:

    1) "red"
    2) "orange"
    3) "yellow"
    4) "green"
    > LRANGE ROYGBV 0 -1
    1) "red"
    2) "orange"
    3) "yellow"
    4) "green"
    5) "blue"
    6) "violet"
    > LRANGE ROYGBV 3 -1
    1) "green"
    2) "blue"
    3) "violet"


Redis is useful as a repository in which to store data with a certain validity. Duration can be specified in seconds or in a Unix timestamp (seconds since 1.1.1970) format.

Two teams to configure:

EXPIRE - specifies the length of data actions TTL - shows the remaining duration.

  > SET classified:information "Secret Stuff"
  > EXPIRE classified:information 45
  (integer) 1
  > TTL classified:information
  (integer) 31 

When you try a sample of expired data, we get nil

  > GET classified:information


Redis supports atomic increment string data. When the increment data access is blocked, so the integrity of the data is carried out.

  > SET population 6
  > INCRBY population 10
  (integer) 16
  > INCR population
  (integer) 17 


Redis also supports the execution of transactions that need to follow two principles:

Teams must be performed in order. They will not be interrupted by other requests within the process. the integrity of the transaction must be provided. Transactions begin with the MULTI command, and run EXEC command. If for any reason the transaction is aborted, wide Redis will block its execution as long as the team redis-check-aof canceled and all changes will be made. The server can be restarted:

   > MULTI
   > SET population 6
   > INCRBY population 10
   > INCR population
   redis> EXEC
   1) OK
   2) (integer) 16
   3) (integer) 1
   4) (integer) 17 

Redis data types

Redis runs with five types of data: Strings (strings), Sets (sets), Sorted Sets (Assorted sets), Lists (lists), Hashes (hash)


Lines - the most common type of data in Redis. Often the teams when working with strings:

SET - setting the key value GET - fetch values ​​by key DEL - Remove the key and values INCR - auto increment key INCRBY - the increment by the specified amount EXPIRE - the data duration in seconds Strings may be used for storing objects sorted according to the key. Example:

   > SET newkey "the redis string begins"
   > GET newkey
   "the redis string begins" 


Group line is possible by means of sets - a set of rows without sorting. Common commands:

SADD - adding one or more values ​​to the set SMEMBERS - sample of the set of values SINTER - Picks common values ​​of several sets SISMEMBER - check the presence of values ​​in the set SRANDMEMBER - random sample values ​​from the set Sets often applied in different situations. Since each of the set value individually, no verification is required before the addition. When the SADD team this check is carried out automatically.

   > SADD colors red
   (integer) 1
   redis> SADD colors orange
   (integer) 1
   redis> SADD colors yellow
   (integer) 1
   redis> SADD colors orange
   (integer) 0
   redis> SMEMBERS colors
   1) "red"
   2) "yellow"
   3) "orange"

An example of the use of the sets can be checked IP addresses on the uniqueness of the site visitor or the removal of random values ​​SRANDMEMBER team.

Sets with sorting

The name speaks for itself - the collection of rows associated with a numerical value, sorted by increasing this value.

This type of data is often used with ranges as adding and deleting data is much faster. Often the teams:

ZADD - adding value to a variety of ZRANGE - display values ​​sorted by index (from lowest to highest) ZREVRANGE - display values ​​sorted by the index (high to low) ZREM - removal value We can create a simple set with the size of the smallest countries in the world.

   > zadd countries 9 Tuvalu
   (integer) 1
   > zadd countries 62 Liechtenstein
   (integer) 1
   > zadd countries .7 Monaco
   (integer) 1
   > zadd countries .2 VaticanCity
   (integer) 1
   > zadd countries 107 Seychelles
   (integer) 1
   redis> zrange countries 0 -1
   1) "VaticanCity"
   2) "Monaco"
   3) "Tuvalu"
   4) "Liechtenstein"
   5) "Seychelles" 


Lists in Redis - sorted collection of values ​​that distinguishes them from the set. Adding an element to the beginning or end of the list is very fast, even if the list is composed of ten million items. Common commands:

LPUSH - adding value in the top of the list RPUSH - adding value to the end of the list LPOP - sampling and disposal value of the first item in the list RPOP - sample and removing the last item in the list LREM - removal of a range of elements from a list LRANGE - sample a range of elements from a list LTRIM - change the list with the removal of all items are not included in the specified range Example of a list of people responsible for lunch:

   > rpush lunch.provider alice
   (integer) 1
   > rpush lunch.provider bob
   (integer) 2
   > rpush lunch.provider carol
   (integer) 3
   > rpush lunch.provider don
   (integer) 4
   > rpush lunch.provider emily
   (integer) 5 

Adding a person in the queue is as follows:

   lpush lunch.provider zoe
   (integer) 6

LRANGE command displays the entire list:

   lrange lunch.provider 0 -1
   1) "zoe"
   2) "alice"
   3) "bob"
   4) "carol"
   5) "don"
   6) "emily" 

List of frequently used for temporary events or collection of a limited number of elements.


Hash in Redis - way to store objects, consisting of a variety of fields. They are designed to hold a large number of fields in a small space. Hash capable of storing more than 4 billion pairs of field-value. Common commands:

HMSET - setting multiple values HSET - setting field with a string value HGET - sample values ​​on the field HMGET - sample all of these field values HGETALL - sample all values Example of the type of hash to describe an example of a user of the site

   > hmset user:1 username jsmith password 4bAc0s email
   > hgetall user:1
   1) "username"
   2) "jsmith"
   3) "password"
   4) "4bAc0s"
   5) "email"
   6) "" 

For specific information, use the command HMGET

   > hmget user:1 username email
   1) "jsmith"


Redis typically holds the whole dataset in memory. Versions up to 2.4 could be configured to use what they refer to as virtual memory[12] in which some of the dataset is stored on disk, but this feature is deprecated. Persistence is now reached in two different ways: one is called snapshotting, and is a semi-persistent durability mode where the dataset is asynchronously transferred from memory to disk from time to time, written in RDB dump format. Since version 1.1 the safer alternative is AOF, an append-only file (a journal) that is written as operations modifying the dataset in memory are processed. Redis is able to rewrite the append-only file in the background in order to avoid an indefinite growth of the journal.

By default, Redis syncs data to the disk at least every 2 seconds, with more or less robust options available if needed. In the case of a complete system failure on default settings, only a few seconds of data would be lost.[13]


Redis supports master-slave replication. Data from any Redis server can replicate to any number of slaves. A slave may be a master to another slave. This allows Redis to implement a single-rooted replication tree. Redis slaves can be configured to accept writes, permitting intentional and unintentional inconsistency between instances. The Publish/Subscribe feature is fully implemented, so a client of a slave may SUBSCRIBE to a channel and receive a full feed of messages PUBLISHed to the master, anywhere up the replication tree. Replication is useful for read (but not write) scalability or data redundancy.[14]

Example of creating replicas

To create a replica, we need two cars, with already installed Redis. As set it has already been described above. On the second machine doing the same thing, with only one difference: when the installation will ask for a port, do not prescribe the standard 6379, and any other, eg 6380. Then simply set up the connection master-slave, using the following programs. By car master prescribes:

 redis-cli -p 6379 config set requirepass mypass 

On the slave machine prescribes:

 redis-cli -p 6380 config set masterauth mypass
 redis-cli -p 6380 SLAVEOF master_ip 6379 

To make sure you can see information about replication. On the master:

 redis-cli -p 6379 -a mypass info replication 

On the slave:

 redis-cli -p 6380 info replication 

Because I use virtual machines to implement this regard, I have to pre-configure the internal network and to connect my virtual machines so that they can communicate with each other.


When the durability of data is not needed, the in-memory nature of Redis allows it to perform extremely well compared to database systems that write every change to disk before considering a transaction committed.[15] There is no notable speed difference between write and read operations. Redis operates as a single process and single-threaded. Therefore a single Redis instance cannot utilize parallel execution of tasks e.g. stored procedures (Lua scripts).


The Redis project has a cluster specification,[16] but the cluster feature is currently in Beta stage.[17] According to a news post by Redis creator Sanfilippo, the first production version of Redis cluster (planned for beta release at end of 2013),[18][needs update?] will support automatic partitioning of the key space and hot resharding, but will support only single key operations.[19] In the future Redis Cluster is planned to support up to 1000 nodes, fault tolerance with heartbeat and failure detection, incremental versioning (“epochs”) to prevent conflicts, slave election and promotion to master, and publish/subscribe between all cluster nodes.[20][21][22]

See also


Cite error: Invalid <references> tag; parameter "group" is allowed only.

Use <references />, or <references group="..." />

External links