Introduction to Redis
Table of Contents + â
Youâve definitely heard this one before:
- A senior dev looks at a slow page and says, âjust put it in Redis.â
- Everyone nods like they get it, and the page magically gets faster.
- But what actually is this Redis thing, right? And why does throwing stuff into it make things quick?
So letâs clear it up properly. By the end of this lesson youâll know what Redis is, why people reach for it, and where it fits in a real system. Weâll keep it beginner-correct. Simple, but not so simple that it becomes wrong.
đŻ Why Redis
Letâs start with the pain, because Redis only makes sense once you feel the problem.
Say Alex builds a web app. Thereâs a database holding all the data, and thereâs the app server handling requests. Now two annoying things keep happening:
- The database is slow for hot data. Some bits of data get asked for again and again, like a popular product or a userâs profile. Every single request goes back to the database, and the database has to do real work each time. That gets slow and expensive.
- App memory doesnât stick around. Sure, the app server can keep stuff in its own memory to go faster. But the moment it restarts, that memory is wiped clean. So everything saved there is just gone.
- App memory isnât shared. And when you run more than one server (which you always do at scale), each one has its own separate memory. Server A doesnât know what Server B remembered. So they canât share anything.
So Alex needs a spot thatâs fast like memory, but also shared across all the servers and not tied to any one of them. That spot is Redis.
The one-line reason Redis exists
Your database is reliable but slow for hot data. Your appâs own memory is fast but private and forgetful. Redis sits in the middle: itâs fast, itâs shared, and it stays up even when your app servers restart.
⥠What is Redis
Hereâs the plain definition. Redis is an in-memory key-value data store. Thatâs a mouthful, so letâs break the two scary words apart.
- In-memory means it keeps its data in RAM, the computerâs fast working memory, instead of on a hard disk. RAM is way faster to read from than a disk. Thatâs the whole secret behind Redis being quick.
- Key-value means you store things as simple pairs: a key (a name you pick) and a value (the data). Itâs just like a dictionary or a labelled box. You hand it a key, it hands you back the value. No tables, no joins, no complicated queries.
A few more things worth knowing right away:
- Redis is famous for being really fast. Most operations finish in well under a millisecond, because itâs all happening in memory.
- It can store more than just plain text. It supports rich data types like lists and sets, which weâll see in a second.
- Itâs often called a cache, but itâs a bit more than that. A cache is just a fast temporary store for data you want to grab again quickly. Redis is great at being a cache, but people use it for other jobs too.
The most common way people use Redis is the cache-aside pattern. The idea is dead simple: check Redis first, and only bother the database if Redis doesnât have it.
So the first request is a little slow because it hits the database. But it stashes a copy in Redis on the way out. Every request after that gets the fast answer straight from memory. That moment when Redis has the data is called a cache hit, and when it doesnât, itâs a cache miss.
đ§ą Redis Data Types
This is where Redis really shines over a plain âstring in, string outâ cache. The value you store doesnât have to be just text. Redis understands a handful of useful shapes, and picking the right one makes your life easier.
| Data Type | What it holds | Good for |
|---|---|---|
| String | A single value, like text or a number | Caching a value, counting things |
| List | An ordered sequence of items | Queues, recent activity feeds |
| Hash | A bunch of field-value pairs under one key | Storing an object, like a user profile |
| Set | A collection of unique items, no duplicates | Tags, tracking unique visitors |
| Sorted Set | A set where each item has a score to rank by | Leaderboards, top-N lists |
You donât need to memorise these. Just remember the headline: Redis isnât only for plain strings. When you need a ranked leaderboard or a quick queue, thereâs usually a built-in type that fits.
â¨ď¸ Basic Commands
Letâs actually touch Redis now. The commands are short and read almost like English, so donât worry. Here are the three youâll use the most.
SET user:1 "Alex"GET user:1EXPIRE user:1 60Letâs go through them one line at a time:
SET user:1 "Alex"stores the value"Alex"under the keyuser:1. Theuser:1is just a name we picked. People like using colons like this to keep keys organized, souser:1means âuser with id 1â.GET user:1asks for whatever is stored underuser:1. Redis hands back"Alex". Thatâs the whole read.EXPIRE user:1 60sets a timer. After 60 seconds, Redis automatically deletes that key. This is huge for caching, because you usually donât want cached data hanging around forever.
That last one points at something important. This automatic-deletion-after-some-time is called a TTL, short for âtime to liveâ. You set a TTL so old cached data clears itself out, and the next request goes and fetches a fresh copy from the database.
Why TTL matters
Without a TTL, your cache holds onto data forever, even after the real data has changed. With a TTL, stale stuff expires on its own and gets refreshed. Itâs the simplest way to keep a cache from going out of date.
đ ď¸ What People Use Redis For
Okay so we know what it is. Now where does it actually show up in real systems? Here are the big ones youâll keep bumping into.
- Caching database results. This is the classic. Save the answer to a slow or repeated query in Redis, so the next request skips the database entirely.
- Session storage. When you log in, the server needs to remember âthis person is Alex and theyâre logged inâ. Storing that session in Redis means any of your servers can read it, so logins work no matter which server handles your next request.
- Leaderboards. That sorted set type makes ranking effortless. Add players with their scores, and Redis keeps them sorted, so âtop 10 playersâ is instant.
- Rate limiting. Want to stop someone overloading your API? Keep a counter in Redis per user, bump it on each request, and give it a TTL. If the count goes over the limit in that window, you block them.
- Queues and messaging. Lists and Redisâs other features let you pass jobs between services, like âsend this emailâ landing in a queue for a worker to pick up later.
Notice the theme across all of these: they all need something fast and shared. Thatâs Redisâs sweet spot.
â ď¸ Things to Know
Redis is great, but itâs not magic. A few honest limits to keep in your head.
- It lives in RAM, so itâs limited by RAM. Memory is fast but itâs smaller and pricier than disk. You canât dump your entire database into Redis. You keep the hot, frequently-used stuff there, not everything.
- Data can be lost. Because itâs in memory, a crash or restart can wipe it if you havenât told it to save. Redis does offer ways to write data to disk so it survives a restart. The two main ones are RDB, which takes snapshots every so often, and AOF, which logs every change. With persistence on, youâre much safer, but for a pure cache people often donât even bother.
- Itâs not your main database. Redis is brilliant as a fast layer, but itâs not built to be the one place that safely holds all your important data forever. Your database is still the source of truth. Redis is the fast helper sitting in front of it.
- It runs commands one at a time. Redis processes commands single-threaded, meaning it handles one command at a time in order. That sounds slow but it isnât, because each command is so quick. It does mean one giant slow command can hold up everyone else, so you avoid those.
â ď¸ Common Mistakes and Misconceptions
A few things trip people up early. Letâs clear them out so you donât say them in an interview.
- âRedis can replace my database.â No. Redis is a fast layer in front of your database, not a replacement for it. Your database is still where the real, durable data lives. Redis holds copies and temporary things.
- âData in Redis is always safe.â Not by default. Itâs in memory, so a restart can lose it unless youâve turned on persistence. Treat anything in Redis as droppable unless youâve planned otherwise.
- âOnce itâs cached, Iâm done.â This is the stale-data trap. The real data in your database changes, but the old copy sits in Redis happily serving wrong answers. Thatâs why you use a TTL or clear the cached key when the data updates.
- âRedis is just a string cache.â Itâs much more. Hashes, sets, sorted sets, and lists let it do leaderboards, queues, and structured data, not just plain text.
đ ď¸ Design Challenge
Try this one yourself to lock it in.
Imagine youâre building a news website, and the homepage shows the âtop 5 trending articlesâ. This list is expensive to compute and gets viewed millions of times an hour. Think through how youâd use Redis here:
- Which data type fits a ranked âtop 5â list?
- Where in the request flow do you check Redis, and when do you fall back to the database?
- What TTL would you put on it, so the trending list updates but you still serve most requests from cache?
Thereâs no single right answer. The point is to reason about hits, misses, and freshness, which is exactly how youâd talk through caching in an interview.
đ§Š What Youâve Learned
You can now explain what Redis is and where it fits. Hereâs what youâve picked up.
- â Redis is an in-memory key-value store, so itâs fast but lives in RAM.
- â It solves the problem of slow hot-data reads and unshared, forgetful app memory.
- â The cache-aside pattern checks Redis first and falls back to the database on a miss.
- â It supports rich data types: strings, lists, hashes, sets, and sorted sets.
- â
Core commands are simple:
SET,GET, andEXPIREfor a TTL. - â People use it for caching, sessions, leaderboards, rate limiting, and queues.
- â Itâs RAM-limited, can lose data without persistence (RDB/AOF), and isnât a replacement for your main database.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is Redis?
Why: Redis keeps data in RAM as key-value pairs, which is what makes it extremely fast.
- 2
Why is Redis so fast?
Why: Reading from memory is far quicker than reading from disk, so most Redis operations finish in under a millisecond.
- 3
What does the EXPIRE command do?
Why: EXPIRE sets a time to live, so Redis removes the key automatically once the timer runs out.
- 4
Which Redis data type fits a ranked leaderboard best?
Why: A sorted set gives each item a score and keeps them ranked, so top-N lists like leaderboards are instant.
đ Whatâs Next?
Youâve got the what and the why of Redis. Next, letâs zoom out and in.
- Introduction to Caching covers the bigger picture of why caching works and where caches live across a system.
- Cache Eviction Policies digs into what happens when the cache fills up, and how Redis decides which data to throw out (like LRU and LFU).
Get those down and youâll have a solid grip on the caching questions that show up in almost every system design interview.