Skip to main content

Video Understanding Pipeline System Design

Comprehensive AI/ML system design for scalable video content analysis, object detection, and semantic understanding with real-time processing capabilities.

Modern video understanding pipelines require sophisticated multi-modal processing capabilities to analyze temporal content, detect objects, and extract semantic meaning from video streams at scale.

1. Problem Statement & ML Task Definition

Clarifying questions

  1. What is the primary use case for video understanding? Is it content recommendation, moderation, search, or content categorization?
  2. What is the expected scale of video data? How many videos are we processing daily, and what is the typical video length?
  3. What is the latency budget for real-time video understanding? Is this a batch processing system or does it require sub-500ms response times?
  4. What is the expected QPS for video understanding? How many videos per second need to be processed in production?
  5. What is the acceptable accuracy threshold for video understanding? Is there a specific AUC or precision@K target?
  6. What is the typical video content distribution? Are videos primarily user-generated, professional content, or a mix?
  7. What is the required freshness of video understanding features? Should features be updated in real-time or can they be batch-computed?

Business use case and success criteria

The video understanding pipeline aims to automatically analyze and extract semantic information from videos to power content recommendation, moderation, search, and categorization systems. The system processes raw video content to generate rich semantic features that enable downstream ML systems to make intelligent decisions about content relevance, safety, and user engagement.

Success criteria include:

  • Content Relevance: 85%+ precision in content categorization for top-5 recommendations
  • Moderation Accuracy: 95%+ recall in detecting inappropriate content
  • Search Relevance: 70%+ NDCG for semantic search results
  • User Engagement: 25%+ improvement in user retention rate with content understanding features

ML problem formulation

This is a multi-modal multi-task learning problem combining:

  • Content Classification: Video category prediction (e.g., sports, news, entertainment)
  • Object Detection & Tracking: Identifying and tracking objects in video frames
  • Action Recognition: Detecting human actions and activities
  • Scene Understanding: Scene type classification (indoor/outdoor, day/night)
  • Audio Analysis: Speech recognition and audio event detection

The problem is formulated as a multi-task learning system where each task shares a common backbone but has specific output heads. This approach enables efficient joint training while maintaining task-specific performance.

ML evaluation metrics

Offline Evaluation Metrics:

  • AUC-ROC: For binary classification tasks (e.g., inappropriate content detection)
  • NDCG: For ranking tasks (content search relevance)
  • Precision@K: For recommendation systems (top-K content matches)
  • BLEU: For video caption generation quality
  • mAP: For object detection and tracking tasks

Online Evaluation Metrics:

  • CTR (Click-Through Rate): For content recommendation systems
  • Engagement Time: Average time spent on recommended content
  • Conversion Rate: User actions following content recommendations
  • Moderation Accuracy: False positive/negative rates in content moderation

Scale and constraints

Back-of-envelope estimates:

Training data size: 50M videos (500GB raw video + 200GB metadata) Number of features: 10,000+ (embedding dimensions + extracted features) Embedding matrix memory footprint: 50GB (1M video IDs × 256 dimensions) Serving QPS math:

  • 10M videos/day → ~115 QPS average
  • Peak load: 500 QPS (assuming 2x peak factor)
  • Per video processing time: 200ms (50ms feature extraction + 150ms model inference)

System constraints:

  • Latency Budget: 200ms end-to-end processing time
  • QPS: 500 concurrent video understanding requests
  • Data Volume: 100TB monthly video data ingestion
  • Freshness: Features updated within 24 hours of content upload

Non-ML baseline strategy

Rule-based fallback system:

  1. Metadata-based classification: Use existing video metadata (title, description, tags) as primary categorization
  2. Simple content filtering: Apply basic rules for inappropriate content (e.g., explicit language detection, age-appropriate content)
  3. Thumbnail-based inference: Use thumbnail image classification as a proxy for video content
  4. Cold start handling: For new videos, use content similarity to existing videos in the same category

Fallback logic:

  • If video understanding pipeline fails or exceeds latency budget, fall back to metadata-based classification
  • For videos with no metadata, use thumbnail-based inference as secondary approach
  • Implement a confidence threshold (e.g., <0.7) to trigger fallback to rule-based system

2. Data Strategy

Training data sources

