추천 서비스 (Recommendation)
개요
추천 서비스는 사용자 행동 데이터를 기반으로 개인화된 상품 추천을 제공합니다. DocumentDB에 사용자 활동을 저장하고, ElastiCache(Valkey)를 통해 추천 결과를 캐싱하여 빠른 응답 속도를 보장합니다.
| 항목 | 값 |
|---|---|
| 언어 | Python 3.11 |
| 프레임워크 | FastAPI |
| 데이터베이스 | DocumentDB (MongoDB 호환) |
| 캐시 | ElastiCache (Valkey) |
| 네임스페이스 | mall-services |
| 포트 | 8000 |
| 헬스체크 | GET /health |
아키텍처
API 엔드포인트
추천 API
| 메서드 | 경로 | 설명 |
|---|---|---|
GET | /api/v1/recommendations/{user_id} | 개인화 추천 |
GET | /api/v1/recommendations/trending | 트렌딩 상품 |
GET | /api/v1/recommendations/similar/{product_id} | 유사 상품 추천 |
요청/응답 예시
개인화 추천
요청:
GET /api/v1/recommendations/user_001?limit=10
응답:
{
"user_id": "user_001",
"recommendations": [
{
"product_id": "prod_101",
"score": 0.95,
"reason": "Based on your browsing history",
"category": "electronics"
},
{
"product_id": "prod_205",
"score": 0.87,
"reason": "Based on your browsing history",
"category": "fashion"
},
{
"product_id": "prod_089",
"score": 0.82,
"reason": "Based on your browsing history",
"category": "electronics"
}
],
"generated_at": "2024-01-15T10:00:00Z"
}
트렌딩 상품
요청:
GET /api/v1/recommendations/trending
응답:
{
"products": [
{
"product_id": "prod_001",
"name": "삼성 갤럭시 S24",
"category": "electronics",
"score": 0.98,
"view_count": 15420,
"purchase_count": 2341
},
{
"product_id": "prod_042",
"name": "나이키 에어맥스",
"category": "fashion",
"score": 0.94,
"view_count": 12890,
"purchase_count": 1876
},
{
"product_id": "prod_078",
"name": "애플 에어팟 프로",
"category": "electronics",
"score": 0.91,
"view_count": 11200,
"purchase_count": 1543
}
],
"generated_at": "2024-01-15T10:00:00Z"
}
유사 상품 추천
요청:
GET /api/v1/recommendations/similar/prod_001?limit=10
응답:
{
"product_id": "prod_001",
"similar": [
{
"product_id": "prod_002",
"score": 0.89,
"reason": "Users who viewed this also viewed",
"category": "electronics"
},
{
"product_id": "prod_015",
"score": 0.76,
"reason": "Users who viewed this also viewed",
"category": "electronics"
},
{
"product_id": "prod_023",
"score": 0.71,
"reason": "Users who viewed this also viewed",
"category": "accessories"
}
],
"generated_at": "2024-01-15T10:00:00Z"
}
데이터 모델
Recommendation
class Recommendation(BaseModel):
product_id: str
score: float = Field(ge=0.0, le=1.0) # 0.0 ~ 1.0
reason: str # 추천 이유
category: Optional[str] = None
UserActivity
class UserActivity(BaseModel):
user_id: str
product_id: str
action: str # view, click, purchase, add_to_cart
timestamp: datetime
metadata: Optional[dict] = None
TrendingProduct
class TrendingProduct(BaseModel):
product_id: str
name: str
category: str
score: float
view_count: int
purchase_count: int
RecommendationResponse
class RecommendationResponse(BaseModel):
user_id: str
recommendations: list[Recommendation]
generated_at: datetime
TrendingResponse
class TrendingResponse(BaseModel):
products: list[TrendingProduct]
generated_at: datetime
SimilarProductsResponse
class SimilarProductsResponse(BaseModel):
product_id: str
similar: list[Recommendation]
generated_at: datetime