Redis for dummies
According to the 2020 StackOverflow Developer’s Survey, Redis is still the most loved ❤ database, I can understand why since it’s so easy to use and versatile, so here’s a fast introduction to it.
Redis is a multi-model, NoSQL, key-value database that enables search, messaging, streaming and much more.
It keeps data in memory to enable fast access, persists data to storage to provide consistency and its replication of content also guarantees durability of the data.
Redis runs as server-side software so its core functionality is in its server component. The server listens for connections from clients, programmatically or through the command-line interface. A simple configuration file specifies the server settings.
The CLI & SDKs
The command-line interface is a powerful tool that gives you complete access to Redis’s data and its functionalities but if you are developing a software or tool that needs to interact with it, there are many SDKs for the most famous languages: redis-py for python; node_redis for Node.js; ServiceStack & StackExchange for C#; Predis for PHP and finally hiredis for C.
There is no formal database creation with Redis, actually there is no formal database at all in Redis. Data is stored in RAM on the server, Redis then writes the contents of the database to disk at varying intervals to persist it in case of failure.
Because of its fast data access Redis is famous for caching, but it’s certainly not its only usage, it also handles very well large datasets and it’s extremely good for full-text search tasks. Another famous use case is to manage messaging and queuing systems.
For example both python-rq and Celery, two of the major queue libraries for Python, support Redis as their backend.
Redis also has some built-in functionalities to operate as a pub-sub manager, maintaining channels that applications can subscribe or publish to.
Data models represent how data is stored within the database. Redis is a multi-model database because it supports multiple models at the same time, meaning that data can be used in the most appropriate manner and stored simultaneously.
- String: the simplest model is the string, a text value
- Bitmap: similar to strings, it’s a simple value representing only a 1 or a 0
- Lists: used to store related data, they use one key to hold several ordered values, added to the head or the tail of the list
- Sets: somewhat like lists, but their values cannot be retrieved by index value as they’re not sorted. Values in a set are unique, so their main use is to query them to check an element’s presence
- Sorted Sets: used to store data that needs to be ranked, like a leader board
- HyperLogLog: used to keep an estimated count of unique items, this is extremely useful when tracking high values changing frequently
This is everything you need to know before working with Redis, its simplicity of use and versatility make it one of the most common and loved database there is. If you want to try it out with Docker, simply run:
docker run --name some-redis -d redis