Introduction to Redis (Part 1)
In this tutorial, we will learn about Redis step by step. Let’s start by exploring what Redis is.
What is Redis?
Redis is an open-source (BSD licensed) in-memory data structure store. It is used as a database, cache, and message broker. Redis supports various data structures including strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries, and streams.
Let’s break the Redis word into 3 parts.
RE(Remote ) DI(Dictionary ) S(Server)
Redis stands for Remote Dictionary Server (Redis) and is essentially a key-value store.
What does this key-value store mean?
A key-value database, also known as a key-value store, utilizes a simple key-value pair method to store data. These databases consist of a unique string (the key) and a large data field (the value). This makes them a simple option for data storage, easy to design and implement. Key-value databases differ significantly from relational databases, which organize data into tables and define relationships between those tables.
Redis is a server/store that stores data as key-value pairs. For example, “Name” = “Kasun”, where “Name” is the key and “Kasun” is the value. Redis can be used to read and write data, functioning as a database.
Let us find out how the user interacts with the Redis key-value store.
We can write data to the Redis server using SET “Name” “Kasun”, and we can also read data from the server using GET “name”. You can use setters and getters for that.
When to use a key-value database
Your application might become unstable if it needs to handle a large number of small, constant read and write operations. Key-value databases provide fast, in-memory access. They are used for storing webpages with the URL as the key and the webpage as the value, fundamental data such as customer information, and preserving the contents of shopping carts, product categories, and e-commerce product information. These databases are suitable for programs that do not require frequent updates or support for complex queries.
Redis can be simple and straightforward.
Let’s look at some useful features.
- It’s a NoSQL database, which means it does not have structures like tables, rows, and columns, or use statements like SELECT, UPDATE, INSERT, and DELETE, unlike MySQL and Oracle databases.
- Redis utilizes various data structures to store data, including primary ones like String, Lists, Sets, Sorted Sets, and Hashes, as well as additional data structures such as Bitmaps, HyperLogLogs, and Geospatial Indexes.
- Interaction with the data is command-based. If you need the full list of commands –
- It’s an in-memory database with options for persistence. This means that Redis stores data in memory (cache) and doesn’t immediately write to disk, making it very fast. However, Redis does have an option to write data to disk.
Is Redis good as a database?
The Redis key-value database, initially designed as a cache, has evolved into a central database. It is commonly used as the primary database in modern applications. While most Redis service providers support Redis as a data keeper and cache maintainer, it is not commonly used as the main database. Consequently, in addition to using the Redis server, another database such as DynamoDB is required. This setup increases complexity, reduces response time, and prevents full utilization of the Redis instance.
The Redis server is a database capable of handling data volumes ranging from megabytes (MB) to terabytes (TB). With Redis Enterprise, it combines database and in-memory cache capabilities into one system, eliminating the complexity and response time of using two separate approaches. Redis can also be used as a primary database supporting multiple models, making it suitable for building modern applications and microservice-based architectures with low latency requirements.
Use Redis Enterprise’s native functionality (modules) to replace the need for different cache options and databases, such as:
- Redis instance JSON, storing JSON documents and streams for gathering and spreading data
- For additional indexes, use RediSearch
- Relationship complexity with RedisGraph
- RedisTimeSeries for monitoring applications
- RedisBloom for leaderboards, gaming, and fraud detection
- RedisAI for inferencing and immediate AI feature storing
- Utilize corporate clustering, auto-scaling, and Geo-Distribution that is Active-Active to take advantage of those mentioned above
Why do we use Redis?
There are some major features of Redis.
- Performance
- Flexibility
- Language Support
- Compatibility
- Durability
- Scaling
Performance and the Speed
You now know that Redis is extremely fast, but we are unsure of its exact speed.
Here you can find all the information about Redis's performance and speed. Redis can handle up to 120000 requests per second and even more.
Simple and Flexible
You know that Redis is a NoSQL data store, so there’s no need to define tables, rows, and columns. Statements are not necessary. Reading from and writing to the Redis server is very straightforward.
Durability
Redis is quite durable. While it primarily works with data in memory/cache, it also offers the option to write data to disk, which is configurable. Additionally, Redis can be used as a caching system or a full-fledged database.
Multi-Language and Platform Support
Please remember the following text: “Here, you can find the complete list of languages and clients supported for Redis.”
Compatibility
One major use of Redis is as a secondary database for applications to speed up transactions. Let’s consider an example.
In your system, requests that require fast service can be handled by storing data in Redis memory. This allows you to quickly serve data to the application. The remaining data can be stored in the main database. Redis acts as a cache, providing a high-speed data store, while the database is slower but more reliable and offers more features. Your application is responsible for retrieving data from Redis. If the data is not found, it should be fetched from the database and added to Redis with appropriate expiration and invalidation mechanisms.
Scaling
Redis offers an excellent master-slave replication feature. You can utilize various instances of Redis, with the master functioning as a write-only data store. One of the slaves can be used for read-only purposes, while the others can be employed for writing data to the disk. This setup can enhance performance, particularly when the master and one of the slaves are handling read-and-write operations.
Some other features
Memory
Redis is an in-memory NoSQL database. Hence there is a RAM restriction. Therefore, Redis employs a defined eviction policy to either cause out-of-memory or data eviction when memory storage data is exhausted.
Connections
Every action a client application sends to Redis occurs within a connection context. Whether it’s due to your operating system, Redis’ setup, or a service provider’s plan, the maximum number of connections to the Redis server is always constrained. A new application client or executive session should always be able to connect if there are sufficient free resources available.
Cache
The program should access the contents saved for the cache to work at its best.
Evictions
If your cache hit ratio falls below a certain threshold, it may indicate that the eviction has been triggered, removing data from Redis and leading to cache misses.
Expired objects
Maybe you are not expiring the cache objects since the eviction started when Redis’ memory was low.
Redis has a configuration file where you can store all configurations in a single file. It is single-threaded which helps eliminate issues related to thread locking timeouts and similar problems. Redis also has a pipelining feature that allows you to cluster multiple commands and send them at once.
Who uses Redis?
Here, you can find well-known companies and some popular sites that use Redis.
I hope you now have a much better understanding of Redis. See you in another tutorial.
Thank You!