Capítulo 1: Escalado de aplicaciones RAG
Desafíos de escalado
| Desafío | Impacto | Solución |
|---|---|---|
| Millones de documentos | Indexación lenta | Procesamiento por lotes |
| Alto QPS | Tiempo de espera | Balanceo de carga |
| Vectores grandes | Límites de memoria | Sharding |
Bases de datos vectoriales distribuidas
# Milvus cluster configuration
from pymilvus import connections, Collection
# Connect to distributed cluster
connections.connect(
alias="default",
host="milvus-cluster",
port="19530"
)
# Data automatically distributed across nodes
Procesamiento asíncrono
import asyncio
from langchain.embeddings import OpenAIEmbeddings
async def batch_embed_async(texts, batch_size=100):
"""Embed texts in async batches"""
embeddings = OpenAIEmbeddings()
async def embed_batch(batch):
return await asyncio.to_thread(
embeddings.embed_documents, batch
)
tasks = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
tasks.append(embed_batch(batch))
results = await asyncio.gather(*tasks)
return [e for batch in results for e in batch]
Indexación basada en colas
# Producer: Add to queue
import redis
r = redis.Redis()
r.lpush("index_queue", document_json)
# Consumer: Process queue
while True:
doc = r.brpop("index_queue")
process_and_index(doc)
Recomendaciones de escalado
| Documentos | Arquitectura |
|---|---|
| <100K | Instancia única |
| 100K-1M | Base de datos replicada |
| 1M-10M | Clúster con sharding |
| >10M | Sistema distribuido |