Ver3.0 ๐ Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ก ๋ง๋๋ ๋๋ํ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ

๐ Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ก ๋ง๋๋ ๋๋ํ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ
์ผํ๋ชฐ ๊ฐ๋ฐ์๋ผ๋ฉด ๊ผญ ์์์ผ ํ ํ๋์ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์ํคํ ์ฒ์ ๋ชจ๋ ๊ฒ
๐ฏ ์ ์ง๊ธ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ์ธ๊ฐ?
์๋ ! ์ค๋์ ์ผํ๋ชฐ ๊ฐ๋ฐ์์ ์ ๋ง ์ค์ํ ์ฃผ์ ๋ฅผ ๋ค๋ค๋ณผ ๊ฑฐ์ผ. ๋ฐ๋ก Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ฅผ ํ์ฉํ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ ์ด์ผ. ๐
ํน์ ์ผํ๋ชฐ์ ์ด์ํ๊ฑฐ๋ ๊ฐ๋ฐํด๋ณธ ์ ์์ด? ๊ทธ๋ผ ์ด๋ฐ ์ํฉ ๊ฒช์ด๋ดค์ ๊ฑฐ์ผ. ๋ธ๋ํ๋ผ์ด๋ฐ์ด๋ ํน๋ณ ์ธ์ผ ๊ธฐ๊ฐ์ ์ฃผ๋ฌธ์ด ํญ์ฃผํ๋ฉด์ ์์คํ ์ด ๋๋ ค์ง๊ฑฐ๋, ์ฌ์ง์ด ๋ค์ด๋๋ ๊ฒฝํ ๋ง์ด์ผ. ์๋๋ฉด ์ฃผ๋ฌธ์ ์ฑ๊ณตํ๋๋ฐ ์ฌ๊ณ ๋ ์ฐจ๊ฐ๋์ง ์์๊ฑฐ๋, ๊ฒฐ์ ๋ ๋๋๋ฐ ์ฃผ๋ฌธ ์ ๋ณด๊ฐ ์ฌ๋ผ์ง๋ ํฉ๋นํ ์ํฉ๋ค...
์ด๋ฐ ๋ฌธ์ ๋ค์ ๋๋ถ๋ถ ์ ํต์ ์ธ ๋๊ธฐ์ ์ฒ๋ฆฌ ๋ฐฉ์์ ํ๊ณ์์ ๋น๋กฏ๋ผ. ๊ทธ๋์ ์์ฆ ๋ง์ ๊ธฐ์ ๋ค์ด ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ก ์ ํํ๊ณ ์์ด. ํนํ Apache Kafka๋ ์ด๋ฐ ์ํคํ ์ฒ์ ํต์ฌ ๋๊ตฌ๋ก ์๋ฆฌ์ก์์ง.
์ ํต์ ๋ฐฉ์ vs ์ด๋ฒคํธ ๊ธฐ๋ฐ ๋ฐฉ์
๋จผ์ ๋ ๋ฐฉ์์ ์ฐจ์ด๋ฅผ ์ดํดํด๋ณด์. ์ ํต์ ์ธ ๋ฐฉ์์์๋ ์ฃผ๋ฌธ์ด ๋ค์ด์ค๋ฉด ์ด๋ ๊ฒ ์ฒ๋ฆฌ๋ผ:
2๏ธโฃ ์ฌ๊ณ ํ์ธ ๋ฐ ์ฐจ๊ฐ (๋๊ธฐ...)
3๏ธโฃ ๊ฒฐ์ ์ฒ๋ฆฌ (๋๊ธฐ...)
4๏ธโฃ ๋ฐฐ์ก ์ ๋ณด ์์ฑ (๋๊ธฐ...)
5๏ธโฃ ์๋ฆผ ๋ฐ์ก (๋๊ธฐ...)
6๏ธโฃ ์๋ฃ ์๋ต
๋ณด์ด์ง? ๊ฐ ๋จ๊ณ๊ฐ ์์ฐจ์ ์ผ๋ก ์งํ๋๊ณ , ์ ๋จ๊ณ๊ฐ ๋๋์ผ ๋ค์ ๋จ๊ณ๋ก ๋์ด๊ฐ. ๋ง์ฝ ๊ฒฐ์ ์์คํ ์ด ๋๋ฆฌ๋ฉด? ์ฌ์ฉ์๋ ํ์ฐธ์ ๊ธฐ๋ค๋ ค์ผ ํด. ๋ ํฐ ๋ฌธ์ ๋ ์ค๊ฐ์ ํ๋๋ผ๋ ์คํจํ๋ฉด ์ ์ฒด ํธ๋์ญ์ ์ ๋กค๋ฐฑํด์ผ ํ๋ค๋ ๊ฑฐ์ผ.
๋ฐ๋ฉด ์ด๋ฒคํธ ๊ธฐ๋ฐ ๋ฐฉ์์ ์ด๋ ๊ฒ ๋์ํด:
2๏ธโฃ ์ฆ์ ์ฌ์ฉ์์๊ฒ ์๋ต โ
3๏ธโฃ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๊ฐ ์๋น์ค๊ฐ ๋ ๋ฆฝ์ ์ผ๋ก ์ฒ๋ฆฌ
- ์ฌ๊ณ ์๋น์ค: ์ด๋ฒคํธ ์์ โ ์ฌ๊ณ ์ฐจ๊ฐ
- ๊ฒฐ์ ์๋น์ค: ์ด๋ฒคํธ ์์ โ ๊ฒฐ์ ์ฒ๋ฆฌ
- ๋ฐฐ์ก ์๋น์ค: ์ด๋ฒคํธ ์์ โ ๋ฐฐ์ก ์ค๋น
- ์๋ฆผ ์๋น์ค: ์ด๋ฒคํธ ์์ โ ์๋ฆผ ๋ฐ์ก
์ฐจ์ด๊ฐ ๋๊ปด์ ธ? ์ฌ์ฉ์๋ ๋ช ์ด ์์ "์ฃผ๋ฌธ์ด ์ ์๋์์ต๋๋ค"๋ผ๋ ์๋ต์ ๋ฐ๊ณ , ๋๋จธ์ง ์ฒ๋ฆฌ๋ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์งํ๋ผ. ๊ฐ ์๋น์ค๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋๊น ํ๋๊ฐ ๋๋ ค๋ ๋ค๋ฅธ ์๋น์ค์ ์ํฅ์ ์ฃผ์ง ์์. ๐
๐ฅ Apache Kafka, ๋ ๋๊ตฌ๋?
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก Kafka์ ๋ํด ์์๋ณด์. Kafka๋ LinkedIn์์ ๊ฐ๋ฐํ ๋ถ์ฐ ์คํธ๋ฆฌ๋ฐ ํ๋ซํผ์ด์ผ. 2011๋ ์ ์คํ์์ค๋ก ๊ณต๊ฐ๋๊ณ , ์ง๊ธ์ Apache ์ฌ๋จ์์ ๊ด๋ฆฌํ๊ณ ์์ด.
Kafka์ ํต์ฌ ๊ฐ๋ ๋ค
Kafka๋ฅผ ์ดํดํ๋ ค๋ฉด ๋ช ๊ฐ์ง ํต์ฌ ๊ฐ๋ ์ ์์์ผ ํด. ํ๋์ฉ ์น๊ตฌ์๊ฒ ์ค๋ช ํ๋ฏ์ด ํ์ด๋ณผ๊ฒ! ๐
์ค์ ์์:
-
order.created : ์ฃผ๋ฌธ ์์ฑ ์ด๋ฒคํธ-
payment.completed : ๊ฒฐ์ ์๋ฃ ์ด๋ฒคํธ-
inventory.updated : ์ฌ๊ณ ์
๋ฐ์ดํธ ์ด๋ฒคํธ
Kafka์ ๊ฐ๋ ฅํ ํน์ง๋ค
๊ทธ๋ผ Kafka๊ฐ ์ ์ด๋ ๊ฒ ์ธ๊ธฐ๊ฐ ๋ง์๊น? ๋ช ๊ฐ์ง ํต์ฌ ํน์ง์ ์ดํด๋ณด์:
๐๏ธ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ ์ํคํ ์ฒ ์ค๊ณ
์ด์ ์ค์ ๋ก Kafka๋ฅผ ํ์ฉํ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ ์ ์ค๊ณํด๋ณด์. ํ์ค์ ์ด๊ณ ์ค์ฉ์ ์ธ ์ํคํ ์ฒ๋ฅผ ๋จ๊ณ๋ณ๋ก ๊ตฌ์ถํด๋ณผ ๊ฑฐ์ผ! ๐จ
์ ์ฒด ์์คํ ๊ตฌ์กฐ
์ฐ๋ฆฌ๊ฐ ๋ง๋ค ์์คํ ์ ์ด๋ฐ ๊ตฌ์กฐ์ผ:
2. Order Service - ์ฃผ๋ฌธ์ ์์ฑํ๊ณ ์ด๋ฒคํธ ๋ฐํ
3. Kafka Cluster - ์ด๋ฒคํธ ๋ธ๋ก์ปค
4. Inventory Service - ์ฌ๊ณ ๊ด๋ฆฌ
5. Payment Service - ๊ฒฐ์ ์ฒ๋ฆฌ
6. Shipping Service - ๋ฐฐ์ก ๊ด๋ฆฌ
7. Notification Service - ์๋ฆผ ๋ฐ์ก
8. Database - ๊ฐ ์๋น์ค๋ณ ๋ฐ์ดํฐ ์ ์ฅ์
์ด๋ฒคํธ ํ๋ก์ฐ ์์ธ ์ค๊ณ
์ฃผ๋ฌธ์ด ์์ฑ๋๊ณ ์๋ฃ๋๊ธฐ๊น์ง์ ์ ์ฒด ํ๋ฆ์ ์ดํด๋ณด์. ๊ฐ ๋จ๊ณ๋ง๋ค ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ณ , ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋๋์ง ์์ธํ ์์๋ณผ๊ฒ!
1๏ธโฃ API Gateway๊ฐ ์์ฒญ์ ๋ฐ์ Order Service๋ก ์ ๋ฌ
2๏ธโฃ Order Service๋ ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ DB์ ์ ์ฅ (์ํ: PENDING)
3๏ธโฃ
order.created ์ด๋ฒคํธ๋ฅผ Kafka์ ๋ฐํ4๏ธโฃ ์ฌ์ฉ์์๊ฒ "์ฃผ๋ฌธ์ด ์ ์๋์์ต๋๋ค" ์๋ต (์ฌ๊ธฐ๊น์ง 1์ด ์ด๋ด!)
์ด๋ฒคํธ ๋ฉ์์ง๋ ์ด๋ ๊ฒ ์๊ฒผ์ด:
{
"eventId": "evt_20240115_123456",
"eventType": "order.created",
"timestamp": "2024-01-15T10:30:00Z",
"orderId": "ORD-2024-001234",
"userId": "USR-98765",
"items": [
{
"productId": "PROD-111",
"productName": "๋ฌด์ ์ด์ดํฐ",
"quantity": 2,
"price": 89000
},
{
"productId": "PROD-222",
"productName": "์ค๋งํธ์์น",
"quantity": 1,
"price": 350000
}
],
"totalAmount": 528000,
"shippingAddress": {
"recipient": "ํ๊ธธ๋",
"phone": "010-1234-5678",
"address": "์์ธ์ ๊ฐ๋จ๊ตฌ ํ
ํค๋๋ก 123",
"zipCode": "06234"
}
}
order.created ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ฉด:1๏ธโฃ ์ฃผ๋ฌธ ์ํ๋ค์ ์ฌ๊ณ ๋ฅผ ํ์ธ
2๏ธโฃ ์ฌ๊ณ ๊ฐ ์ถฉ๋ถํ๋ฉด ์ฐจ๊ฐํ๊ณ ์์ฝ ์ฒ๋ฆฌ
3๏ธโฃ
inventory.reserved ์ด๋ฒคํธ ๋ฐํ4๏ธโฃ ์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ฉด
inventory.insufficient ์ด๋ฒคํธ ๋ฐํ
inventory.insufficient ์ด๋ฒคํธ๋ฅผ ๋ฐ์์:- ์ฃผ๋ฌธ ์ํ๋ฅผ CANCELLED๋ก ๋ณ๊ฒฝ
- ์ฌ์ฉ์์๊ฒ ์ฌ๊ณ ๋ถ์กฑ ์๋ฆผ ๋ฐ์ก
- ํ์์ ํ๋ถ ์ฒ๋ฆฌ ์์
inventory.reserved ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ฉด:1๏ธโฃ ๊ฒฐ์ ๊ฒ์ดํธ์จ์ด(PG์ฌ)์ ๊ฒฐ์ ์์ฒญ
2๏ธโฃ ๊ฒฐ์ ์ฑ๊ณต ์
payment.completed ์ด๋ฒคํธ ๋ฐํ3๏ธโฃ ๊ฒฐ์ ์คํจ ์
payment.failed ์ด๋ฒคํธ ๋ฐํ4๏ธโฃ ๊ฒฐ์ ์ ๋ณด๋ฅผ DB์ ์ ์ฅ
- ์ฌ์๋ ๋ก์ง ๊ตฌํ (์ต๋ 3ํ)
- ํ์์์ ์
payment.timeout ์ด๋ฒคํธ ๋ฐํ- ์๋ ํ์ธ์ด ํ์ํ ์ฃผ๋ฌธ์ผ๋ก ๋ถ๋ฅ
payment.completed ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ฉด:1๏ธโฃ ์ฃผ๋ฌธ ์ํ๋ฅผ CONFIRMED๋ก ๋ณ๊ฒฝ
2๏ธโฃ
order.confirmed ์ด๋ฒคํธ ๋ฐํ3๏ธโฃ ์ด ์ด๋ฒคํธ๋ฅผ ์ฌ๋ฌ ์๋น์ค๊ฐ ๊ตฌ๋
order.confirmed ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ฉด:1๏ธโฃ ๋ฐฐ์ก ์ ๋ณด ์์ฑ (์ก์ฅ๋ฒํธ ๋ฐ๊ธ)
2๏ธโฃ ๋ฌผ๋ฅ ์์คํ ์ ์ถ๊ณ ์์ฒญ
3๏ธโฃ
shipping.prepared ์ด๋ฒคํธ ๋ฐํ4๏ธโฃ ์ค์ ์ถ๊ณ ์
shipping.dispatched ์ด๋ฒคํธ ๋ฐํ
-
order.created โ "์ฃผ๋ฌธ์ด ์ ์๋์์ต๋๋ค"-
payment.completed โ "๊ฒฐ์ ๊ฐ ์๋ฃ๋์์ต๋๋ค"-
shipping.dispatched โ "์ํ์ด ๋ฐ์ก๋์์ต๋๋ค"-
shipping.delivered โ "๋ฐฐ์ก์ด ์๋ฃ๋์์ต๋๋ค"
ํ ํฝ ์ค๊ณ ์ ๋ต
ํ ํฝ์ ์ด๋ป๊ฒ ์ค๊ณํ๋๋์ ๋ฐ๋ผ ์์คํ ์ ์ ์ฐ์ฑ๊ณผ ํ์ฅ์ฑ์ด ๋ฌ๋ผ์ ธ. ์ค๋ฌด์์ ๋ง์ด ์ฐ์ด๋ ํจํด๋ค์ ์๊ฐํ ๊ฒ!
order.createdorder.confirmedorder.cancelledpayment.completedpayment.failedinventory.reservedshipping.dispatched์ฅ์ : ๋ช ํํ ์ฑ ์ ๋ถ๋ฆฌ, ์ดํดํ๊ธฐ ์ฌ์
๋จ์ : ํ ํฝ ๊ฐ์๊ฐ ๋ง์์ง ์ ์์
domain-events - ๋น์ฆ๋์ค ์ด๋ฒคํธsystem-events - ์์คํ
์ด๋ฒคํธnotification-events - ์๋ฆผ ์ด๋ฒคํธ์ฅ์ : ํ ํฝ ๊ด๋ฆฌ๊ฐ ๊ฐ๋จ
๋จ์ : ์ปจ์๋จธ๊ฐ ํ์ ์๋ ๋ฉ์์ง๋ ๋ฐ๊ฒ ๋จ
ํํฐ์ ์ ๋ต
ํํฐ์ ์ ์ด๋ป๊ฒ ๋๋๋๋๋ ์ค์ํด. ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ ์์๋ ๋ณดํต ์ด๋ฐ ์ ๋ต์ ์จ:
๊ฐ์ ์ฌ์ฉ์์ ์ฃผ๋ฌธ์ ๊ฐ์ ํํฐ์ ์ผ๋ก ๊ฐ๊ฒ ํด. ์์ ๋ณด์ฅ์ด ํ์ํ ๋ ์ ์ฉํด.
partitionKey = userId2. ์ฃผ๋ฌธ ID ๊ธฐ๋ฐ
์ฃผ๋ฌธ๋ณ๋ก ๊ท ๋ฑํ๊ฒ ๋ถ์ฐ์์ผ. ๋ถํ ๋ถ์ฐ์ ํจ๊ณผ์ ์ด์ผ.
partitionKey = orderId3. ์ํ ์นดํ ๊ณ ๋ฆฌ ๊ธฐ๋ฐ
ํน์ ์นดํ ๊ณ ๋ฆฌ์ ์ฃผ๋ฌธ์ ๊ฐ์ ํํฐ์ ์ผ๋ก ๋ณด๋ด. ์นดํ ๊ณ ๋ฆฌ๋ณ ๋ถ์์ด ํ์ํ ๋ ์ข์.
partitionKey = categoryId
ํด๊ฒฐ ๋ฐฉ๋ฒ:
- ํํฐ์ ํค๋ฅผ ์ ์ ํํด์ ๊ท ๋ฑ ๋ถ์ฐ
- ํํฐ์ ๊ฐ์๋ฅผ ๋๋ ค์ ๋ถ์ฐ๋ ํฅ์
- ๋ชจ๋ํฐ๋ง์ผ๋ก ๋ถ๊ท ํ ๊ฐ์ง
๐ป ์ค์ ์ฝ๋ ๊ตฌํ
์ด๋ก ์ ์ถฉ๋ถํ ๋ฐฐ์ ์ผ๋ ์ด์ ์ค์ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์! Spring Boot์ Kafka๋ฅผ ์ฌ์ฉํ ๊ตฌํ ์์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ์ดํด๋ณผ๊ฒ. ๐
ํ๋ก์ ํธ ์ค์
๋จผ์ ํ์ํ ์์กด์ฑ์ ์ถ๊ฐํด์ผ ํด. pom.xml์ ์ด๋ ๊ฒ ์ถ๊ฐํด์ค:
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- PostgreSQL Driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
๊ทธ๋ฆฌ๊ณ application.yml ์ค์ :
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
acks: all
retries: 3
consumer:
group-id: order-service-group
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
auto-offset-reset: earliest
properties:
spring.json.trusted.packages: "*"
datasource:
url: jdbc:postgresql://localhost:5432/orderdb
username: postgres
password: password
jpa:
hibernate:
ddl-auto: update
show-sql: true
๋๋ฉ์ธ ๋ชจ๋ธ ์ ์
์ฃผ๋ฌธ ์ํฐํฐ๋ถํฐ ๋ง๋ค์ด๋ณด์:
@Entity
@Table(name = "orders")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String orderId;
@Column(nullable = false)
private String userId;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<OrderItem> items = new ArrayList<>();
@Column(nullable = false)
private BigDecimal totalAmount;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private OrderStatus status;
@Embedded
private ShippingAddress shippingAddress;
@Column(nullable = false)
private LocalDateTime createdAt;
private LocalDateTime confirmedAt;
public enum OrderStatus {
PENDING, // ์ฃผ๋ฌธ ์ ์
INVENTORY_RESERVED, // ์ฌ๊ณ ์์ฝ๋จ
PAYMENT_COMPLETED, // ๊ฒฐ์ ์๋ฃ
CONFIRMED, // ์ฃผ๋ฌธ ํ์
SHIPPING, // ๋ฐฐ์ก ์ค
DELIVERED, // ๋ฐฐ์ก ์๋ฃ
CANCELLED // ์ทจ์๋จ
}
}
์ฃผ๋ฌธ ์์ดํ ์ํฐํฐ:
@Entity
@Table(name = "order_items")
@Getter
@Setter
@NoArgsConstructor
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private Order order;
@Column(nullable = false)
private String productId;
@Column(nullable = false)
private String productName;
@Column(nullable = false)
private Integer quantity;
@Column(nullable = false)
private BigDecimal price;
public BigDecimal getSubtotal() {
return price.multiply(BigDecimal.valueOf(quantity));
}
}
์ด๋ฒคํธ ๋ชจ๋ธ ์ ์
Kafka๋ก ์ฃผ๊ณ ๋ฐ์ ์ด๋ฒคํธ ํด๋์ค๋ค์ ๋ง๋ค์ด๋ณด์:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrderCreatedEvent {
private String eventId;
private String eventType;
private LocalDateTime timestamp;
private String orderId;
private String userId;
private List<OrderItemDto> items;
private BigDecimal totalAmount;
private ShippingAddressDto shippingAddress;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class OrderItemDto {
private String productId;
private String productName;
private Integer quantity;
private BigDecimal price;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class ShippingAddressDto {
private String recipient;
private String phone;
private String address;
private String zipCode;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class InventoryReservedEvent {
private String eventId;
private String eventType;
private LocalDateTime timestamp;
private String orderId;
private List<ReservedItem> reservedItems;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class ReservedItem {
private String productId;
private Integer quantity;
private String reservationId;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PaymentCompletedEvent {
private String eventId;
private String eventType;
private LocalDateTime timestamp;
private String orderId;
private String paymentId;
private BigDecimal amount;
private String paymentMethod;
}
Order Service ๊ตฌํ
์ด์ ํต์ฌ์ธ Order Service๋ฅผ ๊ตฌํํด๋ณด์. ๋จผ์ Kafka Producer ์ค์ :
@Configuration
public class KafkaProducerConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public ProducerFactory<String, Object> producerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
config.put(ProducerConfig.ACKS_CONFIG, "all");
config.put(ProducerConfig.RETRIES_CONFIG, 3);
config.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
return new DefaultKafkaProducerFactory<>(config);
}
@Bean
public KafkaTemplate<String, Object> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
์ฃผ๋ฌธ ์์ฑ ์๋น์ค:
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderService {
private final OrderRepository orderRepository;
private final KafkaTemplate<String, Object> kafkaTemplate;
private static final String ORDER_CREATED_TOPIC = "order.created";
@Transactional
public OrderResponse createOrder(CreateOrderRequest request) {
// 1. ์ฃผ๋ฌธ ์์ฑ
Order order = buildOrder(request);
order.setStatus(Order.OrderStatus.PENDING);
order.setCreatedAt(LocalDateTime.now());
// 2. DB ์ ์ฅ
Order savedOrder = orderRepository.save(order);
log.info("Order created: {}", savedOrder.getOrderId());
// 3. ์ด๋ฒคํธ ๋ฐํ
OrderCreatedEvent event = buildOrderCreatedEvent(savedOrder);
publishEvent(ORDER_CREATED_TOPIC, savedOrder.getOrderId(), event);
// 4. ์๋ต ๋ฐํ
return OrderResponse.from(savedOrder);
}
private Order buildOrder(CreateOrderRequest request) {
Order order = new Order();
order.setOrderId(generateOrderId());
order.setUserId(request.getUserId());
order.setShippingAddress(request.getShippingAddress());
List<OrderItem> items = request.getItems().stream()
.map(itemDto -> {
OrderItem item = new OrderItem();
item.setOrder(order);
item.setProductId(itemDto.getProductId());
item.setProductName(itemDto.getProductName());
item.setQuantity(itemDto.getQuantity());
item.setPrice(itemDto.getPrice());
return item;
})
.collect(Collectors.toList());
order.setItems(items);
order.setTotalAmount(calculateTotalAmount(items));
return order;
}
private OrderCreatedEvent buildOrderCreatedEvent(Order order) {
return OrderCreatedEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("order.created")
.timestamp(LocalDateTime.now())
.orderId(order.getOrderId())
.userId(order.getUserId())
.items(order.getItems().stream()
.map(item -> new OrderCreatedEvent.OrderItemDto(
item.getProductId(),
item.getProductName(),
item.getQuantity(),
item.getPrice()
))
.collect(Collectors.toList()))
.totalAmount(order.getTotalAmount())
.shippingAddress(new OrderCreatedEvent.ShippingAddressDto(
order.getShippingAddress().getRecipient(),
order.getShippingAddress().getPhone(),
order.getShippingAddress().getAddress(),
order.getShippingAddress().getZipCode()
))
.build();
}
private void publishEvent(String topic, String key, Object event) {
try {
kafkaTemplate.send(topic, key, event)
.addCallback(
result -> log.info("Event published successfully: topic={}, key={}", topic, key),
ex -> log.error("Failed to publish event: topic={}, key={}", topic, key, ex)
);
} catch (Exception e) {
log.error("Error publishing event", e);
throw new RuntimeException("Failed to publish event", e);
}
}
private String generateOrderId() {
return "ORD-" + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)
+ "-" + String.format("%06d", new Random().nextInt(999999));
}
private BigDecimal calculateTotalAmount(List<OrderItem> items) {
return items.stream()
.map(OrderItem::getSubtotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
}
ENABLE_IDEMPOTENCE_CONFIG๋ฅผ true๋ก ์ค์ ํ์ด. ์ด๋ ๊ฒ ํ๋ฉด ๋คํธ์ํฌ ๋ฌธ์ ๋ก ์ฌ์๋๊ฐ ๋ฐ์ํด๋ ์ค๋ณต ๋ฉ์์ง๊ฐ ๋ฐํ๋์ง ์์. ์ฃผ๋ฌธ ์ฒ๋ฆฌ์์ ์ ๋ง ์ค์ํ ์ค์ ์ด์ผ!
Inventory Service ๊ตฌํ
์ฌ๊ณ ์๋น์ค๋ ์ฃผ๋ฌธ ์์ฑ ์ด๋ฒคํธ๋ฅผ ๊ตฌ๋ ํด์ ์ฌ๊ณ ๋ฅผ ์ฒ๋ฆฌํด:
@Service
@RequiredArgsConstructor
@Slf4j
public class InventoryService {
private final InventoryRepository inventoryRepository;
private final KafkaTemplate<String, Object> kafkaTemplate;
private static final String INVENTORY_RESERVED_TOPIC = "inventory.reserved";
private static final String INVENTORY_INSUFFICIENT_TOPIC = "inventory.insufficient";
@KafkaListener(
topics = "order.created",
groupId = "inventory-service-group",
containerFactory = "kafkaListenerContainerFactory"
)
public void handleOrderCreated(OrderCreatedEvent event) {
log.info("Received order.created event: orderId={}", event.getOrderId());
try {
// 1. ์ฌ๊ณ ํ์ธ
boolean allAvailable = checkInventoryAvailability(event.getItems());
if (allAvailable) {
// 2. ์ฌ๊ณ ์์ฝ
List<InventoryReservedEvent.ReservedItem> reservedItems =
reserveInventory(event.getItems());
// 3. ์ฌ๊ณ ์์ฝ ์๋ฃ ์ด๋ฒคํธ ๋ฐํ
InventoryReservedEvent reservedEvent = InventoryReservedEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("inventory.reserved")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reservedItems(reservedItems)
.build();
kafkaTemplate.send(INVENTORY_RESERVED_TOPIC, event.getOrderId(), reservedEvent);
log.info("Inventory reserved for order: {}", event.getOrderId());
} else {
// 4. ์ฌ๊ณ ๋ถ์กฑ ์ด๋ฒคํธ ๋ฐํ
InventoryInsufficientEvent insufficientEvent = InventoryInsufficientEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("inventory.insufficient")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reason("์ฌ๊ณ ๊ฐ ๋ถ์กฑํฉ๋๋ค")
.build();
kafkaTemplate.send(INVENTORY_INSUFFICIENT_TOPIC, event.getOrderId(), insufficientEvent);
log.warn("Insufficient inventory for order: {}", event.getOrderId());
}
} catch (Exception e) {
log.error("Error processing order.created event", e);
// ์๋ฌ ์ฒ๋ฆฌ ๋ก์ง (์ฌ์๋, DLQ ์ ์ก ๋ฑ)
}
}
@Transactional
private boolean checkInventoryAvailability(List<OrderCreatedEvent.OrderItemDto> items) {
for (OrderCreatedEvent.OrderItemDto item : items) {
Inventory inventory = inventoryRepository.findByProductId(item.getProductId())
.orElseThrow(() -> new ProductNotFoundException(item.getProductId()));
if (inventory.getAvailableQuantity() < item.getQuantity()) {
return false;
}
}
return true;
}
@Transactional
private List<InventoryReservedEvent.ReservedItem> reserveInventory(
List<OrderCreatedEvent.OrderItemDto> items) {
List<InventoryReservedEvent.ReservedItem> reservedItems = new ArrayList<>();
for (OrderCreatedEvent.OrderItemDto item : items) {
Inventory inventory = inventoryRepository.findByProductId(item.getProductId())
.orElseThrow(() -> new ProductNotFoundException(item.getProductId()));
// ์ฌ๊ณ ์ฐจ๊ฐ
inventory.reserve(item.getQuantity());
inventoryRepository.save(inventory);
// ์์ฝ ์ ๋ณด ์์ฑ
String reservationId = UUID.randomUUID().toString();
reservedItems.add(new InventoryReservedEvent.ReservedItem(
item.getProductId(),
item.getQuantity(),
reservationId
));
}
return reservedItems;
}
}
Payment Service ๊ตฌํ
๊ฒฐ์ ์๋น์ค๋ ์ฌ๊ณ ์์ฝ ์๋ฃ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ ๊ฒฐ์ ๋ฅผ ์ฒ๋ฆฌํด:
@Service
@RequiredArgsConstructor
@Slf4j
public class PaymentService {
private final PaymentRepository paymentRepository;
private final PaymentGateway paymentGateway; // PG์ฌ ์ฐ๋ ์ธํฐํ์ด์ค
private final KafkaTemplate<String, Object> kafkaTemplate;
private static final String PAYMENT_COMPLETED_TOPIC = "payment.completed";
private static final String PAYMENT_FAILED_TOPIC = "payment.failed";
@KafkaListener(
topics = "inventory.reserved",
groupId = "payment-service-group"
)
public void handleInventoryReserved(InventoryReservedEvent event) {
log.info("Received inventory.reserved event: orderId={}", event.getOrderId());
try {
// 1. ๊ฒฐ์ ์ ๋ณด ์กฐํ (์ฃผ๋ฌธ ์ ๋ณด์์ ๊ฐ์ ธ์ด)
PaymentInfo paymentInfo = getPaymentInfo(event.getOrderId());
// 2. PG์ฌ ๊ฒฐ์ ์์ฒญ
PaymentResult result = paymentGateway.processPayment(paymentInfo);
if (result.isSuccess()) {
// 3. ๊ฒฐ์ ์ฑ๊ณต - DB ์ ์ฅ
Payment payment = Payment.builder()
.paymentId(result.getPaymentId())
.orderId(event.getOrderId())
.amount(paymentInfo.getAmount())
.paymentMethod(paymentInfo.getMethod())
.status(PaymentStatus.COMPLETED)
.completedAt(LocalDateTime.now())
.build();
paymentRepository.save(payment);
// 4. ๊ฒฐ์ ์๋ฃ ์ด๋ฒคํธ ๋ฐํ
PaymentCompletedEvent completedEvent = PaymentCompletedEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("payment.completed")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.paymentId(result.getPaymentId())
.amount(paymentInfo.getAmount())
.paymentMethod(paymentInfo.getMethod())
.build();
kafkaTemplate.send(PAYMENT_COMPLETED_TOPIC, event.getOrderId(), completedEvent);
log.info("Payment completed for order: {}", event.getOrderId());
} else {
// 5. ๊ฒฐ์ ์คํจ ์ด๋ฒคํธ ๋ฐํ
PaymentFailedEvent failedEvent = PaymentFailedEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("payment.failed")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reason(result.getFailureReason())
.build();
kafkaTemplate.send(PAYMENT_FAILED_TOPIC, event.getOrderId(), failedEvent);
log.warn("Payment failed for order: {}", event.getOrderId());
}
} catch (Exception e) {
log.error("Error processing payment", e);
handlePaymentError(event.getOrderId(), e);
}
}
@Retryable(
value = {PaymentGatewayException.class},
maxAttempts = 3,
backoff = @Backoff(delay = 2000)
)
private PaymentResult processPaymentWithRetry(PaymentInfo paymentInfo) {
return paymentGateway.processPayment(paymentInfo);
}
private void handlePaymentError(String orderId, Exception e) {
// ์๋ฌ ๋ก๊น
๋ฐ ๋ชจ๋ํฐ๋ง ์๋ฆผ
// DLQ(Dead Letter Queue)๋ก ์ ์ก
log.error("Critical payment error for order: {}", orderId, e);
}
}
@Retryable ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํด์ ์ต๋ 3๋ฒ๊น์ง ์ฌ์๋ํ๋๋ก ํ์ด. ์ฌ์๋ ๊ฐ๊ฒฉ์ 2์ด๋ก ์ค์ ํ๊ณ , ์ด๋ ์ค๋ฌด์์ ๋ง์ด ์ฐ๋ ํจํด์ด์ผ.
Notification Service ๊ตฌํ
์๋ฆผ ์๋น์ค๋ ์ฌ๋ฌ ์ด๋ฒคํธ๋ฅผ ๊ตฌ๋ ํด์ ์ ์ ํ ์๋ฆผ์ ๋ฐ์กํด:
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final EmailService emailService;
private final SmsService smsService;
private final PushNotificationService pushService;
@KafkaListener(topics = "order.created", groupId = "notification-service-group")
public void handleOrderCreated(OrderCreatedEvent event) {
log.info("Sending order created notification: orderId={}", event.getOrderId());
String message = String.format(
"์ฃผ๋ฌธ์ด ์ ์๋์์ต๋๋ค.\n์ฃผ๋ฌธ๋ฒํธ: %s\n์ด ๊ธ์ก: %s์",
event.getOrderId(),
event.getTotalAmount()
);
sendNotification(event.getUserId(), "์ฃผ๋ฌธ ์ ์ ์๋ฃ", message);
}
@KafkaListener(topics = "payment.completed", groupId = "notification-service-group")
public void handlePaymentCompleted(PaymentCompletedEvent event) {
log.info("Sending payment completed notification: orderId={}", event.getOrderId());
String message = String.format(
"๊ฒฐ์ ๊ฐ ์๋ฃ๋์์ต๋๋ค.\n์ฃผ๋ฌธ๋ฒํธ: %s\n๊ฒฐ์ ๊ธ์ก: %s์\n๊ฒฐ์ ์๋จ: %s",
event.getOrderId(),
event.getAmount(),
event.getPaymentMethod()
);
sendNotification(getUserIdByOrderId(event.getOrderId()), "๊ฒฐ์ ์๋ฃ", message);
}
@KafkaListener(topics = "shipping.dispatched", groupId = "notification-service-group")
public void handleShippingDispatched(ShippingDispatchedEvent event) {
log.info("Sending shipping notification: orderId={}", event.getOrderId());
String message = String.format(
"์ํ์ด ๋ฐ์ก๋์์ต๋๋ค.\n์ฃผ๋ฌธ๋ฒํธ: %s\n์ก์ฅ๋ฒํธ: %s\n๋ฐฐ์ก์
์ฒด: %s",
event.getOrderId(),
event.getTrackingNumber(),
event.getCarrier()
);
sendNotification(getUserIdByOrderId(event.getOrderId()), "๋ฐฐ์ก ์์", message);
}
private void sendNotification(String userId, String title, String message) {
try {
// ์ด๋ฉ์ผ ๋ฐ์ก
emailService.sendEmail(userId, title, message);
// SMS ๋ฐ์ก (์ค์ ์๋ฆผ๋ง)
if (isImportantNotification(title)) {
smsService.sendSms(userId, message);
}
// ํธ์ ์๋ฆผ
pushService.sendPush(userId, title, message);
} catch (Exception e) {
log.error("Failed to send notification", e);
// ์๋ฆผ ์คํจ๋ ์์คํ
์ ์ฒด๋ฅผ ๋ฉ์ถ์ง ์์
}
}
private boolean isImportantNotification(String title) {
return title.contains("๊ฒฐ์ ") || title.contains("๋ฐฐ์ก");
}
private String getUserIdByOrderId(String orderId) {
// ์ฃผ๋ฌธ ์ ๋ณด์์ ์ฌ์ฉ์ ID ์กฐํ
return "user-id"; // ์ค์ ๋ก๋ DB ์กฐํ
}
}
๐ก๏ธ ์๋ฌ ์ฒ๋ฆฌ์ ๋ณด์ ํธ๋์ญ์
์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ์์ ๊ฐ์ฅ ๊น๋ค๋ก์ด ๋ถ๋ถ์ด ๋ฐ๋ก ์๋ฌ ์ฒ๋ฆฌ์ผ. ๋ถ์ฐ ์์คํ ์์๋ ์ธ์ ๋ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์๊ฑฐ๋ . ํ๋์ฉ ์ดํด๋ณด์! ๐
Saga ํจํด์ผ๋ก ๋ถ์ฐ ํธ๋์ญ์ ๊ด๋ฆฌ
์ ํต์ ์ธ ACID ํธ๋์ญ์ ์ ๋ถ์ฐ ํ๊ฒฝ์์ ์ฌ์ฉํ๊ธฐ ์ด๋ ค์. ๋์ Saga ํจํด์ ์ฌ์ฉํด์ ์ฌ๋ฌ ์๋น์ค์ ๊ฑธ์น ํธ๋์ญ์ ์ ๊ด๋ฆฌํด.
์ฅ์ :
- ์ค์ ์กฐ์ ์๊ฐ ์์ด์ ๋จ์ํจ
- ์๋น์ค ๊ฐ ๊ฒฐํฉ๋๊ฐ ๋ฎ์
- ํ์ฅ์ฑ์ด ์ข์
๋จ์ :
- ์ ์ฒด ํ๋ก์ฐ๋ฅผ ํ์ ํ๊ธฐ ์ด๋ ค์
- ์ํ ์์กด์ฑ ๋ฐ์ ๊ฐ๋ฅ
- ๋๋ฒ๊น ์ด ๋ณต์กํจ
์ฅ์ :
- ์ ์ฒด ํ๋ก์ฐ๊ฐ ๋ช ํํจ
- ์๋ฌ ์ฒ๋ฆฌ๊ฐ ์ฌ์
- ์ํ ์์กด์ฑ ์์
๋จ์ :
- ์ค์ ์กฐ์ ์๊ฐ ๋จ์ผ ์ฅ์ ์ ์ด ๋ ์ ์์
- ์๋น์ค ๊ฐ ๊ฒฐํฉ๋๊ฐ ๋์์ง
๋ณด์ ํธ๋์ญ์ (Compensating Transaction)
๊ฒฐ์ ๊ฐ ์คํจํ๋ฉด ์์ฝํ ์ฌ๊ณ ๋ฅผ ๋ค์ ํ์ด์ค์ผ ํด. ์ด๋ฐ ๊ฑธ ๋ณด์ ํธ๋์ญ์ ์ด๋ผ๊ณ ํด. ์ฝ๋๋ก ๊ตฌํํด๋ณด์:
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderSagaService {
private final KafkaTemplate<String, Object> kafkaTemplate;
@KafkaListener(topics = "payment.failed", groupId = "order-saga-group")
public void handlePaymentFailed(PaymentFailedEvent event) {
log.warn("Payment failed, starting compensation: orderId={}", event.getOrderId());
// 1. ์ฌ๊ณ ์์ฝ ์ทจ์ ์ด๋ฒคํธ ๋ฐํ
InventoryCancelReservationEvent cancelEvent = InventoryCancelReservationEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("inventory.cancel-reservation")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reason("๊ฒฐ์ ์คํจ๋ก ์ธํ ์ฌ๊ณ ์์ฝ ์ทจ์")
.build();
kafkaTemplate.send("inventory.cancel-reservation", event.getOrderId(), cancelEvent);
// 2. ์ฃผ๋ฌธ ์ทจ์ ์ด๋ฒคํธ ๋ฐํ
OrderCancelledEvent orderCancelEvent = OrderCancelledEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("order.cancelled")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reason("๊ฒฐ์ ์คํจ")
.build();
kafkaTemplate.send("order.cancelled", event.getOrderId(), orderCancelEvent);
log.info("Compensation completed for order: {}", event.getOrderId());
}
@KafkaListener(topics = "inventory.insufficient", groupId = "order-saga-group")
public void handleInventoryInsufficient(InventoryInsufficientEvent event) {
log.warn("Inventory insufficient, cancelling order: orderId={}", event.getOrderId());
// ์ฃผ๋ฌธ ์ทจ์ ์ฒ๋ฆฌ
OrderCancelledEvent cancelEvent = OrderCancelledEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("order.cancelled")
.timestamp(LocalDateTime.now())
.orderId(event.getOrderId())
.reason("์ฌ๊ณ ๋ถ์กฑ")
.build();
kafkaTemplate.send("order.cancelled", event.getOrderId(), cancelEvent);
}
}
์ฌ๊ณ ์๋น์ค์์ ์์ฝ ์ทจ์๋ฅผ ์ฒ๋ฆฌํ๋ ์ฝ๋:
@Service
@RequiredArgsConstructor
@Slf4j
public class InventoryCompensationService {
private final InventoryRepository inventoryRepository;
private final ReservationRepository reservationRepository;
@KafkaListener(
topics = "inventory.cancel-reservation",
groupId = "inventory-compensation-group"
)
@Transactional
public void handleCancelReservation(InventoryCancelReservationEvent event) {
log.info("Cancelling inventory reservation: orderId={}", event.getOrderId());
try {
// 1. ์์ฝ ์ ๋ณด ์กฐํ
List<InventoryReservation> reservations =
reservationRepository.findByOrderId(event.getOrderId());
// 2. ๊ฐ ์์ฝ์ ๋ํด ์ฌ๊ณ ๋ณต๊ตฌ
for (InventoryReservation reservation : reservations) {
Inventory inventory = inventoryRepository
.findByProductId(reservation.getProductId())
.orElseThrow(() -> new ProductNotFoundException(reservation.getProductId()));
// ์ฌ๊ณ ๋ณต๊ตฌ
inventory.releaseReservation(reservation.getQuantity());
inventoryRepository.save(inventory);
// ์์ฝ ์ํ ๋ณ๊ฒฝ
reservation.setStatus(ReservationStatus.CANCELLED);
reservationRepository.save(reservation);
log.info("Released inventory: productId={}, quantity={}",
reservation.getProductId(), reservation.getQuantity());
}
log.info("Inventory reservation cancelled successfully: orderId={}", event.getOrderId());
} catch (Exception e) {
log.error("Failed to cancel inventory reservation", e);
// ๋ณด์ ํธ๋์ญ์
์คํจ ์ ์๋ ์ฒ๋ฆฌ๋ฅผ ์ํ ์๋ฆผ
sendCompensationFailureAlert(event.getOrderId(), e);
}
}
private void sendCompensationFailureAlert(String orderId, Exception e) {
// ๊ด๋ฆฌ์์๊ฒ ์๋ฆผ ๋ฐ์ก
log.error("CRITICAL: Manual intervention required for order: {}", orderId, e);
}
}
1. ์ฌ์๋ ๋ฉ์ปค๋์ฆ ๊ตฌํ
2. Dead Letter Queue(DLQ)๋ก ์คํจํ ๋ฉ์์ง ๋ณด๊ด
3. ๋ชจ๋ํฐ๋ง ์๋ฆผ ์ค์
4. ์๋ ์ฒ๋ฆฌ๋ฅผ ์ํ ๊ด๋ฆฌ์ ๋์๋ณด๋ ๊ตฌ์ถ
๋ฉฑ๋ฑ์ฑ ๋ณด์ฅ (Idempotency)
๊ฐ์ ์ด๋ฒคํธ๊ฐ ์ฌ๋ฌ ๋ฒ ์ฒ๋ฆฌ๋์ด๋ ๊ฒฐ๊ณผ๊ฐ ๊ฐ์์ผ ํด. ์ด๊ฑธ ๋ฉฑ๋ฑ์ฑ์ด๋ผ๊ณ ํด. ๊ตฌํ ๋ฐฉ๋ฒ์ ์์๋ณด์:
@Service
@RequiredArgsConstructor
@Slf4j
public class IdempotentInventoryService {
private final InventoryRepository inventoryRepository;
private final EventLogRepository eventLogRepository;
@KafkaListener(topics = "order.created", groupId = "inventory-service-group")
@Transactional
public void handleOrderCreated(OrderCreatedEvent event) {
String eventId = event.getEventId();
// 1. ์ด๋ฏธ ์ฒ๋ฆฌ๋ ์ด๋ฒคํธ์ธ์ง ํ์ธ
if (eventLogRepository.existsByEventId(eventId)) {
log.info("Event already processed, skipping: eventId={}", eventId);
return;
}
try {
// 2. ์ฌ๊ณ ์ฒ๋ฆฌ ๋ก์ง
processInventory(event);
// 3. ์ด๋ฒคํธ ์ฒ๋ฆฌ ๊ธฐ๋ก ์ ์ฅ
EventLog eventLog = EventLog.builder()
.eventId(eventId)
.eventType(event.getEventType())
.orderId(event.getOrderId())
.processedAt(LocalDateTime.now())
.status(ProcessStatus.SUCCESS)
.build();
eventLogRepository.save(eventLog);
log.info("Event processed successfully: eventId={}", eventId);
} catch (Exception e) {
log.error("Error processing event: eventId={}", eventId, e);
// ์คํจ ๊ธฐ๋ก ์ ์ฅ
EventLog eventLog = EventLog.builder()
.eventId(eventId)
.eventType(event.getEventType())
.orderId(event.getOrderId())
.processedAt(LocalDateTime.now())
.status(ProcessStatus.FAILED)
.errorMessage(e.getMessage())
.build();
eventLogRepository.save(eventLog);
throw e;
}
}
private void processInventory(OrderCreatedEvent event) {
// ์ค์ ์ฌ๊ณ ์ฒ๋ฆฌ ๋ก์ง
}
}
-
eventId: ์ด๋ฒคํธ ์์ฒด์ ๊ณ ์ ID (๊ฐ์ฅ ์์ )-
orderId + eventType: ์ฃผ๋ฌธ๋ณ ์ด๋ฒคํธ ํ์
์กฐํฉ-
orderId + timestamp: ์๊ฐ ๊ธฐ๋ฐ (๊ถ์ฅํ์ง ์์)
Dead Letter Queue (DLQ) ์ฒ๋ฆฌ
์ฒ๋ฆฌ์ ๊ณ์ ์คํจํ๋ ๋ฉ์์ง๋ DLQ๋ก ๋ณด๋ด์ ๋ฐ๋ก ๊ด๋ฆฌํด:
@Configuration
public class KafkaErrorHandlingConfig {
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Object>
kafkaListenerContainerFactory(
ConsumerFactory<String, Object> consumerFactory,
KafkaTemplate<String, Object> kafkaTemplate) {
ConcurrentKafkaListenerContainerFactory<String, Object> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
// ์๋ฌ ํธ๋ค๋ฌ ์ค์
factory.setCommonErrorHandler(new DefaultErrorHandler(
new DeadLetterPublishingRecoverer(kafkaTemplate,
(record, ex) -> {
// DLQ ํ ํฝ ์ด๋ฆ ๊ฒฐ์
String originalTopic = record.topic();
String dlqTopic = originalTopic + ".DLQ";
return new TopicPartition(dlqTopic, record.partition());
}),
new FixedBackOff(1000L, 3L) // 1์ด ๊ฐ๊ฒฉ์ผ๋ก 3๋ฒ ์ฌ์๋
));
return factory;
}
}
@Service
@RequiredArgsConstructor
@Slf4j
public class DLQMonitoringService {
@KafkaListener(topics = "order.created.DLQ", groupId = "dlq-monitoring-group")
public void monitorOrderCreatedDLQ(ConsumerRecord<String, Object> record) {
log.error("Message moved to DLQ: topic={}, key={}, value={}",
record.topic(), record.key(), record.value());
// 1. ๊ด๋ฆฌ์์๊ฒ ์๋ฆผ
sendAdminAlert(record);
// 2. DB์ ๊ธฐ๋ก
saveDLQRecord(record);
// 3. ๋ชจ๋ํฐ๋ง ๋ฉํธ๋ฆญ ์
๋ฐ์ดํธ
updateDLQMetrics(record.topic());
}
private void sendAdminAlert(ConsumerRecord<String, Object> record) {
// Slack, ์ด๋ฉ์ผ ๋ฑ์ผ๋ก ์๋ฆผ ๋ฐ์ก
log.error("ALERT: DLQ message requires manual intervention");
}
private void saveDLQRecord(ConsumerRecord<String, Object> record) {
// DLQ ๋ฉ์์ง๋ฅผ DB์ ์ ์ฅํด์ ๋์ค์ ์ฌ์ฒ๋ฆฌ
}
private void updateDLQMetrics(String topic) {
// Prometheus, CloudWatch ๋ฑ์ ๋ฉํธ๋ฆญ ์ ์ก
}
}
๐ ๋ชจ๋ํฐ๋ง๊ณผ ๊ด์ฐฐ์ฑ (Observability)
๋ถ์ฐ ์์คํ ์์๋ ๋ชจ๋ํฐ๋ง์ด ์ ๋ง ์ค์ํด! ๋ฌธ์ ๊ฐ ์๊ฒผ์ ๋ ๋น ๋ฅด๊ฒ ํ์ ํ๊ณ ๋์ํ ์ ์์ด์ผ ํ๊ฑฐ๋ . ๐
๋ก๊น ์ ๋ต
๊ตฌ์กฐํ๋ ๋ก๊น ์ผ๋ก ์ด๋ฒคํธ ์ถ์ ์ ์ฝ๊ฒ ๋ง๋ค์ด๋ณด์:
@Slf4j
@Component
public class EventLogger {
public void logEventPublished(String topic, String key, Object event) {
Map<String, Object> logData = new HashMap<>();
logData.put("action", "event_published");
logData.put("topic", topic);
logData.put("key", key);
logData.put("eventType", event.getClass().getSimpleName());
logData.put("timestamp", LocalDateTime.now());
if (event instanceof OrderCreatedEvent) {
OrderCreatedEvent orderEvent = (OrderCreatedEvent) event;
logData.put("orderId", orderEvent.getOrderId());
logData.put("userId", orderEvent.getUserId());
logData.put("totalAmount", orderEvent.getTotalAmount());
}
log.info("Event published: {}", toJson(logData));
}
public void logEventConsumed(String topic, String key, Object event) {
Map<String, Object> logData = new HashMap<>();
logData.put("action", "event_consumed");
logData.put("topic", topic);
logData.put("key", key);
logData.put("eventType", event.getClass().getSimpleName());
logData.put("timestamp", LocalDateTime.now());
log.info("Event consumed: {}", toJson(logData));
}
public void logEventProcessingError(String topic, String key, Exception e) {
Map<String, Object> logData = new HashMap<>();
logData.put("action", "event_processing_error");
logData.put("topic", topic);
logData.put("key", key);
logData.put("error", e.getMessage());
logData.put("stackTrace", Arrays.toString(e.getStackTrace()));
logData.put("timestamp", LocalDateTime.now());
log.error("Event processing error: {}", toJson(logData));
}
private String toJson(Map<String, Object> data) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(data);
} catch (Exception e) {
return data.toString();
}
}
}
๋ถ์ฐ ์ถ์ (Distributed Tracing)
์ฌ๋ฌ ์๋น์ค๋ฅผ ๊ฑฐ์น๋ ์์ฒญ์ ์ถ์ ํ๋ ค๋ฉด Correlation ID๋ฅผ ์ฌ์ฉํด:
@Component
public class CorrelationIdInterceptor implements ProducerInterceptor<String, Object> {
private static final String CORRELATION_ID_HEADER = "X-Correlation-ID";
@Override
public ProducerRecord<String, Object> onSend(ProducerRecord<String, Object> record) {
// ํ์ฌ ์ค๋ ๋์ Correlation ID ๊ฐ์ ธ์ค๊ธฐ
String correlationId = MDC.get("correlationId");
if (correlationId == null) {
correlationId = UUID.randomUUID().toString();
MDC.put("correlationId", correlationId);
}
// ํค๋์ Correlation ID ์ถ๊ฐ
Headers headers = record.headers();
headers.add(CORRELATION_ID_HEADER, correlationId.getBytes());
return record;
}
@Override
public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
// ACK ์ฒ๋ฆฌ
}
@Override
public void close() {
// ๋ฆฌ์์ค ์ ๋ฆฌ
}
@Override
public void configure(Map<String, ?> configs) {
// ์ค์
}
}
@Component
public class CorrelationIdConsumerInterceptor implements ConsumerInterceptor<String, Object> {
private static final String CORRELATION_ID_HEADER = "X-Correlation-ID";
@Override
public ConsumerRecords<String, Object> onConsume(ConsumerRecords<String, Object> records) {
for (ConsumerRecord<String, Object> record : records) {
// ํค๋์์ Correlation ID ์ถ์ถ
Header correlationIdHeader = record.headers().lastHeader(CORRELATION_ID_HEADER);
if (correlationIdHeader != null) {
String correlationId = new String(correlationIdHeader.value());
MDC.put("correlationId", correlationId);
}
}
return records;
}
@Override
public void onCommit(Map<TopicPartition, OffsetAndMetadata> offsets) {
MDC.clear();
}
@Override
public void close() {
// ๋ฆฌ์์ค ์ ๋ฆฌ
}
@Override
public void configure(Map<String, ?> configs) {
// ์ค์
}
}
๋ฉํธ๋ฆญ ์์ง
Micrometer๋ฅผ ์ฌ์ฉํด์ Kafka ๊ด๋ จ ๋ฉํธ๋ฆญ์ ์์งํด๋ณด์:
@Configuration
public class MetricsConfig {
@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config()
.commonTags("application", "order-service")
.commonTags("environment", "production");
}
}
@Service
@RequiredArgsConstructor
public class OrderMetricsService {
private final MeterRegistry meterRegistry;
public void recordOrderCreated(Order order) {
// ์ฃผ๋ฌธ ์์ฑ ์นด์ดํฐ
Counter.builder("orders.created")
.tag("status", order.getStatus().name())
.tag("userId", order.getUserId())
.register(meterRegistry)
.increment();
// ์ฃผ๋ฌธ ๊ธ์ก ๋ถํฌ
DistributionSummary.builder("orders.amount")
.baseUnit("KRW")
.register(meterRegistry)
.record(order.getTotalAmount().doubleValue());
}
public void recordEventProcessingTime(String eventType, long durationMs) {
// ์ด๋ฒคํธ ์ฒ๋ฆฌ ์๊ฐ
Timer.builder("events.processing.time")
.tag("eventType", eventType)
.register(meterRegistry)
.record(durationMs, TimeUnit.MILLISECONDS);
}
public void recordEventProcessingError(String eventType, String errorType) {
// ์๋ฌ ์นด์ดํฐ
Counter.builder("events.processing.errors")
.tag("eventType", eventType)
.tag("errorType", errorType)
.register(meterRegistry)
.increment();
}
@Scheduled(fixedRate = 60000) // 1๋ถ๋ง๋ค
public void recordKafkaLag() {
// Kafka Consumer Lag ์ธก์
// AdminClient๋ฅผ ์ฌ์ฉํด์ lag ์ ๋ณด ์์ง
Gauge.builder("kafka.consumer.lag", this, OrderMetricsService::getConsumerLag)
.tag("group", "order-service-group")
.register(meterRegistry);
}
private double getConsumerLag() {
// ์ค์ lag ๊ณ์ฐ ๋ก์ง
return 0.0;
}
}
ํฌ์ค ์ฒดํฌ
Kafka ์ฐ๊ฒฐ ์ํ๋ฅผ ๋ชจ๋ํฐ๋งํ๋ ํฌ์ค ์ฒดํฌ๋ฅผ ๊ตฌํํด๋ณด์:
@Component
public class KafkaHealthIndicator implements HealthIndicator {
private final KafkaTemplate<String, Object> kafkaTemplate;
public KafkaHealthIndicator(KafkaTemplate<String, Object> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
@Override
public Health health() {
try {
// Kafka ํด๋ฌ์คํฐ์ ์ฐ๊ฒฐ ํ
์คํธ
ListenableFuture<SendResult<String, Object>> future =
kafkaTemplate.send("health-check", "ping");
future.get(5, TimeUnit.SECONDS);
return Health.up()
.withDetail("kafka", "Connected")
.withDetail("brokers", kafkaTemplate.getProducerFactory()
.getConfigurationProperties()
.get("bootstrap.servers"))
.build();
} catch (Exception e) {
return Health.down()
.withDetail("kafka", "Disconnected")
.withDetail("error", e.getMessage())
.build();
}
}
}
๐ ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
์ด์ ์์คํ ์ ๋ ๋น ๋ฅด๊ณ ํจ์จ์ ์ผ๋ก ๋ง๋๋ ๋ฐฉ๋ฒ์ ์์๋ณด์! ์ค๋ฌด์์ ๋ฐ๋ก ์ ์ฉํ ์ ์๋ ํ๋ค์ด์ผ. โก
๋ฐฐ์น ์ฒ๋ฆฌ (Batching)
๋ฉ์์ง๋ฅผ ํ๋์ฉ ๋ณด๋ด๋ ๊ฒ๋ณด๋ค ๋ฌถ์ด์ ๋ณด๋ด๋ฉด ํจ์ฌ ํจ์จ์ ์ด์ผ:
@Configuration
public class KafkaProducerOptimizationConfig {
@Bean
public ProducerFactory<String, Object> optimizedProducerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
// ๋ฐฐ์น ์ค์
config.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384); // 16KB
config.put(ProducerConfig.LINGER_MS_CONFIG, 10); // 10ms ๋๊ธฐ
config.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432); // 32MB
// ์์ถ ์ค์
config.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
// ์ฒ๋ฆฌ๋ ์ต์ ํ
config.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 5);
return new DefaultKafkaProducerFactory<>(config);
}
}
batch.size: ๋ฐฐ์น ํฌ๊ธฐ. 16KB๊ฐ ์ผ๋ฐ์ -
linger.ms: ๋ฐฐ์น๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ ์๊ฐ. ์ฒ๋ฆฌ๋๊ณผ ์ง์ฐ์๊ฐ์ ํธ๋ ์ด๋์คํ-
compression.type: snappy๊ฐ ์๋์ ์์ถ๋ฅ ์ ๊ท ํ์ด ์ข์
์ปจ์๋จธ ๋ณ๋ ฌ ์ฒ๋ฆฌ
์ฌ๋ฌ ์ปจ์๋จธ๋ฅผ ๋์์ ์คํํด์ ์ฒ๋ฆฌ๋์ ๋์ฌ๋ณด์:
@Configuration
public class KafkaConsumerOptimizationConfig {
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Object>
parallelKafkaListenerContainerFactory(
ConsumerFactory<String, Object> consumerFactory) {
ConcurrentKafkaListenerContainerFactory<String, Object> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
// ๋์ ์คํ ์ปจ์๋จธ ์ (ํํฐ์
์์ ๋ง์ถฐ์ผ ํจ)
factory.setConcurrency(10);
// ๋ฐฐ์น ๋ฆฌ์ค๋ ํ์ฑํ
factory.setBatchListener(true);
// ์๋ ์ปค๋ฐ ๋ชจ๋
factory.getContainerProperties().setAckMode(AckMode.MANUAL);
return factory;
}
}
@Service
@Slf4j
public class BatchOrderProcessor {
@KafkaListener(
topics = "order.created",
groupId = "batch-order-processor",
containerFactory = "parallelKafkaListenerContainerFactory"
)
public void processBatch(
List<ConsumerRecord<String, OrderCreatedEvent>> records,
Acknowledgment ack) {
log.info("Processing batch of {} orders", records.size());
try {
// ๋ฐฐ์น๋ก ํ๋ฒ์ ์ฒ๋ฆฌ
List<Order> orders = records.stream()
.map(record -> convertToOrder(record.value()))
.collect(Collectors.toList());
// ๋ฒํฌ ์ธ์ํธ
orderRepository.saveAll(orders);
// ์๋ ์ปค๋ฐ
ack.acknowledge();
log.info("Batch processed successfully: {} orders", orders.size());
} catch (Exception e) {
log.error("Error processing batch", e);
// ์๋ฌ ์ฒ๋ฆฌ
}
}
private Order convertToOrder(OrderCreatedEvent event) {
// ๋ณํ ๋ก์ง
return new Order();
}
}
2. ์์ ๋ณด์ฅ: ๊ฐ์ ํํฐ์ ์ ๋ฉ์์ง๋ ์์๊ฐ ๋ณด์ฅ๋์ง๋ง, ๋ค๋ฅธ ํํฐ์ ๊ฐ์๋ ๋ณด์ฅ ์ ๋จ
3. ๋ฆฌ์์ค ๊ด๋ฆฌ: ๋๋ฌด ๋ง์ ์ปจ์๋จธ๋ ์คํ๋ ค ์ฑ๋ฅ ์ ํ๋ฅผ ์ผ์ผํฌ ์ ์์ด
์บ์ฑ ์ ๋ต
์์ฃผ ์กฐํ๋๋ ๋ฐ์ดํฐ๋ ์บ์ํด์ DB ๋ถํ๋ฅผ ์ค์ฌ๋ณด์:
@Service
@RequiredArgsConstructor
@Slf4j
public class CachedInventoryService {
private final InventoryRepository inventoryRepository;
private final RedisTemplate<String, Inventory> redisTemplate;
private static final String INVENTORY_CACHE_PREFIX = "inventory:";
private static final Duration CACHE_TTL = Duration.ofMinutes(10);
public Inventory getInventory(String productId) {
String cacheKey = INVENTORY_CACHE_PREFIX + productId;
// 1. ์บ์ ์กฐํ
Inventory cached = redisTemplate.opsForValue().get(cacheKey);
if (cached != null) {
log.debug("Cache hit: productId={}", productId);
return cached;
}
// 2. DB ์กฐํ
log.debug("Cache miss: productId={}", productId);
Inventory inventory = inventoryRepository.findByProductId(productId)
.orElseThrow(() -> new ProductNotFoundException(productId));
// 3. ์บ์ ์ ์ฅ
redisTemplate.opsForValue().set(cacheKey, inventory, CACHE_TTL);
return inventory;
}
@KafkaListener(topics = "inventory.updated", groupId = "cache-invalidation-group")
public void handleInventoryUpdated(InventoryUpdatedEvent event) {
// ์บ์ ๋ฌดํจํ
String cacheKey = INVENTORY_CACHE_PREFIX + event.getProductId();
redisTemplate.delete(cacheKey);
log.info("Cache invalidated: productId={}", event.getProductId());
}
}
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ต์ ํ
์ธ๋ฑ์ค์ ์ฟผ๋ฆฌ ์ต์ ํ๋ก ์ฑ๋ฅ์ ๋์ด์ฌ๋ ค๋ณด์:
@Entity
@Table(
name = "orders",
indexes = {
@Index(name = "idx_order_id", columnList = "orderId"),
@Index(name = "idx_user_id", columnList = "userId"),
@Index(name = "idx_status", columnList = "status"),
@Index(name = "idx_created_at", columnList = "createdAt"),
@Index(name = "idx_user_status", columnList = "userId, status")
}
)
public class Order {
// ์ํฐํฐ ์ ์
}
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
// ํจ์จ์ ์ธ ์ฟผ๋ฆฌ
@Query("SELECT o FROM Order o " +
"JOIN FETCH o.items " +
"WHERE o.userId = :userId " +
"AND o.status = :status " +
"ORDER BY o.createdAt DESC")
List<Order> findByUserIdAndStatusWithItems(
@Param("userId") String userId,
@Param("status") Order.OrderStatus status,
Pageable pageable
);
// ์นด์ดํธ ์ฟผ๋ฆฌ ์ต์ ํ
@Query("SELECT COUNT(o) FROM Order o " +
"WHERE o.userId = :userId " +
"AND o.createdAt >= :startDate")
long countRecentOrders(
@Param("userId") String userId,
@Param("startDate") LocalDateTime startDate
);
}
2. WHERE, JOIN, ORDER BY์ ์ฌ์ฉ๋๋ ์ปฌ๋ผ ์ฐ์
3. ๋ณตํฉ ์ธ๋ฑ์ค๋ ์นด๋๋๋ฆฌํฐ๊ฐ ๋์ ์ปฌ๋ผ์ ์์
4. ๋๋ฌด ๋ง์ ์ธ๋ฑ์ค๋ INSERT/UPDATE ์ฑ๋ฅ ์ ํ
5. ์ ๊ธฐ์ ์ผ๋ก ์คํ ๊ณํ(EXPLAIN) ๋ถ์
๐ ๋ณด์ ๊ณ ๋ ค์ฌํญ
๋ณด์์ ์ ํ์ด ์๋๋ผ ํ์์ผ! Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์์คํ ์ ๋ณด์์ ๊ฐํํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์. ๐ก๏ธ
Kafka ๋ณด์ ์ค์
spring:
kafka:
bootstrap-servers: kafka-broker:9093
properties:
# SSL/TLS ์ค์
security.protocol: SSL
ssl.truststore.location: /path/to/truststore.jks
ssl.truststore.password: ${TRUSTSTORE_PASSWORD}
ssl.keystore.location: /path/to/keystore.jks
ssl.keystore.password: ${KEYSTORE_PASSWORD}
ssl.key.password: ${KEY_PASSWORD}
# SASL ์ธ์ฆ
sasl.mechanism: SCRAM-SHA-512
sasl.jaas.config: |
org.apache.kafka.common.security.scram.ScramLoginModule required
username="${KAFKA_USERNAME}"
password="${KAFKA_PASSWORD}";
producer:
# ACL ์ค์
properties:
enable.idempotence: true
max.in.flight.requests.per.connection: 5
๋ฉ์์ง ์ํธํ
๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ ์ํธํํด์ ์ ์กํด์ผ ํด:
@Service
@RequiredArgsConstructor
public class SecureEventPublisher {
private final KafkaTemplate<String, Object> kafkaTemplate;
private final EncryptionService encryptionService;
public void publishSecureOrder(Order order) {
// 1. ๋ฏผ๊ฐ ์ ๋ณด ์ํธํ
SecureOrderEvent event = SecureOrderEvent.builder()
.orderId(order.getOrderId())
.userId(order.getUserId())
.encryptedCardNumber(
encryptionService.encrypt(order.getPaymentInfo().getCardNumber())
)
.encryptedAddress(
encryptionService.encrypt(order.getShippingAddress().getFullAddress())
)
.totalAmount(order.getTotalAmount())
.build();
// 2. ์ด๋ฒคํธ ๋ฐํ
kafkaTemplate.send("secure.order.created", order.getOrderId(), event);
}
}
@Service
public class EncryptionService {
private static final String ALGORITHM = "AES/GCM/NoPadding";
private final SecretKey secretKey;
public EncryptionService(@Value("${encryption.key}") String key) {
this.secretKey = new SecretKeySpec(
Base64.getDecoder().decode(key),
"AES"
);
}
public String encrypt(String plaintext) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] iv = new byte[12];
SecureRandom.getInstanceStrong().nextBytes(iv);
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
// IV์ ์ํธ๋ฌธ์ ํจ๊ป ์ธ์ฝ๋ฉ
byte[] combined = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(ciphertext, 0, combined, iv.length, ciphertext.length);
return Base64.getEncoder().encodeToString(combined);
} catch (Exception e) {
throw new EncryptionException("Failed to encrypt data", e);
}
}
public String decrypt(String encrypted) {
try {
byte[] combined = Base64.getDecoder().decode(encrypted);
byte[] iv = new byte[12];
byte[] ciphertext = new byte[combined.length - 12];
System.arraycopy(combined, 0, iv, 0, 12);
System.arraycopy(combined, 12, ciphertext, 0, ciphertext.length);
Cipher cipher = Cipher.getInstance(ALGORITHM);
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
byte[] plaintext = cipher.doFinal(ciphertext);
return new String(plaintext, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new EncryptionException("Failed to decrypt data", e);
}
}
}
์ ๊ทผ ์ ์ด (ACL)
Kafka ACL๋ก ํ ํฝ๋ณ ์ ๊ทผ ๊ถํ์ ๊ด๋ฆฌํด:
# Producer์๊ฒ ํน์ ํ ํฝ ์ฐ๊ธฐ ๊ถํ ๋ถ์ฌ
kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:order-service \
--operation Write --topic order.created
# Consumer์๊ฒ ํน์ ํ ํฝ ์ฝ๊ธฐ ๊ถํ ๋ถ์ฌ
kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:inventory-service \
--operation Read --topic order.created \
--group inventory-service-group
# Consumer Group ๊ด๋ฆฌ ๊ถํ
kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:inventory-service \
--operation Read --group inventory-service-group
โ SASL๋ก ํด๋ผ์ด์ธํธ ์ธ์ฆ
โ ๋ฏผ๊ฐ ๋ฐ์ดํฐ ํ๋ ๋ ๋ฒจ ์ํธํ
โ ACL๋ก ํ ํฝ๋ณ ์ ๊ทผ ์ ์ด
โ ์ ๊ธฐ์ ์ธ ํค ๋กํ ์ด์
โ ๊ฐ์ฌ ๋ก๊ทธ ํ์ฑํ
โ ๋คํธ์ํฌ ๊ฒฉ๋ฆฌ (VPC, ๋ฐฉํ๋ฒฝ)
โ ์ต์ ๊ถํ ์์น ์ ์ฉ
๐ ์ค์ ์ด์ ๋ ธํ์ฐ
์ด๋ก ๊ณผ ์ฝ๋๋ ์ถฉ๋ถํ ๋ดค์ผ๋, ์ด์ ์ค์ ์ด์์์ ๋ง์ฃผ์น๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๊ณต์ ํ ๊ฒ! ๐ช
Kafka ํด๋ฌ์คํฐ ์ด์
num.network.threads: ๋คํธ์ํฌ ์์ฒญ ์ฒ๋ฆฌ ์ค๋ ๋ (CPU ์ฝ์ด ์์ 2๋ฐฐ)num.io.threads: ๋์คํฌ I/O ์ค๋ ๋ (8-16๊ฐ)socket.send.buffer.bytes: 102400 (100KB)socket.receive.buffer.bytes: 102400 (100KB)log.retention.hours: 168 (7์ผ)log.segment.bytes: 1073741824 (1GB)log.retention.check.interval.ms: 300000 (5๋ถ)
- UnderReplicatedPartitions: ๋ณต์ ๊ฐ ์ ๋ ํํฐ์ (0์ด์ด์ผ ํจ)
- ActiveControllerCount: ํ์ฑ ์ปจํธ๋กค๋ฌ ์ (1์ด์ด์ผ ํจ)
- OfflinePartitionsCount: ์คํ๋ผ์ธ ํํฐ์ ์ (0์ด์ด์ผ ํจ)
ํ ํฝ ๋ ๋ฒจ:
- MessagesInPerSec: ์ด๋น ๋ฉ์์ง ์์ ์จ
- BytesInPerSec: ์ด๋น ๋ฐ์ดํธ ์์ ์จ
- BytesOutPerSec: ์ด๋น ๋ฐ์ดํธ ์ ์ก์จ
์ปจ์๋จธ ๋ ๋ฒจ:
- ConsumerLag: ์ปจ์๋จธ ์ง์ฐ (๋ฎ์์๋ก ์ข์)
- RecordsConsumedRate: ์ด๋น ๋ ์ฝ๋ ์๋น์จ
์ฅ์ ๋์ ์๋๋ฆฌ์ค
์์ธ ๋ถ์:
1. ๋ฉ์์ง ์ฒ๋ฆฌ ๋ก์ง์ด ๋๋ ค์ง
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฑ๋ฅ ์ ํ
3. ์ธ๋ถ API ํธ์ถ ์ง์ฐ
ํด๊ฒฐ ๋ฐฉ๋ฒ:
1. ์ปจ์๋จธ ์ธ์คํด์ค ์ฆ๊ฐ (์ค์ผ์ผ ์์)
2. ํํฐ์ ์ ์ฆ๊ฐ
3. ๋ฐฐ์น ์ฒ๋ฆฌ ํฌ๊ธฐ ์กฐ์
4. ๋ณ๋ชฉ ์ง์ ์ต์ ํ (DB ์ฟผ๋ฆฌ, ์บ์ฑ ๋ฑ)
๋์:
1. ๋ค๋ฅธ ๋ธ๋ก์ปค๊ฐ ์๋์ผ๋ก ๋ฆฌ๋ ์ญํ ์ธ์ (replication factor > 1)
2. ํด๋ผ์ด์ธํธ๋ ์๋์ผ๋ก ๋ค๋ฅธ ๋ธ๋ก์ปค๋ก ์ฌ์ฐ๊ฒฐ
3. ๋ค์ด๋ ๋ธ๋ก์ปค ์ฌ์์
4. ํํฐ์ ์ฌ๋ถ๋ฐฐ ํ์ธ
์๋ฐฉ:
- Replication factor๋ฅผ ์ต์ 3์ผ๋ก ์ค์
- ๋ธ๋ก์ปค๋ฅผ ์ฌ๋ฌ ๊ฐ์ฉ ์์ญ์ ๋ถ์ฐ
- ์ ๊ธฐ์ ์ธ ํฌ์ค ์ฒดํฌ
์์ธ:
1. ์ปจ์๋จธ ์ฌ์์ ์ ์คํ์ ์ปค๋ฐ ๋๋ฝ
2. ๋คํธ์ํฌ ๋ฌธ์ ๋ก ์ฌ์๋
3. ๋ฉฑ๋ฑ์ฑ ๋ฏธ๊ตฌํ
ํด๊ฒฐ:
1. ๋ฉฑ๋ฑ์ฑ ํค ๊ธฐ๋ฐ ์ค๋ณต ์ฒดํฌ
2. ์ด๋ฒคํธ ๋ก๊ทธ ํ ์ด๋ธ๋ก ์ฒ๋ฆฌ ์ด๋ ฅ ๊ด๋ฆฌ
3. ํธ๋์ญ์ ์์๋ฐ์ค ํจํด ์ ์ฉ
์ฑ๋ฅ ํ๋ ์ฒดํฌ๋ฆฌ์คํธ
โก linger.ms๋ก ๋ฐฐ์น ๋๊ธฐ ์๊ฐ ์กฐ์ (10-100ms)
โก compression.type ํ์ฑํ (snappy, lz4)
โก acks=all๋ก ์์ ์ฑ ํ๋ณด
โก enable.idempotence=true๋ก ์ค๋ณต ๋ฐฉ์ง
โก buffer.memory ์ถฉ๋ถํ ํ ๋น (32MB ์ด์)
โก fetch.max.wait.ms๋ก ๋๊ธฐ ์๊ฐ ์กฐ์
โก max.poll.records๋ก ํ๋ฒ์ ๊ฐ์ ธ์ฌ ๋ ์ฝ๋ ์ ์ ํ
โก session.timeout.ms์ heartbeat.interval.ms ์ ์ ํ ์ค์
โก enable.auto.commit=false๋ก ์๋ ์ปค๋ฐ
โก ํํฐ์ ์๋งํผ ์ปจ์๋จธ ์ธ์คํด์ค ๋ฐฐ์น
โก replication.factor๋ ์ต์ 3
โก min.insync.replicas๋ 2 ์ด์
โก retention ์ ์ฑ ์ ์ ํ ์ค์
โก cleanup.policy ์ ํ (delete vs compact)
โก compression.type ์ค์
๋น์ฉ ์ต์ ํ
ํด๋ผ์ฐ๋์์ Kafka๋ฅผ ์ด์ํ ๋ ๋น์ฉ์ ์ค์ด๋ ๋ฐฉ๋ฒ๋ค์ด์ผ:
- ์ ์ ํ retention ๊ธฐ๊ฐ ์ค์ (๋ถํ์ํ๊ฒ ๊ธธ์ง ์๊ฒ)
- ์์ถ ํ์ฑํ๋ก ์ ์ฅ ๊ณต๊ฐ ์ ์ฝ
- Tiered Storage ํ์ฉ (์ค๋๋ ๋ฐ์ดํฐ๋ ์ ๋ ดํ ์คํ ๋ฆฌ์ง๋ก)
2. ์ปดํจํ ์ต์ ํ
- ์คํ ์ค์ผ์ผ๋ง์ผ๋ก ์ ํด ๋ฆฌ์์ค ์ต์ํ
- Spot ์ธ์คํด์ค ํ์ฉ (๋นํ๋ก๋์ ํ๊ฒฝ)
- ์ ์ ํ ์ธ์คํด์ค ํ์ ์ ํ
3. ๋คํธ์ํฌ ์ต์ ํ
- ๊ฐ์ ๋ฆฌ์ ๋ด ๋ฐฐ์น๋ก ๋ฐ์ดํฐ ์ ์ก ๋น์ฉ ์ ๊ฐ
- ์์ถ์ผ๋ก ๋คํธ์ํฌ ํธ๋ํฝ ๊ฐ์
- VPC ํผ์ด๋ง ํ์ฉ
๐ ์ค์ ์ฌ๋ก ์ฐ๊ตฌ
์ด๋ก ๋ง์ผ๋ก๋ ๋ถ์กฑํ์ง! ์ค์ ๊ธฐ์ ๋ค์ด ์ด๋ป๊ฒ Kafka๋ฅผ ํ์ฉํ๋์ง ์ดํด๋ณด์. ๐ข
์ฌ๋ก 1: ๋ํ ์ด์ปค๋จธ์ค ํ๋ซํผ
๋์ ๊ณผ์
- ๋ธ๋ํ๋ผ์ด๋ฐ์ด ํธ๋ํฝ ํญ์ฆ (ํ์์ 10๋ฐฐ)
- ์ค์๊ฐ ์ฌ๊ณ ๋๊ธฐํ
- ์ฃผ๋ฌธ-๊ฒฐ์ -๋ฐฐ์ก ๊ฐ ๋ฐ์ดํฐ ์ ํฉ์ฑ
์๋ฃจ์
- Kafka ํด๋ฌ์คํฐ: 15๊ฐ ๋ธ๋ก์ปค, ๊ฐ ํ ํฝ 30๊ฐ ํํฐ์
- ์ฃผ๋ฌธ ์ฒ๋ฆฌ: ํ๊ท ์๋ต์๊ฐ 200ms โ 50ms๋ก ๊ฐ์
- ์ฌ๊ณ ๋๊ธฐํ: 5๋ถ ์ง์ฐ โ ์ค์๊ฐ (1์ด ์ด๋ด)
- ์์คํ ๊ฐ์ฉ์ฑ: 99.9% โ 99.99%
ํต์ฌ ์ํคํ ์ฒ
- Event Sourcing์ผ๋ก ๋ชจ๋ ์ํ ๋ณํ ๊ธฐ๋ก
- CQRS ํจํด์ผ๋ก ์ฝ๊ธฐ/์ฐ๊ธฐ ๋ถ๋ฆฌ
- Saga ํจํด์ผ๋ก ๋ถ์ฐ ํธ๋์ญ์ ๊ด๋ฆฌ
์ฌ๋ก 2: ๊ธ๋ก๋ฒ ๋ฐฐ๋ฌ ํ๋ซํผ
๋์ ๊ณผ์
- ์ด๋น 10๋ง ๊ฑด์ ์์น ์ ๋ฐ์ดํธ
- ์ค์๊ฐ ๋ผ์ด๋-์ฃผ๋ฌธ ๋งค์นญ
- ๋ค๊ตญ๊ฐ ์๋น์ค (์ง์ฐ์๊ฐ ์ต์ํ)
์๋ฃจ์
- ์ง์ญ๋ณ Kafka ํด๋ฌ์คํฐ ๊ตฌ์ถ (๋ฏธ๊ตญ, ์ ๋ฝ, ์์์)
- Kafka Streams๋ก ์ค์๊ฐ ์คํธ๋ฆผ ์ฒ๋ฆฌ
- ์์น ๋ฐ์ดํฐ ์์ถ์ผ๋ก ๋คํธ์ํฌ ๋น์ฉ 70% ์ ๊ฐ
- ํ๊ท ๋งค์นญ ์๊ฐ 30์ด โ 5์ด
๊ธฐ์ ์คํ
- Kafka + Kafka Streams
- Redis (์บ์ฑ)
- PostgreSQL (์ฃผ๋ฌธ ๋ฐ์ดํฐ)
- Elasticsearch (๊ฒ์ ๋ฐ ๋ถ์)
์ฌ๋ก 3: ํํ ํฌ ์คํํธ์
๋์ ๊ณผ์
- ๊ฒฐ์ ๋ฐ์ดํฐ์ ๋์ ๋ณด์ ์๊ตฌ์ฌํญ
- ์ค์๊ฐ ์ฌ๊ธฐ ํ์ง (100ms ์ด๋ด)
- ๊ธ์ต ๊ท์ ์ค์ (๊ฐ์ฌ ๋ก๊ทธ)
์๋ฃจ์
- ์๋ํฌ์๋ ์ํธํ ์ ์ฉ
- Kafka Streams๋ก ์ค์๊ฐ ์ด์ ๊ฑฐ๋ ํ์ง
- Event Sourcing์ผ๋ก ์๋ฒฝํ ๊ฐ์ฌ ์ถ์
- ์ฌ๊ธฐ ํ์ง์จ 85% โ 95% ํฅ์
๋ณด์ ๊ฐํ
- SSL/TLS + SASL ์ธ์ฆ
- ํ๋ ๋ ๋ฒจ ์ํธํ
- ACL ๊ธฐ๋ฐ ์ ๊ทผ ์ ์ด
- ์ ๊ธฐ์ ์ธ ๋ณด์ ๊ฐ์ฌ
1. ๋ช ํํ ์ด๋ฒคํธ ์ค๊ณ: ๋๋ฉ์ธ ์ด๋ฒคํธ๋ฅผ ์ ์ ์ํจ
2. ์ ์ ํ ํํฐ์ ๋: ๋ถํ๋ฅผ ๊ท ๋ฑํ๊ฒ ๋ถ์ฐ
3. ๋ชจ๋ํฐ๋ง ๊ฐํ: ๋ฌธ์ ๋ฅผ ๋น ๋ฅด๊ฒ ๊ฐ์งํ๊ณ ๋์
4. ์ ์ง์ ๋์ : ํ๋ฒ์ ์ ํํ์ง ์๊ณ ๋จ๊ณ์ ์ผ๋ก ์ ์ฉ
5. ํ ๊ต์ก: ๊ฐ๋ฐ์๋ค์ด ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ฌ๊ณ ๋ฐฉ์์ ์ดํด
๐ฏ ๋ง์ด๊ทธ๋ ์ด์ ๊ฐ์ด๋
๊ธฐ์กด ์์คํ ์ Kafka ๊ธฐ๋ฐ์ผ๋ก ์ ํํ๋ ๊ฑด ์ฝ์ง ์์. ์์ ํ๊ฒ ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์! ๐
๋จ๊ณ๋ณ ๋ง์ด๊ทธ๋ ์ด์ ์ ๋ต
ํ๋:
1. ํ์ฌ ์ํคํ ์ฒ ๋ฌธ์ํ
2. ๋ณ๋ชฉ ์ง์ ํ์
3. ์ด๋ฒคํธ ๋ชจ๋ธ ์ค๊ณ
4. ํ ํฝ ๊ตฌ์กฐ ์ค๊ณ
5. ํ์ผ๋ฟ ํ๋ก์ ํธ ์ ์
์ฒดํฌ๋ฆฌ์คํธ:
โก ํ์ฌ ์์คํ ์ ํธ๋ํฝ ํจํด ๋ถ์
โก ๋ฐ์ดํฐ ํ๋ฆ ๋ค์ด์ด๊ทธ๋จ ์์ฑ
โก ์ด๋ฒคํธ ์นดํ๋ก๊ทธ ์์ฑ
โก ํ ๊ต์ก ๊ณํ ์๋ฆฝ
ํ๋:
1. Kafka ํด๋ฌ์คํฐ ๊ตฌ์ถ (๊ฐ๋ฐ ํ๊ฒฝ)
2. ๋จ์ํ ๊ธฐ๋ฅ ํ๋ ์ ํ (์: ์๋ฆผ ์๋น์ค)
3. ์ด๋ฒคํธ ๊ธฐ๋ฐ์ผ๋ก ์ฌ๊ตฌํ
4. ์ฑ๋ฅ ํ ์คํธ
5. ๋ฌธ์ ์ ํ์ ๋ฐ ๊ฐ์
์ฑ๊ณต ๊ธฐ์ค:
โก ๊ธฐ์กด ์์คํ ๊ณผ ๋์ผํ ๊ธฐ๋ฅ ๊ตฌํ
โก ์ฑ๋ฅ ์ ํ ์์
โก ํ์๋ค์ ์ดํด๋ ํฅ์
โก ์ด์ ํ๋ก์ธ์ค ์ ๋ฆฝ
์ ๋ต: Strangler Fig Pattern
๊ธฐ์กด ์์คํ ์ ์ ์งํ๋ฉด์ ์ ์์คํ ์ ์ ์ง์ ์ผ๋ก ๊ตฌ์ถ
์์:
1. ์๋ฆผ ์๋น์ค (์ํฅ๋ ๋ฎ์)
2. ์ฌ๊ณ ๊ด๋ฆฌ
3. ์ฃผ๋ฌธ ์ฒ๋ฆฌ
4. ๊ฒฐ์ ์ฒ๋ฆฌ (๊ฐ์ฅ ์ค์)
๊ฐ ๋จ๊ณ๋ง๋ค:
- ์ ์์คํ ๊ณผ ๊ธฐ์กด ์์คํ ๋ณํ ์ด์
- ํธ๋ํฝ ์ผ๋ถ๋ง ์ ์์คํ ์ผ๋ก ๋ผ์ฐํ
- ๋ชจ๋ํฐ๋ง ๋ฐ ๊ฒ์ฆ
- ๋ฌธ์ ์์ผ๋ฉด ํธ๋ํฝ ๋น์จ ์ฆ๊ฐ
ํ๋:
1. ๋ชจ๋ ํธ๋ํฝ์ ์ ์์คํ ์ผ๋ก ์ ํ
2. ๊ธฐ์กด ์์คํ ๋ชจ๋ํฐ๋ง (๋กค๋ฐฑ ๋๋น)
3. ์์ ํ ๊ธฐ๊ฐ (2์ฃผ)
4. ๊ธฐ์กด ์์คํ ์ ๊ฑฐ
5. ๋ฌธ์ํ ๋ฐ ํ๊ณ
๋กค๋ฐฑ ๊ณํ:
โก ๊ธฐ์กด ์์คํ ์ฆ์ ์ฌํ์ฑํ ๊ฐ๋ฅ
โก ๋ฐ์ดํฐ ๋๊ธฐํ ๋ฐฉ๋ฒ ์ค๋น
โก ์์ฌ๊ฒฐ์ ๊ธฐ์ค ๋ช ํํ
๋ฐ์ดํฐ ๋ง์ด๊ทธ๋ ์ด์
๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ์ ์์คํ ์ผ๋ก ์ฎ๊ธฐ๋ ๋ฐฉ๋ฒ:
@Service
@RequiredArgsConstructor
@Slf4j
public class DataMigrationService {
private final LegacyOrderRepository legacyOrderRepository;
private final KafkaTemplate<String, Object> kafkaTemplate;
@Scheduled(cron = "0 0 2 * * ?") // ๋งค์ผ ์๋ฒฝ 2์
public void migrateHistoricalOrders() {
log.info("Starting historical order migration");
LocalDateTime cutoffDate = LocalDateTime.now().minusDays(1);
Pageable pageable = PageRequest.of(0, 1000);
Page<LegacyOrder> page;
int totalMigrated = 0;
do {
// ๋ฐฐ์น๋ก ์กฐํ
page = legacyOrderRepository.findUnmigratedOrders(cutoffDate, pageable);
for (LegacyOrder legacyOrder : page.getContent()) {
try {
// ์ด๋ฒคํธ๋ก ๋ณํ
OrderCreatedEvent event = convertToEvent(legacyOrder);
// Kafka๋ก ๋ฐํ
kafkaTemplate.send("order.created",
legacyOrder.getOrderId(),
event);
// ๋ง์ด๊ทธ๋ ์ด์
์๋ฃ ํ์
legacyOrder.setMigrated(true);
legacyOrderRepository.save(legacyOrder);
totalMigrated++;
} catch (Exception e) {
log.error("Failed to migrate order: {}",
legacyOrder.getOrderId(), e);
}
}
log.info("Migrated batch: {} orders", page.getNumberOfElements());
} while (page.hasNext());
log.info("Migration completed: {} orders migrated", totalMigrated);
}
private OrderCreatedEvent convertToEvent(LegacyOrder legacyOrder) {
// ๋ณํ ๋ก์ง
return OrderCreatedEvent.builder()
.eventId(UUID.randomUUID().toString())
.eventType("order.created")
.timestamp(legacyOrder.getCreatedAt())
.orderId(legacyOrder.getOrderId())
.userId(legacyOrder.getUserId())
.totalAmount(legacyOrder.getTotalAmount())
.build();
}
}
๋์ผ ๋ผ์ดํธ ํจํด
์ ํ ๊ธฐ๊ฐ ๋์ ๋ ์์คํ ์ ๋์์ ์ฐ๋ ๋ฐฉ๋ฒ:
@Service
@RequiredArgsConstructor
@Slf4j
public class DualWriteOrderService {
private final LegacyOrderService legacyOrderService;
private final KafkaTemplate<String, Object> kafkaTemplate;
private final FeatureToggleService featureToggleService;
@Transactional
public OrderResponse createOrder(CreateOrderRequest request) {
// 1. ๊ธฐ์กด ์์คํ
์ ์ฐ๊ธฐ (ํญ์)
OrderResponse legacyResponse = legacyOrderService.createOrder(request);
// 2. ์ ์์คํ
์๋ ์ฐ๊ธฐ (Feature Toggle๋ก ์ ์ด)
if (featureToggleService.isEnabled("kafka-dual-write")) {
try {
OrderCreatedEvent event = buildEvent(legacyResponse);
kafkaTemplate.send("order.created",
legacyResponse.getOrderId(),
event);
log.info("Dual write successful: orderId={}",
legacyResponse.getOrderId());
} catch (Exception e) {
// ์ ์์คํ
์ฐ๊ธฐ ์คํจํด๋ ๊ธฐ์กด ์์คํ
์ ์ฑ๊ณต
log.error("Dual write to Kafka failed, but legacy write succeeded", e);
// ์๋ฆผ ๋ฐ์กํ์ฌ ์๋ ์ฒ๋ฆฌ
}
}
return legacyResponse;
}
}
1. ๋ฐ์ดํฐ ๋ถ์ผ์น: ํ์ชฝ๋ง ์ฑ๊ณตํ ์ ์์
2. ์ฑ๋ฅ ์ ํ: ๋ ๋ฒ ์ฐ๊ธฐ ๋๋ฌธ์ ๋๋ ค์ง
3. ๋ณต์ก๋ ์ฆ๊ฐ: ์๋ฌ ์ฒ๋ฆฌ๊ฐ ๋ณต์กํด์ง
๊ฐ๋ฅํ ๋นจ๋ฆฌ ์์ ์ ํํ๋ ๊ฒ ์ข์!
๐ฎ ๋ฏธ๋ ํธ๋ ๋์ ๋ฐ์ ๋ฐฉํฅ
Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ ๊ณ์ ์งํํ๊ณ ์์ด. ์์ผ๋ก์ ํธ๋ ๋๋ฅผ ์ดํด๋ณด์! ๐
Kafka์ ์๋ก์ด ๊ธฐ๋ฅ๋ค
์ฅ์ :
- ์ด์ ๋ณต์ก๋ ๊ฐ์ (ZooKeeper ๋ถํ์)
- ๋ ๋น ๋ฅธ ๋ฉํ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ
- ๋ ๋ง์ ํํฐ์ ์ง์ (์๋ฐฑ๋ง ๊ฐ)
- ๋ ๋น ๋ฅธ ์ปจํธ๋กค๋ฌ ํ์ผ์ค๋ฒ
์ฅ์ :
- ์คํ ๋ฆฌ์ง ๋น์ฉ ๋ํญ ์ ๊ฐ
- ๊ธด retention ๊ธฐ๊ฐ ๊ฐ๋ฅ
- ๋ธ๋ก์ปค ๋์คํฌ ์ฉ๋ ์ ์ฝ
- ๋ ๋์ ์ํ ์ ์ฅ์ ๊ด๋ฆฌ
- ํฅ์๋ ์๋์ฐ ์ฒ๋ฆฌ
- ๋ ์ฌ์ด ํ ์คํธ ๋๊ตฌ
- ๋ ๋์ ์๋ฌ ์ฒ๋ฆฌ
์๋ฒ๋ฆฌ์ค Kafka
ํด๋ผ์ฐ๋ ์ ๊ณต์๋ค์ด ๊ด๋ฆฌํ Kafka ์๋น์ค๋ฅผ ์ ๊ณตํ๊ณ ์์ด:
- ์๋ ์ค์ผ์ผ๋ง
- ์ฌ์ฉ๋ ๊ธฐ๋ฐ ๊ณผ๊ธ
- ๊ธ๋ก๋ฒ ํด๋ฌ์คํฐ ์ง์
์ ํฉํ ๊ฒฝ์ฐ:
- ๋น ๋ฅธ ์์์ด ํ์ํ ๋
- ์ด์ ๋ฆฌ์์ค๊ฐ ๋ถ์กฑํ ๋
- ๊ธ๋ก๋ฒ ์๋น์ค๋ฅผ ์ด์ํ ๋
- IAM ๊ธฐ๋ฐ ์ธ์ฆ
- CloudWatch ๋ชจ๋ํฐ๋ง
- ์๋ ํจ์น ๋ฐ ์ ๊ทธ๋ ์ด๋
์ ํฉํ ๊ฒฝ์ฐ:
- AWS ์ํ๊ณ๋ฅผ ์ฌ์ฉ ์ค์ผ ๋
- ๋ณด์ ์๊ตฌ์ฌํญ์ด ๋์ ๋
- ๊ธฐ์กด AWS ์๋น์ค์ ํตํฉํ ๋
์ด๋ฒคํธ ๋ฉ์ (Event Mesh)
์ฌ๋ฌ Kafka ํด๋ฌ์คํฐ๋ฅผ ์ฐ๊ฒฐํ๋ ์๋ก์ด ํจ๋ฌ๋ค์์ด์ผ:
์ฌ์ฉ ์ฌ๋ก:
- ๋ฉํฐ ํด๋ผ์ฐ๋ ํ๊ฒฝ
- ๊ธ๋ก๋ฒ ์๋น์ค
- ํ์ด๋ธ๋ฆฌ๋ ํด๋ผ์ฐ๋
- ์ฌํด ๋ณต๊ตฌ
๊ตฌํ ๋ฐฉ๋ฒ:
- Kafka MirrorMaker 2.0
- Confluent Replicator
- ์ปค์คํ ๋ณต์ ์๋ฃจ์
AI/ML๊ณผ์ ํตํฉ
Kafka๋ ๋จธ์ ๋ฌ๋ ํ์ดํ๋ผ์ธ์ ํต์ฌ์ด ๋๊ณ ์์ด:
์์:
- ์ค์๊ฐ ์ฌ๊ธฐ ํ์ง
- ๊ฐ์ธํ ์ถ์ฒ
- ์ด์ ํ์ง
- ์์ ์์ธก
๊ธฐ์ ์คํ:
- Kafka Streams
- TensorFlow Serving
- MLflow
- Feature Store
์ฌ๋ฅ๋ท์์์ ํ์ฉ ๊ฐ๋ฅ์ฑ
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ์ด๋ฐ ๊ธฐ์ ๋ค์ ํ์ฉํ ์ ์์ด:
์ฌ๋ฅ ์ ๊ณต์์ ๊ตฌ๋งค์๋ฅผ ์ค์๊ฐ์ผ๋ก ๋งค์นญํ๋ ์์คํ ์ Kafka Streams๋ก ๊ตฌํ
2. ๊ฐ์ธํ ์ถ์ฒ
์ฌ์ฉ์์ ํ๋ ๋ฐ์ดํฐ๋ฅผ Kafka๋ก ์์งํ๊ณ , ์ค์๊ฐ์ผ๋ก ๋ง์ถค ์ฌ๋ฅ ์ถ์ฒ
3. ๊ฑฐ๋ ์์ ์ฑ
์ด๋ฒคํธ ์์ฑ์ผ๋ก ๋ชจ๋ ๊ฑฐ๋ ์ด๋ ฅ์ ์๋ฒฝํ๊ฒ ์ถ์ ํ๊ณ ๋ถ์ ํด๊ฒฐ์ ํ์ฉ
4. ์ค์๊ฐ ์๋ฆผ
์๋ก์ด ์ฌ๋ฅ ๋ฑ๋ก, ๊ฑฐ๋ ์ํ ๋ณํ ๋ฑ์ ์ฆ์ ์๋ฆผ
๐ ํ์ต ๋ฆฌ์์ค์ ์ปค๋ฎค๋ํฐ
Kafka๋ฅผ ๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ด๋ฐ ๋ฆฌ์์ค๋ค์ ํ์ฉํด๋ด! ๐
๊ณต์ ๋ฌธ์ ๋ฐ ํํ ๋ฆฌ์ผ
- kafka.apache.org/documentation
- ๊ฐ์ฅ ์ ํํ๊ณ ์ต์ ์ ๋ณด
Confluent ๋ฌธ์
- docs.confluent.io
- ์ค์ฉ์ ์ธ ์์ ์ ๋ฒ ์คํธ ํ๋ํฐ์ค
Spring for Apache Kafka
- spring.io/projects/spring-kafka
- Spring Boot ํตํฉ ๊ฐ์ด๋
์ถ์ฒ ๋์
Kafka์ ๋ฐ์ด๋ธ. ๊น์ด ์๋ ๋ด์ฉ
2. "Designing Event-Driven Systems" - Ben Stopford
์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ ์ค๊ณ ํจํด
3. "Building Microservices" - Sam Newman
๋ง์ดํฌ๋ก์๋น์ค์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ํต์
4. "Event Sourcing and CQRS" - Vaughn Vernon
์ด๋ฒคํธ ์์ฑ ํจํด์ ์ค์ ์ ์ฉ
์จ๋ผ์ธ ๊ฐ์
- "Apache Kafka Series - Learn Apache Kafka for Beginners"
- "Kafka Streams for Data Processing"
Coursera
- "Event-Driven Architecture"
- "Building Scalable Java Microservices"
YouTube
- Confluent ๊ณต์ ์ฑ๋
- Spring Developer ์ฑ๋
์ปค๋ฎค๋ํฐ
- Apache Kafka Slack
- Confluent Community Slack
ํฌ๋ผ
- Stack Overflow (kafka ํ๊ทธ)
- Confluent Community Forum
ํ๊ตญ ์ปค๋ฎค๋ํฐ
- ์นดํ์นด ํ๊ตญ ์ฌ์ฉ์ ๋ชจ์
- ๋ง์ดํฌ๋ก์๋น์ค ์ํคํ ์ฒ ์ฐ๊ตฌํ
์ปจํผ๋ฐ์ค
- Kafka Summit (์ฐ 2ํ)
- Spring One
- ๊ตญ๋ด ๊ฐ๋ฐ์ ์ปจํผ๋ฐ์ค
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด. Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ฅผ ํ์ฉํ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ์์คํ ์ ๋ํด ์ ๋ง ๋ง์ ๊ฑธ ๋ค๋ค์ง? ๐
์ฒ์์๋ ๋ณต์กํด ๋ณด์ผ ์ ์์ง๋ง, ํ๋์ฉ ์ฐจ๊ทผ์ฐจ๊ทผ ์ ์ฉํ๋ค ๋ณด๋ฉด ๋ถ๋ช ์์คํ ์ด ๋ ๊ฒฌ๊ณ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํด์ง๋ ๊ฑธ ๋๋ ์ ์์ ๊ฑฐ์ผ. ํนํ ์ผํ๋ชฐ์ฒ๋ผ ํธ๋ํฝ ๋ณ๋์ด ํฐ ์๋น์ค์์๋ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๊ฐ ์ ๋ง ๊ฐ๋ ฅํ ๋ฌด๊ธฐ๊ฐ ๋์ง!
- ๋์ ์ฒ๋ฆฌ๋๊ณผ ํ์ฅ์ฑ
- ์๋น์ค ๊ฐ ๋์จํ ๊ฒฐํฉ
- ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ก ๋น ๋ฅธ ์๋ต
- ์ฅ์ ๊ฒฉ๋ฆฌ์ ๋ณต์๋ ฅ
2. Kafka์ ํต์ฌ ๊ฐ๋
- Topic, Producer, Consumer
- Partition๊ณผ Consumer Group
- Replication๊ณผ ๋ด๊ตฌ์ฑ
- Offset ๊ด๋ฆฌ
3. ์ค์ ๊ตฌํ ํฌ์ธํธ
- ๋ช ํํ ์ด๋ฒคํธ ์ค๊ณ
- ๋ฉฑ๋ฑ์ฑ ๋ณด์ฅ
- ์๋ฌ ์ฒ๋ฆฌ์ ๋ณด์ ํธ๋์ญ์
- ๋ชจ๋ํฐ๋ง๊ณผ ๊ด์ฐฐ์ฑ
4. ์ด์ ๋ ธํ์ฐ
- ์ ์ ํ ํํฐ์ ๋ ์ ๋ต
- ์ฑ๋ฅ ํ๋
- ๋ณด์ ๊ฐํ
- ์ ์ง์ ๋ง์ด๊ทธ๋ ์ด์
๊ธฐ์ตํด! ์๋ฒฝํ ์์คํ ์ ์ฒ์๋ถํฐ ๋ง๋ค์ด์ง์ง ์์. ์์ ๊ฒ๋ถํฐ ์์ํด์ ์ ์ง์ ์ผ๋ก ๊ฐ์ ํด๋๊ฐ๋ ๊ฒ ์ค์ํด. ํ์ผ๋ฟ ํ๋ก์ ํธ๋ก ์์ํด์ ๊ฒฝํ์ ์๊ณ , ํ์๋ค๊ณผ ํจ๊ป ์ฑ์ฅํ๋ฉด์ ์์คํ ์ ๋ฐ์ ์์ผ ๋๊ฐ๋ฉด ๋ผ. ๐ช
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ์ด๋ฐ ์ํคํ ์ฒ๋ฅผ ์ ์ฉํ๋ฉด ๋ ์์ ์ ์ด๊ณ ํ์ฅ ๊ฐ๋ฅํ ์๋น์ค๋ฅผ ๋ง๋ค ์ ์์ด. ์ฌ์ฉ์๋ค์๊ฒ ๋ ๋์ ๊ฒฝํ์ ์ ๊ณตํ๊ณ , ๊ฐ๋ฐ์๋ค์ ๋ ํจ์จ์ ์ผ๋ก ์ผํ ์ ์๊ฒ ๋์ง!
2. ํ๊ณผ ํจ๊ป ํ์ต: ์คํฐ๋๋ ์ธ๋ฏธ๋ ์งํ
3. ์คํ๊ณผ ์ธก์ : ์ฑ๋ฅ ํ ์คํธ์ ๋ชจ๋ํฐ๋ง
4. ์ ์ง์ ํ์ฅ: ๊ฒ์ฆ๋ ํจํด์ ๋ค๋ฅธ ์์ญ์ ์ ์ฉ
5. ์ปค๋ฎค๋ํฐ ์ฐธ์ฌ: ๊ฒฝํ ๊ณต์ ํ๊ณ ๋ฐฐ์ฐ๊ธฐ
์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ ๋จ์ํ ๊ธฐ์ ์ ์ธ ์ ํ์ด ์๋๋ผ, ๋น์ฆ๋์ค ์๊ตฌ์ฌํญ์ ๋ ์ ๋ฐ์ํ๊ณ ๋ณํ์ ๋น ๋ฅด๊ฒ ๋์ํ ์ ์๋ ์ฌ๊ณ ๋ฐฉ์์ ์ ํ์ด์ผ. ๋๋ฉ์ธ ์ด๋ฒคํธ๋ฅผ ์ค์ฌ์ผ๋ก ์๊ฐํ๋ฉด ์์คํ ์ด ๋ ๋ช ํํด์ง๊ณ , ํ ๊ฐ ํ์ ๋ ์์ํด์ ธ.
๋ง์ง๋ง์ผ๋ก, ๊ธฐ์ ์ ๋๊ตฌ์ผ ๋ฟ์ด์ผ. ์ค์ํ ๊ฑด ์ฌ์ฉ์์๊ฒ ๊ฐ์น๋ฅผ ์ ๋ฌํ๋ ๊ฑฐ์ง. Kafka์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ํคํ ์ฒ๊ฐ ๊ทธ ๋ชฉํ๋ฅผ ๋ฌ์ฑํ๋ ๋ฐ ๋์์ด ๋๊ธธ ๋ฐ๋ผ! ๐
ํ์ดํ ! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.