The video understanding pipeline requires diverse training data sources to build robust models:

  1. Explicit feedback: User engagement signals (watch time, completion rate, re-watches, ratings, likes/dislikes)
  2. Implicit signals: Click-through rates, scroll behavior, time spent viewing thumbnails, user session patterns
  3. Third-party data: Video metadata (categories, tags, descriptions), content moderation labels, external video classification datasets (e.g., YouTube's UCF-101, Kinetics)
  4. Internal platform data: User demographics, device information, geographic location, temporal usage patterns

Labeling pipeline design

The labeling pipeline must handle both structured and unstructured data:

  • Human annotation: For high-quality labels on key video categories, action recognition, and content safety (e.g., NSFW classification)
  • Weak supervision: Using pre-trained models to generate pseudo-labels for large-scale data filtering
  • Programmatic labeling: Automated content classification using existing video metadata, OCR text extraction, and timestamped event detection

Class imbalance handling strategy

Video content exhibits significant class imbalance (e.g., sports videos vs. educational content). Strategy includes:

  1. Oversampling: For underrepresented categories in training data
  2. Weighted loss functions: Adjusting class weights during training to penalize misclassification of rare classes more heavily
  3. Stratified sampling: Ensuring balanced representation in training batches

Negative sampling approach

For action recognition and content classification tasks:

  1. Hard negative mining: Selecting videos that are close to decision boundaries for training
  2. Random negative sampling: Including diverse non-matching content as negatives to prevent overfitting
  3. Temporal negative sampling: Using adjacent frames or segments from same video as negatives to maintain temporal coherence

Data freshness and update frequency requirements

Data TypeSourceUpdate FrequencyReason
User engagementPlatform logsReal-timeFor A/B testing and online learning
Video metadataInternal DBDailyContent updates, category changes
Third-party labelsExternal APIsWeeklyNew content categories, moderation updates

Privacy compliance

  • Anonymization: Remove PII from user session data, blur faces in videos where possible
  • Data retention: 2-year policy for user engagement data, 6-month for raw video features
  • Compliance: GDPR and CCPA adherence with user consent management and data portability support

Data quality validation and monitoring

  • Validation checks: Video integrity (corrupted files, missing frames), metadata consistency, label quality scores
  • Monitoring: Daily data drift detection for feature distributions, labeling accuracy trends, and content freshness metrics
  • Feedback loops: Automated re-labeling pipeline for low-confidence predictions
# Example data validation function
def validate_video_data(video_metadata):
    """Validate video metadata for integrity and consistency"""
    errors = []
    
    # Check for corrupted files
    if not os.path.exists(video_metadata['file_path']):
        errors.append("Corrupted file path")
    
    # Validate metadata fields
    required_fields = ['duration', 'resolution', 'category']
    for field in required_fields:
        if not video_metadata.get(field):
            errors.append(f"Missing required field: {field}")
    
    # Check for temporal consistency
    if video_metadata['duration'] <= 0:
        errors.append("Invalid duration")
    
    return errors

3. Feature Engineering

Feature Taxonomy

Features are categorized into four main types:

  1. User Features: Demographics, viewing history, engagement patterns, preferences (e.g., watch time, favorite categories, time of day preferences)
  2. Item Features: Video metadata (title, description, tags, duration, thumbnail features, content category), temporal properties (upload date, trending status)
  3. Context Features: Device type, network conditions, location, time of day, session context (e.g., consecutive watch sessions)
  4. Interaction Features: User-item interaction signals (click-through rate, completion rate, skip rate, rewatch frequency), temporal decay of engagement

Embedding Design

Entity Embeddings:

  • User ID embeddings (dimension: 128)
  • Video ID embeddings (dimension: 256)
  • Category embeddings (dimension: 64)

Text Embeddings:

  • Video title and description embeddings using BERT (768-dim) with fine-tuned on video content
  • Tag embeddings using Word2Vec (128-dim) trained on video tags

Graph Embeddings:

  • User-video interaction graph embeddings using Node2Vec (128-dim) to capture user preferences and content similarity
  • Content-based graph embeddings using TF-IDF features for semantic clustering

Real-time vs Batch Feature Computation

Feature TypeComputation ModeJustification
User engagement historyReal-timeFor dynamic personalization
Video metadataBatchStatic content, computed once
Temporal decay featuresReal-timeTime-sensitive, requires up-to-date computation
Contextual signalsReal-timeDevice and session context change frequently

Feature Store Design

Online Store:

  • Low-latency key-value store (e.g., Redis or DynamoDB)
  • Optimized for sub-100ms response times
  • Supports real-time feature lookups

Offline Store:

  • Data warehouse (e.g., Snowflake or BigQuery)
  • For training data generation and batch feature computation
  • Supports complex aggregations and historical analysis

Feature Freshness and TTL

FeatureSourceUpdate FrequencyStore Type
User watch historyReal-time streamEvery 5 minOnline
Video metadataContent ingestionDailyOffline
User preferencesBatch aggregationHourlyOnline
Context featuresReal-time streamEvery 1 minOnline
Temporal decayReal-time calculationEvery 5 minOnline

Cross Features and Feature Interactions

Cross features are engineered to capture complex user-item relationships:

  • User-category interaction (e.g., "User X's preference for Category Y")
  • Time-of-day × content-category combinations (e.g., "Morning videos in Sports")
  • Device × engagement patterns (e.g., "Mobile users prefer short-form content")

Cross features are computed using:

  • Feature crosses in model architecture (e.g., embedding concatenation)
  • Polynomial features for linear models
  • Attention-based feature interactions in transformer models

Feature Importance and Selection Methodology

  1. Statistical Analysis: Chi-square test for categorical features, correlation analysis for continuous features
  2. Model-based Importance: SHAP values from tree-based models (XGBoost, LightGBM) to rank feature importance
  3. A/B Testing: Feature impact validation through controlled experiments

Feature selection is performed using:

  • Recursive Feature Elimination (RFE) for linear models
  • Gradient-based feature importance in deep learning models
  • Permutation importance for model-agnostic evaluation
import numpy as np
from sklearn.feature_selection import SelectKBest, chi2

class VideoFeatureSelector:
    def __init__(self, k=100):
        self.k = k
        self.selector = SelectKBest(score_func=chi2, k=k)
        
    def select_features(self, X, y):
        # X: feature matrix (n_samples, n_features)
        # y: target vector
        X_selected = self.selector.fit_transform(X, y)
        selected_indices = self.selector.get_support(indices=True)
        return X_selected, selected_indices

# Example usage for feature importance ranking
def compute_feature_importance(model, X_train, y_train):
    """Compute feature importance using permutation method"""
    baseline_score = model.score(X_train, y_train)
    
    importance_scores = []
    for i in range(X_train.shape[1]):
        X_permuted = X_train.copy()
        X_permuted[:, i] = np.random.permutation(X_permuted[:, i])
        permuted_score = model.score(X_permuted, y_train)
        importance = baseline_score - permuted_score
        importance_scores.append(importance)
    
    return np.array(importance_scores)

4. Model Architecture

Model Selection with Justification

For a video understanding pipeline, we select a multi-modal transformer architecture combining vision and audio modalities with the following rationale:

  • Vision Transformer (ViT): Handles visual content, capturing spatial and temporal patterns in video frames
  • Audio Transformer: Processes audio streams for sound-based understanding (speech, music, ambient sounds)
  • Cross-modal attention: Enables fusion of visual and audio signals for comprehensive video understanding
  • Multi-task learning: Supports downstream tasks like action recognition, scene classification, and content moderation

Trade-offs:

  • Pros: Strong performance on complex video understanding tasks, handles multimodal fusion naturally
  • Cons: Higher computational requirements, more complex to train and serve

Architecture Diagram

graph TD
    A[Video Input] --> B[Frame Extractor]
    C[Audio Stream] --> D[Audio Preprocessor]
    B --> E[ViT Encoder]
    D --> F[Audio Transformer]
    E --> G[Cross-Modal Fusion]
    F --> G
    G --> H[Task-Specific Heads]
    H --> I[Action Recognition]
    H --> J[Scene Classification]
    H --> K[Content Moderation]

Two-Tower / Multi-Task / Ensemble Design

We implement a two-tower architecture where:

  1. Visual Tower: ViT-based encoder processing video frames
  2. Audio Tower: Audio Transformer processing audio streams

These towers are fused at the cross-modal attention layer, followed by task-specific heads for:

  • Action recognition (classification)
  • Scene classification (multi-class)
  • Content moderation (binary classification)

Pre-trained Model Selection and Fine-tuning Strategy

We leverage pre-trained models:

  • ViT-B/16: Pre-trained on ImageNet-21K, fine-tuned on video action recognition datasets (Kinetics-400/600)
  • WavLM: Pre-trained audio model, fine-tuned for video understanding tasks

Fine-tuning strategy:

  1. Stage 1: Freeze ViT and WavLM, train only fusion layers
  2. Stage 2: Gradual unfreezing with lower learning rates (1e-5 for base layers, 1e-4 for fusion)
  3. Stage 3: Full fine-tuning with task-specific learning rates

Model Complexity vs Latency Trade-off Analysis

ModelParametersInference Time (ms)Memory Usage (GB)
ViT-B/1686M452.3
WavLM120M653.2
Cross-Modal Fusion15M250.8
Full Pipeline221M1356.3

Offline Evaluation Baseline Framing

Baseline model: ResNet-50 + LSTM audio encoder with simple concatenation fusion Expected baseline performance:

  • Action Recognition: 65% top-1 accuracy
  • Scene Classification: 70% top-1 accuracy
  • Content Moderation: 85% AUC-ROC

Proposed model: ViT + WavLM + Cross-Attention fusion Target performance:

  • Action Recognition: 75% top-1 accuracy
  • Scene Classification: 80% top-1 accuracy
  • Content Moderation: 92% AUC-ROC

Core Model Code Snippet

import torch
import torch.nn as nn
from transformers import ViTModel, WavLMModel

class VideoUnderstandingTransformer(nn.Module):
    def __init__(self, num_actions=400, num_scenes=100, num_moderation_classes=2):
        super().__init__()
        
        # Vision encoder (ViT)
        self.vision_encoder = ViTModel.from_pretrained("google/vit-base-patch16-224")
        
        # Audio encoder (WavLM)
        self.audio_encoder = WavLMModel.from_pretrained("microsoft/WavLM-Base")
        
        # Cross-modal fusion layer
        self.cross_attention = nn.MultiheadAttention(
            embed_dim=768, num_heads=8, batch_first=True
        )
        
        # Task-specific heads
        self.action_head = nn.Linear(768, num_actions)
        self.scene_head = nn.Linear(768, num_scenes)
        self.moderation_head = nn.Linear(768, num_moderation_classes)
        
        # Fusion layer
        self.fusion_layer = nn.Linear(768 * 2, 768)
        
    def forward(self, video_frames, audio_waveforms):
        # Extract visual features
        vision_features = self.vision_encoder(video_frames).last_hidden_state
        
        # Extract audio features
        audio_features = self.audio_encoder(audio_waveforms).last_hidden_state
        
        # Cross-modal attention fusion
        fused_features, _ = self.cross_attention(
            vision_features, audio_features, audio_features
        )
        
        # Aggregate features (mean pooling)
        pooled_features = fused_features.mean(dim=1)
        
        # Task predictions
        action_logits = self.action_head(pooled_features)
        scene_logits = self.scene_head(pooled_features)
        moderation_logits = self.moderation_head(pooled_features)
        
        return {
            "action": action_logits,
            "scene": scene_logits,
            "moderation": moderation_logits
        }
    
    def compute_loss(self, predictions, targets):
        action_loss = nn.CrossEntropyLoss()(predictions["action"], targets["action"])
        scene_loss = nn.CrossEntropyLoss()(predictions["scene"], targets["scene"])
        moderation_loss = nn.BCEWithLogitsLoss()(predictions["moderation"], targets["moderation"])
        
        return {
            "total_loss": action_loss + scene_loss + moderation_loss,
            "action_loss": action_loss,
            "scene_loss": scene_loss,
            "moderation_loss": moderation_loss
        }

# Example usage:
# model = VideoUnderstandingTransformer()
# output = model(video_frames, audio_waveforms)

This implementation provides:

  • Modular architecture supporting multiple downstream tasks
  • Cross-modal attention for rich fusion of visual and audio signals
  • Clean separation of concerns between encoder, fusion, and task heads
  • Loss computation for multi-task training
  • Production-ready PyTorch module with clear input/output contracts

5. Serving & Inference Architecture

System Diagram

graph TD
    A[User Request] --> B[Load Balancer]
    B --> C[Feature Store Lookup]
    C --> D[Embedding Cache]
    D --> E[Model Serving]
    E --> F[Ranking/Scoring]
    F --> G[Response Formatter]
    G --> H[User Response]

    subgraph "Online Serving Layer"
        C
        D
        E
        F
        G
    end

    subgraph "Feature Store"
        C
        D
    end

    subgraph "Model Serving"
        E
        F
    end

Online vs Batch Serving Decision

Online serving is required for real-time video recommendation with sub-500ms latency SLA. Batch serving is used for pre-computing embeddings and generating weekly recommendation lists.

Two-Stage Pipeline

  1. Candidate Retrieval:
    • Use approximate nearest neighbor (ANN) search over pre-computed video embeddings
    • Filter by user preferences and content categories
  2. Scoring & Re-ranking:
    • Apply learned ranking model to filtered candidates
    • Incorporate real-time features (user session, watch history)
    • Apply diversity and freshness constraints

Serving Infrastructure

TorchServe with custom inference wrapper is selected due to:

  • Native support for PyTorch models
  • Built-in model versioning and A/B testing capabilities
  • Easy integration with Kubernetes orchestration
  • Support for dynamic batching to improve throughput

For video understanding models, we'll leverage TorchServe's multi-GPU support and model parallelism capabilities.

Latency Budget Breakdown

Stagep50 Latencyp99 LatencyBudget
Feature Lookup10ms25ms25ms
Model Inference25ms40ms40ms
Scoring/Re-ranking15ms30ms30ms
Response Formatting5ms10ms10ms
Total55ms105ms105ms

Model Compression Strategy

  • Quantization: INT8 quantization reduces model size by 4x with <2% accuracy loss
  • Pruning: Structured pruning removes 30% of parameters while maintaining 95% of performance
  • Knowledge Distillation: Student model trained to mimic teacher model's outputs

For video understanding models, we'll use quantization-aware training (QAT) to maintain accuracy while reducing memory footprint.

Caching Strategy

  • Prediction Cache: Store top-K recommendations for 10-minute TTL per user
  • Embedding Cache: Keep most frequently accessed video embeddings in memory (LRU eviction)
  • KV Cache: For transformer-based models, cache attention keys/values to avoid recomputation

Auto-scaling and Capacity Planning

  • Horizontal scaling of model serving pods based on QPS
  • Target 95th percentile latency under 100ms with 2x capacity headroom
  • Auto-scaling triggers at >85% CPU utilization or >90th percentile latency >75ms
  • For peak traffic (e.g., live events), pre-warm model instances with warm-up batches

Core Inference Pipeline Code

import torch
from torch import nn
import numpy as np
from typing import List, Dict, Tuple

class VideoUnderstandingInferenceEngine:
    def __init__(self, model_path: str, device: str = "cuda"):
        self.device = device
        self.model = torch.load(model_path).to(device)
        self.model.eval()
        
    def batch_inference(self, video_features: torch.Tensor) -> torch.Tensor:
        """Process batch of video features through model"""
        with torch.no_grad():
            # Video understanding forward pass
            video_embeddings = self.model(video_features)
            
            # Apply attention pooling for sequence-level representation
            pooled_video_emb = torch.mean(video_embeddings, dim=1)
            
            # Apply final classification layer (if needed for ranking)
            scores = self.model.classifier(pooled_video_emb)
            
        return scores
    
    def rank_videos(self, user_features: Dict[str, torch.Tensor], 
                   candidate_videos: List[torch.Tensor]) -> List[Tuple[int, float]]:
        """Rank candidate videos for a user"""
        # Prepare user embedding (simplified)
        user_emb = self._compute_user_embedding(user_features)
        
        # Compute scores for each candidate
        scores = []
        for video_tensor in candidate_videos:
            video_emb = self._extract_video_embedding(video_tensor)
            
            # Compute similarity score (cosine similarity)
            score = torch.cosine_similarity(
                user_emb.unsqueeze(0), 
                video_emb.unsqueeze(0)
            ).item()
            
            scores.append((len(scores), score))
        
        # Sort by score descending
        return sorted(scores, key=lambda x: x[1], reverse=True)
    
    def _compute_user_embedding(self, user_features: Dict[str, torch.Tensor]) -> torch.Tensor:
        """Compute user representation from features"""
        # Simplified user embedding computation
        # In practice, this would involve multiple embeddings and fusion
        return torch.randn(512).to(self.device)  # Placeholder
    
    def _extract_video_embedding(self, video_tensor: torch.Tensor) -> torch.Tensor:
        """Extract single video embedding"""
        with torch.no_grad():
            # Forward pass through video understanding model
            video_emb = self.model(video_tensor)
            return torch.mean(video_emb, dim=1)  # Sequence-level embedding

# Example usage:
# engine = VideoUnderstandingInferenceEngine("video_model.pth")
# user_features = {"age": torch.tensor([25]), "interests": torch.tensor([1, 2, 3])}
# candidate_videos = [torch.randn(10, 512) for _ in range(5)]
# ranked = engine.rank_videos(user_features, candidate_videos)

6. Evaluation Framework

Offline Evaluation Methodology

For offline evaluation, we use temporal cross-validation to ensure our model generalizes across time and avoids data leakage. We split the dataset chronologically, with 80% for training, 10% for validation, and 10% for testing. This approach is critical because video content evolves over time and temporal patterns in user behavior are important for accurate predictions.

We implement a 3-fold temporal cross-validation where:

  • Fold 1: Train (Jan-Mar), Validate (Apr), Test (May)
  • Fold 2: Train (Feb-Apr), Validate (May), Test (Jun)
  • Fold 3: Train (Mar-May), Validate (Jun), Test (Jul)

This ensures robust evaluation that accounts for temporal drift in video content and user preferences.

Slice-Based Evaluation

We define critical slices to ensure model fairness and performance consistency across different user segments:

SliceDefinitionRationale
User DemographicsAge groups (18-25, 26-35, 36+)Different content consumption patterns
Geographic RegionUS, EU, APACContent relevance varies by region
Device TypeMobile, Desktop, TVViewing behavior differs by device
Content CategorySports, News, EntertainmentModel performance may vary by content type

We monitor key metrics (AUC, Precision@K) for each slice to detect performance degradation or bias.

A/B Test Design

We design a randomized A/B test with the following specifications:

Randomization Unit: User ID (to prevent spill-over between control and treatment groups)

Sample Size Calculation:

  • Target effect size: 2% improvement in CTR
  • Power: 80%
  • Significance level: 5%
  • Estimated baseline CTR: 2.5%
  • Expected sample size: ~18,000 users per group

Test Duration: 4 weeks (to capture weekly patterns and reduce seasonality bias)

Primary Metric: Click-Through Rate (CTR) on recommended videos

Secondary Metrics:

  • Watch time ratio
  • Video completion rate
  • User engagement score (normalized)

Guardrail Metrics and Blocking Conditions

We implement guardrails to prevent harmful rollouts:

MetricThresholdAction
CTR Drop< 1%Block rollout
User Engagement Drop< 5%Pause A/B test
Content Diversity< 10%Alert ML team
Model Confidence< 30%Re-evaluate model

Shadow Mode Rollout Plan

Before full A/B testing, we conduct shadow mode rollout:

  1. Phase 1 (Week 1): 5% of traffic routed to new model, no action taken
  2. Phase 2 (Week 2): 15% traffic, compare offline metrics to baseline
  3. Phase 3 (Week 3): 50% traffic, begin logging predictions for offline analysis
  4. Phase 4 (Week 4): Full A/B test launch with randomization

Long-Term Metric Measurement

We track delayed conversion and engagement decay:

  • Delayed Conversion: Measure 7-day and 30-day user retention after video recommendation
  • Engagement Decay: Monitor watch time decay over 24h, 7d, and 30d windows
  • User Lifetime Value (LTV): Track revenue contribution from users exposed to recommendations

We use cohort analysis to understand how different recommendation strategies impact long-term user behavior and monetization potential.

# Example A/B test evaluation logic in Python

def evaluate_ab_test_results(control_ctr, treatment_ctr, control_users, treatment_users):
    """
    Evaluate A/B test results for CTR improvement
    """
    import scipy.stats as stats
    
    # Calculate confidence intervals
    control_ci = stats.norm.interval(0.95, loc=control_ctr, scale=control_ctr * (1 - control_ctr) / control_users)
    treatment_ci = stats.norm.interval(0.95, loc=treatment_ctr, scale=treatment_ctr * (1 - treatment_ctr) / treatment_users)
    
    # Check for statistical significance
    p_value = stats.ttest_ind_from_stats(
        control_ctr, 
        control_ctr * (1 - control_ctr) / control_users,
        control_users,
        treatment_ctr, 
        treatment_ctr * (1 - treatment_ctr) / treatment_users,
        treatment_users
    ).pvalue
    
    # Return evaluation results
    return {
        "improvement": (treatment_ctr - control_ctr) / control_ctr * 100,
        "p_value": p_value,
        "confidence_intervals": {
            "control": control_ci,
            "treatment": treatment_ci
        },
        "statistically_significant": p_value < 0.05
    }

# Example usage:
# results = evaluate_ab_test_results(0.025, 0.026, 10000, 10000)
# print(f"Improvement: {results['improvement']:.2f}%")

7. Training Pipeline

Training Data Pipeline

The training pipeline begins with ingestion from multiple sources including:

  • Raw video data: Stored in cloud object storage (S3/GCS) with metadata in a centralized database
  • Manual annotations: Human-labeled video segments for action classification, object detection, and scene understanding
  • Weakly supervised signals: Automatic labeling from pre-trained models or crowd-sourced tags
  • Temporal consistency: Video clips with timestamps and metadata for temporal alignment
# Example data pipeline using PyTorch DataLoader
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms

class VideoDataset(Dataset):
    def __init__(self, video_paths, labels, transform=None):
        self.video_paths = video_paths
        self.labels = labels
        self.transform = transform
        
    def __len__(self):
        return len(self.video_paths)
    
    def __getitem__(self, idx):
        video_path = self.video_paths[idx]
        label = self.labels[idx]
        
        # Load video frames (simplified)
        frames = load_video_frames(video_path, max_frames=16)
        
        if self.transform:
            frames = self.transform(frames)
            
        return frames, label

# Data preprocessing pipeline
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

dataset = VideoDataset(video_paths, labels, transform=transform)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True, num_workers=4)

