AI Model Wiki

    Skip to main content
    40 min
    Lesson 1 of 5

    Event-Driven AI Architecture

    From the course AI Webhooks & Event-Driven Pipelines

    Why Event-Driven AI?

    Instead of synchronous request-response, event-driven architectures process AI tasks asynchronously — enabling better scaling, reliability, and user experience.

    Synchronous vs. Event-Driven

    Synchronous:

    User → API → Wait 30s for AI → Response
    

    Problems: Timeouts, blocked UI, wasted resources

    Event-Driven:

    User → API → Queue → Response "Processing..."
                    ↓
              Worker → AI API → Store Result
                                  ↓
                             Notify User (webhook/poll)
    

    Common Triggers

    • User actions: File upload, form submission, message sent
    • Scheduled: Daily report generation, batch analysis
    • Data changes: New database record, file modified
    • External events: Email received, payment processed, API callback

    Architecture Components

    1. Event source: Where events originate (UI, API, schedule)
    2. Event bus/queue: Buffer and route events (Redis, SQS, Kafka)
    3. Workers: Process events and call AI APIs
    4. Storage: Store results (database, file storage)
    5. Notification: Inform clients of completion (webhooks, WebSocket, polling)

    Example: Content Moderation Pipeline

    Image Upload → Queue → Moderation Worker → {
      if (safe) → Store & Display
      if (flagged) → Review Queue → Human Review
      if (rejected) → Notify User & Delete
    }
    

    Common Pitfall: Processing AI Tasks Synchronously in API Request Handlers

    Calling the OpenAI API directly inside a web request handler blocks the response for 5–30 seconds. If the AI call fails or the user's browser closes, the task is lost. Always push AI tasks to a queue and respond immediately with a job ID:

    // Bad: blocks for 30 seconds
    app.post("/analyze", async (req, res) => {
      const result = await openai.chat.completions.create({ ... });  // 30s wait
      res.json(result);
    });
    
    // Good: enqueue and respond in <100ms
    app.post("/analyze", async (req, res) => {
      const jobId = crypto.randomUUID();
      await queue.add({ jobId, ...req.body });
      res.json({ jobId, status: "queued" });
    });
    

    Best Practice: Store Job Status in a Database

    Use a jobs table to track every AI task's lifecycle. This enables clients to poll for status, helps with debugging failures, and provides an audit log:

    // Track status transitions: queued → processing → completed/failed
    await db.jobs.create({ id: jobId, status: "queued", input: payload, createdAt: new Date() });
    // Update when processing starts, completes, or fails
    await db.jobs.update(jobId, { status: "processing", startedAt: new Date() });
    await db.jobs.update(jobId, { status: "completed", result, completedAt: new Date() });
    

    Named Product Reference: BullMQ for Node.js Queues

    BullMQ is the most popular Redis-based job queue for Node.js. It provides concurrency control, priority queues, scheduled jobs, retry with backoff, and a built-in UI (Bull Board) for monitoring. For teams already using Redis, BullMQ is the standard choice for AI task queuing without adding new infrastructure.

    Key Takeaways

    • •Event-driven architecture decouples AI processing from user requests
    • •Queues buffer events for reliable, scalable processing
    • •Workers process events asynchronously with AI APIs
    • •Notification systems inform clients when processing completes
    0 of 5 lessons marked complete

    Progress is kept in this browser only.