🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Scaling Pipelines: Handling Petabytes, Real-time vs Batch

Data Scientist Role InterviewScaling Data Pipelines & Infrastructure⭐ Premium
⚡

Asked at Amazon & Meta

Scaling Pipelines

Handling Petabytes, Real-time vs Batch

The Interview Question

"You need to build a recommendation system that processes 100M user events per hour. How would you design the data pipeline?"

This question tests whether you can design systems that work at scale — not just in a notebook, but in production with real constraints.


Why Companies Ask This

â„šī¸

Amazon and Meta operate at scales most companies can't imagine. They need data scientists who understand distributed systems, can work with big data tools, and design pipelines that are reliable, efficient, and cost-effective.

Interviewers evaluate:

  1. System Design — Can you architect scalable solutions?
  2. Tool Knowledge — Do you understand distributed data tools?
  3. Trade-off Awareness — Can you balance latency, cost, and complexity?
  4. Production Mindset — Do you think about monitoring, failures, and recovery?
  5. Cost Consciousness — Do you optimize for both performance and cost?

Sizing the Problem on the Whiteboard

Interviewers do not expect a finished architecture in five minutes; they expect honest arithmetic. Start with the event rate: 100M user events per hour is roughly 27,800 events per second. At 400 bytes per serialized event, that is about 11 MB/s or 960 GB per day of raw traffic.

Now size the buffer. Seven days of retention is about 6.7 TB before replication; with a replication factor of three, budget roughly 20 TB of broker storage for the raw topic alone, plus headroom for consumer-lag recovery. Sizing 100 partitions means each partition carries around 280 events per second — comfortable for one consumer thread, while leaving room to scale consumers horizontally when a downstream job falls behind.

Then say what the numbers buy you. Partition count is your parallelism ceiling, retention is your replay window, and the serialization format decides whether a schema change next quarter is a non-event or an outage. Candidates who narrate this arithmetic sound like people who have been paged at 2 AM; candidates who jump straight to tool names do not.


The Pipeline Design Framework

Step 1: Understand Requirements

python
requirements_gathering = {
    'data_volume': 'How much data per day/hour/minute?',
    'latency': 'How quickly do we need results? (real-time, near-real-time, batch)',
    'freshness': 'How stale can the data be?',
    'reliability': 'What\'s the acceptable downtime?',
    'cost': 'What\'s the budget for infrastructure?',
    'complexity': 'What\'s the team\'s technical capability?',
}

Step 2: Choose Processing Paradigm

python
processing_paradigms = {
    'batch': {
        'description': 'Process data in large batches (hourly, daily)',
        'tools': ['Apache Spark', 'Hive', 'BigQuery', 'Redshift'],
        'latency': 'Minutes to hours',
        'use_cases': [
            'Daily reports',
            'Model training',
            'Historical analysis',
            'Cost-sensitive workloads',
        ],
        'pros': ['Efficient for large volumes', 'Easier to debug', 'Lower cost'],
        'cons': ['Higher latency', 'Not suitable for real-time'],
    },
    'streaming': {
        'description': 'Process data as it arrives',
        'tools': ['Apache Kafka', 'Apache Flink', 'Spark Streaming', 'Pub/Sub'],
        'latency': 'Milliseconds to seconds',
        'use_cases': [
            'Real-time recommendations',
            'Fraud detection',
            'Live dashboards',
            'Alerting systems',
        ],
        'pros': ['Low latency', 'Up-to-date results', 'Event-driven'],
        'cons': ['Higher complexity', 'Higher cost', 'Harder to debug'],
    },
    'micro_batch': {
        'description': 'Process data in small batches (every few seconds)',
        'tools': ['Spark Structured Streaming', 'Kafka Streams'],
        'latency': 'Seconds to minutes',
        'use_cases': [
            'Near-real-time analytics',
            'Aggregations',
            'Feature engineering',
        ],
        'pros': ['Balance of latency and efficiency', 'Easier than pure streaming'],
        'cons': ['Not truly real-time', 'Batch boundary issues'],
    },
    'lambda_architecture': {
        'description': 'Combines batch and streaming for completeness',
        'tools': ['Spark + Kafka + Cassandra'],
        'latency': 'Both batch and real-time',
        'use_cases': [
            'Systems needing both historical and real-time views',
            'Complex event processing',
        ],
        'pros': ['Complete data view', 'Fault-tolerant'],
        'cons': ['Complex to maintain', 'Two codebases'],
    },
}

