Redis
Last updated
Last updated
What is Redis? β Redis (REmote DIctionary Server) is an open source code used to store structured data, which can be used as a database, cache or a message broker.
It is a data storage system with a very powerful and popular KEY-VALUE format today. Redis stands out by supporting many basic data structures such as: hash, list, set, sorted set, string... All data is written and saved on ram, so the data reading and writing speed is very fast.
After the concept of what Redis is, let's go to the application of Redis. In addition to the KEY-VALUE storage feature on RAM, Redis also supports the function of sorting, querying, and backing up data on the hard disk, allowing you to restore data. Data recovery when the system crashes... and can be cloned (Run multiple Redis Servers at the same time).
Caching: Used as a cache. Because of its fast read and write speed, Redis can be used as a cache, a place to share data between applications or as a temporary database. In addition, Redis can be used to make a page Cache for the website. Also because of the consistency of Redis, even if Redis is restarted, users will not feel slow when loading the page.
Counter: Used as a counter. With the property of increasing and decreasing parameters very quickly while data is stored in RAM, sets and sorted sets are used to count views of a website, for example in game rankings. Redis supports thread safety so it can synchronize data between requests.
Publish/Suscribe (Pub/Sub): Create a data-sharing channel. Redis supports creating channels to exchange data between publishers and subscribers like channels in Socket Cluster or topics in Apache Kafka. For example, Pub/Sub is used to track connections in social networks or chat systems.
Queues: Create queues to process requests one after another. Redis allows storing lists and provides many operations with list elements, so it can also be used as a message queue.
Unlike RDMS like MySQL, or PostgreSQL, Redis does not have a table. Redis stores data as key-values. In fact, memcache does the same, but memcache's data types are limited and not as diverse as Redis, so it cannot support many user operations. Below is an overview of the Redis data types used to store values.
β STRING : string, integer or float. Redis can work with whole strings, parts of strings, as well as increase/decrease the value of integer and float.
β LIST : List is a list of strings, arranged in insert order. Redis can add an element to the beginning or end of a list. Lists are suitable for problems that need to manipulate elements near the beginning and end because this access is extremely fast, even when inserting millions of elements. However, the disadvantage is that accessing elements in the middle of the list is very slow.
β SET : collection of strings (not sorted). Redis supports the operations of adding, reading, deleting each element, and checking the presence of an element in the set. In addition, Redis also supports set operations, including intersect/union/difference.
β HASH : stores hash table of key-value pairs, in which keys are arranged randomly, not in any order. Redis supports adding, reading, and deleting individual elements, as well as reading all values.
β SORTED SET (ZSET) : is a list, in which each element is a map of a string (member) and a floating-point number (score), the list is sorted according to this score. The elements of zset are arranged in order from smallest to largest score.
In addition, Redis also supports other data types such as: Bit arrays, HyperLogLogs, Streams. To install, please refer to the Redis docs .
Besides storing key-values ββin RAM memory, Redis has 2 background threads that specialize in periodically writing data to the hard disk.
There are 2 types of files recorded to hard disk:
RDB (Redis DataBase file)
RDB creates and backs up snapshots of the DB to the hard drive every certain period of time.
Advantage
RDB allows users to save different versions of the DB, which is very convenient when problems occur.
By storing data in a fixed file, users can easily transfer data to different data centers and servers.
RDB helps optimize Redis performance. The main Redis process will only do work on RAM, including basic operations requested from the client such as add/read/delete, while a child process will take care of disk I/O operations. . This organization helps maximize the performance of Redis.
When restarting the server, using RDB to work with large amounts of data will have higher speed than using AOF.
Defect
RDB is not a good choice if you want to minimize the risk of data loss.
Normally users will set up to create RDB snapshots every 5 minutes (or more). Therefore, in case of a problem, Redis cannot operate, and data in the last minutes will be lost.
RDB needs to use fork() to create child processes for disk I/O operations. In case the data is too large, the fork() process can take time and the server will not be able to respond to the request from the client within a few milliseconds or even 1 second depending on the amount of data and CPU performance.
AOF saves all write operations received by the server, these operations will be rerun when restarting the server or resetting the original dataset.
Advantage
Using AOF will help ensure the dataset is more persistent than using RDB. Users can configure Redis to log each query or once every second.
Redis writes the AOF log appended to the end of the existing file, so the search process on the existing file is not necessary. In addition, even if only half of the commands are recorded in the log file (possibly because the drive is full), Redis still has a mechanism to manage and repair that way (redis-check-aof).
Redis provides a background process that allows rewriting AOF files when the file size is too large.
While the server still performs operations on the old file, a completely new file is created with the minimum number of operations needed to create the current dataset. And once the new file is finished writing, Redis will switch to performing logging operations on the new file.
Defect
AOF files are often larger than RDB files with the same dataset.
AOF can be slower than RDB depending on how the time interval for backing up to the hard drive is set. However, if you set log every 1 second, you can achieve performance equivalent to RDB.
Redis developers have encountered a bug with AOF (although it is very rare), which is the error that AOF cannot accurately recreate the dataset when restarting Redis. This error has never been encountered when working with RDB.
Redis is a great choice when we need a data storage server that requires high scalability and sharing by many processes, many applications and many different servers