Skip to content
Open Sourcebeginner

What is Redis?

Updated Nov 30, 20263 min readBased on official documentation
What is Redis?

In short

Redis is an open source, in-memory data store. It keeps data in the computer's RAM, so it can read and write values very quickly. Developers use it as a cache, a message broker, a session store, and a simple database for data that changes often.

What Redis is

Redis is a data store. A data store is a program that keeps information and lets other programs read and write it. Most databases save data to disk. Redis keeps its main data in memory, which is the fast working memory of a computer. Because of this, reads and writes usually take very little time.

Redis is open source, so its code is free to read, use, and change. The project offers several editions. Redis Open Source is the version you can run yourself on your own machine or server. The official documentation describes the other editions, including managed cloud services, but you do not need them to start learning.

Redis stores more than plain text. It supports several data structures, such as strings, lists, hashes, sets, and sorted sets. Each structure has its own set of commands. This is one reason developers like it: a single command can add an item to a set or fetch a range from a sorted list without extra work in your application.

How it works

You run a Redis server, and your application connects to it over a network connection. Your code sends commands such as SET to store a value and GET to read it back. Most programming languages have a client library that handles the connection and sends these commands for you.

Because the data lives in memory, the amount you can store is limited by the RAM on the machine. Redis can also save copies of its data to disk, so that the data survives a restart. Check the official documentation for the persistence options that match your needs, since the details depend on the version you run.

Redis handles commands one at a time in a single main thread. This keeps the logic simple and predictable. Each command is short, so many clients can share one server without waiting long for each other.

When to use it

Redis fits well in several common cases:

  • Caching results that are slow to compute or fetch, such as database query results or rendered pages.
  • Storing user sessions, so that many web servers can see the same login state.
  • Counting things, such as page views or API requests, with atomic increment commands.
  • Passing messages between parts of a system with publish and subscribe, or with streams.
  • Keeping leaderboards or rankings with sorted sets.

Redis is less suited to data you must never lose and that does not fit in memory. For large records with complex relations, a traditional database is usually the better choice. If you are weighing storage options for a small app, our guide on When to Use SQLite? explains a different style of lightweight storage.

A simple example

The following commands show the basic idea. You type them into the Redis CLI, a command-line tool that comes with Redis.

SET visits 0
INCR visits
INCR visits
GET visits

The first command stores the value 0 under the key visits. The two INCR commands add one to that number each time. The final GET returns 2. In an application, you would make the same calls through a client library, for example from Python or Node.js. Each key is a name you choose, and each value is stored under that name.

Common mistakes

Beginners often make a few predictable errors. The first is treating Redis as the only copy of important data. Keep a reliable primary database for records you cannot afford to lose, and use Redis to speed things up or to handle short-lived state.

The second mistake is forgetting expiry. Cached values can pile up and use all the memory if you never remove them. Redis lets you set a time-to-live on a key, so that it is deleted automatically after a set period. Use this for caches and sessions.

The third mistake is running Redis open to the whole internet with no protection. Limit network access, set a password, and read the security section of the official documentation before you deploy.

Alternatives

Several other tools solve similar problems. Memcached is a simple in-memory cache. Relational databases such as PostgreSQL or SQLite store data on disk and support rich queries. Each tool has trade-offs in speed, durability, and features. Pick Redis when you need fast access to simple structures in memory, and pick a traditional database when your main concern is durable, structured storage.

To learn more about the software world around tools like this, see What is Open Source? on our site. The official Redis documentation at https://redis.io/docs/latest/ is the best place to check current commands and options.

FAQ

Is Redis a database?

Redis is a data store that can act as a database for some workloads. It keeps data mainly in memory, so it is often used alongside a disk-based database rather than instead of one.

Is Redis free to use?

Redis Open Source is open source and free to run yourself. The project also offers paid editions and managed cloud services, which are described in the official documentation.

Does Redis lose data when it restarts?

It depends on how you configure persistence. Redis can save copies of its data to disk, so the data can survive a restart, but you should check the options for your version.

Which programming languages can use Redis?

Most major languages have Redis client libraries. The official documentation lists supported clients for languages such as Python, Node.js, Java, Go, and C#.

Sources

This page is based on the official documentation below. Read it for full detail.

Redis — official documentation ↗

Redis in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.