๐ŸŽ Get the FREE AI Skills Starter Guide โ€” Subscribe โ†’
BytesAgainBytesAgain
๐Ÿฆ€ ClawHub

Teamgram Server Architecture

by @zhihang9978

Teamgram Server architecture guide for building Telegram-compatible backends. Use when designing service topology, implementing MTProto services, or self-hos...

Versionv1.0.1
Downloads459
TERMINAL
clawhub install teamgram-server-architecture

๐Ÿ“– About This Skill


name: teamgram-server-architecture description: Teamgram Server architecture guide for building Telegram-compatible backends. Use when designing service topology, implementing MTProto services, or self-hosting Teamgram. Covers serviceๆ‹†ๅˆ†, data flow, deployment patterns, and development workflows based on the official teamgram/teamgram-server repository. version: 1.0.1

Teamgram Server Architecture

Complete architecture guide based on the official teamgram/teamgram-server repository.

โš ๏ธ ๅ…่ดฃๅฃฐๆ˜ŽไธŽๅฎ‰ๅ…จๆ็คบ

> ๆœฌๆŠ€่ƒฝๅŸบไบŽๅฏนๅผ€ๆบ้กน็›ฎ teamgram/teamgram-server ็š„ๅˆ†ๆžๆ•ด็†๏ผŒไป…ไพ›ๅญฆไน ๅ‚่€ƒใ€‚ > > ้‡่ฆๆ็คบ: > - ๅ†…ๅฎนๅฏ่ƒฝ้šๅฎ˜ๆ–นไป“ๅบ“ๆ›ดๆ–ฐ่€Œ่ฟ‡ๆ—ถ๏ผŒ่ฏทไปฅๅฎ˜ๆ–นๆœ€ๆ–ฐ็‰ˆๆœฌไธบๅ‡† > - ็”Ÿไบง็Žฏๅขƒไฝฟ็”จๅ‰่ฏท่‡ช่กŒ้ชŒ่ฏๆ‰€ๆœ‰้…็ฝฎๅ’Œไปฃ็  > - ้ƒจ็ฝฒ้…็ฝฎไธญ็š„ๅฏ†็ ใ€ๅฏ†้’ฅ็ญ‰ๅฟ…้กปไฝฟ็”จๅผบๅฏ†็ ๅนถ้€š่ฟ‡ๅฎ‰ๅ…จๆ–นๅผๆณจๅ…ฅ > - ๅปบ่ฎฎ็›ดๆŽฅๅ‚่€ƒๅฎ˜ๆ–นๆ–‡ๆกฃ: https://github.com/teamgram/teamgram-server > - ็”Ÿไบง็Žฏๅขƒ้ƒจ็ฝฒๅ‰่ฏท่ฟ›่กŒๅฎ‰ๅ…จๅฎก่ฎกๅ’Œๆธ—้€ๆต‹่ฏ•


Overview

Teamgram Server is an unofficial open-source MTProto server implementation in Go, compatible with Telegram clients and supporting self-hosted deployment.

API Layer: 223 (ๆˆช่‡ณๆŠ€่ƒฝๅˆ›ๅปบๆ—ถ๏ผŒ่ฏทไปฅๅฎ˜ๆ–นไป“ๅบ“ๆœ€ๆ–ฐ็‰ˆๆœฌไธบๅ‡†) MTProto Versions: Abridged, Intermediate, Padded intermediate, Full