Distributed Training Strategy

For large-scale video understanding, we use data parallelism with model parallelism for attention modules:

  • GPU Cluster: 8x A100 GPUs (40GB VRAM each) for training
  • Model Parallelism: Split transformer layers across GPUs for attention computation
  • Pipeline Parallelism: For very deep models, pipeline stages can be distributed across multiple nodes

Training Infrastructure

  • Compute Cluster: 2x8-GPU nodes (16 GPUs total) with NVLink connectivity
  • Training Time Estimate: 2-3 weeks for full model training on 1M videos
  • Storage: SSD-backed storage with 50TB capacity for training data and model checkpoints

Hyperparameter Tuning Approach

We use Bayesian Optimization with Optuna for hyperparameter tuning:

import optuna
from sklearn.model_selection import cross_val_score

def objective(trial):
    # Hyperparameter search space
    lr = trial.suggest_float('lr', 1e-5, 1e-2, log=True)
    batch_size = trial.suggest_categorical('batch_size', [16, 32, 64])
    num_heads = trial.suggest_int('num_heads', 8, 16)
    hidden_dim = trial.suggest_int('hidden_dim', 512, 2048)
    
    # Model instantiation and training
    model = VideoTransformer(
        num_heads=num_heads,
        hidden_dim=hidden_dim
    )
    
    # Cross-validation score (simplified)
    score = cross_val_score(model, X_train, y_train, cv=3).mean()
    
    return score

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=50)

