System Overview

Detailed overview of the Staque IO system architecture and component interactions.

System Layers

Presentation

  • Next.js React Components
  • Tailwind CSS
  • Context Providers
  • Client-side State

Application

  • Next.js API Routes
  • Business Logic
  • Authentication
  • Authorization

Integration

  • AWS SDK Integration
  • NVIDIA NIM API
  • OpenAI API
  • Pricing Services

Data

  • PostgreSQL Database
  • Connection Pooling
  • JSONB Storage
  • Transactional Queries

Request Processing Flow

┌─────────────┐
│   Browser   │
│  (Client)   │
└──────┬──────┘
       │ 1. HTTP Request
       │    (includes JWT token)
       ▼
┌─────────────────────────────────┐
│      Next.js Frontend           │
│  • React Components             │
│  • Client-side Routing          │
│  • State Management (Context)   │
└──────┬──────────────────────────┘
       │ 2. API Call
       │    (fetch with auth header)
       ▼
┌─────────────────────────────────┐
│    Next.js API Routes           │
│  • extractUserFromRequest()     │
│  • JWT Verification             │
│  • Role-based Authorization     │
└──────┬──────────────────────────┘
       │ 3. Authenticated Request
       ├─────────────────┬─────────────────┐
       ▼                 ▼                 ▼
┌──────────────┐  ┌──────────────┐  ┌──────────────┐
│  Database    │  │  AWS APIs    │  │  External    │
│  Queries     │  │  • Bedrock   │  │  APIs        │
│              │  │  • SageMaker │  │  • NVIDIA    │
│              │  │  • Pricing   │  │  • OpenAI    │
└──────┬───────┘  └──────┬───────┘  └──────┬───────┘
       │                 │                 │
       └─────────────────┴─────────────────┘
                      │ 4. Aggregated Data
                      ▼
          ┌──────────────────────┐
          │  Response Formatter  │
          │  • JSON Serialization│
          │  • Error Handling    │
          └──────────┬───────────┘
                     │ 5. HTTP Response
                     ▼
          ┌──────────────────────┐
          │      Client          │
          │  • Update State      │
          │  • Re-render UI      │
          └──────────────────────┘

Core Subsystems

Authentication & Authorization System

┌──────────────────────────────────────────────┐
│         Authentication Flow                  │
├──────────────────────────────────────────────┤
│                                              │
│  Login Request → Database Query              │
│       ↓                                      │
│  Password Verification (bcrypt)              │
│       ↓                                      │
│  JWT Token Generation                        │
│       ↓                                      │
│  Token Storage (Client)                      │
│       ↓                                      │
│  Subsequent Requests (with token)            │
│       ↓                                      │
│  Token Extraction & Verification             │
│       ↓                                      │
│  Role-based Access Control                   │
│                                              │
└──────────────────────────────────────────────┘

Model Deployment Pipeline

┌─────────────────────────────────────────────────┐
│           Deployment Pipeline                   │
├─────────────────────────────────────────────────┤
│                                                 │
│  1. User selects model & configuration          │
│       ↓                                         │
│  2. Validation (instance type, region, etc.)    │
│       ↓                                         │
│  3. Dry run simulation (optional)               │
│       ↓                                         │
│  4. Platform-specific deployment:               │
│     ┌──────────────────────────────────┐       │
│     │ Bedrock: Verify model access     │       │
│     │ SageMaker: Create infrastructure │       │
│     │ NVIDIA NIM: Verify API access    │       │
│     └──────────────────────────────────┘       │
│       ↓                                         │
│  5. Database record creation                    │
│       ↓                                         │
│  6. Model configuration setup                   │
│       ↓                                         │
│  7. Status monitoring (polling)                 │
│       ↓                                         │
│  8. Ready for chat interactions                 │
│                                                 │
└─────────────────────────────────────────────────┘

Conversation & Chat System

┌─────────────────────────────────────────────────┐
│          Chat Interaction Flow                  │
├─────────────────────────────────────────────────┤
│                                                 │
│  User Message → API Endpoint                    │
│       ↓                                         │
│  Load Conversation Thread from DB               │
│       ↓                                         │
│  Retrieve System Prompt                         │
│       ↓                                         │
│  Build Context (system + history + new msg)     │
│       ↓                                         │
│  Platform-specific API call:                    │
│     ┌──────────────────────────────────┐       │
│     │ Bedrock: InvokeModel             │       │
│     │ SageMaker: InvokeEndpoint        │       │
│     │ NVIDIA NIM: /v1/chat/completions │       │
│     └──────────────────────────────────┘       │
│       ↓                                         │
│  Parse Response & Extract Tokens                │
│       ↓                                         │
│  Calculate Cost (tokens × pricing)              │
│       ↓                                         │
│  Update Thread in DB:                           │
│     • Append user & assistant messages          │
│     • Update token counters                     │
│     • Update cost totals                        │
│     • Update request count                      │
│       ↓                                         │
│  Return Response to Client                      │
│                                                 │
└─────────────────────────────────────────────────┘

Data Flow Patterns

Read Path (Data Retrieval)

  • Optimized for fast reads with indexed queries
  • JSONB columns for flexible metadata retrieval
  • Aggregated statistics computed at write time
  • Pagination support for large datasets

Write Path (Data Persistence)

  • Transactional writes for data consistency
  • Atomic updates for conversation threads
  • Denormalized fields for query performance
  • Timestamp tracking (created_at, updated_at)

Scalability Architecture

Horizontal Scaling

  • Stateless API Layer: Next.js API routes can be deployed across multiple instances
  • Database Connection Pooling: Efficiently manages concurrent connections
  • CDN for Static Assets: Reduces load on application servers
  • Load Balancing: Distributes traffic across application instances

Vertical Scaling

  • Database Resources: Increase CPU, memory, and storage as needed
  • Connection Pool Size: Adjust based on concurrent user load
  • Application Instance Size: Scale compute resources for API layer

Resilience & Fault Tolerance

Error Handling Strategy

try {
  // API operation
  const result = await performOperation()
  return { success: true, data: result }
} catch (error) {
  // Log error details
  console.error('Operation failed:', error)
  
  // Return standardized error response
  return {
    success: false,
    error: 'User-friendly error message',
    details: error.message  // For debugging
  }
}

Failure Recovery

  • Database Reconnection: Automatic retry with exponential backoff
  • AWS API Retries: Built-in SDK retry logic with exponential backoff
  • Transaction Rollback: Failed operations don't leave partial state
  • Graceful Degradation: System continues to function even if some features fail

🔍 Performance Metrics

Target Performance:

  • API Response Time: < 200ms (excluding AI model latency)
  • Database Query Time: < 50ms
  • AI Model Response: 0.5-3 seconds (varies by model)
  • Page Load Time: < 1 second (first contentful paint)
  • Concurrent Users: 1000+ (with proper scaling)