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”