Notes are based of 7DB - Redis - Day 1 & Day 2
Follow the setup instructions
No collection of data structures would be complete without sets. To illustrate, let's add tags to our sites:
sadd edu cs.montana.edu umt.edu nyu.edu unc.edu
sadd cs cs.montana.edu google.com acm.org
But sets are only really useful if we can perform set operations like intersection
sinter edu cs
Or difference
sdiff edu cs
Or Union
sunion edu cs
Sorted sets are a little slower to work with over hashes (since operations take
time proportional to the log of the size of the set instead of const). Sorted
operations are prefixed with Z. Let's created a sorted list of visits per
shorted url:
zadd visits 10 um 100 msu 50 goog
We can retrieve by range
zrange 1 2
And change scores with ZINCRBY
zincrby visits 10 msu
I think that I have used Redis in some form in almost every big project that I have created over the past 7 years. In part because it is really easy to create a message queue. Let's see an example. In a second terminal start a second client
docker run --net=host -v ${PWD}/env/redis/mnt:/mnt/ -it adv-db/clients redis-cli
In the first client block on a list that waits for comments to post for 300 seconds
BRPOP comments 300
Now, hop back to the first client and post a comment
LPUSH comments "This is super cool!"
Notice that the second client stopped waiting and outputted the comments and if
we check out the contents of comments, our comment was popped. There are
a few other various, see other BR* commands for options.
Pub/sub (Publication and subscriptions) is a popular paradigm in which you have multiple consumers consuming messages from a single channel. For example, in a social network, if I have an activity feed of my friends and my friends have an activity feed of their friends (including me), they would want to subscribe to the content I share. Let's implement this!!
Let's start a few more clients (Let's say 3):
docker run --net=host -v ${PWD}/env/redis/mnt:/mnt/ -it adv-db/clients redis-cli
In two of the clients run:
subscribe dave:comments
We have now created two clients that are hanging on my every word. So, I can't keep my public waiting. In the third client run the command
publish dave:comments "Billy, after ... the Pacific, you are my favorite ocean - JD"
And behold, all receive a great line from a great show.
Note On my system, at least, Ctrl-C is not working, so I ended the
subscription by just killing containers. Here is a one liner
for x in $(docker ps | grep clients | cut -c1-12); do docker kill $x; done
A common operation in a system is incrementing a counter. Often, one is tempted to do the following:
set my-counter 0
get my-counter
... proccessing ...
... update counter
set my-counter 1
Question If there are multiple users, what is the problem?
So, if all you are doing is incrementing, redis has an increment function to make this easier:
incr my-counter
In fact we can control the amount of the increment
incrby my-counter 10
But what if you want to do a bunch of commands together? Transactions would be helpful!
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set dave http://millman.us
QUEUED
127.0.0.1:6379> incr my-counter
QUEUED
127.0.0.1:6379> exec
1) OK
2) (integer) 13
Question What happens if you miss type a command and have an error while in a transaction?
Note that transactions in Redis are different than we have seen in other DBs. Each command is queued and then run as a batch. So if I run
127.0.0.1:6379> multi
127.0.0.1:6379> set a 1
127.0.0.1:6379> set b 2
127.0.0.1:6379> set c 3
127.0.0.1:6379> discard
None of the previous commands will have run
You can use the commands FLUSHDB and FLUSHALL to remove all keys from the
current db and all dbs. (Note that we didn't cover here, but you can change to
different DBs with SELECT).
Solve the quiz problems