Spring

[Spring] Redis as a cache memory

montmer27 2026. 2. 26. 14:02

Redis Study Notes


Chapter 1 - Redis & RedisTemplate

  • Redis is a fast, memory-based database.
  • To use Redis in a Java-based Spring Boot project, something is needed to bridge the connection between Java and Redis.
    • RedisTemplate is what fulfills this role.
    • RedisTemplate can be thought of as a translator between Java and Redis.
    • It enables communication by mutually converting Java commands into Redis commands, and vice versa.

Chapter 2 - Redis Data Storage & Serialization

Storage Format

  • Redis stores all data in key-value format.
  • All data types are stored as strings.

Serialization (Java → Redis)

  • Storing a Java object in Redis requires parsing it into string format.
  • This process is called serialization, and it parses the object into JSON format.
  • Different serialization tools are applied to keys and values.
    • Key : Uses StringRedisSerializer
    • Value : Varies depending on whether the type is string or hash.

Deserialization (Redis → Java)

  • Restoring JSON string data from Redis back into a Java object requires a deserialization tool.
  • By registering Jackson2JsonRedisSerializer or GenericJackson2JsonRedisSerializer as the value serializer during configuration, RedisTemplate will automatically deserialize and return the data as a Java object.
  • Type casting is performed after that.

Chapter 3 - Key Naming Conventions

  • Since all data in Redis is key-based, it is vulnerable to overwriting.
  • To prevent unintended value overwrites caused by duplicate key names, naming conventions exist for identification purposes.
    • Since these are conventions, the specific rules may vary by organization.

Basic Format : {domain}:{resource}:{identifier}

  • The colon (:) is a common agreement in Redis for expressing hierarchical structure.
  • Keys are typically managed as static final constants.

Chapter 4 - Cache Characteristics & the Need for TTL

Characteristics of Cache Memory

  • Cache memory refers to memory with small capacity and volatility, but extremely fast data access.
  • The reason cache is fast is because data is stored in RAM. Small capacity and volatility are resulting characteristics of RAM, not the cause of its speed.
  • Redis is primarily used to serve as this kind of cache memory.

Why Cache Needs to Be Cleared Periodically

  • To guarantee fast cache lookup performance, it is necessary to periodically clear the cache so that too much data does not accumulate.
  • TTL (Time To Live) is a Redis feature that sets an expiration time on each key, and is the primary mechanism for automating this cache-clearing process.

The Need for Cache Synchronization

  • Cache serves as a supplement to the database.
  • Since cached data is premised on being a copy of the database's values, any discrepancy between a change in the database and the value in cache must be reflected immediately — this is referred to as cache synchronization.

Trade-offs in Cache Synchronization Frequency

  • If cache synchronization occurs too frequently, cache performance degrades.
    • If the cache's stored values are not trusted and the database is queried regardless, there is no point in using cache at all.
  • On the other hand, the appropriate frequency of cache synchronization varies depending on the nature of the data and users' information retrieval patterns.
  • Since there is no single correct answer, it is necessary to tune the cache synchronization frequency according to the needs of the business and the characteristics of the data.