Skip to content

Commit

Permalink
feat: Lambda와 SQS의 재시도 매커니즘
Browse files Browse the repository at this point in the history
  • Loading branch information
leehjhjhj committed Jul 5, 2024
1 parent 6fc8df0 commit 2d8521d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
38 changes: 37 additions & 1 deletion AWS/Lambda와 SQS의 재시도 메커니즘.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,40 @@ def lambda_handler(event, context):
```

- 해당 Lambda는 SQS로부터 메시지를 받아서 user_id가 `1`이면 예외 처리를 통해 DLQ로 전송시킨다.
- 그리고, 재시도 실험을 위해서 user_id가 `2`일 때, 예외를 터뜨린다.
- 그리고, 재시도 실험을 위해서 user_id가 `2`일 때, 예외를 터뜨린다.

### 결과

- 하지만 막상 로그를 보면 람다 함수 자체에서는 재시도 되지 않은 것을 볼 수 있다.

![alt text](image/1/image.png)

![alt text](image/1/image-1.png)

![alt text](image/1/image-2.png)

- 세 사진은 두 개의 Consumer 람다 로그와 한 개의 SQS DLQ Consumer의 로그이다. 타임라인은 다음과 같다.

### 타임라인

- 18시 16분 34초에 첫 번째 에러가 발생
- Lambda 함수가 재시도를 하지 않았고, visibility timeout(30초) 이후에 메시지가 다시 SQS로 돌아감
- 18시 17분 03초에 Lambda 함수가 다시 메시지를 받아 재실행되었고, 다시 실패
- visibility timeout 30초 이후에 메시지가 다시 SQS 큐에 들어갔고, 이때 ReceiveCount가 2가 되어 DLQ(Dead Letter Queue)로 이동

### 이유는? 람다의 비동기식 재시도
- 예상대로라면 첫번째 실패 이후 한번 재시도 설정으로 한번 더 실행 후 실패한다.
- 그리고 나서 visibility timeout 이후에 다시 SQS 큐에 들어가고, 람다가 메시지를 받는다.
- 이후 첫번재를 반복해여 총 4개의 에러 로그를 기대하였다.
- 하지만 정작 재시도는 이루어지지 않은 채로 2번의 에러로그만 발생했는데 이유는 공식 문서에서 찾을 수 있었다.

```
Lambda는 함수의 비동기 이벤트 대기열을 관리하고 오류를 다시 시도합니다.
함수가 오류를 반환하면 Lambda는 함수를 두 번 더 실행하려 합니다.
이때 첫 두 시도 간에는 1분의 대기 시간이 있으며, 두 번째와 세 번째 시도 간에는 2분의 대기 시간이 있습니다.
```
[출처 - AWS 공식 문서](https://docs.aws.amazon.com/ko_kr/lambda/latest/dg/invocation-async.html)

- 즉, visibility timeout이 기본 30초이기 때문에 람다가 1분 텀으로 재시도 되기 전에 이미 SQS로 돌아갔기 때문이다.
- 굳이 재시도를 원하지 않으면 SQS의 visibility timeout과 DQL 최대 수신 수 만으로도 SQS 만으로도 재시도를 시도해도 나쁘지 않겠다.
- 비동기식 호출 재시도는 한 람다에서 다른 람다를 event 형식으로 invoke 할 때 설정하면 되겠다.
Binary file added AWS/image/1/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/1/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/1/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions python/amazaing dictionary.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# Amazing Dictionary

```python
dict1 = {"name": "현제", "old": 27, "job": "Developer"}
dict2 = {"address": "Seoul"}
dict3 = {**dict1, **dict2}
print(dict3)# {'name': '현제', 'old': 27, 'job': 'Developer', 'address': 'Seoul'}

dict4 = {"address": "homeless"}
dict5 = {**dict1, **dict2, **dict4}
print(dict5) # {'name': '현제', 'old': 27, 'job': 'Developer', 'address': 'homeless'}

dict6 = {**dict4, "status": "hungry"}
print(dict6) # {'address': 'homeless', 'status': 'hungry'}
```

- 파이썬 딕셔너리는

## Example: 문자열의 각 문자 개수 세기

```
from collections import Counter
count = Counter("hello world")
print(count) # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
```

## 잊을 법한 문법
dict6.update({"car": None, "computer": "Mac"}) # {'address': 'homeless', 'status': 'hungry', 'car': None, 'computer': 'Mac'}
print(dict6)

0 comments on commit 2d8521d

Please sign in to comment.