Ray Serve는 Ray 생태계 위에서 동작하는 확장 가능한 모델 서빙 프레임워크입니다. 하나의 파이썬 함수/클래스를 분산 배포하여 트래픽에 따라 자동으로 스케일링하며, GPU/CPU 리소스를 효율적으로 사용합니다.
요청 수에 따라 replica 수를 동적으로 조절. zero-scaledown도 지원하여 비용 최적화.
하나의 엔드포인트에서 여러 모델을 조합하거나, 버전별 라우팅, A/B 테스트 가능.
동적 배치 처리, WebSocket 스트리밍, FastAPI 통합으로 실시간 서비스 구축.
기존 서빙 방식(TensorFlow Serving, TorchServe, Flask/Gunicorn)과 비교한 Ray Serve의 차별점:
| 비교 항목 | Ray Serve | Flask + Gunicorn | TF Serving / TorchServe |
|---|---|---|---|
| 분산 스케일링 | ✅ 기본 내장 (Ray 클러스터) | ❌ 수동 (워커 관리) | ⚠️ 제한적 (모델별) |
| 멀티 모델 라우팅 | ✅ 네이티브 (DAG, composition) | ❌ 직접 구현 | ⚠️ 부분 지원 |
| GPU 공유 / 동적 배치 | ✅ 자동 배치, fraction GPU | ❌ | ✅ (정적 배치) |
| 스트리밍 / WebSocket | ✅ FastAPI + WebSocket | ⚠️ (확장 필요) | ❌ |
| Python 생태계 | ✅ 완전한 Python (any lib) | ✅ | ⚠️ (제한적 커스텀) |
Ray Serve의 주요 구성 요소와 아키텍처를 이해하는 것이 중요합니다.
@serve.deployment 데코레이터.설치부터 첫 번째 모델 서빙까지 5분 가이드.
pip install "ray[serve]"
또는 conda:
conda install -c conda-forge ray-serve
model.py — 간단한 텍스트 분류기 서빙:
from ray import serve
from transformers import pipeline
@serve.deployment(
num_replicas=2,
ray_actor_options={"num_gpus": 0.2},
autoscaling_config={
"min_replicas": 1,
"max_replicas": 5,
"target_num_ongoing_requests_per_replica": 10,
}
)
class TextClassifier:
def __init__(self):
self.model = pipeline("sentiment-analysis")
async def __call__(self, request):
text = (await request.json())["text"]
result = self.model(text)
return {"result": result}
serve.run(TextClassifier.bind())
CLI로 실행:
serve run model:TextClassifier
요청 테스트 (curl):
curl -X POST http://localhost:8000/ \
-H "Content-Type: application/json" \
-d '{"text": "Ray Serve is amazing!"}'
{"result": [{"label": "POSITIVE", "score": 0.999}]}
| 파라미터 | 타입 | 설명 | 예시 |
|---|---|---|---|
num_replicas | int | 고정 replica 수 (autoscaling 비활성) | 2 |
autoscaling_config | dict | 자동 스케일링 정책 | {"min_replicas":1, "max_replicas":10} |
ray_actor_options | dict | GPU/CPU 리소스 할당 | {"num_gpus": 0.5} |
max_concurrent_queries | int | replica당 동시 요청 제한 | 100 |
user_config | any | 런타임 업데이트 가능한 설정 | {"batch_size": 32} |
route_prefix | str | HTTP 경로 (예: /classify) | "/v1/predict" |
자세한 옵션은 공식 Deployment API 참조.
해결: ray_actor_options={"num_gpus": 0.3} 으로 GPU fraction 사용. 또는 max_concurrent_queries를 낮춰 메모리 경합 완화.
해결: min_replicas=1 이상 유지. 모델 로딩 시간이 길면 health_check_period_s 조정.
해결: @serve.batch(max_batch_size=32, batch_wait_timeout_s=0.1) 데코레이터로 동적 배칭.
serve status 확인.RAY_OBJECT_STORE_ALLOW_SLOW_STORAGE=1.target_num_ongoing_requests_per_replica 값을 낮춰보세요.