TIL

2021.09.21 기록장

Gisungcu 2021. 9. 21. 18:27

ToDo

  • 알고리즘 문제 풀기
  • 책 읽기
  •  

Done

  • 책 읽기

Weekly goal

  • 책 읽기

 

 

 

toy-project

 

이전에 고민했던 동시성에 대한 해법을 생각해봤다.

첫 번째 문제.

 

하나의 음식점에 유저는 하나의 리뷰만 쓸 수 있다.

그렇기에 분기문 처리로 검사를 했었다. 근데 만약 2개의 request가 동시에 들어와서 동시에 select 하면 둘 다 처음 리뷰라고 인식할 것이다.

이 부분은 어쩔 수 없다고 판단했다. mysql에서는 없는 raw에 대해서는 lock을 걸지 못하기 때문에 테이블 전체에 lock을 걸던가 해야 한다. 하지만 테이블 lock은 dead lock을 더 많이 만들어 낼 테고 이는 해법이 아닌 것 같았다.

그래서 생각해 낸 것은 다중 유니크 인덱스를 통해 insert시에 제약조건에 걸리면 rollback 되는 것이다.

 

https://stackoverflow.com/questions/17068686/how-do-i-lock-on-an-innodb-row-that-doesnt-exist-yet

 

How do I lock on an InnoDB row that doesn't exist yet?

How can I guarantee that I can search if a username exists in my database, then insert that username into the database as a new row without any intercept between the SELECT and INSERT statements? ...

stackoverflow.com

https://stackoverflow.com/questions/28037539/how-to-check-if-the-record-exists-before-insert-to-prevent-duplicates

 

how to check if the record exists before insert to prevent duplicates?

I want to select CustomerID, OrderTypeID, and LoanNumber from tblOrder and insert it with a new OrderTypeID but same CustomerID and LoanNumber. My query does that but it duplicates the values, ever...

stackoverflow.com

 

두 번째 문제.

음식점의 첫 리뷰는 포인트를 더 준다는 설정이다.

근데 동시에 request가 접근해서 자신들이 첫 리뷰인 줄 안다면? 이는 문제가 된다.

그렇기에 쓰기 lock을 걸었다. 

음식점을 가지고 올 때 쓰기 lock을 걸어서 select for update 문이 나가게 하는 것이다.

그럼 다른 트랜젝션에서는 음식점의 해당 raw에는 접근이 대기되게 되고 순차적으로 처리가 될 것이다.