Core Features

  • โœ… Private Chat - End-to-end encrypted messaging
  • โœ… Basic Group - Small group chats (up to 200 members)
  • โš ๏ธ Super Group - Large groups (requires additional implementation)
  • โœ… Contacts - Contact management and sync
  • โœ… Web - Web client support
  • Service Architecture

    High-Level Topology

                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚   Load Balancer โ”‚
                        โ”‚    (Nginx/HA)   โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ”‚                    โ”‚                    โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚   gnetway     โ”‚   โ”‚   httpserver    โ”‚   โ”‚   session   โ”‚
    โ”‚  (TCP/MTProto)โ”‚   โ”‚   (HTTP API)    โ”‚   โ”‚ (WebSocket) โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚                    โ”‚                    โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚   BFF Layer     โ”‚
                        โ”‚ (Business Logic)โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ”‚                    โ”‚                    โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚   Service     โ”‚   โ”‚    Service      โ”‚   โ”‚   Service   โ”‚
    โ”‚   Layer       โ”‚   โ”‚    Layer        โ”‚   โ”‚   Layer     โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    

    Interface Layer (app/interface)

    | Service | Protocol | Purpose | |---------|----------|---------| | gnetway | TCP/MTProto | Main client gateway, handles MTProto encryption | | httpserver | HTTP/REST | Bot API and webhooks | | session | WebSocket | Web client connections |

    BFF Layer (app/bff)

    Backend-for-Frontend aggregation layer:

  • Aggregates multiple service calls
  • Handles client-specific logic
  • Reduces client-side complexity
  • Service Layer (app/service)

    | Service | Responsibility | Key Features | |---------|----------------|--------------| | authsession | Authentication & Session | Auth key management, session validation | | biz | Core Business Logic | Chat, message, user, dialog, updates | | dfs | Distributed File Storage | File upload/download, MinIO integration | | geoip | Geo-location | IP geolocation for security | | idgen | ID Generation | Snowflake-style distributed IDs | | media | Media Processing | Thumbnail generation, FFmpeg integration | | status | Online Status | User presence, last seen |

    Messenger Layer (app/messenger)

    | Service | Purpose | |---------|---------| | msg | Message routing and delivery | | sync | Multi-device synchronization |

    Biz Service Breakdown

    The biz service is a monolithic business logic container:

    app/service/biz/
    โ”œโ”€โ”€ biz/        # Core business operations
    โ”œโ”€โ”€ chat/       # Group/channel management
    โ”œโ”€โ”€ code/       # Verification codes (SMS/email)
    โ”œโ”€โ”€ dialog/     # Conversation management
    โ”œโ”€โ”€ message/    # Message storage and retrieval
    โ”œโ”€โ”€ updates/    # Real-time updates push
    โ””โ”€โ”€ user/       # User profiles and settings
    

    Recommended Refactoring

    For large-scale deployments, split biz into:

    chat-service/      - Group & channel management
    message-service/   - Message CRUD and search
    user-service/      - User profiles and contacts
    notification-service/ - Push notifications
    

    Data Flow

    Message Sending Flow

    Client โ†’ gnetway โ†’ session โ†’ msg โ†’ message (biz)
                                               โ†“
                                        MySQL (persist)
                                               โ†“
                                        Kafka (broadcast)
                                               โ†“
                                  sync โ†’ updates โ†’ Client
    

    Authentication Flow

    Client โ†’ gnetway โ†’ authsession
                            โ†“
                       MySQL (auth_keys)
                            โ†“
                       Redis (sessions)
    

    File Upload Flow

    Client โ†’ gnetway โ†’ dfs โ†’ MinIO
                      โ†“
                   MySQL (file_metadata)
    

    Infrastructure Dependencies

    | Component | Purpose | Required | |-----------|---------|----------| | MySQL 5.7+ | Primary data store | โœ… Yes | | Redis | Cache, sessions, deduplication | โœ… Yes | | etcd | Service discovery & config | โœ… Yes | | Kafka | Message pipeline, events | โœ… Yes | | MinIO | Object storage | โœ… Yes | | FFmpeg | Media transcoding | โš ๏ธ Optional |

    Project Structure

    teamgram-server/
    โ”œโ”€โ”€ app/
    โ”‚   โ”œโ”€โ”€ bff/              # Backend-for-Frontend
    โ”‚   โ”œโ”€โ”€ interface/        # Gateway layer
    โ”‚   โ”‚   โ”œโ”€โ”€ gnetway/      # MTProto gateway
    โ”‚   โ”‚   โ”œโ”€โ”€ httpserver/   # HTTP API
    โ”‚   โ”‚   โ””โ”€โ”€ session/      # WebSocket session
    โ”‚   โ”œโ”€โ”€ messenger/        # Message routing
    โ”‚   โ”‚   โ”œโ”€โ”€ msg/          # Message service
    โ”‚   โ”‚   โ””โ”€โ”€ sync/         # Sync service
    โ”‚   โ””โ”€โ”€ service/          # Core services
    โ”‚       โ”œโ”€โ”€ authsession/  # Auth & session
    โ”‚       โ”œโ”€โ”€ biz/          # Business logic
    โ”‚       โ”œโ”€โ”€ dfs/          # File storage
    โ”‚       โ”œโ”€โ”€ geoip/        # Geo location
    โ”‚       โ”œโ”€โ”€ idgen/        # ID generator
    โ”‚       โ”œโ”€โ”€ media/        # Media processing
    โ”‚       โ””โ”€โ”€ status/       # Online status
    โ”œโ”€โ”€ pkg/                  # Shared packages
    โ”‚   โ”œโ”€โ”€ code/             # Error codes
    โ”‚   โ”œโ”€โ”€ conf/             # Configuration
    โ”‚   โ”œโ”€โ”€ net2/             # Network utilities
    โ”‚   โ””โ”€โ”€ ...
    โ”œโ”€โ”€ clients/              # Client SDKs
    โ”œโ”€โ”€ data/                 # SQL schemas
    โ”œโ”€โ”€ docs/                 # Documentation
    โ””โ”€โ”€ specs/                # Architecture specs
    

    Development Workflow

    1. Code Generation

    Teamgram uses TL (Type Language) schema:

    # Generate Go code from TL schema
    make generate
    

    or

    dalgenall.sh

    2. Database Migration

    # Initialize database
    mysql -u root -p < data/teamgram.sql

    Run migrations

    make migrate

    3. Service Development Pattern

    Each service follows this structure:

    app/service//
    โ”œโ”€โ”€ cmd/              # Entry point
    โ”œโ”€โ”€ etc/              # Configuration
    โ”œโ”€โ”€ internal/
    โ”‚   โ”œโ”€โ”€ config/       # Config structures
    โ”‚   โ”œโ”€โ”€ core/         # Business logic
    โ”‚   โ”œโ”€โ”€ dao/          # Data access
    โ”‚   โ”œโ”€โ”€ server/       # gRPC/HTTP handlers
    โ”‚   โ””โ”€โ”€ svc/          # Service context
    โ””โ”€โ”€ .go         # Main service file
    

    4. Adding New RPC

    1. Define in TL schema (specs/mtproto.tl) 2. Run code generation 3. Implement handler in internal/core/ 4. Register in internal/server/ 5. Update client SDKs

    Configuration

    Service Configuration (YAML)

    # app/service/biz/etc/biz.yaml
    Name: biz
    Host: 0.0.0.0
    Port: 20001

    MySQL: DataSource: user:password@tcp(localhost:3306)/teamgram?charset=utf8mb4

    Redis: Host: localhost:6379

    Etcd: Hosts: - localhost:2379 Key: biz

    Environment Variables

    # .env file
    MYSQL_DATA_SOURCE=user:password@tcp(localhost:3306)/teamgram
    REDIS_HOST=localhost:6379
    ETCD_ENDPOINTS=localhost:2379
    KAFKA_BROKERS=localhost:9092
    MINIO_ENDPOINT=localhost:9000
    

    Deployment Patterns

    Docker Compose (Development)

    docker-compose up -d
    

    Kubernetes (Production)

    # Example deployment
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: teamgram-biz
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: teamgram-biz
      template:
        spec:
          containers:
          - name: biz
            image: teamgram/biz:latest
            resources:
              requests:
                memory: "512Mi"
                cpu: "500m"
              limits:
                memory: "2Gi"
                cpu: "2000m"
    

    Scaling Considerations

    Horizontal Scaling

  • Stateless services: biz, httpserver, dfs (easy to scale)
  • Stateful services: gnetway (connection-based), session (session affinity)
  • Database: MySQL read replicas, Redis Cluster
  • Vertical Scaling

  • Media service: CPU-intensive (FFmpeg)
  • Message service: Memory-intensive (caching)
  • Auth service: Low resource usage
  • Security Best Practices

    1. Network Isolation - Internal services behind VPC - Only gnetway/httpserver exposed publicly

    2. Encryption - MTProto end-to-end encryption - TLS for HTTP/WebSocket - mTLS between services (optional)

    3. Authentication - Auth keys in secure storage - Session tokens with expiration - Rate limiting per user/IP

    4. Data Protection - Database encryption at rest - MinIO bucket encryption - Backup encryption

    Monitoring

    Metrics

    - Request rate per service
    
  • Response latency (p50, p95, p99)
  • Error rates
  • Active connections
  • Message throughput
  • Logging

    // Structured logging
    log.Info().
        Str("service", "biz").
        Str("method", "messages.sendMessage").
        Int64("user_id", userID).
        Int64("msg_id", msgID).
        Dur("latency", duration).
        Msg("request processed")
    

    References

  • Architecture DeepWiki
  • Service Topology
  • Dependencies
  • Official Repository
  • See Also

  • references/service-development.md - Step-by-step service creation
  • references/deployment.md - Production deployment guide
  • references/tuning.md - Performance optimization
  • โš™๏ธ Configuration

    Service Configuration (YAML)

    # app/service/biz/etc/biz.yaml
    Name: biz
    Host: 0.0.0.0
    Port: 20001

    MySQL: DataSource: user:password@tcp(localhost:3306)/teamgram?charset=utf8mb4

    Redis: Host: localhost:6379

    Etcd: Hosts: - localhost:2379 Key: biz

    Environment Variables

    # .env file
    MYSQL_DATA_SOURCE=user:password@tcp(localhost:3306)/teamgram
    REDIS_HOST=localhost:6379
    ETCD_ENDPOINTS=localhost:2379
    KAFKA_BROKERS=localhost:9092
    MINIO_ENDPOINT=localhost:9000