Redis Module #62 Flashcards
What kind of database is Redis, generally speaking?
NoSQL Yay! And it uses a key/value store model to store data.
Redis has a reputation for being very fast. What makes the software work so quickly?
Redis is fast because it is an in-memory database. In other words, instead of storing data on a hard drive it saves in memory(RAM). Must use quite a lot of RAM eh?
How do you make sure that Redis starts automatically when the computer is rebooted?
brew services start redis
alternatively, maybe even better you can start it manually
redis-server /usr/local/etc/redis.conf
Important detail: on which port does Redis listen?
Redis listens on port 6379
What’s the basic syntax/structure to access a remote Redis server?
From the command line:
redis-cli -h -p -a
Important detail: how do you drop into the Redis CLI?
Type: redis-cli
then you will know you’re in the redis-cli when you see the local host IP and port number.
tomphillips@Toms-MBP-2 Redis % redis-cli
127.0.0.1:6379>
How can we store data into the Redis CLI?
Use the syntax SET
Example: SET name “Flavio”
In the above keyname = name value = Flavio
How can we retrieve a value from the DB?
GET
What if we’re not sure if a particular key exists?
EXISTS
If a 1 is returned, the key exists
0 means there was no match, key doesn’t exist
How are keys deleted?
Use DEL name
What if we want to set up a key only if it doesn’t yet exist?
SETNX name
How can we see a list of keys?
You can see all keys by typing:
KEYS* (big list) or you can filter by this pattern
KEYS n* to find all keys that start with an “n”
How can we create a temporary key that expires at a predefined span of time?
SETEX
Also, you can find the remaining time before a key expires by calling TTL
How much data can a single key store?
512mb
How is null represented in Redis and when might you expect to see this value?
Null is represented in Redis as nil and you’d see this when returning a key that has expired.
How does Redis define a list?
A list is a set of key-values pairs linked to each other.
How are lists created in Redis?
By using LPUSH (to add list items to the top of the list)
Both RPUSH and LPUSH accept multiple arguments for the value so you can add more than 1 list item at a time.
LPUSH for example,
LPUSH names “Flavio”
RPUSH keyname value (to add list items to the BOTTOM of the list.
How can we increment or decrement keys?
#Increment or decrement key values INCR keyname DECR keyname #Increment or decrement by a set value INCRBY keyname amount DECRBY keyname amount
These commands are very well suited for high concurrent operations where many clients might interact with the same data, to ensure atomic transactions.
How many items can a list hold in Redis?
4 Billion
How can we remove multiple items from a list?
LTRIM listname 0 1
cuts the list to just 2 items, item at position 0 (the first) and item at position 1.
What are the main differences between a set and a list in Redis?
- Sets are not ordered (indexed)
2. Sets can only hold an item once (no duplicates)
How do you add keys to a set?
SADD setname value Example: SADD names "Flavio" SADD names "Roger" SADD names "Tony" "Mark" "Jane"
How can we return the items in a set?
SMEMBERS setname
example:
SMEMBERS names
1) “Roger”
2) “Flavio”
3) “Syd”
Suppose you wanted to find out if a value is in a set, what’s the command for that?
SISMEMBER setname value
Example:
SISMEMBER names “Flavio”
What if you need to find the number of items in a set
SCARD setname
Return a random member from a set
SRANDMEMBER setname
Extract (and remove) an item from the set, casually ordered:
SPOP setnames
How would you extract multiple items at once from a set?
SPOP setname 2
Remove an item from a set by value
SREM setname “Flavio”
What is a sorted set?
A sorted set associates a rank to each item in a set.
How do you target sorted sets?
The same way you work with regular sets except you change the S to a Z example:
SADD -> ZADD
SPOP -> ZPO
ZADD names 1 “Flavio”
ZADD names 2 “Syd”
ZADD names 2 “Roger”
We know we can set the score of an item in a sorted set but how can we do that\?
Run the ZRANK command example:
ZRANK names “Flavio”
What does ZRANGE do?
It works the same as LRANGE except on sorted set
What does WITHSCORES do?
It returns the scores of a set
Example: ZRANGE myset 0 -1 WITHSCORES
If you need to increment the score of an item in a sorted set, how is that done?
Use ZINCRBY
The score value should be the string representation of a numeric value, and accepts double precision floating point numbers. It is possible to provide a negative value to decrement the score.
Example:
ZINCRBY myzset 2 “one”
#returns “3”
Generally speaking, what are hashes used for?
Hashes let us associate more than one value to a single key, and they are perfect to store object-like items.
How are hashes added to the database?
Add Hahses (Keys with multiple values) to the DB
HMSET hashname field1 “Value of Field1” field2 “Value of Field2”
HMSET myhash field1 “Hello” field2 “World”
What if you need to return the value of a field in a HASH
Return the value of a field in a Hash
HGET hashname requested-field
HGET myhash field2
How can we update a hash field/property?
HSET hashname property1 “propertyval1” property2 “propertyval2”
HSET person:1 age 38
Hash values can be incremented, what does that look like?
HINCRBY hashname fieldname value
Example:
HSET myhash field 5
(integer) 1
HINCRBY myhash field 1
(integer) 6
Flavios example:
HINCRBY person:1 age 2
How can we set up a publishing/subscription model?
SUBSCRIBE channel
PUBLISH channelname message