Valkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters
This lab gives each student a Valkey server of their own to type commands at. Valkey is a key-value store: it keeps a value under a name and gives it back when asked, which is what websites use for sessions, caches and counters. It speaks the same commands as Redis, so what a student learns here carries over.
It is not an attack lab. Every copy has no route to the internet and none to any other student, and it has no web page, so everything is typed in the terminal beside it.
What students learn
How a key-value store differs from a table: SET and GET store and fetch a value by its key, a key can be given a lifetime with EX and counted down with TTL, and the store has a few structures of its own, a list (RPUSH, LRANGE), a hash of named fields (HSET, HGETALL) and a counter (INCR) that adds exactly even when many clients ask at once. They also read the store’s replies, the protocol called RESP, and see what survives the lab being stopped: the data is held in memory, and every write is logged to a file in a named volume, which is how it comes back. It suits an introduction to databases and data stores, or to the caches and sessions of web development.
Run it in your browser
The lab starts one service, valkey, and waits until it answers. The terminal does not have the usual Valkey client, valkey-cli. The lesson sends each command with nc instead, through a one-line function called v, and the replies are printed exactly as they arrive: +OK for a success, $5 then the text for a value five bytes long, $-1 for a key that is not there and :3 for a number.
- Open the Container page. Open Build on the bar and choose Run a container. Anybody may open it, and the Git lab and the SQL lab start with no account. Every other lab needs a free account: create one, sign in, then press Start.
- Find the lab. The Sample labs card lists every sample. Find Valkey.
- Press Start. Start runs it as it is, in one press. Edit the file first opens the compose file so you can change it before you start.
- Open it. On the lab’s page, press Open terminal. There is no link under In your browser because Valkey has no web page. Type nc valkey 6379 and then SET greeting hello, press Enter, and the store answers +OK. GET greeting answers $5 and then hello. The lesson defines the function v so that v SET greeting hello does the same without typing a connection first.
What to ask students to hand in
The replies to a short session of their own: a key with an expiry and TTL counting down, a list, a hash and a counter. Then a sentence on what the expiry did, and one on what was still there after they stopped and started the lab, and why. The last one shows whether they understood data in memory against a file in a volume.
A basic lesson on this lab
Valkey Basics is a small lesson worked in this lab. In it a student makes the v function, stores and fetches a value, gives a key five seconds to live and sees it disappear, and adds to a list, a hash and a counter. They then stop and start the lab to see what was kept.
Free personal accounts have it in the Basics module, which is on Home, every college can copy it from the Library’s Basics folder, and a new college opens with it in a module called Lab Basics (Sample). It is deliberately small: it shows a teacher what a lesson worked in a lab is, and the lab does the rest. Copy it into a module of your own to change it.
The code on GitHub
The compose file has no Dockerfile: it runs one stock public image, valkey/valkey, pinned to a version, as an unprivileged user with 64 MB of memory and an append-only file in a named volume. Valkey is licensed BSD-3-Clause and is not distributed by us. It is the open source fork of Redis, used here instead of Redis 8 and later, which have a different licence. The lab has no way out to the internet and no password, since nothing else can reach the store; it is one service, so features that need a second server, such as replication, are not part of it.
Set it as work
Say in the task what to hand in.
- Make the lab. In your module add a task and choose the Container lab card, then In a GitHub repository. Give https://github.com/mitsurutech/lab-valkey with the branch main. The compose file is the default, compose.yaml. Fork the repository first if you want to change it.
- Publish and try it. Publish pins the commit you were shown, so a later change in the repository does not alter a lab you have already set. Start your own copy first.
- Hand it out. Assign it like any task, with a due date if you want one, and say in the task what to do and what to hand in.
- Mark what is handed in. Open a student’s copy and the work they handed in, mark it, and send it back with a reason if it needs more.
Questions people ask
- Is this Redis?
- It is Valkey, the open source fork of Redis, and it answers the same commands. The lab uses Valkey because it is BSD-3-Clause licensed, where Redis 8 and later are not.
- Why does the terminal use nc and not valkey-cli?
- The lab’s terminal carries psql, mariadb, curl, git and nc, and no Valkey client. Valkey accepts a command as a line of plain text, so nc can send one and print the reply. On your own machine you can use valkey-cli or redis-cli with the same lab.
- What is still there after I stop and start the lab?
- What was written and had not expired. The store logs every write to a file in a named volume, which the platform saves, and rebuilds its memory from it when it starts. A key given a lifetime that has run out stays gone.