Redis 101
Let's learn about Redis
Reference: redis.io, https://brunch.co.kr/@jehovah/20
Intro to Redis
What is Redis?
In-Memory Data Structure Store
A
key-valuebased In-Memory data storeSince it is key-value based, results can be retrieved without needing separate queries
A
memory-based data store, not disk-basedSince it processes data in memory rather than writing data to disk, the speed is remarkably fast
It does not rely solely on memory, so data is not lost even when the server shuts down
Uses a
snapshotapproach that saves to disk in between, andRecords all logs so data is recovered based on logs when the server restarts
Open Source (BSD 3 License)
A proven open source solution used by many companies including Samsung Electronics, Naver, LINE, etc.
DataTypes
Redis supports various data types
Strings
The most basic Redis value
Simple
key-valuemapping structure
Lists
Array-style data structure
With Lists, inserting and removing data at the beginning and end is fast, but inserting data in the middle is difficult
Sets
An unordered collection of String data
Does not allow duplicates
Duplicate data is treated as one
Sorted sets
Same structure as
Sets, but order can be determined through scoresUsing
Sorted setsmakes it easy to implement features like Leaderboards
Hashes
A structure well-suited for storing object types with multiple
key-valuepairs
Redis Use Cases
1. Caching
As an
In-Memory data structure store, it can serve as a cacheActs as a shield to prevent DB overload
2. Session Management
Rediscan be used as a session for user information management, etc.
3. Real-time Leaderboards
Using Redis's
sorted setdata type, you can build leaderboards that are automatically sorted by desired values
4. Queues
Queues can be easily implemented using Redis's
listtype
5. Chat
Redis is widely used in actual chat services
Since it natively supports the
PUB/SUBstandard, high-performance chat rooms, inter-server communication, etc. can be implemented
2. Managing Redis
A key characteristic of Redis is that it is Single Threaded
That is, Redis can only execute one command at a time
If a
Packetcontaining a command is larger than the MTU (Maximum Transmission Unit), thepacketmay arrive splitRedis combines the split commands and executes the command the moment it becomes a complete command
You might think Redis is slow because it is Single Threaded, but on average,
Get/Setcommands can process approximately 100,000 per secondHowever, an important note is that if a long-processing command is inserted in the middle, all subsequent commands must wait
ex) The
Keyscommand that retrieves all keys takes a very long time to process; if theKeyscommand is executed in the middle, subsequentGet/Setcommands may timeout and fail
When operating a service with Redis, the server's memory may reach its limit. The memory limit can be set with the
maxmemoryvalueWhen memory fills up to the
maxmemorylimit, Redis secures additional memory according to the Max memory policy
maxmemory-policy Settings
noeviction
allkeys-lru
volatile-lru
allkeys-random
volatile-random
volatile-ttl
allkeys-lfu
volatile-lfu
eviction
When data is deleted due to
maxmemorybeing exceeded, it is called evictionYou can check whether eviction has occurred by connecting to Redis, running the INFO command, and checking the
evicted_keysmetricThe
evicted_keysvalue is the count of evicted keys, so the larger it is, the more data has been deleted from memoryWhen using Amazon ElastiCache, you can check through the eviction graph in the
monitoring tabYou can also receive alerts about eviction occurrences
used_memory_rss
The
used_memory_rssvalue is the actual memory Redis is using, including dataThis value can be larger than the actual
used_memoryvalueThis occurs because the OS allocates memory in multiples of page size
ex) If the page size is 4096 and the requested memory size is 10, the OS allocates 4096 of memory
This is called the Fragmentation phenomenon, which is the cause of the difference between actual used memory and allocated memory
Redis Replication
Among the ways to configure Redis, there is a
Master/Slavestructure for read distribution and data redundancyThe Master node performs both reads and writes,
The Slave node can only perform reads
For this to work, the
slavemust have all the data from themasterThis is when Replication occurs
Replication refers to the process of copying data from the
masterand moving it to theslave
Master-Slave Replication Process
Master-Slave Replication ProcessConfigure
replicaof <Master IP> <Master Port>on the slave configuration side, or use theREPLICAOFcommand to request data sync from the masterThe master proceeds with the process of creating an RDB file containing the current memory state in the background
At this point, the master copies memory through fork
Then, the forked process dumps the current memory information to disk
Simultaneously with step 2, the master stores incoming WRITE commands in a buffer
When the dump is complete, the
mastersends the RDB file to the slave, and theslavesaves it to disk and then loads it into memoryThe WRITE commands accumulated in step 3 are sent to the slave
Key Facts
Replication is asynchronous
Replication is non-blocking on the
MasterReplication can block operations on
ReplicaMastercan have any number ofReplicasGenerally
Replicasare read-onlyReplicacan beMasterfor other Replicas
Need for replication
High availability
Spread the read load across replicas
Replication in lieu of Persistence
Types of Replication
1. Full Sync
Entire data is transferred to
ReplicaDump not saved on
Masterin diskless modeUsed when
Bootstrapping a new
replicaExisting replica too far behind to do partial sync
2. Partial Sync
More to be added
Last updated