Skip to content

Add load-shedding behavior under pressure for AI service (#621)#635

Merged
Cedarich merged 4 commits into
Pulsefy:mainfrom
gidadoabdullateef5:feat/621-load-shedding
Jun 27, 2026
Merged

Add load-shedding behavior under pressure for AI service (#621)#635
Cedarich merged 4 commits into
Pulsefy:mainfrom
gidadoabdullateef5:feat/621-load-shedding

Conversation

@gidadoabdullateef5

@gidadoabdullateef5 gidadoabdullateef5 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Closes #621


Summary

Implements load-shedding for the AI service when the system is under pressure, addressing #621.

When overloaded, the service now returns HTTP 503 with the standardized ErrorEnvelope (code: SERVICE_OVERLOADED) and emits a dedicated requests_shed_total Prometheus counter so shed requests are observable independently of general traffic metrics.

Load shedding triggers on three conditions:

Condition Reason label Where checked
RAM/VRAM above threshold (default 90%) memory Request middleware (all /v1/* routes)
Celery queue at capacity (default ≥ 100) queue_full Middleware + create_task()
Redis/broker unreachable broker_unavailable Middleware + create_task()
All configured LLM providers circuit-open provider_down Middleware on humanitarian verify route

Infrastructure routes (/health, /ai/metrics, docs, legacy redirects) remain exempt so load balancers and scrapers stay healthy during overload.

Changes

  • New services/load_shedder.py — central shed evaluation, response builder, queue depth probe
  • New LoadShedError exception + global handler returning 503 envelope with Retry-After: 30
  • metrics.pyrequests_shed_total{reason,method,endpoint} counter + celery_queue_depth gauge
  • config.pyload_shed_memory_threshold_percent, load_shed_max_celery_queue_depth
  • main.py — middleware delegates to load shedder (replaces raw {"detail": ...} 503)
  • tasks.pyensure_queue_capacity() before Celery enqueue
  • humanitarian_verification.pyall_providers_unavailable() for provider-down detection
  • Teststest_load_shedder.py + envelope/throttle coverage updates

Acceptance criteria

  • Returns 503 with clear error envelope (SERVICE_OVERLOADED, reason in details)
  • Emits metric for shed requests (requests_shed_total)

Test plan

  • pytest tests/test_load_shedder.py tests/test_error_envelope.py::TestServiceOverloaded — 14 passed
  • Verify /health returns 200 even when memory pressure is simulated
  • Verify /v1/ai/inference returns 503 + envelope when queue depth exceeds threshold
  • Verify /ai/metrics exposes requests_shed_total after a shed event
  • Confirm Retry-After: 30 header is present on 503 responses

Monitoring

Suggested alert:

rate(requests_shed_total[5m]) > 0

Return standardized 503 error envelopes and emit requests_shed_total metrics when memory, queue, or provider capacity is exhausted.
@vercel

vercel Bot commented Jun 24, 2026

Copy link
Copy Markdown

@gidadoabdullateef5 is attempting to deploy a commit to the Cedarich's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Cedarich

Copy link
Copy Markdown
Contributor

@gidadoabdullateef5 fix workflow

@drips-wave

drips-wave Bot commented Jun 25, 2026

Copy link
Copy Markdown

@gidadoabdullateef5 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Only shed humanitarian routes when configured providers have open circuit breakers, not when no providers are configured at all.
@gidadoabdullateef5

Copy link
Copy Markdown
Contributor Author

Hi Maintainer, kindly review the workflow

@Cedarich

Copy link
Copy Markdown
Contributor

@gidadoabdullateef5 fix CI

Resolve merge conflicts with upstream cache changes and stop shedding
job routes when Redis is unreachable. Only shed on measured queue_full so
validation and OCR job handlers run normally without a local broker.
@gidadoabdullateef5

Copy link
Copy Markdown
Contributor Author

Hello Maintainer, the CI has been now fixed alongside with the conflict, please check again

@Cedarich Cedarich merged commit c21f536 into Pulsefy:main Jun 27, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Load-Shedding Behavior Under Pressure

2 participants