Databases

Redis: Super-fast data

The in-memory database behind caches, sessions, leaderboards and queues. Learn it one command at a time.

9 chapters · 39 lessons · free

Try a free lesson Create your free account

What you will learn

1. Hello, Redis

What Redis is and why apps love it. Save keys, read them, delete them, and name them like a pro.

  • Your first key Try it SET key value saves, GET key reads. Redis answers every command.
  • Keys with a pattern Exercise Name keys like object:id:field, and put text with spaces in quotes. Values are always stored as text.
  • Delete a cart Exercise DEL deletes keys and EXISTS checks if a key is there (1 or 0).
  • The motto that broke Debug Spaces separate values in Redis. Text with spaces goes in quotes.
  • Quick check Quiz SET, GET, DEL and EXISTS, key names with colons, and quotes for text with spaces.

2. Counters and time

Count visits and coins in one safe step, and make keys that delete themselves after a while.

  • Count the visitors Exercise INCR adds 1 and INCRBY adds any number, safely, even with many users at once.
  • Spend and earn Exercise DECRBY and DECR take away, INCRBY and INCR add.
  • A code that expires Exercise EX seconds makes a key expire, and TTL tells how long it has left.
  • A flash sale Exercise When the TTL runs out, Redis deletes the key by itself.
  • Ten is not a number Debug INCR, INCRBY and DECRBY need values written as whole numbers with digits.
  • Quick check Quiz INCR/INCRBY/DECRBY, EX and TTL, and keys that delete themselves.

3. Lists

Ordered values: to-do lists, job queues and "the latest 10" lists.

  • A to-do list Exercise RPUSH/LPUSH add to the end/start of a list, and LRANGE key 0 -1 reads all of it.
  • A queue of jobs Exercise RPUSH + LPOP make a queue: first in, first out. LLEN counts what is waiting.
  • Only the latest 3 Exercise LPUSH + LTRIM key 0 n keeps a list of the newest items, at a fixed size.
  • GET on a list Debug Each type has its own commands: GET for strings, LRANGE for lists. TYPE key tells you which one it is.
  • Quick check Quiz RPUSH, LPUSH, LPOP, LRANGE, LLEN and LTRIM: queues and "latest" lists.

4. Hashes

One key, many fields: user profiles and objects, and counters inside them.

  • A user in one key Exercise A hash holds fields: HSET sets them, HGET reads one, HGETALL reads all.
  • Coins inside the profile Exercise HINCRBY adds to a number field of a hash.
  • Remove a field Exercise HDEL removes a field, HEXISTS checks if one is there.
  • A field without a value Debug HSET takes field-value pairs: every field needs a value right after it.
  • Quick check Quiz HSET, HGET, HGETALL, HINCRBY, HDEL and HEXISTS.

5. Sets

Unique values: tags, who is online, and friends in common.

  • Unique tags Exercise A set keeps each value once. SADD, SMEMBERS, SCARD.
  • Who is online? Exercise SISMEMBER checks if a value is in a set: 1 or 0.
  • Friends in common Exercise SINTER (in both), SUNION (in either) and SDIFF (in the first only) combine sets.
  • LRANGE on a set Debug Sets are read with SMEMBERS, not LRANGE: they have no positions.
  • Quick check Quiz SADD, SMEMBERS, SCARD, SISMEMBER, SINTER, SUNION and SDIFF.

6. Sorted sets

Members with scores, always in order: leaderboards, ranks and score ranges.

  • A leaderboard Exercise ZADD adds members with scores; ZREVRANGE ... WITHSCORES lists them from the top.
  • A new leader Exercise ZINCRBY adds to a score, ZREVRANK gives the place from the top (0 = first).
  • Players in a range Exercise ZRANGEBYSCORE and ZCOUNT work on a range of scores.
  • The upside-down top 3 Debug ZRANGE starts at the lowest score, ZREVRANGE at the highest.
  • Quick check Quiz ZADD, ZINCRBY, ZREVRANGE, ZREVRANK, ZRANGEBYSCORE and ZCOUNT.

7. Redis in real apps

The patterns behind real products: caches, rate limiters, login sessions and transactions.

  • A cache Exercise Cache: GET first; on (nil), compute and SET with EX; the next GET is instant.
  • A rate limiter Exercise A rate limiter: INCR per request, EXPIRE on the first one, and block above the limit.
  • A login session Exercise Sessions: a hash with the details plus EXPIRE, so they clean themselves up.
  • Send coins safely Exercise MULTI queues commands and EXEC runs them together, with nothing in between.
  • The cache that never refreshes Debug Cache keys always need an expiry, or they serve old data forever.
  • Quick check Quiz Caches, rate limiters, sessions and transactions.

8. Redis exam

Ten questions about everything in this course. Pass with 8 right answers.

  • The Redis exam Exam Strings, counters and expiry, lists, hashes, sets, sorted sets, and real-app patterns.

9. Final project

Design the data of an online game in Redis: profiles, a leaderboard, inventories, sessions and counters. Then send it for review.

  • A game backend Final project Choosing the right type for each job: that is designing with Redis.

How the course works

You learn by doing: each lesson explains one idea, then you write real code in the editor and see the result right away. The first lesson is free to try without an account; sign up to save your progress and continue.

  • Try it

    Change working code and see the result immediately.

  • Exercise

    Complete a task. A checklist confirms each requirement as you type.

  • Debug

    Fix code that is broken on purpose, and learn why it failed.

  • Quiz

    Check your understanding at the end of a chapter.

More in Databases