# mamanullah7/listenbrainz-server architecture

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

- Interactive diagram: https://gitdiagram.com/mamanullah7/listenbrainz-server
- Repository: https://github.com/mamanullah7/listenbrainz-server

## Overview

##### Project type and purpose
- This is a full-stack, microservices-style web platform (“ListenBrainz Server”) for tracking and analyzing music-listening data.
- It consists of:
– A React/TypeScript Single-Page Application (frontend)
– Multiple Python WSGI/worker services (backend)
– Spark batch pipelines for heavy data processing
– A Timescale/PostgreSQL database (and auxiliary stores)
– Messaging infrastructure for real-time updates and background tasks

##### Main components
A. Client & UI Layer
– Web browser
– React/TypeScript SPA served as static assets via Nginx
B. Edge & Load Balancing
– Nginx (TLS termination, static file hosting, routing to API)
– Consul (service discovery) + consul-template for dynamic config
C. API Layer
– uWSGI workers hosting the Python webserver (listenbrainz/webserver + labs_api)
– RESTful endpoints, WebSockets endpoint for real-time dispatch
D. Microservices & Workers
– Metadata-cache services (Spotify, SoundCloud, Apple, Last.fm)
– Importers (lastfm_importer, spotify_reader, mbid_mapping_writer)
– Background tasks service (cron, RabbitMQ consumers)
– Timescale-writer service (writes Spark output to TimescaleDB)
– TROi and other domain-specific jobs
E. Batch-Processing Cluster
– Spark cluster (docker-spark image)
– HDFS or object storage for dumps
– Spark jobs: listens processing, incremental stats, recommendations, missing-mb data, fresh-releases, year-in-music, similarity models
F. Data Stores
– Primary PostgreSQL + Timescale extension (user/listen/stat tables)
– CouchDB (legacy/compat dumps)
– Redis (optional caching, rate limiting)
– RabbitMQ (pub/sub of listen events, task queue)
G. External Services
– MusicBrainz API
– Spotify, SoundCloud, Apple Music APIs
– Last.fm API

##### Relationships & interactions
– Browser → Nginx: fetch static UI; WebSocket handshake
– Browser → Nginx → uWSGI API: REST calls for data & auth
– API → PostgreSQL/Timescale: read/write user/listen data
– API → RabbitMQ: publish events (e.g., new listen)
– WebSocket worker → RabbitMQ → Browser: real-time listen dispatch
– Cron jobs → RabbitMQ or direct DB migrations (admin/sql scripts)
– Metadata-cache services → external APIs → PostgreSQL
– Spark cluster jobs consume dumps from HDFS, produce metrics → write to TimescaleDB via timescale_writer
– MBID-mapping service → reads open data dumps, writes mapping tables in DB

##### Architectural patterns & principles
– Layered separation: Presentation (UI), Application (API), Data (DB), Batch (Spark), Integration (external services)
– Microservices: independently deployable workers for caching, import, mapping, stats
– Event-driven: RabbitMQ broker mediates real-time updates and background processing
– Batch vs. online: Spark for large-scale analytics; API for low-latency user queries
– Infrastructure as code: Dockerfiles, docker-compose, Consul templates, GitHub Actions CI/CD

