Redis는 완전한 데이터 정합성을 보장하기 위해 아래와 같이 뮤텍스와 세마포어를 사용한다.

두 프로세스가 거의 동시에 세마포어를 얻으려고 하는 경우를 대비해 락을 걸어놓은 것 같다.

def acquire_semaphore_with_lock(conn, semname, limit, timeout=10):
   identifier = acquire_lock(conn, semname, acquire_timeout=.01)
   if identifier:
      try:
         return acquire_fair_semaphore(conn, semname, limit, timeout)
   finally:
      release_lock(conn, semname, identifier)

물론 데이터 정합성 측면의 trade-off를 감수하고 더 simple한 세마포어를 사용하는 옵션도 지원한다고 한다.

(다만 System clock을 사용해야 하며, 스레드가 세마포어 limit을 초과하여 data에 접근하는 경우가 발생할 수 있다.)

 

 

[참고 사이트]

https://redis.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-3-counting-semaphores/6-3-4-preventing-race-conditions/

'~2022 > Database' 카테고리의 다른 글

[DB] ANSI SQL이란?  (0) 2021.08.22

+ Recent posts