In this article we will cover the basics of Redis and what are the different data structures supported in Redis. We will also cover how to install and connect to Redis using docker.
1. What is Redis
Redis is an open-source, networked, in-memory, key-value data store with optional durability. It is written in ANSI C.
The name Redis means REmote DIctionary Server.
The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it.
Redis is not a plain key-value store, it is actually a data structures server, supporting different kinds of values. What this means is that, while in traditional key-value stores you associated string keys to string values, in Redis the value is not limited to a simple string, but can also hold more complex data structures.
2. Installing and connecting to Redis Server
2.1 Installation using Docker
- Pull a redis docker Image
docker pull redis
After you run this command, you can see the docker image downloaded on your machine.
- Creating the container
docker run --name <container-name> -v <local-dir-for-data>:/data --hostname <hostname> -p 6379:6379 -d redis redis-server --appendonly yes
e.g docker run --name my-redis -v /home/sunitc/redis/data:/data --hostname sunit-hp -p 6379:6379 -d redis redis-server --appendonly yes
In above example,
– ‘my-redis‘ is the name of the container,
– ‘/home/sunitc/redis/data’ is the directory for virtual mapping of /data directory from redis container.
– ‘-p 6379:6379′, is for exporting the redis port 6379
– ‘–appendonly yes’ is for starting server in persistence mode. It stores all the commands in a file ‘appendonly.aof’. It can replay all the events when redis server restarts.
– ‘sunit-hp’, is the machine host name.
After above command runs successfully, you can see following on the host machine
[sunitc@sunit-hp ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a95348903764 redis "docker-entrypoint..." 40 hours ago Up 26 minutes 0.0.0.0:6379->6379/tcp my-redis
- Stopping and starting Redis docker container
docker start my-redis
docker stop my-redis
where my-redis is the container name (which was created in previous step).
- Connect to Redis client from command line
docker run -it --link my-redis:redis --rm redis redis-cli -h redis -p 6379
3. Redis Keys
- Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like “test” to the content of a JPEG file.
- The maximum allowed key size is 512 MB.
- The empty string is also a valid key.
What is a good key
Very long keys are not good, because it will require more memory and also key lookup in the dataset will be costly. Very short keys are not too good.
It’s a good idea to try to stick with a schema. For instance “object-type:id” is a good idea.
E.g instead of having the key as u1000tags, a good key is user:1000:tags.
4. Redis Data Structures
The following is the list of all the data structures supported by Redis
- Strings are the most basic kind of Redis value.
- Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.
- In Redis, Strings are used to store three types of values
- Byte string values
- Integer values
- Floating-point values
- A String value can be at max 512 Megabytes in length
- If a String value can be interpreted as a number, then that value can be incremented or decremented also.
- Redis Lists are simply lists of strings, sorted by insertion order.
- They are basically linked lists.
- The max length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per list).
- A new list is created when one of this operations is performed against an empty key.
- Similarly the key is removed from the key space if a list operation will empty the list.
- The main features of Redis Lists from the point of view of time complexity are the support for constant time insertion and deletion of elements near the head and tail, even with many millions of inserted items. Accessing elements is very fast near the extremes of the list but is slow if you try accessing the middle of a very big list, as it is an O(N) operation.
- SETs are similar to LISTs in that they’re a sequence of strings.
- But unlike LISTs, Redis SETs use a hash table to keep all strings unique (though there are no associated values).
- Because Redis SETs are unordered, we can’t push and pop items from the ends like we did with LISTs.
- It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).
- Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth)
- A hash with a few fields (where few means up to one hundred or so) is stored in a way that takes very little space, so you can store millions of objects in a small Redis instance.
- Every hash can store up to 232 – 1 field-value pairs
4.5 Sorted Sets
- Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings.
- The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.
- While members are unique, scores may be repeated.
- With sorted sets you can add, remove, or update elements in a very fast way (in a time proportional to the logarithm of the number of elements).
- Since elements are taken in order and not ordered afterwards, you can also get ranges by score or by rank (position) in a very fast way.
- Accessing the middle of a sorted set is also very fast, so you can use Sorted Sets as a smart list of non repeating elements where you can quickly access everything you need: elements in order, fast existence test, fast access to elements in the middle!
With Sorted Sets you can:
- Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK. Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.
- Sorted Sets are often used in order to index data that is stored inside Redis. For instance if you have many hashes representing users, you can use a sorted set with elements having the age of the user as the score and the ID of the user as the value. So using ZRANGEBYSCORE it will be trivial and fast to retrieve all the users with a given interval of ages.
- Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type.
- Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 232 different bits
- Bit operations are divided into two groups:
- constant-time single bit operations, like setting a bit to 1 or 0, or getting its value, and
- operations on groups of bits, for example counting the number of set bits in a given range of bits (e.g., population counting).
- One of the biggest advantages of bitmaps is that they often provide extreme space savings when storing information. For example in a system where different users are represented by incremental user IDs, it is possible to remember a single bit information (for example, knowing whether a user wants to receive a newsletter) of 4 billion of users using just 512 MB of memory.
- A HyperLogLog is a probabilistic data structure used in order to count unique things (technically this is referred to estimating the cardinality of a set).
- Usually counting unique items requires using an amount of memory proportional to the number of items you want to count, because you need to remember the elements you have already seen in the past in order to avoid counting them multiple times.
- However there is a set of algorithms that trade memory for precision: you end with an estimated measure with a standard error, which in the case of the Redis implementation is less than 1%.
- The magic of this algorithm is that you no longer need to use an amount of memory proportional to the number of items counted, and instead can use a constant amount of memory! 12k bytes in the worst case, or a lot less if your HyperLogLog has seen very few elements.
- HLLs in Redis, while technically a different data structure, are encoded as a Redis string, so you can call GET to serialize a HLL, and SET to deserialize it back to the server.
Next – Redis: Commands Reference