Experiment Tracking and Reproducibility

  • MLflow for experiment tracking, parameter logging, and model versioning
  • Git integration: Model checkpoints tied to specific git commits
  • Configuration files: YAML-based configuration management for reproducibility

Model Versioning and Registry Strategy

  • Model Registry: MLflow or custom registry storing model artifacts, metadata, and performance metrics
  • Versioning: Semantic versioning (v1.0.0, v1.0.1) with automatic rollback capability
  • Tagging: Models tagged by performance (AUC, NDCG), dataset version, and use case

Continual / Online Learning Design

For real-time adaptation to new video content:

  • Online learning window: Last 30 days of video data for incremental updates
  • Model drift detection: Using KL divergence between current and historical video feature distributions
  • Incremental training: Update model weights using recent batches with reduced learning rate (0.1x base LR)
  • Ensemble maintenance: Retain previous model versions for fallback during drift periods

The training pipeline is designed to handle 1M+ video samples with 200+ hours of training time, supporting both batch and incremental learning for evolving video content.

8. Monitoring & Observability

Data Drift Detection

Data drift detection is critical for maintaining model performance as input distributions change over time. We implement drift monitoring using:

  • PSI (Population Stability Index) for categorical features with thresholds at 0.1 (warning) and 0.25 (alert)
  • KL Divergence for continuous features with thresholds at 0.05 (warning) and 0.1 (alert)
  • Feature-wise drift detection for user demographics, video metadata, and interaction signals
import numpy as np
from scipy.stats import ks_2samp, entropy

class DriftDetector:
    def __init__(self, feature_names):
        self.feature_names = feature_names
        self.baseline_stats = {}
    
    def compute_psi(self, new_dist, old_dist, bins=10):
        """Compute Population Stability Index"""
        # Ensure same binning
        _, bins = np.histogram(np.concatenate([new_dist, old_dist]), bins=bins)
        
        new_hist, _ = np.histogram(new_dist, bins=bins, density=False)
        old_hist, _ = np.histogram(old_dist, bins=bins, density=False)
        
        # Avoid zero probability
        new_hist = np.clip(new_hist, 1e-6, None)
        old_hist = np.clip(old_hist, 1e-6, None)
        
        psi = np.sum((new_hist - old_hist) * np.log(new_hist / old_hist))
        return psi
    
    def detect_drift(self, new_data, old_data):
        drift_results = {}
        for feature in self.feature_names:
            if feature in new_data and feature in old_data:
                drift_results[feature] = {
                    'psi': self.compute_psi(new_data[feature], old_data[feature]),
                    'ks_test': ks_2samp(new_data[feature], old_data[feature]).pvalue
                }
        return drift_results

Prediction Drift and Label Drift Monitoring

Prediction drift monitors changes in model outputs that may indicate performance degradation:

  • Prediction distribution shift: Track distribution of predicted scores over time using statistical tests
  • Label drift: Monitor changes in actual labels for training data (if available)
  • Confidence drift: Track distribution of prediction confidence scores

