The Trinity Beast Infrastructure — Message Queue Configuration

SQS-powered usage log pipeline — fire-and-forget from the hot path, guaranteed delivery to Aurora via Lambda consumer.

Region: us-east-2 (Ohio) Queue: trinity-beast-queued-usage-logs Consumer: trinity-beast-queued-writer Version: v15 Updated: May 2026

Table of Contents

  1. Overview
  2. Architecture
  3. Application Parameters
  4. Environment Variables
  5. Profile Integration
  6. Cost
  7. Extensibility
  8. IAM Permissions

1. Overview

Problem

During Run 17 stress testing (746,374 RPS, 1.34B requests), the in-process usage log writer's worker pool saturated under extreme load. Background work was silently shed — only ~12K usage_logs appeared in Aurora after 1.34 billion requests. The price response was never affected (0ms), but usage logging completeness was sacrificed.

Solution

Decouple usage logging from the hot path using SQS. The LPO server sends a fire-and-forget message to SQS for every price request. A purpose-built Go Lambda (trinity-beast-queued-writer) consumes batches from the queue and batch-inserts into Aurora. Zero logs shed. Zero latency added to the price response.

Key Guarantee: The price hot path remains at 0ms additional latency. SQS send is non-blocking — if the buffer is full, the message is dropped rather than blocking the response. Under normal and production loads, no messages are dropped.

Data Flow

Diagram 1.1 — SQS Usage Log Pipeline
graph LR
    Client["Client"] -->|"price request"| LPO["LPO Server"]
    LPO -->|"0ms response"| Client
    LPO -->|"fire-and-forget"| SQS["SQS Queue
trinity-beast-queued-usage-logs"] SQS -->|"batch of 100"| Lambda["Lambda Consumer
trinity-beast-queued-writer"] Lambda -->|"batch INSERT"| Aurora["Aurora
usage_logs table"]

2. Architecture

Producer: SQSProducer

Source: internal/database/sqs_producer.go

Message Envelope

All messages use a standard envelope format. The type field routes processing in the Lambda consumer:

{
  "type": "usage_log",
  "timestamp": "2026-05-02T15:30:00.000Z",
  "node": "BeastMain",
  "payload": {
    "api_key_id": "ak_live_abc123",
    "asset": "BTC",
    "source": "binance",
    "response_time_us": 42,
    "cache_tier": "L1_MEMORY"
  }
}
Extensible: Add new message types without changing queue or Lambda infrastructure. The envelope pattern supports any write type — just add a new type value and a corresponding handler case.

Consumer: trinity-beast-queued-writer Lambda

PropertyValue
Runtimeprovided.al2023 (Go binary)
Memory256 MB
RoutingRoutes on message type field
Write StrategyBatch INSERT into Aurora
Side EffectUpdates current_usage counters per API key

Queue: trinity-beast-queued-usage-logs

PropertyValue
TypeStandard (not FIFO — ordering not required for usage logs)
Message Retention4 days
Visibility Timeout60 seconds
Event Source Mapping — Batch Size100
Event Source Mapping — Max Batching Window5 seconds

3. Application Parameters

All 4 SQS parameters are stored in the application_parameters table and are runtime-adjustable — no redeploy needed.

Parameter Default Range Purpose
sqs_batch_size 10 1–10 Messages per SQS SendMessageBatch call
sqs_flush_ms 100 >0 Flush interval in milliseconds
sqs_buffer_size 50,000 >0 Channel buffer capacity before dropping
sqs_timeout_ms 3,000 >0 Per-batch SQS API call timeout
Note: All parameters are runtime-adjustable via the application_parameters table — no redeploy needed. Changes take effect on the next flusher tick.

4. Environment Variables

Variable Value Set On
SQS_USAGE_LOG_QUEUE_URL https://sqs.us-east-2.amazonaws.com/211998422884/trinity-beast-queued-usage-logs ECS task definitions
DB_SECRET_NAME trinity-beast-secrets Lambda function

5. Profile Integration

The 4 SQS parameters are included in all 16 system profiles. Each profile tunes the queue behavior for its intended workload:

Profile Category SQSFlushMs SQSBufferSize Intent
Demo / Debug 500 10,000 Relaxed — low throughput, easy to observe
Production 100 50,000 Standard — balanced throughput and cost
Stress 50 100,000 Aggressive — maximum throughput for load testing

6. Cost

SQS Rate
$0.40 / 1M msgs
Normal Production
~$1/month
Stress Test (1.34B msgs)
~$536 one-time
Lambda Cost
fractions of ¢
Scenario Volume Cost
Normal production ~2.5M messages/month ~$1/month
Stress test (746K RPS × 30 min) 1.34B messages ~$536 one-time
Lambda (256MB, ~50ms per batch) Per million logs Fractions of a cent

7. Extensibility

The message envelope pattern supports future write types without infrastructure changes:

Adding a New Message Type

  1. Add h.SQSProducer.Send("analytics_event", data) in any handler
  2. Add case "analytics_event": in the Lambda consumer
  3. Same queue, same Lambda, same IAM — just a new case statement
Zero infrastructure changes: No new queues, no new Lambdas, no IAM policy updates. The envelope pattern means the pipeline is type-agnostic — it routes on the type field inside the message body.
// Producer side — any handler
h.SQSProducer.Send("analytics_event", AnalyticsPayload{
    Event:  "page_view",
    Path:   "/pricing",
    UserID: session.UserID,
})

// Consumer side — Lambda handler
switch msg.Type {
case "usage_log":
    batchInsertUsageLogs(msg.Payload)
case "analytics_event":
    batchInsertAnalytics(msg.Payload)
}

8. IAM Permissions

ECS Task Role (ecsTaskRole)

Permission Resource
sqs:SendMessage arn:aws:sqs:us-east-2:211998422884:trinity-beast-queued-usage-logs
sqs:SendMessageBatch arn:aws:sqs:us-east-2:211998422884:trinity-beast-queued-usage-logs

Lambda Role (trinity-beast-queued-writer-role)

Permission Resource
sqs:ReceiveMessage arn:aws:sqs:us-east-2:211998422884:trinity-beast-queued-usage-logs
sqs:DeleteMessage arn:aws:sqs:us-east-2:211998422884:trinity-beast-queued-usage-logs
sqs:GetQueueAttributes arn:aws:sqs:us-east-2:211998422884:trinity-beast-queued-usage-logs
secretsmanager:GetSecretValue arn:aws:secretsmanager:us-east-2:211998422884:secret:trinity-beast-secrets*
AWSLambdaBasicExecutionRole AWS managed policy (CloudWatch Logs)