====== Redis Distributed Lock ====== ===== Lock 방식 ===== * Spin Lock : ''SET NX EX'' 로 원자적으로 값을 설정하고 Expiration 지정. 그리고 Lock 을 다른 쪽에서 소유하면 spin 을 돌면서 지정 시간마다 다시 Lock 획득 재요청. * Pub/Sub : Lock 을 다른쪽이 소유해도 재요청 안함. Lock 획득이 가능해지면 message 를 publish 하는 방식. 구현 복잡도가 높고 Redis CPU 점유율, 과잉 트래픽 발생등의 문제가 발생할 수 있음. * 일반적인 Spin Lock과 Pub/Sub Lock 은 단일 Redis 인스턴스 기반으로 인스턴스에 문제가 생길경우 lock 이 꼬일 수 있다. * 좀 더 안전한 분산 Lock을 원한다면 [[https://redis.io/topics/distlock|Distributed locks with Redis – Redis]] 에서 추천하는 **Redlock**을 사용해야 한다. * 하지만 일반적인 경우에는 Spin Lock 으로 충분하다. ===== Lettuce 기반 ===== * [[nosql:redis:lettuce|Lettuce - Redis Java Client]] 는 공식적으로 분산 Lock 기능을 제공하지 않음.직접 구현 필요. * [[https://docs.spring.io/spring-integration/reference/html/redis.html|Spring Integration Redis Support]] 에서 lettuce 기반 분산 락 구현을 볼 수 있음. * [[https://tanzu.vmware.com/developer/guides/spring-integration-lock/|Working With the Spring Distributed Lock | VMware Tanzu Developer Center]] * Spin lock 과 pub/sub 기반이 있는데 spin lock 은 락을 획득할 때까지 계속해서 락을 획들할 수 있냐고 물어보는 방식이고, pub/sub 은 락 획득 실패시 메시징 기반으로 기다렸다가 획득가능해졌다는 메시지를 받으면 획득하는 방식. * Spin Lock 의 성능이 더 좋다. ===== Redisson ===== * [[nosql:redis:redisson|Redisson]]은 거의 모든 방식의 분산 Lock 구현을 제공해준다. ===== 참조 ===== * [[https://dzone.com/articles/distributed-lock-implementation-with-redis|Distributed Lock Implementation With Redis - DZone Java]] * [[https://dzone.com/articles/spring-boot-configure-jetty-server-using-gradle-sp|How to Configure the Jetty Server in Spring Boot Using Gradle [Video] - DZone Java]] * [[https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-2-distributed-locking/|6.2 Distributed locking | Redis Labs]] * [[https://comsystoreply.de/blog-post/distributed-locks-with-redis-reinvent-the-wheel-but-with-monitoring-review|Distributed locks with Redis - reinvent the wheel but with monitoring Review]] * [[http://antirez.com/news/101|Is Redlock safe? - ]] * [[https://mono.software/2017/05/19/Redis-Distributed-Locking-for-Pragmatists/|Redis distributed locking for pragmatists | Mono Software]] * [[https://hyperconnect.github.io/2019/11/15/redis-distributed-lock-1.html|레디스와 분산 락(1/2) - 레디스를 활용한 분산 락과 안전하고 빠른 락의 구현 | Hyperconnect Tech Blog]] * [[https://charsyam.wordpress.com/2022/12/31/%ec%9e%85-%ea%b0%9c%eb%b0%9c-%eb%b6%84%ec%82%b0-%eb%9d%bd%ec%97%90-%eb%8c%80%ed%95%b4%ec%84%9c/|[입 개발] 분산 락에 대해서… | Charsyam's Blog]] * [[https://tonymoly-tech.medium.com/redis-lettuce-distributed-lock-for-data-integrity-4bc5c8623f05|Redis Lettuce를 활용한 안전한 분산 락 구현 — 실시간 동시성 문제 해결 | Medium]] * [[https://medium.com/sjk5766/redis%EA%B0%80-%EC%A0%9C%EA%B3%B5%ED%95%98%EB%8A%94-redlock%EC%9D%84-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90-2feb7278411e|Redis가 제공하는 RedLock을 알아보자]] * [[https://velog.io/@ohjinseo/Redis-%EB%B6%84%EC%82%B0%EB%9D%BD-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-Redlock|Redis 분산락 알고리즘 Redlock의 특징과 한계]] * [[https://esperer.tistory.com/65|[Redis] Distributed Lock (Redisson, Lettuce, RedLock) — 메모장 희망편]]