├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── test │ └── java │ │ └── com │ │ └── example │ │ └── managestockconcurrency │ │ ├── ManageStockConcurrencyApplicationTests.java │ │ └── domain │ │ └── stock │ │ └── StockServiceTest.java └── main │ ├── java │ └── com │ │ └── example │ │ └── managestockconcurrency │ │ ├── domain │ │ └── stock │ │ │ ├── repository │ │ │ ├── StockRepository.java │ │ │ ├── StockOptimisticLockRepository.java │ │ │ ├── StockPessimisticLockRepository.java │ │ │ └── StockRedisRepository.java │ │ │ ├── facade │ │ │ ├── StockOptimisticLockFacade.java │ │ │ ├── StockLettuceLockFacade.java │ │ │ └── StockRedissonLockFacade.java │ │ │ ├── entiry │ │ │ └── Stock.java │ │ │ └── service │ │ │ └── StockService.java │ │ └── ManageStockConcurrencyApplication.java │ └── resources │ └── application.yml ├── .gitignore ├── README.md ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'manage-stock-concurrency' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hyune-s-lab/manage-stock-concurrency/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/test/java/com/example/managestockconcurrency/ManageStockConcurrencyApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ManageStockConcurrencyApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/repository/StockRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.repository; 2 | 3 | import com.example.managestockconcurrency.domain.stock.entiry.Stock; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface StockRepository extends JpaRepository { 7 | 8 | Stock getByProductId(Long productId); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | 3 | datasource: 4 | driver-class-name: com.mysql.cj.jdbc.Driver 5 | url: ${DATASOURCE_URL:jdbc:mysql://localhost:4306/stock_concurrency} 6 | username: ${DATASOURCE_USERNAME:root} 7 | password: ${DATASOURCE_PASSWORD:password1} 8 | 9 | jpa: 10 | hibernate: 11 | ddl-auto: create 12 | show-sql: true 13 | database-platform: org.hibernate.dialect.MySQL5InnoDBDialect 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/ManageStockConcurrencyApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ManageStockConcurrencyApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ManageStockConcurrencyApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/repository/StockOptimisticLockRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.repository; 2 | 3 | import com.example.managestockconcurrency.domain.stock.entiry.Stock; 4 | import javax.persistence.LockModeType; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Lock; 7 | 8 | public interface StockOptimisticLockRepository extends JpaRepository { 9 | 10 | @Lock(LockModeType.OPTIMISTIC) 11 | Stock getByProductId(Long productId); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/repository/StockPessimisticLockRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.repository; 2 | 3 | import com.example.managestockconcurrency.domain.stock.entiry.Stock; 4 | import javax.persistence.LockModeType; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Lock; 7 | 8 | public interface StockPessimisticLockRepository extends JpaRepository { 9 | 10 | @Lock(LockModeType.PESSIMISTIC_WRITE) 11 | Stock getByProductId(Long productId); 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/repository/StockRedisRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.repository; 2 | 3 | import java.time.Duration; 4 | import lombok.RequiredArgsConstructor; 5 | import org.springframework.data.redis.core.RedisTemplate; 6 | import org.springframework.stereotype.Component; 7 | 8 | @RequiredArgsConstructor 9 | @Component 10 | public class StockRedisRepository { 11 | 12 | private final RedisTemplate redisTemplate; 13 | 14 | public Boolean lock(final Long key) { 15 | return redisTemplate 16 | .opsForValue() 17 | .setIfAbsent(key.toString(), "lock", Duration.ofMillis(3_000)); 18 | } 19 | 20 | public Boolean unlock(final Long key) { 21 | return redisTemplate.delete(key.toString()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/facade/StockOptimisticLockFacade.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.facade; 2 | 3 | import com.example.managestockconcurrency.domain.stock.service.StockService; 4 | import lombok.RequiredArgsConstructor; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Slf4j 9 | @RequiredArgsConstructor 10 | @Service 11 | public class StockOptimisticLockFacade { 12 | 13 | private final StockService stockService; 14 | 15 | public void decreaseV4(final Long productId, final Long quantity) throws InterruptedException { 16 | while (true) { 17 | try { 18 | stockService.decreaseV4(productId, quantity); 19 | break; 20 | } catch (final Exception ex) { 21 | log.info("### optimistic lock version 충돌"); 22 | Thread.sleep(50); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/facade/StockLettuceLockFacade.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.facade; 2 | 3 | import com.example.managestockconcurrency.domain.stock.repository.StockRedisRepository; 4 | import com.example.managestockconcurrency.domain.stock.service.StockService; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.stereotype.Component; 7 | 8 | @RequiredArgsConstructor 9 | @Component 10 | public class StockLettuceLockFacade { 11 | 12 | private final StockRedisRepository stockRedisRepository; 13 | private final StockService stockService; 14 | 15 | /** 16 | * [v5] 재고 감소 - lettuce lock 17 | */ 18 | public void decreaseV5(final Long productId, final Long quantity) throws InterruptedException { 19 | while (!stockRedisRepository.lock(productId)) { 20 | Thread.sleep(100); 21 | } 22 | 23 | try { 24 | // 기본 재고 감소 로직 활용 25 | stockService.decreaseV1(productId, quantity); 26 | } finally { 27 | stockRedisRepository.unlock(productId); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/entiry/Stock.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.entiry; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.persistence.Version; 8 | import lombok.AccessLevel; 9 | import lombok.Getter; 10 | import lombok.NoArgsConstructor; 11 | 12 | @Getter 13 | @NoArgsConstructor(access = AccessLevel.PROTECTED) 14 | @Entity 15 | public class Stock { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | private Long productId; 22 | 23 | private Long quantity; 24 | 25 | @Version 26 | private Long version; 27 | 28 | public Stock(final Long productId, final Long quantity) { 29 | this.productId = productId; 30 | this.quantity = quantity; 31 | } 32 | 33 | public void decrease(final Long quantity) { 34 | if (this.quantity - quantity < 0) { 35 | throw new RuntimeException("재고 부족"); 36 | } 37 | 38 | this.quantity -= quantity; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/facade/StockRedissonLockFacade.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.facade; 2 | 3 | import com.example.managestockconcurrency.domain.stock.service.StockService; 4 | import java.util.concurrent.TimeUnit; 5 | import lombok.RequiredArgsConstructor; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.redisson.api.RLock; 8 | import org.redisson.api.RedissonClient; 9 | import org.springframework.stereotype.Component; 10 | 11 | @Slf4j 12 | @RequiredArgsConstructor 13 | @Component 14 | public class StockRedissonLockFacade { 15 | 16 | private final RedissonClient redissonClient; 17 | private final StockService stockService; 18 | 19 | /** 20 | * [v6] 재고 감소 - redisson lock 21 | */ 22 | public void decreaseV6(final Long productId, final Long quantity) throws InterruptedException { 23 | final RLock lock = redissonClient.getLock(productId.toString()); 24 | 25 | try { 26 | if (!lock.tryLock(10, 1, TimeUnit.SECONDS)) { 27 | log.info("### redisson getLock timeout"); 28 | return; 29 | } 30 | 31 | // 기본 재고 감소 로직 활용 32 | stockService.decreaseV1(productId, quantity); 33 | } finally { 34 | lock.unlock(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/example/managestockconcurrency/domain/stock/service/StockService.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock.service; 2 | 3 | import com.example.managestockconcurrency.domain.stock.entiry.Stock; 4 | import com.example.managestockconcurrency.domain.stock.repository.StockOptimisticLockRepository; 5 | import com.example.managestockconcurrency.domain.stock.repository.StockPessimisticLockRepository; 6 | import com.example.managestockconcurrency.domain.stock.repository.StockRepository; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | @RequiredArgsConstructor 12 | @Service 13 | public class StockService { 14 | 15 | private final StockRepository stockRepository; 16 | private final StockPessimisticLockRepository stockPessimisticLockRepository; 17 | private final StockOptimisticLockRepository stockOptimisticLockRepository; 18 | 19 | /** 20 | * [v1] 재고 감소 21 | */ 22 | @Transactional 23 | public void decreaseV1(final Long productId, final Long quantity) { 24 | final Stock stock = stockRepository.getByProductId(productId); 25 | stock.decrease(quantity); 26 | } 27 | 28 | /** 29 | * [v2] 재고 감소 - syncronized 30 | */ 31 | public synchronized void decreaseV2(final Long productId, final Long quantity) { 32 | final Stock stock = stockRepository.getByProductId(productId); 33 | stock.decrease(quantity); 34 | stockRepository.saveAndFlush(stock); 35 | } 36 | 37 | /** 38 | * [v3] 재고 감소 - pessimistic lock 39 | */ 40 | @Transactional 41 | public void decreaseV3(final Long productId, final Long quantity) { 42 | final Stock stock = stockPessimisticLockRepository.getByProductId(productId); 43 | stock.decrease(quantity); 44 | } 45 | 46 | /** 47 | * [v4] 재고 감소 - optimistic lock 48 | */ 49 | @Transactional 50 | public void decreaseV4(final Long productId, final Long quantity) { 51 | final Stock stock = stockOptimisticLockRepository.getByProductId(productId); 52 | stock.decrease(quantity); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [인프런] 재고시스템으로 알아보는 동시성이슈 해결방법 2 | 3 | [Inflearn Link](https://www.inflearn.com/course/%EB%8F%99%EC%8B%9C%EC%84%B1%EC%9D%B4%EC%8A%88-%EC%9E%AC%EA%B3%A0%EC%8B%9C%EC%8A%A4%ED%85%9C) 4 | 5 | ## 환경 6 | 7 | - Docker MySql - port: 4306, schema: stock_concurrency 8 | - Docker Redis - port: 6379 9 | 10 | ## 테스트 11 | 12 | ![image](https://user-images.githubusercontent.com/55722186/185759295-1841e37c-9936-4c9b-a4b0-7ee3ea986a98.png) 13 | 14 | - 테스트 케이스에서는 version 충돌이 많기에 optimistic lock 이 느립니다. 15 | 16 | ## 재고 감소 기본 17 | 18 | > StockService 19 | 20 | ### [v1] 동시성이 고려되지 않은 기초적인 로직 21 | 22 | - 메서드 레벨에 `@Transactional` 사용. 23 | 24 | ### [v2] syncronized 25 | 26 | - 메서드 레벨에 `syncronized` 사용. 27 | - 하지만 `syncronized` 는 같은 프로세스에서만 동시성을 보장하여 분산 환경에 적합하지 않습니다. 28 | 29 | ### [v3] pessimistic lock 30 | 31 | - 메서드 레벨에 `@Transactional`, DB 조회시 `@Lock(LockModeType.PESSIMISTIC_WRITE)` 사용. 32 | - 트랜잭션 시작시 Shared Lock 또는 Exclusive Lock 을 걸게 됩니다. 33 | - 충돌이 잦을 것으로 예상되거나, 동시성을 강력하게 지켜야될 때 적합합니다. ex) 계좌 잔고 34 | - 하지만 속도가 느리고 데드락의 위험이 있습니다. 35 | 36 | ### [v4] optimistic lock 37 | 38 | - 메서드 레벨에 `@Transactional`, DB 조회시 `@Lock(LockModeType.OPTIMISTIC)` 사용. 39 | - version 을 통해 애플리케이션 레벨에서 동시성을 관리합니다. 40 | - 하지만 추가 개발 구현이 필요합니다. 41 | - version 관리를 테이블 변경. 42 | - version 충돌시 재시도 로직 구현. 43 | - DB 트랜잭션을 활용하지 않기에 롤백 직접 구현. 44 | 45 | ### pessimistic lock vs optimistic lock 46 | 47 | - 충돌이 적은 경우 optimistic lock 이 빠르지만, 충돌이 많다면 pessimistic lock 이 더 빠릅니다. 48 | - 따라서 업무 성격을 분석한 후 선택해야 합니다. 49 | - named lock 은 생략합니다. 50 | 51 | ## 재고 감소 with 분산 락 (distributed lock) 52 | 53 | ### [v5] lettuce lock 54 | 55 | - spin lock 방식을 활용하여 분산 락 획득. 56 | - spin lock 은 sleep time 이 작을 수록 redis 에 더 큰 부하를 줍니다. 57 | - lucette 는 Spring Data Redis 에서 인터페이스를 제공하기에 별도의 학습이 필요하지 않습니다. 58 | 59 | ### [v6] redisson lock 60 | 61 | - pub-sub 방식을 활용하여 분산 락 획득. 62 | - redis를 직접 제어하지 않고 별도의 인터페이스를 제공 합니다. 63 | - leaseTime을 잘못 잡으면 작업 도중 lock 이 해제될 수 위험이 있습니다. - `IllegalMonitorStateException` 64 | 65 | ### lettuce lock vs redisson lock 66 | 67 | - lucette 는 Spring Data Redis 에서 인터페이스를 제공하기에 별도의 학습이 필요하지 않습니다. 68 | - 하지만 redisson 은 별도의 인터페이스이기에 학습이 필요 합니다. 69 | - 업무 성격상 lock 획득 실패 시 재시도가 필요하지 않은 경우는 luttuce 를, 재시도가 필요한 경우는 redisson 활용을 권장합니다. 70 | 71 | ## 참고 자료 72 | 73 | - https://hyperconnect.github.io/2019/11/15/redis-distributed-lock-1.html 74 | - https://devroach.tistory.com/83 75 | - redisson lock 사용시 leaseTime의 중요성 76 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if %ERRORLEVEL% equ 0 goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if %ERRORLEVEL% equ 0 goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | set EXIT_CODE=%ERRORLEVEL% 84 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 85 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 86 | exit /b %EXIT_CODE% 87 | 88 | :mainEnd 89 | if "%OS%"=="Windows_NT" endlocal 90 | 91 | :omega 92 | -------------------------------------------------------------------------------- /src/test/java/com/example/managestockconcurrency/domain/stock/StockServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.example.managestockconcurrency.domain.stock; 2 | 3 | import static org.assertj.core.api.Assertions.*; 4 | 5 | import com.example.managestockconcurrency.domain.stock.entiry.Stock; 6 | import com.example.managestockconcurrency.domain.stock.facade.StockLettuceLockFacade; 7 | import com.example.managestockconcurrency.domain.stock.facade.StockOptimisticLockFacade; 8 | import com.example.managestockconcurrency.domain.stock.facade.StockRedissonLockFacade; 9 | import com.example.managestockconcurrency.domain.stock.repository.StockRepository; 10 | import com.example.managestockconcurrency.domain.stock.service.StockService; 11 | import java.util.concurrent.CountDownLatch; 12 | import java.util.concurrent.ExecutorService; 13 | import java.util.concurrent.Executors; 14 | import java.util.stream.IntStream; 15 | import org.junit.jupiter.api.AfterEach; 16 | import org.junit.jupiter.api.BeforeEach; 17 | import org.junit.jupiter.api.DisplayName; 18 | import org.junit.jupiter.api.Test; 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.boot.test.context.SpringBootTest; 21 | 22 | @SpringBootTest 23 | class StockServiceTest { 24 | 25 | @Autowired private StockService stockService; 26 | @Autowired private StockOptimisticLockFacade stockOptimisticLockFacade; 27 | @Autowired private StockLettuceLockFacade stockLettuceLockFacade; 28 | @Autowired private StockRedissonLockFacade stockRedissonLockFacade; 29 | 30 | @Autowired private StockRepository stockRepository; 31 | 32 | private final int threadCount = 300; 33 | private final long productId = 10L; 34 | private final long quantity = 1L; 35 | private final long initQuantity = 300L; 36 | 37 | private ExecutorService executorService; 38 | private CountDownLatch countDownLatch; 39 | 40 | @BeforeEach 41 | public void beforeEach() { 42 | stockRepository.save(new Stock(productId, initQuantity)); 43 | 44 | executorService = Executors.newFixedThreadPool(threadCount); 45 | countDownLatch = new CountDownLatch(threadCount); 46 | } 47 | 48 | @AfterEach 49 | public void afterEach() { 50 | stockRepository.deleteAll(); 51 | } 52 | 53 | @DisplayName("[v1] 재고 감소") 54 | @Test 55 | void stock_decreaseV1() { 56 | // given 57 | 58 | // when 59 | stockService.decreaseV1(productId, quantity); 60 | 61 | // then 62 | final long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 63 | assertThat(afterQuantity).isEqualTo(initQuantity - 1); 64 | } 65 | 66 | @DisplayName("[v1] 재고 감소 - 동시에 300개 요청") 67 | @Test 68 | void stock_decreaseV1_concurrency() throws InterruptedException { 69 | // given 70 | 71 | // when 72 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 73 | try { 74 | stockService.decreaseV1(productId, quantity); 75 | } finally { 76 | countDownLatch.countDown(); 77 | } 78 | } 79 | )); 80 | 81 | countDownLatch.await(); 82 | 83 | // then 84 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 85 | System.out.println("### afterQuantity=" + afterQuantity); 86 | assertThat(afterQuantity).isNotZero(); 87 | } 88 | 89 | @DisplayName("[v2] 재고 감소 - syncronized") 90 | @Test 91 | void stock_decreaseV2() throws InterruptedException { 92 | // given 93 | 94 | // when 95 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 96 | try { 97 | stockService.decreaseV2(productId, quantity); 98 | } finally { 99 | countDownLatch.countDown(); 100 | } 101 | } 102 | )); 103 | 104 | countDownLatch.await(); 105 | 106 | // then 107 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 108 | System.out.println("### afterQuantity=" + afterQuantity); 109 | assertThat(afterQuantity).isZero(); 110 | } 111 | 112 | @DisplayName("[v3] 재고 감소 - pessimistic lock") 113 | @Test 114 | void stock_decreaseV3() throws InterruptedException { 115 | // given 116 | 117 | // when 118 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 119 | try { 120 | stockService.decreaseV3(productId, quantity); 121 | } finally { 122 | countDownLatch.countDown(); 123 | } 124 | } 125 | )); 126 | 127 | countDownLatch.await(); 128 | 129 | // then 130 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 131 | System.out.println("### afterQuantity=" + afterQuantity); 132 | assertThat(afterQuantity).isZero(); 133 | } 134 | 135 | @DisplayName("[v4] 재고 감소 - optimistic lock") 136 | @Test 137 | void stock_decreaseV4() throws InterruptedException { 138 | // given 139 | 140 | // when 141 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 142 | try { 143 | stockOptimisticLockFacade.decreaseV4(productId, quantity); 144 | } catch (final InterruptedException ex) { 145 | throw new RuntimeException(ex); 146 | } finally { 147 | countDownLatch.countDown(); 148 | } 149 | } 150 | )); 151 | 152 | countDownLatch.await(); 153 | 154 | // then 155 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 156 | System.out.println("### afterQuantity=" + afterQuantity); 157 | assertThat(afterQuantity).isZero(); 158 | } 159 | 160 | @DisplayName("[v5] 재고 감소 - lettuce lock") 161 | @Test 162 | void stock_decreaseV5() throws InterruptedException { 163 | // given 164 | 165 | // when 166 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 167 | try { 168 | stockLettuceLockFacade.decreaseV5(productId, quantity); 169 | } catch (final InterruptedException ex) { 170 | throw new RuntimeException(ex); 171 | } finally { 172 | countDownLatch.countDown(); 173 | } 174 | } 175 | )); 176 | 177 | countDownLatch.await(); 178 | 179 | // then 180 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 181 | System.out.println("### afterQuantity=" + afterQuantity); 182 | assertThat(afterQuantity).isZero(); 183 | } 184 | 185 | @DisplayName("[v6] 재고 감소 - redisson lock") 186 | @Test 187 | void stock_decreaseV6() throws InterruptedException { 188 | // given 189 | 190 | // when 191 | IntStream.range(0, threadCount).forEach(e -> executorService.submit(() -> { 192 | try { 193 | stockRedissonLockFacade.decreaseV6(productId, quantity); 194 | } catch (final InterruptedException ex) { 195 | throw new RuntimeException(ex); 196 | } finally { 197 | countDownLatch.countDown(); 198 | } 199 | } 200 | )); 201 | 202 | countDownLatch.await(); 203 | 204 | // then 205 | final Long afterQuantity = stockRepository.getByProductId(productId).getQuantity(); 206 | System.out.println("### afterQuantity=" + afterQuantity); 207 | assertThat(afterQuantity).isZero(); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Stop when "xargs" is not available. 209 | if ! command -v xargs >/dev/null 2>&1 210 | then 211 | die "xargs is not available" 212 | fi 213 | 214 | # Use "xargs" to parse quoted args. 215 | # 216 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 217 | # 218 | # In Bash we could simply go: 219 | # 220 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 221 | # set -- "${ARGS[@]}" "$@" 222 | # 223 | # but POSIX shell has neither arrays nor command substitution, so instead we 224 | # post-process each arg (as a line of input to sed) to backslash-escape any 225 | # character that might be a shell metacharacter, then use eval to reverse 226 | # that process (while maintaining the separation between arguments), and wrap 227 | # the whole thing up as a single "set" statement. 228 | # 229 | # This will of course break if any of these variables contains a newline or 230 | # an unmatched quote. 231 | # 232 | 233 | eval "set -- $( 234 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 235 | xargs -n1 | 236 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 237 | tr '\n' ' ' 238 | )" '"$@"' 239 | 240 | exec "$JAVACMD" "$@" 241 | --------------------------------------------------------------------------------