LabHub

Blog

What Is a Cache Database? (Redis)

한국어English日本語

This article is a summary based on the video 10-minute Techie Talk: Cache & Redis by Jeomun and Raon.

Why Use Cache

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

  1. Data that is frequently used but rarely changes
  2. Data where loss is not a critical issue
  3. 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

Using Redis Gracefully

  1. 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.

  2. 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.

  3. 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.

  4. Purpose clarity Clearly distinguish whether it is for caching or for storage, and turn off the Persistence feature if possible.

References

Quiz

Q1: What is the main topic covered in "What Is a Cache Database? (Redis)"? What Is a Cache Database? (Redis)

Q2: 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

Q3: Explain the core concept of 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.

Q4: What are the key aspects of 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

Q5: How does Redis (Remote Dictionary) work? 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.

Comments

No comments yet.

Sign in to leave a comment