A Case Study in Building a Resilient, High-Performance Content Delivery System
Note: This is a closed-source enterprise project. This document serves as a high-level technical overview of the system's architecture, engineering decisions, and solutions to complex scaling challenges.
Legacy Notice: The live bot retains its original username (
@Netflix_666_bot) from its V1 release two years ago to preserve its active 10,000+ user base. However, the entire underlying system has been migrated to this V2 Enterprise Architecture.
Building a media-heavy bot serving concurrent users introduces severe architectural bottlenecks. The core challenges this V2 system successfully solved include:
- The 64-Byte State Bottleneck: Telegram strictly limits callback queries to 64 bytes. Passing complex state (pagination, movie metadata, provider context) in the UI is impossible without hitting payload limits.
- Tightly Coupled Providers: Scraping engines are notorious for frequent structural changes. Hardcoding providers into the main bot handlers causes the entire system to break when a single site goes down.
- State Mutation in Async Environments: Handling user-specific data (e.g., localized language strings, session IDs) in a highly concurrent asynchronous environment often leads to race conditions or passing state objects redundantly across hundreds of functions.
- Database I/O Spikes: Spam clicks and concurrent broadcast events can easily overwhelm connection pools, causing the "N+1" query problem and locking the database.
We adopted a strictly decoupled Clean Architecture, completely isolating the Telegram Presentation layer from the Core Business Logic and External APIs.
flowchart TB
%% Styling & Theming
classDef userLayer fill:#24A1DE,stroke:#fff,stroke-width:2px,color:#fff;
classDef middleware fill:#F4A261,stroke:#fff,stroke-width:2px,color:#000;
classDef core fill:#2A9D8F,stroke:#fff,stroke-width:2px,color:#fff;
classDef asyncTasks fill:#8A2BE2,stroke:#fff,stroke-width:2px,color:#fff;
classDef db fill:#336791,stroke:#fff,stroke-width:2px,color:#fff;
classDef redis fill:#DC382D,stroke:#fff,stroke-width:2px,color:#fff;
classDef network fill:#264653,stroke:#fff,stroke-width:2px,color:#fff;
classDef scraper fill:#E9C46A,stroke:#fff,stroke-width:2px,color:#000;
%% 1. Client & Presentation Layer
User([👤 Users & Admins]) -->|Interactions / Queries| TG[📱 Telegram API <br/> Pyrogram / Pyrofork]:::userLayer
%% 2. Security & Middleware Layer
subgraph "Gateway & Security (Middlewares)"
TG -->|Incoming Updates| MW{🛡️ Global Middleware <br/> ContextVars i18n}:::middleware
MW <-->|"1. SET NX EX (Atomic Lock)"| Redis[(🔴 Redis 7 <br/> State & Locks)]:::redis
MW <-->|"2. Load User / Check Bans"| DB[(🐘 PostgreSQL 16 <br/> Asyncpg + JSONB)]:::db
MW -.->|Circuit Breaker Fallback| Handlers
end
%% 3. Core Application Layer (Plugins)
subgraph "Application Core (Clean Architecture)"
MW -->|Validated Payloads| Handlers[⚙️ Core Handlers]:::core
Handlers -->|User Actions| Search[🔍 Search & Details Engine]:::core
Handlers -->|Admin Actions| Admin[👑 Admin Dashboard <br/> Settings & Suggestions]:::core
end
%% 4. Advanced Async Processing Engine
subgraph "Async Optimization & Background Tasks"
Search <-->|Parallel Poster Fetch| GIF[🖼️ GIF Compilation Engine <br/> In-Memory Buffer]:::asyncTasks
GIF -.->|Cache file_id| Redis
Admin -->|Trigger Broadcast| Broadcast[🚀 Async Broadcaster <br/> Background Task]:::asyncTasks
Broadcast <-->|"Yield (LIMIT/OFFSET)"| DB
Broadcast <-->|"Fetch 30-Day Payload & file_id"| Redis
Broadcast -.->|OOM-Safe Mass Send| TG
end
%% 5. Media Scraping Ecosystem (OCP)
subgraph "Scraping & Network Ecosystem"
Search -->|Scope Routing| Factory[🏭 Provider Factory <br/> Enum Resolver]:::network
Factory -->|Instantiate| Base[🧩 Base Provider Interface]:::network
Base --> Akwam[🕸️ Akwam Provider <br/> On-Demand Decryption]:::scraper
Base --> MyCima[🕸️ MyCima Provider <br/> Stream Sanitization]:::scraper
Akwam --> Interceptor[🌐 Network Interceptor <br/> DYNAMIC_DOMAIN Replacement]:::network
MyCima --> Interceptor
Interceptor <-->|Fetch Live Domains| DB
Interceptor -->|Aiohttp Requests| Web((🌍 External <br/> Media Mirrors)):::userLayer
end
%% Extra Mappings for clarity
Search <-->|Short-ID Pagination Mapping| Redis
The most critical architectural decision was decoupling the bot's core from the external providers.
- We implemented a
ProviderFactoryutilizing the Factory Design Pattern. The core bot logic has zero knowledge of how "Akwam" or "MyCima" works. It simply interfaces with aBaseProviderabstract class. - Impact: The system perfectly adheres to the Open-Closed Principle (OCP). Adding a new provider (e.g., EgyBest) requires adding a new class, with absolutely zero modifications to the core routing or UI handlers.
To bypass Telegram's strict payload limits, we transformed the UI into a fully stateless interface.
- Solution: Heavy JSON payloads (search results, metadata) are stored in Redis with a TTL. The system issues a unique 8-character
short_idwhich is injected into the inline keyboards. - Impact: Reduced payload size transmitted over the network from ~2.5KB to exactly 8 Bytes (a 99.6% reduction). Pagination and data retrieval are now executed in memory at sub-millisecond speeds.
- Solution: Instead of passing the user's language and session instance through every layer of the application (which clutters the codebase), we utilized Python's
ContextVars. - Impact: Variables like
current_langandcurrent_userare isolated per asynchronous task. The bot dynamically serves Arabic and English concurrently with 100% thread safety and zero hardcoded handler strings.
- Atomic Redis Locks: Implemented a circuit breaker using Redis
SET NX EX. It traps and drops millisecond spam clicks instantly at the cache layer, shielding PostgreSQL entirely. - Just-In-Time (JIT) Extraction: Direct download links are decrypted only at the exact moment the user presses the download button. This reduces unnecessary scraping requests by over 80% and protects the server from IP-banning.
Engineered to scale, the architectural changes yielded the following measurable impacts:
- I/O Database Reduction: Middleware caching and
selectinloadfor eager loading eliminated the N+1 query problem, dropping redundant database I/O hits by ~85%. - Constant Memory
O(1)Broadcasting: The Masterpiece Broadcast Engine uses Async Generators to stream user IDs in chunks (LIMIT&OFFSETof 500). Memory consumption remains flat whether broadcasting to 1,000 or 1,000,000 users. - Zero-Bandwidth File Delivery: By caching the Telegram
file_idfor merged GIF posters, broadcasting media consumes 0 bytes of server upload bandwidth after the initial caching event. - Parallel Execution Speedup: Fetching 5 top search results' posters sequentially took ~4 seconds. Utilizing
asyncio.gatherfor parallel fetching reduced the total execution time to ~800ms.
This project is strictly an educational case study focused on System Design, Software Architecture, and High-Performance Async processing. The developer does not host, upload, or distribute any media files. The system acts purely as a real-time web scraping aggregator and search engine for publicly available links on the internet.
"Write code and design systems that don't just work — make them unbreakable."
Alsaeed Hasan
Backend Software Engineer
- 🌐 LinkedIn: linkedin.com/in/alsaeed-hasan
- 💻 GitHub: github.com/AlsaeedHasan
- ✉️ Email: saeedhasan.dev@gmail.com
If you are a technical recruiter or engineering manager interested in the system design details, feel free to reach out.