Add load-shedding behavior under pressure for AI service (#621)#635
Merged
Cedarich merged 4 commits intoJun 27, 2026
Conversation
Return standardized 503 error envelopes and emit requests_shed_total metrics when memory, queue, or provider capacity is exhausted.
|
@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. |
Contributor
|
@gidadoabdullateef5 fix workflow |
|
@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! 🚀 |
Only shed humanitarian routes when configured providers have open circuit breakers, not when no providers are configured at all.
Contributor
Author
|
Hi Maintainer, kindly review the workflow |
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.
Contributor
Author
|
Hello Maintainer, the CI has been now fixed alongside with the conflict, please check again |
Cedarich
approved these changes
Jun 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 dedicatedrequests_shed_totalPrometheus counter so shed requests are observable independently of general traffic metrics.Load shedding triggers on three conditions:
memory/v1/*routes)queue_fullcreate_task()broker_unavailablecreate_task()provider_downInfrastructure routes (
/health,/ai/metrics, docs, legacy redirects) remain exempt so load balancers and scrapers stay healthy during overload.Changes
services/load_shedder.py— central shed evaluation, response builder, queue depth probeLoadShedErrorexception + global handler returning 503 envelope withRetry-After: 30metrics.py—requests_shed_total{reason,method,endpoint}counter +celery_queue_depthgaugeconfig.py—load_shed_memory_threshold_percent,load_shed_max_celery_queue_depthmain.py— middleware delegates to load shedder (replaces raw{"detail": ...}503)tasks.py—ensure_queue_capacity()before Celery enqueuehumanitarian_verification.py—all_providers_unavailable()for provider-down detectiontest_load_shedder.py+ envelope/throttle coverage updatesAcceptance criteria
SERVICE_OVERLOADED, reason indetails)requests_shed_total)Test plan
pytest tests/test_load_shedder.py tests/test_error_envelope.py::TestServiceOverloaded— 14 passed/healthreturns 200 even when memory pressure is simulated/v1/ai/inferencereturns 503 + envelope when queue depth exceeds threshold/ai/metricsexposesrequests_shed_totalafter a shed eventRetry-After: 30header is present on 503 responsesMonitoring
Suggested alert: