redis-cli # Local
redis-cli -h host -p port -a password # Run Commands on the Remote Server
redis-cli INFO memory
- Sorted Sets
SET first "First Key value"
# EXPIRE command adds an expiration time (in seconds) to a given key.
EXPIRE first 10
# TTL (Time To Live) command returns the amount of seconds a given key has left to live.
# MSET command sets the values of multiple keys at once.
MSET first "First Key value" second "Second Key value"
# MGET command retrieves the values of multiple key names at once
MGET first second
# INCR increments a key by 1 and returns the incremented value
# INCRBY increments a key by the given integer and returns the incremented value
INCRBY counter 5
DECRBY counter 100
# LPUSH inserts data at the beginning of a List (left push).
LPUSH books "Clean Code"
# RPUSH inserts data at the end of a List (right push).
RPUSH books "Code Complete"
# LLEN returns the length of a List.
# LINDEX returns the element in a given index (indices are zero-based).
LINDEX books 1
# LRANGE returns an array with all elements from a given index range.
LRANGE books 0 -1
# LPOP removes and returns the first element of a List.
# RPOP removes and returns the last element of a List.
HSET movie "title" "The Godfather"
HMSET movie "year" 1972 "rating" 9.2 "watchers" 10000000
HINCRBY movie "watchers" 3
HGET movie "title"
HMGET movie "title" "watchers"
HDEL movie "watchers"
HSCAN movie 0
SADD user:max:favorite_artists "Arcade Fire" "Arctic Monkeys" "Belle & Sebastian" "Lenine"
SADD user:hugo:favorite_artists "Daft Punk" "The Kooks" "Arctic Monkeys"
# SINTER returns only the favorite artists that both Max and Hugo have on their lists.
SINTER user:max:favorite_artists user:hugo:favorite_artists
# SDIFF returns an array with all members of the first Set that do not exist in the Sets that follow it.
SDIFF user:max:favorite_artists user:hugo:favorite_artists
# SUNION returns the names of all artists in both users' Sets of favorite artists.
SUNION user:max:favorite_artists user:hugo:favorite_artists
# SRANDMEMBER returns random members from a Set.
# SISMEMBER checks whether a member exists in a Set.
SISMEMBER user:max:favorite_artists "Arctic Monkeys"
# SREM removes and returns members from a Set.
SREM user:max:favorite_artists "Arctic Monkeys"
# SCARD returns the number of members in a Set.
# SMEMBERS returns an array with all members of a Set.
# ZADD command adds one or many members to a Sorted Set.
ZADD leaders 100 "Alice"
# ZRANGE returns elements from the lowest to the highest score.
# ZREVRANGE returns elements from the highest to the lowest score.
ZREVRANGE leaders 0 -1 WITHSCORES
# ZREM command removes a member from a Sorted Set:
ZREM leaders "Hugo"
# ZSCORE: This returns the score of a member.
ZSCORE leaders "Max"
# ZRANK: This returns the member rank (or index) ordered from low to high.
ZRANK leaders "Max"
# ZREVRANK: This returns the member rank (or index) ordered from high to low.
ZREVRANK leaders "Max"
KEYS pattern # KEYS * is dangerous!
Sentinel vs. Cluster
- Sentinel’s goal is to provide reliable automatic failover in a master/slave topology without sharding data.
- Cluster’s goal is to distribute data across different Redis instances and perform automatic failover if any problem happens to any master instance.
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
- Use benchmarks before committing to a solution.