Hamutaro - Hamtaro 4

Database

[Redis] Redis ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ์Šคํ”„๋ง๋ถ€ํŠธ ์‚ฌ์šฉ ์˜ˆ์ œ

carsumin 2026. 2. 12. 08:40
Redis๋ž€?
  • Redis๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ Key-Value ์ €์žฅ์†Œ (์ธ๋ฉ”๋ชจ๋ฆฌ)
  • ๋น ๋ฅธ ์กฐํšŒ๊ฐ€ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ๊ณ ์„ฑ๋Šฅ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ

 

Redis ์‚ฌ์šฉ ์ด์œ 
  • ์บ์‹œ (Cache)
    • DB ๋ถ€ํ•˜ ๊ฐ์†Œ
    • ์‘๋‹ต ์†๋„ ํ–ฅ์ƒ
    • ์˜ˆ) ์ธ๊ธฐ ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ์ˆ˜, ์ธ๊ธฐ ์ƒํ’ˆ ๋ชฉ๋ก, ์‚ฌ์šฉ์ž ์„ธ์…˜ ์ •๋ณด ๋“ฑ
  • ๋ถ„์‚ฐ ๋ฝ (Distributed Lock)
    • ์—ฌ๋Ÿฌ ์„œ๋ฒ„์—์„œ ๋™์‹œ์— ๊ฐ™์€ ์ž์›์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ์ œ์–ด
    • ์˜ˆ) ์žฌ๊ณ  ์ฐจ๊ฐ, ํฌ์ธํŠธ ์ฐจ๊ฐ
  • ์„ธ์…˜ ์ €์žฅ์†Œ / ํ† ํฐ ์ €์žฅ์†Œ
    • ๋กœ๊ทธ์ธ ์„ธ์…˜, RefreshToken ์ €์žฅ

 

Redis ๊ธฐ๋ณธ ๊ตฌ์กฐ
  • Redis๋Š” ํ…Œ์ด๋ธ”์ด ์•„๋‹ˆ๋ผ Key-Value ๊ตฌ์กฐ
    • Key๋Š” ๋ฌธ์ž์—ด
    • Value๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด
    • TTL(๋งŒ๋ฃŒ ์‹œ๊ฐ„) ์„ค์ • ๊ฐ€๋Šฅ -> ์ž๋™ ์‚ญ์ œ
key → value

"USER:1"  →  {name: "ํ™๊ธธ๋™", age: 25}
"RT:100"  →  "eyJhbGciOiJIUzI1Ni..."
"POST:VIEW:10" →  1234

 

 

Spring Boot + Redis ์˜ˆ์ œ ์ฝ”๋“œ (RefreshToken)
  • ์˜์กด์„ฑ ์ถ”๊ฐ€ (build.gradle)
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

 

  • application.yml
spring:
  data:
    redis:
      host: localhost
      port: 6379

 

  • RedisConfig
    • Spring์—์„œ Redis๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์—ฐ๊ฒฐ ์„ค์ •
    • StringRedisTemplate = Redis ์ „์šฉ ํด๋ผ์ด์–ธํŠธ ๊ฐ์ฒด
      • Redis ์ง๋ ฌํ™” ๋ฐฉ์‹์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•  ๋•Œ
      • JSON ์ €์žฅ, ๊ฐ์ฒด ์ €์žฅ
      • RedisTemplate ์„ค์ • ๋ณ€๊ฒฝ
      • ์—ฌ๋Ÿฌ Redis ์„œ๋ฒ„๋ฅผ ์“ธ ๋•Œ (๊ธฐ๋ณธ Redis +  ์บ์‹œ์šฉ Redis ๋ถ„๋ฆฌ)
      • ์šด์˜ ํ™˜๊ฒฝ์—์„œ ์„ธ๋ฐ€ํ•œ ํŠœ๋‹์ด ํ•„์š”ํ•  ๋•Œ
package com.draftory.global.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class RedisConfig {

    @Bean
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory connectionFactory) {

        return new StringRedisTemplate(connectionFactory);
    }
}

 

  • RefreshTokenStore
package com.draftory.global.auth;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class RefreshTokenStore {

    private final StringRedisTemplate redisTemplate;

    public RefreshTokenStore(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * Redis Key ์ƒ์„ฑ ๊ทœ์น™
     * ์˜ˆ) RT:100 (userId = 100)
     */
    private String key(Long userId) {
        return "RT:" + userId;
    }

    /**
     * Refresh Token ์ €์žฅ
     */
    public void save(Long userId, String refreshToken, long ttlSeconds) {

        redisTemplate.opsForValue()
                .set(key(userId), refreshToken, Duration.ofSeconds(ttlSeconds));
    }

    /**
     * Refresh Token ์กฐํšŒ
     */
    public String get(Long userId) {

        return redisTemplate.opsForValue().get(key(userId));
    }

    /**
     * Refresh Token ์‚ญ์ œ (๋กœ๊ทธ์•„์›ƒ ์‹œ ์‚ฌ์šฉ)
     */
    public void delete(Long userId) {

        redisTemplate.delete(key(userId));
    }
}