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...
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
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:
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.sqlRun 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: 20001MySQL:
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
Vertical Scaling
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
See Also
โ๏ธ Configuration
Service Configuration (YAML)
# app/service/biz/etc/biz.yaml
Name: biz
Host: 0.0.0.0
Port: 20001MySQL:
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