Skip to content

feat: compliance badge endpoint generates SVG but has no cache-control headers β€” every badge render hits the database, causing unnecessary load from README embeds refreshing every few secondsΒ #1475

Description

@prince-pokharna

πŸ› Problem Statement

AegisAI's recently shipped compliance badge feature (GET /badge/{system_id}/svg) generates a live SVG badge showing an AI system's compliance status. These badges are designed to be embedded in READMEs and documentation. However, README badge embeds are fetched by GitHub's Camo image proxy every time someone views the README β€” potentially hundreds of times per hour for popular repositories.

Without Cache-Control and ETag headers on the badge endpoint:

  1. Every badge render triggers a full database query to look up the AI system's compliance status
  2. GitHub Camo will re-fetch the badge on every page view rather than serving a cached version
  3. Under moderate traffic, this creates a significant unnecessary database load that scales with repository popularity

Proposed Fix

Add appropriate HTTP caching headers to the badge endpoint in backend/app/api/v1/badge.py:

from fastapi import APIRouter, Response
from datetime import timedelta
import hashlib

router = APIRouter()

@router.get("/{system_id}/svg")
async def get_compliance_badge(
    system_id: str,
    db: AsyncSession = Depends(get_db),
    response: Response = None,
):
    system = await db.get(AISystem, system_id)
    if not system:
        raise HTTPException(status_code=404)

    svg_content = generate_badge_svg(system.risk_level, system.compliance_status)

    # Generate ETag from content hash so clients only re-fetch when status changes
    etag = hashlib.md5(svg_content.encode()).hexdigest()

    response.headers["Content-Type"] = "image/svg+xml"
    response.headers["Cache-Control"] = "public, max-age=3600, s-maxage=3600"  # 1 hour
    response.headers["ETag"] = f'"{etag}"'
    response.headers["Vary"] = "Accept-Encoding"

    return Response(content=svg_content, media_type="image/svg+xml", headers=response.headers)

Also add Redis caching for the compliance status lookup:

CACHE_KEY = f"badge:{system_id}"
CACHE_TTL = 3600  # 1 hour

cached = await redis.get(CACHE_KEY)
if cached:
    return Response(content=cached, media_type="image/svg+xml")

# ... generate badge, then cache:
await redis.setex(CACHE_KEY, CACHE_TTL, svg_content)

Cache should be invalidated when the system's compliance status is updated via PATCH /ai-systems/{id}.

Files to Modify

File Change
backend/app/api/v1/badge.py Add Cache-Control, ETag headers + Redis cache
backend/app/api/v1/ai_systems.py Invalidate badge cache on status update

Suggested labels: enhancement, performance, backend, level: beginner

I would like to work on this. Could you please assign it to me?

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions