Skip to content
wannacode

Key-value and the cache

In short: A key-value store such as Redis is an in-memory dictionary that returns a value by its key very fast, which makes it a good cache, session store and counter.

Problem

The page that asks the same thing thousands of times

The shop's home page shows the three bestselling products. This is the query:

SQLdatabase: shop

It joins three tables and adds up all the order rows. With 2000 visits a minute the database runs it 2000 times, while the answer changes once every few hours. The cart and the login also ask the database the same thing on every page. You need a place where the ready answer is fetched in a fraction of a millisecond.

The idea

A key and a value

A key-value database is a dictionary: every value has a key, and you get it by the key. There are no tables, columns or queries by value. That is why it is so fast: Redis keeps everything in memory and answers in microseconds.

The field is a simulator with the Redis commands. Keys are usually written with colons, so that it is clear what is in them:

key-value (like Redis)simulator

GET on a key that is not there gives (nil). The value is text, even when it is a number: the database does not know that 7.90 is a price.

Try it

A view counter

INCR increments the number in a key by 1 and returns the new one. If the key is not there, it starts from 0. Two pages that count at the same time do not get in each other's way: every command in Redis runs to the end before the next one.

Product 7 was viewed three times and product 9 once. Count the views in the keys views:product:7 and views:product:9.

key-value (like Redis)simulator

The idea

A cache that expires

A cache is a copy of an answer that is expensive to compute. The program first looks in the cache. If there is nothing there, it asks the database and puts the answer in the cache for a set time (EX in seconds). That time is the TTL (time to live), and TTL shows how much is left:

key-value (like Redis)simulator

After 60 seconds the key disappears on its own, and the next visit asks the database again. So the cache is never more than a minute stale, and the database runs the query once a minute, not 2000 times.

More precisely, if you are curious

SLEEP is a command of the simulator only: it moves its clock forward, so that you do not really wait. In the real Redis time just passes. The simulator sorts the answer of SMEMBERS and KEYS alphabetically, while Redis promises no order at all. And KEYS is not run on a real server with millions of keys: it goes through all of them and blocks every other command. There you use SCAN.

See it

The stale copy

The price of the coffee drops from 14.90 € to 12.90 €. The database is updated, but the cache still keeps the old page for another 10 minutes:

key-value (like Redis)simulator

This is the most common mistake with a cache: the copy differs from the truth. So the program that changes the product in the database immediately deletes its copy in the cache (DEL). The next visit finds nothing, asks the database and stores the new price. The TTL stays as a safety net, in case you forget some time.

Try it

A new price for the cheese

The cache holds the pages of the white cheese (product 7) and the butter (product 9). The price of the cheese just changed in the database. Make the next visit to the cheese get the new price, without touching the butter.

key-value (like Redis)simulator

The idea

Not only text

A value in Redis can also be a structure. Each has its own commands:

  • hash (HSET, HGET, HINCRBY, HGETALL): fields under one key, like a cart with a product and a quantity;
  • list (LPUSH, LRANGE, LTRIM): ordered values, like the recently viewed;
  • sorted set (ZADD, ZINCRBY, ZREVRANGE): members with a score, ordered by it, like a leaderboard.

The cart of customer 42 (product 9, butter, two pieces, and product 5, milk, one), which expires after a week without activity:

key-value (like Redis)simulator

The last three viewed products. LPUSH puts in front, and LTRIM cuts everything after the third:

key-value (like Redis)simulator

The pieces sold in October, ranked:

key-value (like Redis)simulator

With equal scores Redis orders the members alphabetically (here in reverse, because the leaderboard goes from the top down).

Exercise

A session, a cart and a leaderboard

1. Customer 42 logs in. The site gives them the session number 9f2c and keeps in Redis who they are.

Store the customer's number (42) in the key session:9f2c so that the session expires on its own after 30 minutes.

key-value (like Redis)simulator

2. The cart of customer 7 is in the hash cart:7: the field is the product number, and the value is the quantity.

Customer 7 puts 2 packs of butter (product 9) and 1 milk (product 5) in the cart. Then they add one more butter and remove the milk.

key-value (like Redis)simulator

3. The key rating holds the average rating of every product.

Show the three products with the highest rating, together with the ratings, from the highest down.

key-value (like Redis)simulator

The idea

When key-value, when not

A key-value database is good when you always know the key: a cache, sessions, carts, counters, rate limiting ("at most 5 login attempts a minute"), job queues. It is not good when you ask by value ("all products under 5 €"), when the data is connected ("the customer's orders with their products") and when losing it is not acceptable. That is why it usually sits next to the SQL database, not instead of it: the database is the truth, and Redis is its fast copy.

More precisely, if you are curious

Besides Redis (and Valkey, which is compatible with it), key-value databases are Memcached (a cache only, without structures), DynamoDB in AWS (with a key and a sort key, on disk, distributed) and etcd (for cluster settings, with a focus on consistency). Redis keeps the data on disk in two ways: a snapshot every few minutes (RDB) or a log of every command (AOF).

Test

Check yourself

1. Which of these can NOT be done in a key-value database without reading all the keys?
2. The list of bestsellers is kept in the cache for 60 seconds. What is the price of that?
3. The price of the coffee changed in the database. What is the safest thing for the cache?
4. Redis keeps the data in memory. What happens on a restart?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·