Motivation
Recently, I maintain services that use Redis frequently for caching. It is very interesting since the services handle millions of messages a day. Before using Redis on the application, it is good to know what Redis is and how to use it.
Installation
Basically, I have written how to install Redis using docker in this article. Now, I’ll explain more detail how to install the Redis with its configuration from official docker image (https://hub.docker.com/_/redis).
Basic Container
Redis can be installed in various ways. But docker is my preferred one.
|
|
This command will do the following:
- Pull the latest image from the docker hub
- Create and run the container and name it: my-redis
- Route port 6379 on my laptop to port 6379 inside the container. 6379 is Redis default port and can be changed
source: Medium
To enter terminal on Redis container, use
|
|
And use redis-cli -h localhost
to use command line interface. Run redis-cli --help
to see what kind of command that you can use.
With Volume
|
|
This command will do the same as previous but it mount local folder in /path/to/local/data
to container folder /usr/local/etc/redis
.
Volume can be used to mount Redis configuration file. The configuration file by default can be accessed in this github link (https://github.com/redis/redis/blob/6.0/redis.conf). From the link, you can switch any version of Redis that you want to use.
|
|
Config Example
In this section, I put some examples taken from PZN channel on YouTube. To follow these sections you need to clone my github repository containing docker-compose of Redis example:
|
|
I put the example on my preferred folder ~/docker/example
but you can put anywhere you like.
Note: everytime you run up a redis container with config, please make sure that config installed properly by command docker logs redis-container-name
. If it’s installed correctly, there will be a Redis logo on your terminal screen.
|
|
With Command
We will use Redis feature called pipeline
to run command from the terminal Linux/Unix. To start, run the cloned docker compose example.
|
|
The docker-compose.yaml
looks like
|
|
This docker-compose.yaml
create container with two volumes, for config file and command file.
Enter to the container terminal docker exec -it redis-with-command /bin/bash
and run
|
|
With Security
We will enable security, therefore client we’ll require to provide username and password to access the database.
|
|
This docker-compose create two container in order to simulate client that would access to server with security. Let’s run the yaml file
|
|
Now, Enter to Redis server and try to ping from redis-cli
|
|
See that you can ping
redis from it’s own machine.
Next, use the client container to login..
|
|
and enter redis-cli
server from client using command redis-cli -h redis-with-security
. Then, you’ll get a rejection !
|
|
To solve this problem, commented out this line (line number 5) on the config file
|
|
Then reset these redis containers by docker-compose down
and docker-compose up -d
. Again, enter to the terminal of client container and run redis-cli -h redis-with-security
. You shall be succed enter the redis-cli
but you still get an error once you try to ping
from the cli.
As you see on the error message, you need to set protected-mode no
so you can do ping
command from cli.
With Authentication
Disabling protected-mode
is not recomended when you build real apps. We should use authentication instead. Put this setting on redis.conf
file.
|
|
Then run docker-compose -f $HOME/Docker/example/redis/redis-with-acl/docker-compose.yaml up -d
. The default user setting above should be added to because, at first time on cli mode, the ‘default’ user need to have authorization for connection.
Try again enter cli mode, put username and password authentication… and do ping
!
|
|
And tada… we can ‘ping’ without disabliing ‘protected-mode’. You can ensure it by using command config get <pattern>
|
|
Redis GUI
These are some free Redis GUIs that you can try (stats updated 15 July 2022):
- AnotherRedisDesktopManager: Star 21.6k
- RedisDesktopManager: Star 20.5k
- redis-commander: Star 3k
- QuickRedis: Star 1.3k
- P3X Redis UI: Star 522
- redis-gui: Star 338
Redis Data Type
Basic
There are three types of Redis data type
- Strings
- Lists
- Hash
Strings
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. A string value can be at max 512 Megabytes in length (https://redis.io/docs/manual/data-types/#strings).
The strings command can be found here, https://redis.io/commands/?group=string. These are some method on strings:
- The commond method for caching are
GET
,MGET
,SET
,MSET
. It is possible to cache text or binary data in Redis, which could be anything from HTML pages and API responses to images and videos. - for cache with automation expiration:
SETEX
,EXPIRE
,EXPIREAT
. This is very useful when database queries take a long time to run and can be cached for a given period time. Consequently, this avoids running those queries too frequently. - for counting:
INCR
andINCRBY
. Good examples of counters are page views, video views and like.
Lists
Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list (https://redis.io/docs/manual/data-types/#lists).
Hashes
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)
Advance
- Sets
- Sorted Sets
- Bitmaps
- HyperLogLogs
Sets
Redis sets an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1)
Sorted Sets
Time Series
Commands
Pub/Sub
Transaction
Scripting
Miscellaneous Commands
References: