Skip to content

System Architecture

PCBZ edited this page Nov 9, 2025 · 2 revisions

System Architecture

graph TB
    Users[👤 Users] --> ALB[Application Load Balancer]
    
    ALB --> AGW[API Gateway Service<br/>ECS Fargate<br/>Request Router]
    

    subgraph "Microservices Layer"
        AGW --> US[User Service<br/>ECS Fargate]
        AGW --> SGS[Social Graph Service<br/>ECS Fargate] 
        AGW --> PS[Post Service<br/>ECS Fargate]
        AGW --> TS[Timeline Service<br/>ECS Fargate]
    end
    
    subgraph "Data Layer"
        US --> RDS[(RDS PostgreSQL<br/>User Profiles<br/>Authentication)]
        SGS --> Neptune[(Neptune Graph DB<br/>Social Relationships<br/>Follow/Following)]
        PS --> DDB1[(DynamoDB<br/>Posts Storage)]
        TS --> DDB2[(DynamoDB<br/>Timeline Cache)]
    end
    
    subgraph "Message Queue Layer"
        PS --> SNS[SNS Topic<br/>Post Created Events]
        SNS --> SQS[SQS Queue<br/>Timeline Updates]
        SQS --> TS
    end
Loading

Push Model Data Flow

sequenceDiagram
    participant U as User
    participant PS as Post Service
    participant SNS as SNS Topic
    participant SQS as SQS Queue
    participant TS as Timeline Service
    participant TimelineDB as DynamoDB Timeline
    
    Note over U,TimelineDB: Post Creation Flow (Async)
    
    U->>PS: POST /posts
    PS->>U: 201 Created (immediate response)
    
    Note over PS,TimelineDB: Async Fan-out Process
    PS->>SNS: Publish post created event
    SNS->>SQS: Queue timeline update message
    SQS->>TS: Consume message (async)
    
    loop For each follower
        TS->>TimelineDB: Write post to follower's timeline
    end
    
    TimelineDB-->>TS: Timeline updates completed
    
    Note over U,TimelineDB: Timeline Retrieval Flow (Fast Read)
    
    U->>TS: GET /timeline/{user_id}
    TS->>TimelineDB: Query user's timeline table
    TimelineDB-->>TS: Return pre-computed timeline
    TS->>U: 200 OK with timeline data
Loading

Pull Model Data Flow

sequenceDiagram
    participant U as User
    participant PS as Post Service
    participant TS as Timeline Service
    participant PostDB as DynamoDB Posts
    participant SS as Social Graph Service
    
    Note over U, SS: POST Creation (Fastest Response)
    U->>PS: POST /posts
    PS->>PostDB: Store post data only
    PostDB-->>PS: Post stored
    PS->>U: 201 Created ✅ (fastest response)
    
    Note over U, SS: Timeline Read (Slower - Real-time Aggregation)
    U->>TS: GET /timeline/{user_id}
    TS->>SS: Get users this user follows
    SS-->>TS: Return following list
    
    loop For each followed user
        TS->> PS: Get recent posts
        PS-->>TS: Return posts
    end
    
    TS->>TS: Merge & sort posts by timestamp
    TS->>U: 200 OK ⏳ (slower response)
Loading

Hybrid Data Flow

sequenceDiagram
    participant U as User (Celebrity)
    participant U2 as User (Regular)
    participant PS as Post Service
    participant SNS as SNS Topic
    participant SQS as SQS Queue
    participant TS as Timeline Service
    participant PostDB as DynamoDB Posts
    participant SS as Social Graph Service
    participant TimelineDB as DynamoDB Timeline
    
    Note over U,TimelineDB: Celebrity POST (Pull Strategy)
    U->>PS: POST /posts (1M followers)
    PS->>PS: Check user type: Celebrity
    PS->>PostDB: Store post data only
    PostDB-->>PS: Post stored
    PS->>U: 201 Created ✅ (fast - no fan-out)
    
    Note over U2,TimelineDB: Regular User POST (Push Strategy)  
    U2->>PS: POST /posts (50 followers)
    PS->>PS: Check user type: Regular
    PS->>PostDB: Store post data
    PS->>SNS: Publish post created event
    SNS->>SQS: Queue timeline update
    PS->>U2: 201 Created ✅ (fast response)
    
    SQS->>TS: Consume message (async)
    TS->> SS: Get followers (50 users)
    loop For each follower
        TS->>TimelineDB: Write to timeline (push)
    end
    
    Note over U,TimelineDB: Timeline Read (Adaptive)
    U2->>TS: GET /timeline (follows celebrities)
    TS->>TS: Check following list composition
    
    alt Mixed Timeline Strategy
        TS->>TimelineDB: Get cached timeline
        TS->>PS: Get recent posts
        TS->>TS: Merge cached + real-time data
    end
    
    TS->>U2: 200 OK ⚡ (optimized response)
Loading

Clone this wiki locally