[📌 Часть 2/2] — 28 июля 2026 г. в 03:22:32.782
[📌 Часть 2/2] Динамическое ограничение токенов и cost-aware rate limiting в Ingress-слое для LLM-кластеров (продолжение) - redis-cli EVAL "local key = KEYS[1]; local window = tonumber(ARGV[1]); local limit = tonumber(ARGV[2]); now = redis.call('TIME')[1]; pipe = redis.pipeline(); pipe.zremrangebyscore(key, '-inf', now - window); pipe.zadd(key, now, tostring(now)); local count = pipe.zcard(key); pipe.expire(key, window * 2); return pipe.execute()" 1 "llm:ratelimit:{tenant_id}" 60 50 — базовый Lua-шаблон для Redis sorted sets с динамическим окном. - В nginx.conf секция location /v1/chat/completions { access_by_lua_block { ... } proxy_pass http://llm_upstream; } с переменной $dynamic_retry_after, вычисляемой через math.ceil(weight * current_queue_depth / throughput_pps). - Параметры весов: weight = (input_tokens * 0.25) + (max_tokens * 0.75) для моделей с heavy decoding overhead. Калибровать под vLLM continuous batching — при fill ratio > 0.8 уменьшать лимит на 15%. - Circuit breaker thresholds: error_rate > 0.05 for 30s или p95_latency > 2.5s → switch to 429 + header X-Circuit-State: OPEN. Автоматический half-open проброс 10% трафика для health-check. - Мониторинг через Prometheus custom exporter: rate_limit_tokens_total, llm_queue_depth_seconds, circuit_breaker_state{model="qwen2.5-7b"}. Grafana dashboard с алертом на rate(llm_rejected_tokens[5m]) > 0.1 * rate(llm_processed_tokens[5m]). - Интеграция с Service Mesh (Istio/Linkerd): переопределение timeout и retries на уровне sidecar, чтобы не дублировать логику в ingress. Envoy filter может читать Redis напрямую через Lua или gRPC-плагин для sub-millisecond decision time. - Fallback strategy: при OPEN состоянии circuit breaker возвращать 200 с пустым choices[] и заголовком X-Cache-Hit: true, если запрос попал в deduplication hash (md5(prompt+params)), иначе шунтировать на меньшую модель (например, Phi-3.5-mini) с явным заголовком X-Model-Degraded: true. Такой подход убирает bottleneck с LLM-воркеров, превращая Ingress в интеллектуальный budget controller. Клиенты получают предсказуемый SLA без ручного твикирования лимитов при смене модели или изменении batch size. #LLMOps #IngressController #RateLimiting #AIInfrastructure #OpenResty

