CPSC/DATA 350 - Applications of Databases - Spring 2025

Assignment #7

Possible experience: +40XP

Due: Friday, Apr. 4, midnight

The DB World Tour®, stop #2 of 4: Redis

In the second installment of your DB World Tour project, you'll add the next fourth of your website, backed by a Redis database.

Your topic and assignment #7

Identify some different aspect of your assignment #6 topic that would lend itself well to the data structure types Redis provides. Remember that these are atomic key/value pairs, sets, hashes, linked lists, and sorted sets.

As before, pick something interesting and non-trivial, but not exhaustive.

Setup

To get Python bindings for the Redis commands, make sure to run:

$ pip install redis

after activating your virtual environment. (You only need to do this once.)

Also take a moment to ensure you can bring up the Redis command-line interface:

$ redis-cli

and that you get a happy prompt. (Type exit to exit.)

Logical database instances

For this assignment, each student will be assigned a different logical Redis database instance, which will correspond to different key-value stores (so you're not all reading and writing on top of each other). The number of your instance has been added to the port numbers page.

When you connect to Redis in Python, you'll do this:

import redis
r = redis.Redis(db=N, decode_responses=True)

where N is the number of your Redis instance.

When you talk to Redis via its command line interface, you'll type:

$ redis-cli
127.0.0.1:6379> select N
127.0.0.1:6379[N]>  ... more Redis commands ...

to do so, again replacing N with the number of your Redis instance.

PLEASE do not be a jerk and go into someone else's Redis instance and mess around in it, even as a joke. Not only will this be considered an Honor Code violation, but if the person whose database you hacked is sufficiently pissed, it might even cost you your life.

Assignment #7 requirements

Your assignment #7 should fulfill the following requirements:

  1. For this assignment, you must include two of the following Redis data structures:

    Think carefully about which data structures would be appropriate for the kind of data you want to track. If you choose poorly, your database and your code is going to be a mess.

  2. In addition to displaying data in the Redis database, your website must provide at least some way to interact with and change/modify that data. I'm very flexible about the way you do this. In particular, you do not need to mimic my example below.
  3. Finally, you must make some use of Flask sessions in this milestone of your project. I'll know this works if I can access your website simultaneously (1) from Firefox on my laptop, (2) from Chrome on my laptop, and (3) from Firefox on my tablet, and see different behavior for the three. One example would be to do something like what we did in the Ben & Jerry's example: we allowed each user to choose a favorite recipe and a favorite color, and had their favorite recipe prominently displayed in some kind of banner while the background of each page was colored according to their preference. Another example would be to do something like what we did in the Hobby Manager® example: allow users to login with name/password, and maintain their own information (list of hobbies) on the server.

Example

My crime database uses two Redis data structures to hold information about the shows my wife and I watch together. A linked list called tour:queue holds the titles of our "queue" of shows that we plan to watch soon. A sorted set called tour:time_investments keeps track of the total amount of our lives (in minutes) we've spent watching each show. Here's what they look like from the Redis command-line interface:

127.0.0.1:6379[43]> lrange tour:queue 0 -1
1) "Innocent"
2) "Mare of Easttown"
3) "No.1 Ladies Detective Agency"

127.0.0.1:6379[43]> zrevrange tour:time_investments 0 -1 withscores 
 1) "A Touch of Frost"
 2) "110"
 3) "Bang"
 4) "95"
 5) "Injustice"
 6) "90"
 7) "No.1 Ladies Detective Agency"
 8) "45"
 9) "Goliath"
10) "45"
11) "Columbo"
12) "45"

My revised page now looks like:

The new elements on the right-side are from Redis. The queue is shown at the top in our planned watching order. Choosing an entry from the "Show to queue:" drop-down, and pressing the "Add to queue" button, refreshes the page and adds the show to the queue in the bottom position. When we watch a show, we press the "Watch!" button (which includes the show at the top of the queue) and enter the number of minutes for that episode. The show is then dequeued from the top of the queue, and the total time for that show is incremented in the sorted set accordingly.

This is just how I chose to do my Redis stuff. You do not need to imitate these choices whatsoever. Use two different Redis data structures in whatever way you wish. You do not need to have a queue, or a total time investment list, or even use a linked list or sorted set at all. Be creative.

Turning it in

Send me an email with subject line "CPSC/DATA 350 Assignment #7 turn-in". In the body of the email, paste the link to the landing page of your website. Also in the body of the email, explain in plain English how I can go about verifying that you used sessions properly. You could write something like, "in one browser, Stephen, go to the XYZ page and do ABC; then, while that browser is still open, open a different browser and go to the XYZ page and do DEF; then, play around with the site from both browsers simultaneously and see how they each maintain, display, and let the user interact with their own information."

Getting help

Come to office hours, or send me email with subject line "CPSC 350 Assignment #7 help!!"