docker run --rm --name redis-master redis -p 6379
# MASTER
docker run --rm -v /c/Users/Rodrigo/Desktop/Workspace/redis-starter/master:/usr/local/etc/redis --name redis-master redis redis-server /usr/local/etc/redis/redis.conf
# REPLICA
docker run --rm -v /c/Users/Rodrigo/Desktop/Workspace/redis-starter/slave:/usr/local/etc/redis --name redis-slave redis redis-server /usr/local/etc/redis/redis.conf
Edit the redis.conf file.
bind 127.0.0.1
port 6379
requirepass Str0ngP4s5;
bind 127.0.0.1
port 6380
replicaof 127.0.0.1 6379
masterauth Str0ngP4s5;
# Snapshot every 60 seconds where there was a change of 1 item
CONFIG SET SAVE "60 1"
# Access the container shell
docker exec -it redis-master sh
# Connect to the server using redis-cli with params
redis-cli -a Str0ngP4s5; -p 6380
get firstName
get name
set address 453
incr address
incrby address 100
decr address
decrby address 100
set country North Africa
set country "North Africa"
get country
getset firstName Stephanie
mset street seaward city ventura country usa zip "92101-2878292"
mget street city country zip
set street "Yonge St"
exists street
del street
exists street
set zip "90200-982374"
expire zip 10
get zip
set zip "90200-982374" ex 10
hmset user:345 firstName Tracey lastName Larventz street "Yonge St" city Toronto
hgetall user:345
hget user:345 city
hmget user:345 firstName lastName
hexists user:345 zip
hexists user:345 firstName
hmset user:345 address 345
hgetall user:345
hincrby user:345 address 100
# Create a list / Append items to the end
rpush groceries apples cherries pears "red meat"
# Retrieve items in range
lrange groceries 0 -1
# Add items to the beginning of the list
lpush groceries grapes
# Pop item from the beginning
lpop groceries
# Pop item from the end
rpop groceries
# Trim to a certain number of items
ltrim groceries 0 4
Used for unique data that does not need to be ordered.
sadd tags react "react native" graphql javascript
smembers tags
sadd tags framer sketch
smembers tags
sismember tags typescript
# Add subcategory to tags
sadd tags:react "react router" redux "react transition group"
smember tags:react
# Copy subcategorr to new category
sunionstore tags:"react native" tags:react
smembers tags:"react native"
# Remove from subcategory
spop tags:"react native"
# Count the number of items in a specific category
# scard tags
zadd rocket 1969 "Apollo 11" 1998 "Deep Space 1" 2008 "Falcon 1"
zrange rocket 0 -1
zrange rocket 0 -1 withscores
zrevrange rocket 0 -1 withscores
zrangebyscore rocket -inf 1998 withscores
zrangebyscore rocket -inf 1969 withscores
zrank rocket "Deep space 1"
- Implement ACL at the application level.
- Add password to the master instance of Redis.
- Add password to replicas accordingly.
- Make sure Redis address and port is behind a firewall and protected from unrestricted access.
- Bind Redis to a single network interface.
Useful to create a messaging system or any type of instant data system.
subscribe messages news
publish messages "How are things going?"
- Monitor master and slave instances and handle automatic failover if any of them go down.
- At least 3 instances are required to use Sentinel. [MASTER > SLAVE >> SLAVE]
- Great for small implementations with high-availability concerns.
- Distributes data across several servers, providing failover, automatic management, replication.