๐ ๊ฐ์
์ด ๊ธ์์๋ FastAPI + PostgreSQL + pgvector๋ฅผ ํ์ฉํ ๋๊ท๋ชจ ๋ฒกํฐ ๊ฒ์ ์ฑ๋ฅ ์ต์ ํ ๋ฐฉ๋ฒ์ ๋ค๋ฃน๋๋ค.
โ pgvector์ HNSW(Hierarchical Navigable Small World) ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ ๊ฒ์ ์๋ ๊ฐ์
โ ๋๋ ๋ฒกํฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฐ ๊ฒ์ ์ต์ ํ
โ Kubernetes์์ FastAPI์ Auto Scaling ์ ์ฉ
๐ 1. pgvector์ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ HNSW ์ธ๋ฑ์ค ์ ์ฉ
pgvector๋ ๋ฒกํฐ ๊ฒ์์ ์ต์ ํํ๊ธฐ ์ํด L2 distance (์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ), Cosine similarity, Inner product ๋ฑ์ ๋ฐฉ๋ฒ์ ์ง์ํฉ๋๋ค.
HNSW(Hierarchical Navigable Small World) ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ๋ฉด ๋๊ท๋ชจ ๋ฐ์ดํฐ์์๋ ๋น ๋ฅธ ๊ฒ์์ด ๊ฐ๋ฅํฉ๋๋ค.
1๏ธโฃ ๊ธฐ์กด ๋ฒกํฐ ํ ์ด๋ธ ํ์ธ
๋จผ์ , ํ์ฌ ๋ฒกํฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํ ์ด๋ธ ๊ตฌ์กฐ๋ฅผ ํ์ธํฉ๋๋ค.
\dt
โ ์ถ๋ ฅ ์์
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+---------
public | embeddings | table | postgres
๐ ํ์ฌ ํ ์ด๋ธ ๊ตฌ์กฐ ์กฐํ
\d embeddings
โ ์ถ๋ ฅ ์์
Column | Type | Collation | Nullable | Default
----------+---------+-----------+----------+---------
id | integer | | not null | nextval('embeddings_id_seq'::regclass)
content | text | | |
embedding| vector(3) | | |
Indexes:
"embeddings_pkey" PRIMARY KEY, btree (id)
2๏ธโฃ HNSW ์ธ๋ฑ์ค ์ ์ฉ
HNSW ์ธ๋ฑ์ค๋ ๋ฒกํฐ ๊ฒ์ ์ฑ๋ฅ์ ๋ํญ ํฅ์์ํค๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
CREATE INDEX embeddings_hnsw ON embeddings USING hnsw (embedding vector_l2_ops);
โ ์ ์ฉ๋ ์ธ๋ฑ์ค ํ์ธ
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'embeddings';
โ ์ถ๋ ฅ ์์
indexname | indexdef
---------------+----------------------------------------------
embeddings_pkey | CREATE UNIQUE INDEX embeddings_pkey ON embeddings USING btree (id)
embeddings_hnsw | CREATE INDEX embeddings_hnsw ON embeddings USING hnsw (embedding vector_l2_ops)
๐ HNSW ์ธ๋ฑ์ค๊ฐ ์ ์ฉ๋์๋์ง ํ์ธ ํ ๊ฒ์ ์ฑ๋ฅ ํ ์คํธ ์งํ
๐ 2. ๋๊ท๋ชจ ๋ฒกํฐ ๋ฐ์ดํฐ ๊ฒ์ ์ต์ ํ
HNSW ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ ๊ฒ์ ์ต์ ํ ์ฟผ๋ฆฌ๋ฅผ ์คํํฉ๋๋ค.
๐ HNSW ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ ์ต๊ทผ์ ์ด์ ๊ฒ์
SELECT content, embedding <-> '[0.2, 0.2, 0.2]' AS distance
FROM embeddings
ORDER BY distance
LIMIT 5;
โ ์ถ๋ ฅ ์์
content | distance
--------------------+-----------
Hello, world! | 0.141421
How are you? | 0.173205
This is PostgreSQL | 0.500000
(3 rows)
๐ Cosine Similarity ๊ฒ์
SELECT content, embedding <#> '[0.2, 0.2, 0.2]' AS similarity
FROM embeddings
ORDER BY similarity DESC
LIMIT 5;
โ ์ถ๋ ฅ ์์
content | similarity
--------------------+------------
How are you? | 0.98
Hello, world! | 0.95
This is PostgreSQL | 0.80
(3 rows)
๐ 3. Kubernetes์์ FastAPI Auto Scaling ์ ์ฉ
1๏ธโฃ Kubernetes HPA (Horizontal Pod Autoscaler) ํ์ฑํ
FastAPI๊ฐ ๋ง์ ์์ฒญ์ ์ฒ๋ฆฌํ ๋ ์๋์ผ๋ก ์ค์ผ์ผ๋งํ ์ ์๋๋ก ์ค์ ํฉ๋๋ค.
๐ HPA ์ ์ฉ์ ์ํ Metrics Server ์ค์น
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
๐ HPA๋ฅผ ์ํ FastAPI autoscaling.yaml ์์ฑ
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: fastapi-vector-search-hpa
namespace: fastapi
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: fastapi-vector-search
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
๐ HPA ์ ์ฉ
kubectl apply -f autoscaling.yaml -n fastapi
โ HPA ์ํ ํ์ธ
kubectl get hpa -n fastapi
โ ์ถ๋ ฅ ์์
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
fastapi-vector-search-hpa Deployment/fastapi-vector-search 50%/70% 1 5 2
HPA๊ฐ ํธ๋ํฝ์ ๋ฐ๋ผ ์๋์ผ๋ก FastAPI Pod์ ํ์ฅํ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๐ 4. ์ต์ข ์ ๋ฆฌ
โ HNSW ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ ๋ฒกํฐ ๊ฒ์ ์ฑ๋ฅ ์ต์ ํ
โ Cosine Similarity, L2 distance๋ฅผ ํ์ฉํ ๋ฒกํฐ ๊ฒ์
โ Kubernetes HPA๋ฅผ ์ด์ฉํ FastAPI Auto Scaling ์ ์ฉ
โ ๋ฒกํฐ ๊ฒ์ ์ฑ๋ฅ์ ๋ํญ ํฅ์์์ผ ๋๋์ ๋ฐ์ดํฐ์์๋ ๋น ๋ฅธ ๊ฒ์ ๊ฐ๋ฅ