Skip to content

High-Level Design (HLD) ​

Version: 1.0.0 Last Updated: 2025-01-25 Status: Approved

Executive Summary ​

Purpose ​

A TypeScript/Express backend serving a personal portfolio website with:

  • Content Management System (CMS) for portfolio content
  • AI-powered chat for visitor engagement
  • MCP (Model Context Protocol) server for AI tooling integration

Goals ​

  1. Showcase Backend Skills: Demonstrate proficiency in modern backend patterns
  2. Production-Ready: Deployable with proper observability, security, and resilience
  3. Maintainable: Clean architecture with clear separation of concerns
  4. Extensible: Easy to add new features without major refactoring

Scope ​

In ScopeOut of Scope
REST API for content and chatUser authentication (admin-only API key)
AI chat with rate limitingMulti-tenant support
MCP server integrationReal-time features (WebSocket)
Content versioningMedia file storage
Observability (logs, metrics, traces)Full-text search engine

System Context ​

C4 Level 1: System Context Diagram ​

External Systems ​

SystemPurposeProtocolNotes
TursoPrimary database (libSQL/SQLite)HTTPSEdge-replicated SQLite
RedisCaching, rate limiting stateTCPOptional, falls back to memory
LLM ProviderAI chat responsesHTTPSOpenAI-compatible API
OTLP CollectorTrace collectionHTTPSOptional, for observability

Actors ​

ActorDescriptionInteraction
Portfolio VisitorAnonymous users viewing the portfolioRead content, use chat
AdminPortfolio owner managing contentCRUD operations via API key
AI ToolsClaude Desktop, other MCP clientsQuery portfolio via MCP protocol

Container Architecture ​

C4 Level 2: Container Diagram ​

Container Responsibilities ​

ContainerTechnologyResponsibility
Express AppExpress.jsHTTP API, routing, middleware
ServicesTypeScriptBusiness logic, orchestration
RepositoriesDrizzle ORMData access abstraction
InfrastructureVariousCross-cutting concerns
MCP ServerMCP SDKAI tool integration (stdio + HTTP transports)

TIP

For detailed rationale on architectural decisions, see the Architecture Decision Records.

Data Flow ​

Content Read Flow ​

Chat Message Flow ​

Chat Message Flow with Tools ​

The chat service uses OpenAI function calling to query portfolio data:

Key components:

ComponentDescription
chatToolDefinitionsOpenAI function schemas for list_content, get_content, search_content
executeToolCall()Executes tool calls and returns JSON results
Tool loopUp to 5 iterations until LLM has enough context
Core toolsShared implementation with MCP server (see ADR-008)

Non-Functional Requirements ​

Performance ​

MetricTargetMeasurement
API Response Time (p95)< 200msPrometheus histogram
API Response Time (p99)< 500msPrometheus histogram
Chat Response Time (p95)< 5sPrometheus histogram
Content Bundle Load< 100msPrometheus histogram
Throughput100 req/sLoad testing

Availability ​

MetricTargetStrategy
Uptime99.9%Health checks, graceful degradation
Recovery Time< 30sContainer restart, circuit breaker
Data Durability99.99%Turso replication

Scalability ​

DimensionCurrentPath to Scale
Concurrent Users10-50Single instance sufficient
Content Items1000sSQLite handles well
Chat Sessions100s/dayRate limiting protects resources
Horizontal ScaleN/ARedis enables multi-instance

Security ​

ConcernMitigation
API AuthenticationAPI key for admin routes
Input ValidationZod schemas, sanitization
Rate LimitingToken bucket per IP
Data PrivacyPII detection and sanitization in LLM responses
TransportHTTPS only in production
HeadersHelmet security headers

Security Architecture ​

Authentication & Authorization ​

Data Protection ​

Data TypeProtectionStorage
Admin API KeyEnvironment variableNever logged
User MessagesStored as-is, output sanitizedPII redacted from LLM responses
IP AddressesHashed (SHA-256)Logs, rate limit keys
Visitor IDsClient-generatedSession tracking only

Deployment Architecture ​

Container Deployment ​

Health Checks ​

ProbeEndpointPurposeInterval
Liveness/api/health/liveIs process alive?30s
Readiness/api/health/readyCan accept traffic?10s
Startup/api/health/startupHas initialization completed?5s
Metrics/api/metricsPrometheus scrape (requires X-Admin-Key)15s

Background Jobs ​

JobScheduleDescription
Session cleanupHourlyExpires inactive chat sessions and updates their status

Monitoring & Observability ​

Three Pillars ​

Key Metrics ​

CategoryMetricLabelsAlert Threshold
HTTPhttp_requests_totalmethod, path, status-
HTTPhttp_request_duration_secondsmethod, path, statusp99 > 1s
HTTPhttp_errors_totalstatus, code> 10/min
Chatchat_messages_totalrole-
Chatchat_tokens_totaltype> 10k/hour
LLMllm_requests_totalprovider, statuserrors > 5/min
LLMllm_request_duration_secondsproviderp95 > 10s
Circuitcircuit_breaker_statenamestate = 2 (open)
Raterate_limit_hits_total-> 100/hour
Cachecache_hits_totalkey_prefix-
Cachecache_misses_totalkey_prefixratio < 0.5

Log Structure ​

json
{
  "level": "info",
  "time": 1706184000000,
  "context": "http",
  "requestId": "550e8400-e29b-41d4-a716-446655440000",
  "traceId": "abc123...",
  "method": "POST",
  "path": "/api/v1/chat",
  "statusCode": 200,
  "duration": 1523,
  "ip": "sha256:...",
  "userAgent": "Mozilla/5.0..."
}

Appendix ​

Glossary ​

TermDefinition
ADRArchitecture Decision Record
Circuit BreakerPattern to prevent cascading failures
CQRSCommand Query Responsibility Segregation
LLMLarge Language Model
MCPModel Context Protocol
OTLPOpenTelemetry Protocol
PIIPersonally Identifiable Information
Token BucketRate limiting algorithm

References ​

Released under the MIT License.