Model Performance Degradation Alerting

We implement alerting thresholds for key performance indicators:

MetricWarning ThresholdAlert ThresholdMonitoring Frequency
AUC-ROC0.85 → 0.830.83 → 0.80Hourly
NDCG@100.75 → 0.720.72 → 0.68Daily
Precision@50.60 → 0.550.55 → 0.50Daily
Recall@1000.80 → 0.750.75 → 0.70Daily

Retraining Trigger Strategy

We implement a multi-trigger retraining strategy:

  • Scheduled retraining: Monthly model refresh for drift detection
  • Drift-triggered: Retrain when PSI > 0.25 or KL divergence > 0.1 for any feature
  • Performance-triggered: Retrain when AUC drops below 0.80 or NDCG drops below 0.65

Logging Design

Prediction logs capture all relevant information for debugging and retraining:

import json
from datetime import datetime

class PredictionLogger:
    def __init__(self, model_id):
        self.model_id = model_id
        self.log_file = f"prediction_logs_{model_id}.json"
    
    def log_prediction(self, user_id, video_id, features, prediction, confidence, timestamp):
        log_entry = {
            "timestamp": timestamp.isoformat(),
            "model_id": self.model_id,
            "user_id": user_id,
            "video_id": video_id,
            "features": features,
            "prediction": prediction,
            "confidence": confidence,
            "metadata": {
                "source": "production",
                "environment": "prod"
            }
        }
        
        with open(self.log_file, 'a') as f:
            f.write(json.dumps(log_entry) + '\n')

Feedback Loop for Continuous Improvement

We implement a feedback collection pipeline:

  1. User engagement signals: Track clicks, watch time, and completion rates
  2. Explicit feedback: Collect user ratings and preferences through surveys
  3. Re-ranking feedback: Monitor if users interact with recommended videos differently than expected
  4. Label correction pipeline: Identify and correct mislabeled training examples
class FeedbackCollector:
    def __init__(self):
        self.feedback_queue = []
    
    def collect_feedback(self, user_id, video_id, engagement_type, timestamp):
        feedback = {
            "user_id": user_id,
            "video_id": video_id,
            "engagement_type": engagement_type,
            "timestamp": timestamp.isoformat(),
            "feedback_source": "user_interaction"
        }
        
        # Store in feedback queue for batch processing
        self.feedback_queue.append(feedback)
        
        # If queue is large enough, process and update model
        if len(self.feedback_queue) >= 1000:
            self.process_feedback_batch()
    
    def process_feedback_batch(self):
        # Process feedback to identify patterns
        # Could trigger retraining or model updates based on feedback
        pass

Model Card Documentation

We maintain model cards with:

  • Performance metrics across different slices (user age, region, device type)
  • Data sources and preprocessing steps
  • Known limitations and edge cases
  • Usage guidelines and best practices

Incident Response Plan

For model failures or performance degradation:

  1. Immediate alert: Slack notification to ML team when thresholds breached
  2. Rollback capability: Maintain previous model versions for quick rollback
  3. Graceful degradation: Fallback to rule-based system when model fails
  4. Root cause analysis: Investigate drift, data quality issues, or infrastructure problems

Observability Dashboard

Key metrics dashboard includes:

  • Real-time prediction performance (AUC, NDCG)
  • Data drift indicators per feature
  • Model serving latency and error rates
  • Retraining schedule and success rate
  • User engagement metrics (click-through rate, watch time)

9. Responsible AI & Safety

Bias Analysis

  • Training data audit: Conduct comprehensive analysis of video content distribution across demographics, geographic regions, and cultural contexts to identify potential biases in representation. Key areas to examine include gender distribution in video content, cultural sensitivity in visual content, and geographic representation in training samples.
  • Slice-based fairness evaluation: Evaluate model performance across user segments (age groups, gender, region) and content categories to detect potential bias in video understanding capabilities. Monitor for disparities in accuracy rates across different content types or user demographics.

Fairness Metrics and Constraints

  • Demographic parity is prioritized for content recommendation fairness, ensuring equal video relevance scores across different user demographics
  • Equalized odds is applied to minimize false positive rates across content categories and user segments
  • Decision rationale: Content recommendation systems benefit from demographic parity to ensure equitable exposure across user groups, while equalized odds prevents systematic misclassification of content types

