Skip to content

Latest commit

 

History

History
149 lines (90 loc) · 3.17 KB

2021-11-03.md

File metadata and controls

149 lines (90 loc) · 3.17 KB

Redis

Notes are based of 7DB - Redis - Day 1 & 2

Setup

Follow the setup instructions

Sets

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

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

Expiry

One of my favorite uses for Redis is a cache, but as with all good caching systems, you must have someway to determine if data is no longer valid. Time is a great option, which can be accomplished with EXPIRY for example

set ice "I'm melting...."
expire ice 10

And now lets check

> exists ice
(integer) 1
> exists ice
(integer) 0

In fact, we can set and expire as a 1 liner

setex ice 10 "I'm melting...."
exists ice
...

And we can check the TTL

setex ice 10 "I'm melting...."
ttl ice
...
ttl ice

Transactions

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

Clean up

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).

Quiz

Write Redis commands (or a sequence of commands) for the following:

  1. Creates two sets A and B such that A \intersect B is non-empty

  2. In a transaction, implement symmetric difference. Hint You will need to make some temp sets (look at SUNIONSTORE and SINTERSTORE). You must cleanup any temporary data that you create.