Example: Recommendation System Pipeline

Architecture

python
recommendation_pipeline = {
    'data_sources': {
        'user_events': {
            'volume': '100M events/hour',
            'format': 'JSON',
            'source': 'Kafka topic',
        },
        'user_profiles': {
            'volume': '500M users',
            'format': 'Parquet',
            'source': 'Data warehouse',
        },
        'item_catalog': {
            'volume': '100M items',
            'format': 'Protobuf',
            'source': 'Service API',
        },
    },
    
    'ingestion_layer': {
        'tool': 'Apache Kafka',
        'partitions': 100,
        'replication_factor': 3,
        'retention': '7 days',
    },
    
    'processing_layer': {
        'real_time': {
            'tool': 'Apache Flink',
            'purpose': 'Real-time feature computation',
            'features': [
                'User\'s last 10 interactions',
                'Session-level aggregations',
                'Trending items',
            ],
        },
        'batch': {
            'tool': 'Apache Spark',
            'purpose': 'Model training and batch features',
            'features': [
                'User historical preferences',
                'Item similarity scores',
                'Collaborative filtering embeddings',
            ],
        },
    },
    
    'storage_layer': {
        'feature_store': {
            'tool': 'Redis or Cassandra',
            'purpose': 'Low-latency feature serving',
            'latency': '< 10ms',
        },
        'data_warehouse': {
            'tool': 'BigQuery or Redshift',
            'purpose': 'Historical data and batch analytics',
        },
        'model_registry': {
            'tool': 'MLflow or SageMaker',
            'purpose': 'Model versioning and deployment',
        },
    },
    
    'serving_layer': {
        'tool': 'TensorFlow Serving or custom API',
        'latency': '< 100ms for recommendations',
        'scaling': 'Auto-scaling based on traffic',
    },
}

Implementation

python
# Real-time feature computation with Flink (pseudocode)
class RealTimeFeatureEngine:
    """
    Compute real-time features from user event stream.
    """
    
    def __init__(self):
        self.feature_store = RedisFeatureStore()
        self.window_size = 3600  # 1 hour
    
    def process_event(self, event):
        """
        Process a single user event and update features.
        """
        user_id = event['user_id']
        item_id = event['item_id']
        timestamp = event['timestamp']
        
        # Update interaction history
        self.feature_store.append_to_list(
            f'user:{user_id}:recent_items',
            item_id,
            max_length=100,
            expiry=self.window_size
        )
        
        # Update item interaction counts
        self.feature_store.increment(
            f'item:{item_id}:interaction_count',
            expiry=self.window_size
        )
        
        # Compute session features
        session_key = f'user:{user_id}:session'
        self.feature_store.increment(session_key, expiry=3600)
        
        # Compute trending score
        self.update_trending_score(item_id, timestamp)
    
    def get_features(self, user_id, item_id):
        """
        Retrieve features for model serving.
        """
        features = {
            'user_recent_items': self.feature_store.get_list(
                f'user:{user_id}:recent_items'
            ),
            'item_interaction_count': self.feature_store.get(
                f'item:{item_id}:interaction_count'
            ),
            'user_item_similarity': self.compute_similarity(
                user_id, item_id
            ),
            'trending_score': self.feature_store.get(
                f'item:{item_id}:trending_score'
            ),
        }
        return features

