- Authors

- Name
- Youngju Kim
- @fjvbn20031
This article is a summary based on the video 10-minute Techie Talk: Cache & Redis by Jeomun and Raon.
Why Use Cache
- Temporal locality: There is a high probability of accessing data that was previously accessed
- Spatial locality: There is a high probability of continuously accessing data in adjacent locations
Cache Strategy Patterns
Read Strategies
Look Aside Pattern A strategy that fetches data from the DB when the data is not in the cache. Advantage: If there is a problem with the cache, requests are delegated to the DB Disadvantage: Difficult to maintain data consistency between cache and DB. DB overload occurs on first query
Read Through Pattern A strategy that always reads data from the cache. Advantage: Guarantees data consistency between cache and DB Disadvantage: If the cache goes down, it causes application problems
Write Strategies
Write Around Write bypass. Data is written directly to the DB, not to the cache. If used in combination with a read strategy, it also writes to the cache store when a cache miss occurs. Advantage: Good performance. Resources are saved by not putting unnecessary data in the cache and writing directly. Disadvantage: Difficult to maintain consistency because there is no connection between cache and DB
Write Back Write operations are first written to the cache, then reflected in the DB later. A large number of write operations are scheduled at once. Advantage: Can reduce write frequency costs Disadvantage: Possibility of data loss in the cache
Write Through All data writes go through the Cache. Advantage: Data consistency is guaranteed. Disadvantage: Performance must be considered since two writes are always required.
Precautions When Using Cache
- Data that is frequently used but rarely changes
- Data where loss is not a critical issue
- Data consistency issues must be considered when used with a database
Redis (Remote Dictionary)
Key-value structure. Similar to a HashMap. Memory-based key-value store. Cache implementation methods include Memcached, Redis, and local memory-based approaches. Also used for temporary work queues, real-time chat, and message brokers.
Characteristics
- Can handle 100,000 TPS for Get/Set operations.
- Provides various data structures (Collections)
- Key: string
- Value: Hash, List, Set, Sorted Set
- Single Thread
- Processes only one command at a time
- Race conditions (almost) never occur
- Persistence: Data stored in memory can be persisted to disk.
- RDB (Redis Database Backup): Small in size because it is compressed and stored. Takes snapshots.
- AOF (Append Only File): Records to a log file every time an insert, update, or delete occurs. Large in size and takes longer to restore.
Using Redis Gracefully
Use appropriate data structures based on data types By leveraging the sorting feature of sorted sets, you can accomplish SQL ORDER BY (sorting) and DISTINCT (deduplication) at once.
Be cautious with O(N) commands Executing O(N) commands like KEYS/FLUSHALL, FLUSHDB, DELETE Collections, or Get All Collections may prevent single-threaded Redis from processing other commands.
Memory management Memory fragmentation (internal fragmentation, external fragmentation) occurs. New memory space allocation may be impossible even when there is sufficient available space. Therefore, RSS (Resident Set Size) monitoring is necessary.
Purpose clarity Clearly distinguish whether it is for caching or for storage, and turn off the Persistence feature if possible.