일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- python
- BIG-O NOTATION
- 빅오노테이션
- test
- 테라폼 기본 문법
- minikube mac 설치
- zsh
- Django
- terraform 문법
- 파이썬
- terraform
- iterm2 shortcuts
- Bash
- 도커컴포즈
- sftp란
- customize
- 도커
- linux
- zshrc
- Shell
- nosql
- 테라폼 문법
- docker-compose
- server
- 컨테이너
- DynamoDB
- docker
- minikube 설치 방법
- AWS
- iterm2 단축키
- Today
- Total
sklass의 s-class 프로그래밍 blog
[Django] select_for_update()를 활용한 동시성 제어 본문
django의 sql 데이터베이스를 사용하다보면 트래픽이 적은곳에서는 잘 작동하는데, 트래픽이 몰리는 경우, 데이터를 update를 하는 과정에서 동시적으로 update가 발생해서 데이터가 유실되는 상황이 속출하게 된다.
이러한 문제를 해결하려면 select_for_update()라는 django의 내장함수를 사용해서 sql에 data를 업데이트 할 때 row lock을 걸어서 동시성을 제어합니다.
select_for_update()의 파라미터로 nowait과 skip_locked라는게 있는데, 이 둘의 기본값은 False로 세팅되어 있습니다. nowait=False 인 경우, 조회하고자 하는 데이터에 row lock이 걸려있다면 해당 lock이 풀릴 때까지 대기를 합니다. True일 경우, 에러를 발생시킵니다. skip_locked=True는 조회한 데이터가 lock이 잡혀있을 경우 무시를 합니다. 이러한 속성 때문에 nowait과 skip_locked는 둘 중 1가지만 True로 사용할 수 이쌰습니다. (둘 다 True인 경우, 에러가 발생합니다.)
from django.db import transaction
def change_last_name(self) -> None:
with transaction.atomic():
person = Person.objects.select_for_update().get(
firstname="Joseph"
)
person.lastname = "On"
person.save()
source: https://docs.djangoproject.com/en/3.2/ref/models/querysets/
QuerySet API reference | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
'django' 카테고리의 다른 글
[django] drf의 request.data.get() (0) | 2021.10.22 |
---|---|
[Django] unittest @tag (0) | 2021.09.11 |
[Django] test 명령어 argument customize (0) | 2021.09.11 |
[Django] Signals (0) | 2021.09.10 |
[Django] manage.py 명령어 Customize (0) | 2021.09.10 |