What you will do
A key-value store keeps a value under a name, and gives it back when you ask for the name. Websites use one to remember a visitor's session or to count page views, because it answers in a fraction of a millisecond. Valkey is a free one that speaks the same commands as Redis. You will store and fetch a value, make a value expire, and meet a list, a hash and a counter.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The store is a service called valkey, and the terminal reaches it by that name. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
1. Say hello to the store
This terminal has no Valkey client, so the first command makes a tiny one: a function called v that sends its words to the store on port 6379 with nc and prints what comes back. It prints nothing itself. Then SET stores a value under a key and the store answers +OK. GET asks for it: the reply is $5, the length of the answer, and on the next line hello.
v() ( printf '%s\r\n' "$*" | nc -w 1 valkey 6379 )Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
v SET greeting helloShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
v GET greetingShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
2. Make a value expire
EX 5 says this key lives for five seconds, and v TTL code in your own terminal prints how many seconds are left, if you are quick. Run the second command after about six seconds: the reply is $-1, the store's way of saying there is nothing there. That is how a login session is made to time out.
v SET code 1234 EX 5Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
v GET codeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
3. A list, a hash and a counter
RPUSH adds to the end of a list and answers how long the list is now, :3. HSET stores named fields in one key, a hash, and answers how many it added, :2. INCR adds one to a number and answers the new value, :1: press it again for :2, and a thousand visitors at once would still be counted exactly. In your own terminal v LRANGE tasks 0 -1 reads the list back and v HGETALL user:1 the hash. A reply starting : is a number and $ a piece of text.
v RPUSH tasks wake eat codeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
v HSET user:1 name Ada born 1815Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
v INCR visitsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
4. See what lasts
Press Stop the lab on the lab bar and then Start the lab again. The new terminal has forgotten v, so press step 1's first Run to make it again, then its v GET greeting Run: it is still hello, and code has long expired. Valkey keeps everything in memory, which is why it is fast, and logs each write to a file in a named volume, which the platform saves, so it can rebuild the memory when it starts. A store with no such file would start empty every time.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one small service and one named volume, and nothing here marks itself: the student hands in the replies of their own session, a key with an expiry and TTL, a list, a hash and a counter, with a sentence on what the expiry did and what was kept after a stop and a start. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.