# Batch feature computation with Spark
def compute_batch_features(spark, user_events_df, item_catalog_df):
    """
    Compute batch features for model training.
    """
    from pyspark.sql import functions as F
    from pyspark.ml.feature import VectorAssembler
    
    # User-level aggregations
    user_features = user_events_df.groupBy('user_id').agg(
        F.count('*').alias('total_interactions'),
        F.countDistinct('item_id').alias('unique_items'),
        F.avg('rating').alias('avg_rating'),
        F.stddev('rating').alias('rating_std'),
        F.max('timestamp').alias('last_interaction'),
        F.expr('percentile_approx(rating, 0.5)').alias('median_rating'),
    )
    
    # Item-level aggregations
    item_features = user_events_df.groupBy('item_id').agg(
        F.count('*').alias('total_interactions'),
        F.countDistinct('user_id').alias('unique_users'),
        F.avg('rating').alias('avg_rating'),
        F.expr('percentile_approx(rating, 0.5)').alias('median_rating'),
    )
    
    # User-item interaction matrix for collaborative filtering
    interaction_matrix = user_events_df.groupBy('user_id', 'item_id').agg(
        F.count('*').alias('interaction_count'),
        F.avg('rating').alias('avg_rating'),
    )
    
    return user_features, item_features, interaction_matrix

Real-time vs Batch Decision Framework

python
def choose_processing_paradigm(requirements):
    """
    Help choose between real-time and batch processing.
    """
    decision_factors = {
        'latency_requirement': {
            '< 1 second': 'streaming',
            '1-60 seconds': 'micro_batch',
            '1-60 minutes': 'batch',
            '> 1 hour': 'batch',
        },
        'data_volume': {
            '< 1GB/hour': 'micro_batch',
            '1-100GB/hour': 'streaming or micro_batch',
            '> 100GB/hour': 'streaming with careful design',
        },
        'cost_budget': {
            'low': 'batch',
            'medium': 'micro_batch',
            'high': 'streaming',
        },
        'team_expertise': {
            'batch_only': 'batch',
            'some_streaming': 'micro_batch',
            'expert': 'streaming',
        },
    }
    
    recommendations = []
    
    for factor, value in requirements.items():
        if factor in decision_factors and value in decision_factors[factor]:
            recommendations.append({
                'factor': factor,
                'recommendation': decision_factors[factor][value],
            })
    
    # Count recommendations
    from collections import Counter
    paradigm_counts = Counter(r['recommendation'] for r in recommendations)
    
    return {
        'primary_recommendation': paradigm_counts.most_common(1)[0][0],
        'all_recommendations': recommendations,
    }

Rules that survive the design review

Freshness requirements dominate every other factor. If the business can act on data that is 20 minutes old, a streaming stack is pure operational tax; if drivers reposition themselves based on demand signals, 20 minutes of staleness is worthless and streaming is non-negotiable.

Second rule: escalate reluctantly. Batch with incremental hourly runs handles a surprising share of "real-time" requests, and it is dramatically easier to debug and replay. Move to micro-batch when hour-level staleness starts showing up in product metrics, and to true streaming only when a user-visible latency budget forces it.

Third rule: price the decision in the room. A streaming cluster costs roughly two to three times a comparable batch stack once you count on-call load, and the debugging tax compounds for as long as the system runs. Bring that number to the design review instead of arguing about tool preferences.


Common Pipeline Patterns

1. Event-Driven Architecture

python
event_driven_architecture = {
    'pattern': 'Events trigger downstream processing',
    'example': 'User clicks item → Update features → Refresh recommendations',
    'tools': ['Kafka', 'EventBridge', 'Pub/Sub'],
    'pros': ['Loose coupling', 'Scalable', 'Auditable'],
    'cons': ['Eventual consistency', 'Complex debugging'],
}

2. CDC (Change Data Capture)

python
cdc_pattern = {
    'pattern': 'Capture database changes in real-time',
    'example': 'User updates profile → Capture change → Update feature store',
    'tools': ['Debezium', 'AWS DMS', 'Maxwell'],
    'pros': ['Real-time sync', 'No impact on source DB'],
    'cons': ['Schema evolution challenges', 'Ordering guarantees'],
}

3. Materialized Views