Explainability Requirements and Approach

  • SHAP values for video feature importance analysis to understand which visual/audio elements drive content classification decisions
  • Attention visualization for transformer-based video understanding models to show which temporal segments receive highest attention during content analysis
  • LIME-style explanations for individual video frames to provide granular reasoning for classification decisions

For LLM Systems: Safety Guardrails

  • Content filtering: Implement real-time content safety checks to detect and filter inappropriate video content (violence, explicit material, misinformation)
  • Output validation: Validate video understanding results against known content categories and cross-reference with external fact-checking APIs
  • Rate limiting: Enforce rate limits on video processing to prevent abuse and ensure system stability
  • Red teaming: Regular adversarial testing with diverse content samples to identify edge cases and safety gaps in video understanding models

Model Card Documentation Plan

  • Model metadata: Version, training data sources, model architecture details, performance metrics across different content categories
  • Usage guidelines: Recommended use cases, known limitations, and safety considerations for video understanding applications
  • Bias documentation: Detailed analysis of bias detection results and mitigation strategies implemented

Incident Response Plan for Model Failures or Harmful Outputs

  • Failure detection: Automated monitoring of abnormal prediction patterns, content drift, and unexpected classification behaviors
  • Immediate response: Isolation of affected model versions, fallback to rule-based content categorization, and alerting to ML team
  • Post-mortem process: Detailed investigation of failure root causes, including data drift analysis and model retraining when necessary
  • User impact mitigation: Communication protocol for users encountering inappropriate content classification and feedback collection mechanisms

10. Scalability & Reliability

Bottleneck Analysis per Pipeline Stage

Data Ingestion & Preprocessing:

  • Bottleneck: High I/O for video file retrieval and decoding
  • Back-of-envelope: 100k videos/day × 50MB average = 5TB/day storage, 200GB/s peak read bandwidth
  • Mitigation: CDN caching, parallel video decoding with GPU acceleration

Model Training:

  • Bottleneck: Distributed training synchronization overhead
  • Back-of-envelope: 10M videos × 100 features = 1TB training data; 500GB model size → 200ms sync overhead per batch with 100 GPUs
  • Mitigation: Gradient compression, pipeline parallelism

Serving Inference:

  • Bottleneck: Latency-sensitive real-time scoring
  • Back-of-envelope: 10k QPS × 50ms latency budget = 500GB/s bandwidth requirement for model serving
  • Mitigation: Model quantization, embedding caching, multi-stage pipeline

Scale-up Strategy

Horizontal Scaling of Serving:

  • Strategy: Deploy model replicas across multiple regions with load balancing
  • Implementation: Auto-scaling groups in cloud environments (AWS ECS, GCP GKE)
  • Metrics: CPU utilization, memory usage, request latency per replica

Distributed Feature Computation:

  • Strategy: Use Spark or Ray for distributed feature extraction from video streams
  • Implementation: Feature extraction jobs running on Kubernetes clusters with GPU workers
  • Metrics: Job completion time, worker resource utilization

Fault Tolerance

Model Serving Fallbacks:

  • Strategy: Implement graceful degradation to rule-based fallback when model fails
  • Implementation: Circuit breaker pattern in inference pipeline with fallback logic to pre-computed embeddings or basic content tags
  • Metrics: Fallback trigger rate, degradation in performance

Stale Model Serving on Failure:

  • Strategy: Cache last successful model version and serve with timeout fallback
  • Implementation: Model registry with version control and automatic rollback capability
  • Metrics: Model freshness, fallback success rate

Cold Start Handling Strategy

New Users:

  • Fallback: Use demographic-based content recommendations (age, location, language)
  • Implementation: Rule engine that returns popular videos from user's demographic segment
  • Metrics: Cold start coverage rate, engagement from fallback recommendations

New Items (Videos):

  • Fallback: Content-based retrieval using metadata tags and thumbnails
  • Implementation: Embedding similarity search with pre-computed video features from content metadata
  • Metrics: New item coverage, relevance of fallback recommendations

Multi-region Serving Strategy

Strategy: Deploy model replicas in multiple regions to reduce latency and increase availability

  • Implementation: Cloud provider multi-region deployment with traffic routing based on user location
  • Metrics: Regional latency, availability (99.9% uptime), cross-region sync delay

Cross-Region Consistency:

  • Strategy: Implement eventual consistency model with conflict resolution
  • Implementation: Distributed consensus protocol for model updates across regions
  • Metrics: Update propagation delay, consistency window, regional performance variance
# Example cold start handling logic in inference pipeline
class ColdStartHandler:
    def __init__(self):
        self.fallback_model = RuleBasedRecommender()
        self.embeddings_cache = {}
    
    def handle_new_user(self, user_profile):
        if not self.is_known_user(user_profile['user_id']):
            return self.fallback_model.recommend_by_demographics(user_profile)
        return self.model.serve(user_profile)
    
    def handle_new_item(self, video_metadata):
        if not self.is_known_video(video_metadata['video_id']):
            return self.fallback_model.recommend_by_content(video_metadata)
        return self.model.serve(video_metadata)

Reliability Metrics

System Availability:

  • Target: 99.9% uptime for core recommendation service
  • Monitoring: Service health checks, auto-healing mechanisms

Latency Reliability:

  • Target: 95th percentile < 200ms for user-facing requests
  • Monitoring: Request latency distribution, error rate tracking

Data Consistency:

  • Target: < 5% data drift in feature values over 30-day period
  • Monitoring: Feature drift detection, model retraining triggers

11. Open Questions, Risks & Trade-offs

Decisions requiring product / engineering / policy alignment

  • Content moderation integration: Should video understanding pipeline integrate with existing content moderation systems for real-time filtering? This requires cross-team coordination and policy alignment on what constitutes "harmful" content.
  • User privacy vs. model performance: How much personalization can we enable while maintaining user privacy? This affects whether to use user-specific embeddings vs. generic representations.
  • Multi-modal fusion strategy: Should we prioritize text + visual understanding or include audio features? This decision affects infrastructure costs and model complexity.

