# tonyoliver0208/social-network-microservices architecture

> Architecture diagram of the GitHub repository tonyoliver0208/social-network-microservices, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tonyoliver0208/social-network-microservices
- Repository: https://github.com/tonyoliver0208/social-network-microservices

## Overview

##### Project type and purpose
- This is a cloud-native, microservices-based social network backend. It uses an API Gateway to route and secure traffic, and four domain services (Identity, Media, Post, Search). Each service is containerized and communicates via REST and asynchronous messaging.

##### Top-level architecture and main components
a. API Gateway
- Exposes a unified HTTP interface
- Contains middleware for authentication, rate limiting, error handling
- Forwards requests to the appropriate service proxy (Identity, Media, Post, Search)
b. Identity Service
- Manages user accounts, tokens, refresh flows
- Exposes REST endpoints for login, signup, token refresh
- Stores users and refresh tokens in a database
c. Media Service
- Handles file uploads, transforms, storage via Cloudinary
- Emits and consumes events (e.g., media‐uploaded) via RabbitMQ
- Caches metadata in Redis
d. Post Service
- Manages user posts (CRUD)
- Emits change events (post-created, post-deleted) on RabbitMQ
- Uses Redis for caching hot data
e. Search Service
- Indexes posts and media to support search queries
- Subscribes to post/media events from RabbitMQ to update its index
- Persists search indices in a database
f. Shared infrastructure
- Databases (one per service)
- Redis instances (for rate-limiting, caching, sessions)
- RabbitMQ (event bus)
- External cloud service: Cloudinary

##### Relationships and data flows
- Client → API Gateway → appropriate service (REST)
- Identity Service ↔ Redis (session caching)
- Media/Post → RabbitMQ → Search Service (asynchronous event flow)
- Media Service → Cloudinary (external API)
- All services may log to a central logger or service (utility in each service)

##### Architectural patterns and principles
- Microservices: each domain is an autonomous service with its own data store
- API Gateway pattern for cross-cutting concerns (auth, rate limit, logging)
- Event-driven architecture using RabbitMQ for eventual consistency and decoupling
- Polyglot persistence: each service picks its optimal database + caching layer
- Twelve-factor app: containerized, environment-driven config, separate build and run, logs as streams

##### How to reflect this
- Edge Layer: Client / Mobile / Web
- API Layer: API Gateway
- Microservices Layer: Identity, Media, Post, Search
- Messaging Layer: RabbitMQ broker
- External Services: Cloudinary
- From client to API Gateway (HTTP)
- From API Gateway to each microservice (HTTP, label REST)
- From services to Redis/DB/Cloudinary
- Blue rectangles for services
d. Labels:
- Clearly name each component (e.g., “Identity Service”, “Post Service”)
- Protocols (HTTP, AMQP) and middleware responsibilities

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Edge Layer
    subgraph "Edge Layer"
        Client["Client\n(Web/Mobile)"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        APIGW["API Gateway"]:::gateway
    end

    %% Microservices Layer
    subgraph "Microservices Layer"
        Identity["Identity Service"]:::service
        Media["Media Service"]:::service
        Post["Post Service"]:::service
        Search["Search Service"]:::service
    end

    %% Messaging Layer
    subgraph "Messaging Layer"
        RabbitMQ["RabbitMQ Broker"]:::broker
    end

    %% Data Layer
    subgraph "Data Layer"
        IDDB["Identity DB"]:::db
        IDCache["Identity Redis"]:::cache
        MedDB["Media DB"]:::db
        MedCache["Media Redis"]:::cache
        PostDB["Post DB"]:::db
        PostCache["Post Redis"]:::cache
        SearchDB["Search DB"]:::db
    end

    %% External Layers
    subgraph "External Services"
        Cloudinary["Cloudinary"]:::external
    end

    %% Connections
    Client -->|"HTTP"| APIGW
    APIGW -->|"REST"| Identity
    APIGW -->|"REST"| Media
    APIGW -->|"REST"| Post
    APIGW -->|"REST"| Search

    Identity -->|"Reads/Writes"| IDDB
    Identity -->|"Cache Sessions"| IDCache

    Media -->|"Reads/Writes"| MedDB
    Media -->|"Cache Metadata"| MedCache
    Media -->|"publish events (media-uploaded)"| RabbitMQ
    Media -->|"uploads"| Cloudinary

    Post -->|"Reads/Writes"| PostDB
    Post -->|"Cache Hot Data"| PostCache
    Post -->|"publish events (post-created/deleted)"| RabbitMQ

    RabbitMQ -->|"subscribe events"| Search
    Search -->|"Reads/Writes"| SearchDB

    %% Click Events
    click APIGW "https://github.com/tonyoliver0208/social-network-microservices/tree/main/api-gateway/src"
    click Identity "https://github.com/tonyoliver0208/social-network-microservices/tree/main/identity-service/src"
    click Media "https://github.com/tonyoliver0208/social-network-microservices/tree/main/media-service/src"
    click Post "https://github.com/tonyoliver0208/social-network-microservices/tree/main/post-service/src"
    click Search "https://github.com/tonyoliver0208/social-network-microservices/tree/main/search-service/src"

    %% Styles
    classDef gateway fill:#1f77b4,stroke:#333,stroke-width:1px,color:#fff;
    classDef service fill:#17becf,stroke:#333,stroke-width:1px,color:#fff;
    classDef db fill:#ff7f0e,stroke:#333,stroke-width:1px,color:#fff;
    classDef cache fill:#2ca02c,stroke:#333,stroke-width:1px,color:#fff;
    classDef broker fill:#9467bd,stroke:#333,stroke-width:1px,color:#fff;
    classDef external fill:#8c564b,stroke:#333,stroke-width:1px,color:#fff;
```