##### Detailed breakdown for the principal
– From Nginx, branch:
- Static → React SPA
- /api → uWSGI API container → Python webserver module
- /ws → WebSocket container → RabbitMQ listener
– At the bottom, place data stores: TimescaleDB, CouchDB, Redis. Show connections from API, services, Spark writer.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layers
    subgraph "User/Browser Layer" 
        direction TB
        Browser["Web Browser"]:::blue
    end

    subgraph "Edge & Load Balancing" 
        direction TB
        Nginx["Nginx (TLS Termination, Static Host)"]:::green
        Consul["Consul + consul-template"]:::green
    end

    subgraph "API & Real-Time Layer"
        direction TB
        APIGW["uWSGI Python API"]:::yellow
        WebSocket["WebSocket Dispatcher"]:::yellow
    end

    subgraph "Message Broker"
        direction TB
        RabbitMQ["RabbitMQ (AMQP)"]:::purple
    end

    subgraph "Microservices & Workers"
        direction TB
        Metadata["Metadata-Cache Services"]:::yellow
        Importers["Importers"]:::yellow
        Background["Background Tasks & Cron"]:::yellow
        TimescaleWriter["Timescale-Writer Service"]:::yellow
        TROi["TROi Domain Jobs"]:::yellow
    end

    subgraph "Batch Processing (Spark)" 
        direction TB
        SparkCluster["Spark Cluster"]:::orange
        HDFS["HDFS / Object Storage"]:::orange
    end

    subgraph "Data Stores"
        direction TB
        Postgres["PostgreSQL + TimescaleDB"]:::purple
        CouchDB["CouchDB"]:::purple
        Redis["Redis"]:::purple
    end

    subgraph "External Services"
        direction TB
        MBAPI["MusicBrainz API"]:::cloud
        SpotifyAPI["Spotify API"]:::cloud
        SoundCloudAPI["SoundCloud API"]:::cloud
        AppleAPI["Apple Music API"]:::cloud
        LastfmAPI["Last.fm API"]:::cloud
    end

    subgraph "Infrastructure & CI/CD"
        direction TB
        DockerInfra["Docker & GitHub Actions CI/CD"]:::infra
    end

    %% Connections
    Browser -->|HTTPS/HTTP| Nginx
    Browser -->|WSS| Nginx
    Nginx -->|REST /api| APIGW
    Nginx -->|WS /ws| WebSocket
    APIGW -->|HTTP| Postgres
    APIGW -->|AMQP publish| RabbitMQ
    WebSocket -->|AMQP consume| RabbitMQ
    RabbitMQ -->|AMQP deliver| Browser
    RabbitMQ -->|AMQP| Background
    Background -->|DB writes| Postgres
    Metadata -->|HTTP| External
    Metadata -->|DB writes| Postgres
    Importers -->|DB writes| Postgres
    TimescaleWriter -->|DB writes| Postgres
    TROi -->|DB writes| Postgres
    SparkCluster -->|read dumps| HDFS
    SparkCluster -->|write analytics| TimescaleWriter
    SparkCluster -->|metrics| Postgres
    APIGW -->|cache| Redis
    APIGW -->|legacy| CouchDB
    DockerInfra --- Consul
    DockerInfra --- Nginx
    DockerInfra --- APIGW
    DockerInfra --- WebSocket
    DockerInfra --- Metadata
    DockerInfra --- Importers
    DockerInfra --- Background
    DockerInfra --- TimescaleWriter
    DockerInfra --- TROi
    DockerInfra --- SparkCluster

    %% External arrows grouping
    External --> Metadata

    %% Click Events
    click Browser "https://github.com/mamanullah7/listenbrainz-server/tree/master/frontend/"
    click Nginx "https://github.com/mamanullah7/listenbrainz-server/blob/master/Dockerfile.nginx.prod"
    click Nginx "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/prod/nginx/nginx.conf"
    click Consul "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/consul-template.conf"
    click Consul "https://github.com/mamanullah7/listenbrainz-server/blob/master/consul_config.py.ctmpl"
    click APIGW "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/webserver/"
    click APIGW "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/uwsgi/uwsgi.ini.ctmpl"
    click APIGW "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/uwsgi/uwsgi.service"
    click WebSocket "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/websockets/"
    click WebSocket "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/websockets/websockets.service"
    click Metadata "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/metadata_cache/"
    click Metadata "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/spotify_metadata_cache/spotify_metadata_cache.service"
    click Metadata "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/soundcloud_metadata_cache/soundcloud_metadata_cache.service"
    click Metadata "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/apple_metadata_cache/apple_metadata_cache.service"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/listens_importer/lastfm.py"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/lastfm_importer/lastfm_importer.service"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/listens_importer/spotify.py"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/spotify_reader/spotify_reader.service"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/mbid_mapping_writer/mbid_mapping_writer.py"
    click Importers "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/mbid_mapping_writer/mbid_mapping_writer.service"
    click Background "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/background/"
    click Background "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/background_tasks/background_tasks.service"
    click Background "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/services/cron/cron-config.service"
    click TimescaleWriter "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/timescale_writer/timescale_writer.py"
    click TimescaleWriter "https://github.com/mamanullah7/listenbrainz-server/tree/master/admin/timescale/"
    click TROi "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/troi/"
    click SparkCluster "https://github.com/mamanullah7/listenbrainz-server/blob/master/Dockerfile.spark"
    click SparkCluster "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/Dockerfile.spark.base"
    click SparkCluster "https://github.com/mamanullah7/listenbrainz-server/tree/master/docker/spark-cluster-config/"
    click SparkCluster "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz/spark/"
    click SparkCluster "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz_spark/"
    click HDFS "https://github.com/mamanullah7/listenbrainz-server/tree/master/listenbrainz_spark/hdfs/"
    click Postgres "https://github.com/mamanullah7/listenbrainz-server/tree/master/admin/sql/"
    click CouchDB "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/db/couchdb.py"
    click CouchDB "https://github.com/mamanullah7/listenbrainz-server/blob/master/docker/couchdb_test.ini"
    click Redis "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/listenstore/redis_listenstore.py"
    click RabbitMQ "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/webserver/rabbitmq_connection.py"
    click RabbitMQ "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/websockets/listens_dispatcher.py"
    click MBAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/musicbrainz.py"
    click SpotifyAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/spotify.py"
    click SoundCloudAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/soundcloud.py"
    click AppleAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/apple.py"
    click LastfmAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/lastfm.py"
    click LastfmAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/critiquebrainz.py"
    click LastfmAPI "https://github.com/mamanullah7/listenbrainz-server/blob/master/listenbrainz/domain/importer_service.py"
    click External "https://github.com/mamanullah7/listenbrainz-server/tree/master/---"
    click DockerInfra "https://github.com/mamanullah7/listenbrainz-server/tree/master/---"

    %% Styles
    classDef blue fill:#cce5ff,stroke:#005cbf,color:#005cbf;
    classDef green fill:#d4edda,stroke:#155724,color:#155724;
    classDef yellow fill:#fff3cd,stroke:#856404,color:#856404;
    classDef orange fill:#ffe5b4,stroke:#ff8c00,color:#ff8c00;
    classDef purple fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef cloud fill:#f0f8ff,stroke:#2b7cff,color:#2b7cff,stroke-dasharray: 5 5;
    classDef infra fill:#f5f5f5,stroke:#333,stroke-dasharray: 2 2;
```