Key ML risks

RiskImpactLikelihoodMitigation
Data drift in video content distributionHigh (model performance degradation)MediumImplement continuous drift detection and automatic retraining triggers
Inconsistent labeling across annotatorsHigh (model bias)HighEstablish strict annotation guidelines and inter-annotator agreement thresholds
Model hallucination in video understandingMedium (user experience degradation)MediumImplement content filtering and human-in-the-loop validation
Cold start for new video categoriesMedium (relevance degradation)HighDesign robust embedding initialization and category expansion strategies
Privacy compliance violationsHigh (regulatory penalties)LowImplement comprehensive data anonymization and retention policies

Trade-offs made with justification

Trade-offJustification
Accuracy vs. latencyPrioritize sub-100ms serving latency for real-time video recommendation over 5% accuracy improvement, as user engagement drops significantly with >200ms delays
Freshness vs. costAccept 1-2 day data freshness for batch training to reduce infrastructure costs, while maintaining real-time feature updates via online store
Model size vs. qualityOpt for medium-sized transformer model (12M parameters) to balance between serving efficiency and understanding capability, rather than full-scale models
Online vs. batch processingUse hybrid approach with real-time features for user context and batch features for content embeddings to minimize latency while maintaining quality
Custom vs. pre-trained modelsUse pre-trained vision-language models as foundation with domain-specific fine-tuning to balance cost, performance, and time-to-market

Alternatives considered and rejected

  • Alternative 1: Pure vision-only model approach - Rejected due to insufficient semantic understanding of video content without textual context
  • Alternative 2: End-to-end training from raw video frames - Rejected due to high computational cost and complexity in handling temporal dynamics
  • Alternative 3: Rule-based content classification - Rejected due to poor scalability and inability to handle nuanced video semantics

Technical debt acknowledged

  • Feature store implementation: Current design assumes single source of truth, but may need to support multiple feature sources with versioning for future flexibility
  • Model retraining pipeline: Currently designed for batch retraining only, but should support incremental learning to reduce operational overhead
  • Cross-feature dependencies: Feature interaction handling is simplified and may not capture complex temporal or user-item dynamics in future iterations

12. Phased Implementation Roadmap

Phase 1: MVP (Weeks 1-8)

Objective: Establish baseline video understanding capability with rule-based fallback

Deliverables:

  • Rule-based content classification pipeline (audio/video metadata analysis)
  • Basic feature extraction from video frames and audio streams
  • Simple ML model (e.g., Logistic Regression) for content categorization
  • Online serving infrastructure with 100 QPS capacity

Dependencies:

  • Video ingestion pipeline (already available)
  • Basic feature store setup
  • ML training infrastructure

Critical Path:

  1. Video metadata extraction (Week 1-2)
  2. Rule-based content classification engine (Week 2-3)
  3. Simple ML model training pipeline (Week 3-5)
  4. Online serving infrastructure setup (Week 5-6)
  5. A/B testing framework integration (Week 6-8)

Phase 2: Scale (Weeks 9-16)

Objective: Expand to distributed training, real-time features, and A/B testing

Deliverables:

  • Distributed model training with data parallelism
  • Real-time feature computation pipeline
  • Feature store with online/offline synchronization
  • A/B testing infrastructure with statistical significance
  • Multi-model ensemble capability

Dependencies:

  • Feature store with real-time capabilities
  • Distributed training cluster (50+ GPUs)
  • A/B testing platform integration

Critical Path:

  1. Feature store architecture and implementation (Week 9-10)
  2. Distributed training pipeline (Week 10-12)
  3. Real-time feature computation engine (Week 12-13)
  4. A/B testing infrastructure (Week 13-14)
  5. Ensemble model deployment (Week 14-16)

Phase 3: Excellence (Weeks 17-24)

Objective: Advanced models, continuous learning, and production optimization

Deliverables:

  • Transformer-based video understanding model (e.g., ViViT)
  • Continuous learning pipeline with drift detection
  • Advanced real-time feature engineering (temporal context, user behavior)
  • Production-grade monitoring and alerting
  • Multi-region serving capability

Dependencies:

  • Advanced model training infrastructure (100+ GPUs)
  • Real-time monitoring system
  • Multi-region cloud deployment capability

Critical Path:

  1. Advanced model architecture design (Week 17-18)
  2. Continuous learning implementation (Week 18-20)
  3. Multi-region deployment (Week 20-21)
  4. Production monitoring system (Week 21-22)
  5. Performance optimization and tuning (Week 22-24)

Risk Mitigation Strategy:

  • Technical Debt: Regular code reviews and refactoring cycles every 2 weeks
  • Data Quality Issues: Automated data validation pipelines in each phase
  • Latency Requirements: Performance benchmarking at every phase with 50ms p99 target
  • Scalability: Horizontal scaling capability built into infrastructure from Phase 1

Success Metrics:

  • QPS capacity: 1000+ (Phase 2), 5000+ (Phase 3)
  • Model accuracy: 85%+ (content classification)
  • Latency: <50ms p99 for serving requests
  • A/B test statistical significance: 95% confidence level
  • System availability: 99.9% uptime

Resource Allocation:

  • Phase 1: 2 engineers, 1 ML engineer, 1 DevOps engineer
  • Phase 2: 3 engineers, 2 ML engineers, 1 DevOps engineer
  • Phase 3: 4 engineers, 3 ML engineers, 2 DevOps engineers

Rollout Strategy:

  • Phase 1: Internal testing with 5% of video content
  • Phase 2: Gradual rollout to 25% of traffic with A/B testing
  • Phase 3: Full production deployment with multi-region support

Was this article helpful?

Topics in this article

More from AI/ML System Design

Stay in the Loop

Get curated DSA patterns, coding problems, frontend and backend LLD/HLD, and AI/ML System Design insights delivered straight to your inbox. No spam, just focused interview and engineering content.