Prompt

How do I set up event-driven backend code for HTTP requests and queues?

Technology / Cloud Infrastructure1 observationLast seen Oct 26, 2026

Latest observation

Oct 26, 2026OpenAI APIWeb search: off

A good way to build event-driven backend code for both HTTP requests and queues is to separate:

  1. transport handling

    • HTTP controllers / route handlers
    • queue consumers / workers
  2. business logic

    • services / use-cases
    • domain events
  3. event dispatching

    • emit internal events after work succeeds
    • subscribe handlers to those events

That gives you one core flow that can be triggered by either an HTTP request or a queue message.


Recommended architecture

1) Keep HTTP and queue handlers thin

They should only:

  • validate input
  • convert incoming data into a command/request object
  • call the same application service
  • return/ack success or failure

2) Put real work in application services

Example responsibilities:

  • create user
  • charge payment
  • send notification
  • persist data

3) Publish domain/application events from services

Examples:

  • UserCreated
  • OrderPaid
  • EmailRequested

Then attach event listeners:

  • send email
  • update read model
  • publish to another queue
  • audit logging

Typical flow

HTTP request flow

  1. Request hits POST /orders
  2. Controller validates input
  3. Controller calls CreateOrderService
  4. Service saves order
  5. Service emits OrderCreated
  6. Event handlers run:
    • send confirmation email
    • enqueue downstream jobs
    • update analytics

Queue flow

  1. Worker receives message
  2. Worker validates payload
  3. Worker calls same service or use-case
  4. Service processes it
  5. Service emits events
  6. Event handlers run

Example structure

src/
  http/
    controllers/
    middleware/
  queue/
    consumers/
    jobs/
  application/
    services/
    commands/
    events/
  domain/
    models/
    events/
  infrastructure/
    db/
    message-bus/
    queue/
    mail/

Simple event-driven pattern

Event emitter

Use an in-process event bus for internal decoupling:

// eventBus.js
import { EventEmitter } from "node:events";

export const eventBus = new EventEmitter();

Service emits event

// orderService.js
import { eventBus } from "./eventBus.js";

export async function createOrder(data) {
  const order = await saveOrderToDb(data);

  eventBus.emit("order.created", {
    orderId: order.id,
    userId: order.userId,
  });

  return order;
}

Event listener handles side effects

// handlers.js
import { eventBus } from "./eventBus.js";

eventBus.on("order.created", async (event) => {
  await sendOrderConfirmationEmail(event.userId, event.orderId);
});

HTTP controller

app.post("/orders", async (req, res) => {
  const order = await createOrder(req.body);
  res.status(201).json(order);
});

Queue consumer

queueConsumer.on("message", async (msg) => {
  const payload = JSON.parse(msg.body);
  await createOrder(payload);
  msg.ack();
});

Important production considerations

1) Use durable queues for reliability

For real queue-based work, use something like:

  • RabbitMQ
  • SQS
  • Kafka
  • Redis streams / BullMQ

2) Make handlers idempotent

Queue messages may be delivered more than once.
Make sure processing the same message twice doesn’t break data.

Use:

  • unique message IDs
  • deduplication tables
  • idempotency keys

3) Don’t lose events on DB failure

If you write to the DB and emit an event, consider the outbox pattern:

  • save business data + event record in the same DB transaction
  • a separate publisher sends the event to the queue/bus

This avoids cases where the DB write succeeds but the event never gets published.

4) Separate sync and async concerns

  • HTTP request: usually return quickly
  • Queue: do longer tasks asynchronously
  • If the user needs immediate result, do it in the request path
  • If it can happen later, move it to a queue/event handler

5) Add retries and dead-letter handling

For queue consumers:

  • retry transient errors
  • send permanently failing messages to a dead-letter queue
  • log and alert on repeated failures

6) Use structured logging and tracing

Include:

  • request ID
  • correlation ID
  • message ID
  • user ID / order ID

This helps connect HTTP requests to downstream queue processing.


Practical pattern to follow

A very common setup is:

  • Controller → calls Use Case
  • Queue consumer → calls same Use Case
  • Use Case → persists data and emits event
  • Event handler → performs side effects

This keeps your system:

  • testable
  • reusable
  • easy to extend
  • not tied to one transport

If you want a concrete stack example

I can show this in:

  • Node.js/Express + BullMQ
  • Node.js/NestJS + RabbitMQ
  • Python/FastAPI + Celery
  • Go + HTTP + Kafka/SQS
  • Java/Spring Boot + Kafka

If you want, I can give you a complete working example in one of those stacks.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.