python
materialized_views = {
    'pattern': 'Pre-compute aggregations for fast queries',
    'example': 'Pre-compute daily active users by country',
    'tools': ['BigQuery Materialized Views', 'Spark + Parquet', 'ClickHouse'],
    'pros': ['Fast queries', 'Reduced compute cost'],
    'cons': ['Staleness', 'Storage cost'],
}

Amazon-Specific Pipeline Considerations

The "Two-Pizza Team" Pattern

python
amazon_pipeline_design = {
    'principle': 'Keep teams small and services decoupled',
    'application': 'Each pipeline component should be owned by a small team',
    'benefit': 'Independent scaling, deployment, and failure isolation',
}

AWS Services for Data Pipelines

python
aws_services = {
    'ingestion': ['Kinesis', 'MSK (Managed Kafka)', 'DynamoDB Streams'],
    'processing': ['EMR (Spark)', 'Glue', 'Lambda', 'Flink on KDA'],
    'storage': ['S3', 'DynamoDB', 'Redshift', 'Athena'],
    'orchestration': ['Step Functions', 'MWAA (Airflow)'],
    'monitoring': ['CloudWatch', 'X-Ray'],
}

Meta-Specific Pipeline Considerations

Scale and Speed

python
meta_pipeline_scale = {
    'daily_data': 'Petabytes',
    'events_per_second': 'Millions',
    'models_trained': 'Thousands per day',
    'serving_requests': 'Billions per day',
}

The "Move Fast" Pipeline Philosophy

python
meta_philosophy = {
    'principle': 'Ship quickly, iterate based on data',
    'application': 'Build pipelines that can be modified quickly',
    'trade_off': 'May sacrifice some efficiency for flexibility',
}

Failure Modes You Will Be Asked About

Once the happy path is designed, interviewers probe what happens when reality intrudes. Rehearse these five.

Duplicate delivery: at-least-once semantics mean every consumer must be idempotent. Key writes on an event identifier and design state updates so replaying the same event twice is a no-op.

Late and out-of-order data: event time and processing time diverge whenever a mobile client reconnects. Choose watermarks and allowed lateness explicitly, and say what happens to events that arrive after the window closes — drop them, side-output them, or trigger a recompute.

Backpressure: when a downstream store slows, an unbounded queue turns latency into data loss. Buffer depth, load-shedding rules, and alert thresholds are all fair game in the follow-up question.

Schema evolution: fields get added and types get renamed. A schema registry with backward-compatible changes keeps old consumers alive; ad-hoc payloads without contracts do not.

Replay: if logic changes retroactively, you need the raw history and a path to reprocess without downtime. Retention policy and a re-run plan are design decisions, not afterthoughts.


Common Mistakes to Avoid

âš ī¸

These mistakes can cause pipeline failures at scale:

  1. Not planning for failure — Networks fail, disks crash, services go down
  2. Ignoring data quality — Bad data propagates through pipelines
  3. Over-engineering — Start simple, add complexity as needed
  4. Not monitoring — You can't fix what you can't see
  5. Ignoring cost — Cloud costs can spiral without optimization
  6. Not testing — Test with production-like data volumes

Quiz: Test Your Understanding

Key Takeaways

  • Size the pipeline out loud: convert events per hour into per-second rates, storage with replication, and a partition count that buys parallelism, then explain what each number controls.
  • Freshness dominates the batch-versus-streaming choice, and escalating to streaming should require a user-visible latency budget, not a preference for newer tools.
  • Design for the unhappy path first — idempotent consumers, watermark policy, backpressure rules, schema contracts, and a replay path — because that is what the interview questions are about.
  • Every streaming decision has an operational price; quoting the cost and on-call load alongside the latency win is what separates a systems thinker from a notebook author.

Related Topics

🔒

Premium Content

Scaling Pipelines: Handling Petabytes, Real-time vs Batch

You've previewed the first section. Unlock this full lesson and 900+ advanced tutorials with a Premium plan.

đŸŽ¯End-to-end Projects
đŸ’ŧInterview Prep
📜Certificates
🤝Community Access

Already a member? Log in

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-17

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Advertisement