Redis Module #62 Flashcards

1
Q

What kind of database is Redis, generally speaking?

A

NoSQL Yay! And it uses a key/value store model to store data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Redis has a reputation for being very fast. What makes the software work so quickly?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you make sure that Redis starts automatically when the computer is rebooted?

A

brew services start redis

alternatively, maybe even better you can start it manually

redis-server /usr/local/etc/redis.conf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Important detail: on which port does Redis listen?

A

Redis listens on port 6379

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the basic syntax/structure to access a remote Redis server?

A

From the command line:

redis-cli -h -p -a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Important detail: how do you drop into the Redis CLI?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we store data into the Redis CLI?

A

Use the syntax SET

Example: SET name “Flavio”
In the above keyname = name value = Flavio

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we retrieve a value from the DB?

A

GET

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What if we’re not sure if a particular key exists?

A

EXISTS

If a 1 is returned, the key exists
0 means there was no match, key doesn’t exist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are keys deleted?

A

Use DEL name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What if we want to set up a key only if it doesn’t yet exist?

A

SETNX name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we see a list of keys?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we create a temporary key that expires at a predefined span of time?

A

SETEX

Also, you can find the remaining time before a key expires by calling TTL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How much data can a single key store?

A

512mb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is null represented in Redis and when might you expect to see this value?

A

Null is represented in Redis as nil and you’d see this when returning a key that has expired.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does Redis define a list?

A

A list is a set of key-values pairs linked to each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How are lists created in Redis?

A

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.

18
Q

How can we increment or decrement keys?

A
#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.

19
Q

How many items can a list hold in Redis?

A

4 Billion

20
Q

How can we remove multiple items from a list?

A

LTRIM listname 0 1

cuts the list to just 2 items, item at position 0 (the first) and item at position 1.

21
Q

What are the main differences between a set and a list in Redis?

A
  1. Sets are not ordered (indexed)

2. Sets can only hold an item once (no duplicates)

22
Q

How do you add keys to a set?

A
SADD setname value
Example: 
SADD names "Flavio"
SADD names "Roger"
SADD names "Tony" "Mark" "Jane"
23
Q

How can we return the items in a set?

A

SMEMBERS setname
example:

SMEMBERS names

1) “Roger”
2) “Flavio”
3) “Syd”

24
Q

Suppose you wanted to find out if a value is in a set, what’s the command for that?

A

SISMEMBER setname value

Example:
SISMEMBER names “Flavio”

25
Q

What if you need to find the number of items in a set

A

SCARD setname

26
Q

Return a random member from a set

A

SRANDMEMBER setname

27
Q

Extract (and remove) an item from the set, casually ordered:

A

SPOP setnames

28
Q

How would you extract multiple items at once from a set?

A

SPOP setname 2

29
Q

Remove an item from a set by value

A

SREM setname “Flavio”

30
Q

What is a sorted set?

A

A sorted set associates a rank to each item in a set.

31
Q

How do you target sorted sets?

A

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”

32
Q

We know we can set the score of an item in a sorted set but how can we do that\?

A

Run the ZRANK command example:

ZRANK names “Flavio”

33
Q

What does ZRANGE do?

A

It works the same as LRANGE except on sorted set

34
Q

What does WITHSCORES do?

A

It returns the scores of a set

Example: ZRANGE myset 0 -1 WITHSCORES

35
Q

If you need to increment the score of an item in a sorted set, how is that done?

A

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”

36
Q

Generally speaking, what are hashes used for?

A

Hashes let us associate more than one value to a single key, and they are perfect to store object-like items.

37
Q

How are hashes added to the database?

A

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”

38
Q

What if you need to return the value of a field in a HASH

A

Return the value of a field in a Hash

HGET hashname requested-field
HGET myhash field2

39
Q

How can we update a hash field/property?

A

HSET hashname property1 “propertyval1” property2 “propertyval2”

HSET person:1 age 38

40
Q

Hash values can be incremented, what does that look like?

A

HINCRBY hashname fieldname value
Example:
HSET myhash field 5
(integer) 1

HINCRBY myhash field 1
(integer) 6

Flavios example:
HINCRBY person:1 age 2

41
Q

How can we set up a publishing/subscription model?

A

SUBSCRIBE channel

PUBLISH channelname message