Skip to content

Instantly share code, notes, and snippets.

@rbento
Last active August 27, 2021 06:00
Show Gist options
  • Save rbento/765269f844e602df45c3d680ab2d282b to your computer and use it in GitHub Desktop.
Save rbento/765269f844e602df45c3d680ab2d282b to your computer and use it in GitHub Desktop.

Redis

Docker


Start docker
docker run --rm --name redis-master redis -p 6379
Start docker specifying a redis.conf
# 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

Configuring


Edit the redis.conf file.

Configure as master
bind 127.0.0.1
port 6379
requirepass Str0ngP4s5;
Configure as slave/replica
bind 127.0.0.1
port 6380
replicaof 127.0.0.1 6379
masterauth Str0ngP4s5;

Command Line Interface


Change settings in a non-persistent manner.
# Snapshot every 60 seconds where there was a change of 1 item
CONFIG SET SAVE "60 1" 
Connecting to the server using redis-cli
# 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

Commands


Keys
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
Multiple Keys
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
Hashes
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
Lists
# 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

Sets

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

Sorted Sets
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"

Security


  • 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.

Publish/Subscribe


Useful to create a messaging system or any type of instant data system.

subscribe messages news

publish messages "How are things going?"

Sentinel


  • 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.

Cluster


  • Distributes data across several servers, providing failover, automatic management, replication.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment