Programming/Java, Kotlin

[Spring] redis ๋ถ„์‚ฐ ๋ฝ ์ „๋žต์„ ํ†ตํ•œ ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ(์„ ์ฐฉ์ˆœ, ๋”ํ‹ฐ ๋ฆฌ๋“œ, ํŒฌํ…€ ๋ฆฌ๋“œ)

Space_Jin 2025. 2. 23. 17:29
728x90
๋ฐ˜์‘ํ˜•

Spring๊ณผ Redis๋ฅผ ํ™œ์šฉํ•œ ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ ๋ฐ ๋ฐ์ดํ„ฐ ์ผ๊ด€์„ฑ

ํ˜„๋Œ€์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ๊ฐ€ ์ค‘์š”ํ•œ ์š”์†Œ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์€ ํฐ ๋„์ „ ๊ณผ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 

 

์‹ค์ œ๋กœ ๋ฌด์ž‘์œ„ ํฌ์ธํŠธ(๋žœ๋ค ํฌ์ธํŠธ) ์ง€๊ธ‰ ์ด๋ฒคํŠธ๋ฅผ ๊ฐœ๋ฐœํ–ˆ๋˜ ๊ฒฝํ—˜์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ๊ธ€์„ ์ž‘์„ฑํ•ด ๋ด…๋‹ˆ๋‹ค.


1. ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ์˜ ํ•„์š”์„ฑ

๋™์‹œ์„ฑ์ด๋ž€ ์—ฌ๋Ÿฌ ํŠธ๋žœ์žญ์…˜์ด ๋™์‹œ์— ์‹คํ–‰๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ์œผ๋กœ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ์˜ ์ฝ๊ธฐ ๋ฐ ์“ฐ๊ธฐ ์ž‘์—…์˜ ์ถฉ๋Œ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์ด ์ €ํ•ด๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์— ๋ถ€์ •์ ์ธ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. Spring์—์„œ์˜ ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€

Spring์—์„œ๋Š” ํŠธ๋žœ์žญ์…˜์˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ ์„ค์ •ํ•˜์—ฌ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉฐ, ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค:

โ— READ UNCOMMITTED: ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์œผ๋กœ ๋”ํ‹ฐ ๋ฆฌ๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ— READ COMMITTED: ์ปค๋ฐ‹๋œ ๋ฐ์ดํ„ฐ๋งŒ ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋”ํ‹ฐ ๋ฆฌ๋“œ๋Š” ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŒฌํ…€ ๋ฆฌ๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ— REPEATABLE READ: ๊ฐ™์€ ํŠธ๋žœ์žญ์…˜ ๋‚ด์—์„œ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ์„ ๊ฒฝ์šฐ, ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. ํŒฌํ…€ ๋ฆฌ๋“œ๋Š” ๋ฐฉ์ง€ํ•˜์ง€๋งŒ ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โ— SERIALIZABLE: ๊ฐ€์žฅ ๋†’์€ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์œผ๋กœ, ํŠธ๋žœ์žญ์…˜์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


3. Redis์˜ ๋ถ„์‚ฐ ๋ฝ

Redis๋Š” ์ธ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋น ๋ฅธ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ถ„์‚ฐ ๋ฝ์„ ํ†ตํ•ด ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. Redis์˜ ๋ถ„์‚ฐ ๋ฝ์„ ํ™œ์šฉํ•˜๋ฉด ํŠน์ • ํŠธ๋žœ์žญ์…˜์ด ์ง„ํ–‰ ์ค‘์ผ ๋•Œ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

๋น ๋ฅธ ์„ฑ๋Šฅ: Redis๋Š” ์ธ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋ฏ€๋กœ ๋†’์€ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
๋™์‹œ์„ฑ ๊ด€๋ฆฌ: ๋ถ„์‚ฐ ๋ฝ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์˜ ๋™์‹œ ์ˆ˜์ • ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


์‹ค์ œ ์‚ฌ์šฉํ–ˆ๋˜ ๋žœ๋ค ํฌ์ธํŠธ ์ง€๊ธ‰ ์ด๋ฒคํŠธ ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ ์˜ˆ์‹œ ์ฝ”๋“œ

Redis 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.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
public class RedisConfig {
    
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }
}

 

Redis ๋ถ„์‚ฐ ๋ฝ ์„œ๋น„์Šค
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class DistributedLockService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean acquireLock(String lockKey, long timeout) {
        String lockValue = UUID.randomUUID().toString();
        Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, timeout, TimeUnit.MILLISECONDS);
        
        if (success != null && success) {
            return true; // ๋ฝ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์–ป์Œ
        }
        return false; // ๋ฝ์„ ์–ป์ง€ ๋ชปํ•จ
    }

    public void releaseLock(String lockKey, String lockValue) {
        // ํ˜„์žฌ ๋ฝ์˜ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๋™์ผํ•  ๊ฒฝ์šฐ์—๋งŒ ๋ฝ ํ•ด์ œ
        if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
            redisTemplate.delete(lockKey);
        }
    }
}

 

ํฌ์ธํŠธ ๊ด€๋ฆฌ ์„œ๋น„์Šค
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class PointConfigService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private PointConfigRepository pointConfigRepository; // ๋ฉ”์ธ DB์™€ ์—ฐ๊ฒฐ๋œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ

    // ์ดˆ๊ธฐ๊ฐ’ ๋กœ๋”ฉ
    public void loadInitialPointConfig() {
        int initialPoints = pointConfigRepository.getDailyPointLimit();
        redisTemplate.opsForValue().set("daily_point_limit", initialPoints);
    }

    // ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ํฌ์ธํŠธ ์กฐ์ •
    public void updateDailyPointLimit(int newLimit) {
        // ๋ฉ”์ธ DB ์—…๋ฐ์ดํŠธ
        pointConfigRepository.updateDailyPointLimit(newLimit);
        // Redis ์—…๋ฐ์ดํŠธ
        redisTemplate.opsForValue().set("daily_point_limit", newLimit);
    }

    // ํฌ์ธํŠธ ์กฐํšŒ
    public int getDailyPointLimit() {
        Integer limit = (Integer) redisTemplate.opsForValue().get("daily_point_limit");
        if (limit == null) {
            // Redis์— ๊ฐ’์ด ์—†์œผ๋ฉด ๋ฉ”์ธ DB์—์„œ ์กฐํšŒ
            limit = pointConfigRepository.getDailyPointLimit();
            // Redis์— ์บ์‹œ
            redisTemplate.opsForValue().set("daily_point_limit", limit);
        }
        return limit;
    }
}

 

ํฌ์ธํŠธ ์ง€๊ธ‰ ์„œ๋น„์Šค
@Service
public class PointService {

    @Autowired
    private DistributedLockService distributedLockService;

    @Autowired
    private PointRepository pointRepository; // ํฌ์ธํŠธ ์ •๋ณด๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๋ ˆํฌ์ง€ํ† ๋ฆฌ

    public int getCurrentPoints(String userId) {
        // Redis์—์„œ ํฌ์ธํŠธ ์กฐํšŒ
        Integer points = redisTemplate.opsForValue().get(userId);
        if (points == null) {
            // Redis์— ๊ฐ’์ด ์—†์œผ๋ฉด DB์—์„œ ์กฐํšŒ
            points = pointRepository.getCurrentPoints(userId);
            // Redis์— ์บ์‹œ
            redisTemplate.opsForValue().set(userId, points);
        }
        return points;
    }

    public void allocatePoints(String userId, int pointsToAllocate) {
        String lockKey = "point_lock";
        long timeout = 30000; // 30์ดˆ

        if (distributedLockService.acquireLock(lockKey, timeout)) {
            try {
                // ๋ฉ”์ธ DB์—์„œ ํฌ์ธํŠธ ์—…๋ฐ์ดํŠธ
                pointRepository.allocatePoints(userId, pointsToAllocate);
                // Redis์—์„œ๋„ ๊ฐ’ ์—…๋ฐ์ดํŠธ
                int currentPoints = redisTemplate.opsForValue().get(userId);
                redisTemplate.opsForValue().set(userId, currentPoints + pointsToAllocate);
            } finally {
                distributedLockService.releaseLock(lockKey, UUID.randomUUID().toString());
            }
        } else {
            // ๋ฝ์„ ์–ป์ง€ ๋ชปํ–ˆ์„ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
        }
    }
}

 

๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์ผ์ผ ์ง€๊ธ‰ ๊ฐ€๋Šฅํ•œ ํฌ์ธํŠธ ์กฐ์ •
public void adjustPoints(int dailyPointLimit) {
    PointConfigService pointConfigService = new PointConfigService();
    
    // ์ดˆ๊ธฐ๊ฐ’ ๋กœ๋”ฉ
    pointConfigService.loadInitialPointConfig();
    
    // ํฌ์ธํŠธ ์กฐ์ •
    pointConfigService.updateDailyPointLimit(dailyPointLimit); // ์ƒˆ๋กœ์šด ํฌ์ธํŠธ ํ•œ๋„ ์„ค์ •
}

์‹ค์ œ ์š”๊ตฌ์‚ฌํ•ญ์—์„œ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€๋ฅผ ํ†ตํ†ต ์ผ์ผ ์ง€๊ธ‰ ๊ฐ€๋Šฅํ•œ ํฌ์ธํŠธ ์กฐ์ •์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ•„์š”ํ–ˆ์–ด์„œ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

 

4. ๋”ํ‹ฐ ๋ฆฌ๋“œ์™€ ํŒฌํ…€ ๋ฆฌ๋“œ

- ๋”ํ‹ฐ ๋ฆฌ๋“œ(Dirty Read)
๋”ํ‹ฐ ๋ฆฌ๋“œ๋Š” ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ํ˜„์ƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ ํŠธ๋žœ์žญ์…˜์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ • ์ค‘์ผ ๋•Œ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ฉด, ์ˆ˜์ •์ด ์™„๋ฃŒ๋˜๊ธฐ ์ „์— ์ž˜๋ชป๋œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์ €ํ•ดํ•ฉ๋‹ˆ๋‹ค.

- ํŒฌํ…€ ๋ฆฌ๋“œ(Phantom Read)
ํŒฌํ…€ ๋ฆฌ๋“œ๋Š” ํ•œ ํŠธ๋žœ์žญ์…˜์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ์กฐํšŒํ•  ๋•Œ, ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜์—ฌ ์ด์ „์— ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ์…‹๊ณผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ํ˜„์ƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ์˜ค๋ฅ˜๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•