Skip to content
Vibhrav Jha.Let’s talk
All projectsDistributed Systems

Stock Streaming Engine

Stream-to-storage recovery with explicit checkpoints

My contributionProducer and consumer implementation, serialization, offset management and persisted output within the course project.

FieldDistributed Systems / Data Engineering

View repository

Academic project · simulated market data

Architecture diagram of simulated stock records flowing through Kafka to HDFS

The work
behind the interface.

Made stream recovery inspectable through saved partition offsets and predictable batch files, carrying typed, simulated stock records from Kafka into HDFS and Parquet.

  • Python
  • Kafka
  • Protobuf
  • MySQL
  • HDFS
  • Parquet
  • Docker

The result

A partitioned stream-to-storage workflow with a concrete restart and checkpoint path.

The problem

A streaming pipeline has to account for the moment between reading a record and publishing its output. I implemented a course-built system that sends typed, simulated stock records through Kafka into HDFS and Parquet, with explicit offsets and checkpoint recovery.

A decision worth examining

Make replay inspectable through recorded offsets and predictable output identity.

The approach

Protobuf defines the record format. The consumer resumes from saved partition offsets and uses deterministic batch names and temporary-file replacement when publishing output. Making data and checkpoint writes explicit exposes the failure boundaries that a replay strategy has to handle.

Read the README.

View original README
READMEArchitecture, setup and usage

Real-Time Stock Data Streaming Pipeline

A high-throughput streaming data pipeline using Apache Kafka for ingesting and processing stock market data. Features exactly-once semantics, crash recovery with checkpointing, and scalable storage via HDFS with atomic Parquet writes for analytical workloads.

Overview

This system simulates a real-time stock trading platform where high-frequency price data flows from a MySQL database through Kafka topics into HDFS for long-term analytics. It demonstrates production-grade streaming patterns including consumer groups, partition management, and fault-tolerant checkpointing.

Architecture

System Architecture

Data Flow:

  1. Price Generator → Simulates real-time stock ticks into MySQL
  2. Kafka Producer → Streams database records to partitioned topics
  3. Kafka Broker → Distributes messages across 4 partitions
  4. Kafka Consumers → Process partitions in parallel with crash recovery
  5. HDFS Storage → Atomic writes of Parquet files for analytics

Key Features

📊 High-Throughput Streaming

  • Partitioned Topics: 4 partitions for parallel processing
  • Protocol Buffers: Efficient binary serialization
  • Batched Writes: 500-record batches for optimal I/O
  • Configurable Retries: Up to 10 retries with acknowledgment guarantees

🔒 Exactly-Once Semantics

  • Idempotent Producer: Retries without duplicates
  • All-Replicas Acknowledgment: Ensures data durability
  • Atomic Checkpointing: Offset and batch ID written together
  • Crash Recovery: Seamless resume from last checkpoint

💾 Scalable Storage

  • HDFS Integration: Distributed file system for big data
  • Parquet Format: Columnar storage optimized for analytics
  • Atomic Writes: Temporary files prevent partial reads
  • Partition Isolation: Independent consumer processes per partition

🛡️ Fault Tolerance

  • Checkpoint Files: JSON-based offset tracking per partition
  • Automatic Recovery: Seeks to last committed offset on restart
  • Consumer Groups: Debug group for monitoring without affecting main consumers
  • Manual Assignment: Fine-grained control over partition consumption

Technical Stack

  • Streaming: Apache Kafka
  • Database: MySQL 8.0
  • Storage: Hadoop HDFS
  • Data Format: Apache Parquet (columnar)
  • Serialization: Protocol Buffers (gRPC)
  • Backend: Python 3.x (kafka-python, pyarrow, SQLAlchemy)
  • Containerization: Docker, Docker Compose

Data Model

Protocol Buffer Schema

// report.proto
syntax = "proto3";

message Report {
  string date = 1;    // Format: YYYY-MM-DD
  double price = 2;   // Stock price
  string ticker = 3;  // Symbol (AAPL, NVDA, etc.)
}

Kafka Topic Structure

Topic: stock_prices

  • Partitions: 4 (for parallel consumption)
  • Replication: 1 (single broker setup)
  • Key: Ticker symbol (ensures same stock always routes to same partition)
  • Value: Serialized protobuf Report message

Checkpoint Format

{
  "batch_id": 19,
  "offset": 10000
}

Pipeline Components

1. Price Generator

Simulates real-time stock market data:

  • Tickers: AAPL, MSFT, GOOG, AMZN, TSLA, NFLX, META, NVDA
  • Frequency: ~0.01 seconds per simulated day
  • Storage: MySQL stock_prices table
  • Start Date: 2000-01-01

2. Kafka Producer

Streams database records to Kafka with reliability guarantees:

producer = KafkaProducer(
    bootstrap_servers='localhost:9092',
    retries=10,  # Retry failed sends
    acks='all'   # Wait for all replicas
)

# Read new records from MySQL
for record in db_query(f"SELECT * FROM stock_prices WHERE id > {last_id}"):
    report = Report(
        date=record.date,
        price=record.price,
        ticker=record.ticker
    )
    
    # Send with ticker as key for consistent partitioning
    producer.send(
        topic='stock_prices',
        key=record.ticker.encode(),
        value=report.SerializeToString()
    )

Features:

  • Incremental reads (only fetch new records by ID)
  • Automatic retry on transient failures
  • Acknowledgment from all in-sync replicas
  • Protobuf serialization for efficiency

3. Debug Consumer

Monitoring tool for validating data flow:

consumer = KafkaConsumer(
    'stock_prices',
    group_id='debug',
    bootstrap_servers='localhost:9092',
    auto_offset_reset='latest'
)

for message in consumer:
    report = Report()
    report.ParseFromString(message.value)
    print({
        'ticker': report.ticker,
        'date': report.date,
        'price': f'{report.price:.4f}',
        'partition': message.partition
    })

Output Example:

{'ticker': 'AMZN', 'date': '2001-05-04', 'price': '209.0088', 'partition': 1}
{'ticker': 'TSLA', 'date': '2001-05-04', 'price': '205.6900', 'partition': 3}
{'ticker': 'NFLX', 'date': '2001-05-04', 'price': '198.4718', 'partition': 0}

4. Parquet Consumer with Crash Recovery

Production consumer with fault tolerance:

# Consume partition 0
python3 consumer.py 0

# Consume partition 2 (independent process)
python3 consumer.py 2

Implementation Highlights:

Manual Partition Assignment
partition = TopicPartition('stock_prices', partition_id)
consumer = KafkaConsumer(bootstrap_servers='localhost:9092')
consumer.assign([partition])
Checkpoint-Based Recovery
checkpoint_file = f'partition-{partition_id}.json'

if os.path.exists(checkpoint_file):
    with open(checkpoint_file) as f:
        checkpoint = json.load(f)
    consumer.seek(partition, checkpoint['offset'])
    batch_id = checkpoint['batch_id'] + 1
else:
    consumer.seek(partition, 0)
    batch_id = 0
Batched Parquet Writes
buffer = []

for message in consumer:
    report = Report()
    report.ParseFromString(message.value)
    buffer.append({
        'ticker': report.ticker,
        'date': report.date,
        'price': report.price
    })
    
    if len(buffer) >= 500:
        write_parquet_batch(buffer, batch_id)
        save_checkpoint(batch_id, consumer.position(partition))
        buffer.clear()
        batch_id += 1
Atomic File Writes
def write_parquet_batch(data, batch_id):
    path = f'hdfs://boss:9000/data/partition-{partition_id}-batch-{batch_id}.parquet'
    path_tmp = path + '.tmp'
    
    # Write to temporary file
    df = pd.DataFrame(data)
    with hdfs.open_output_stream(path_tmp) as f:
        df.to_parquet(f)
    
    # Atomic rename
    hdfs.move(path_tmp, path)

Crash Recovery Demonstration

Scenario: Consumer Crashes Mid-Batch

Initial State:

// partition-0.json
{"batch_id": 19, "offset": 10000}

Consumer Processing:

  1. Reads 250 messages (offset 10000-10249)
  2. CRASH - Process terminated
  3. No checkpoint written (batch incomplete)

Recovery on Restart:

# Load checkpoint
checkpoint = load_checkpoint('partition-0.json')
# {"batch_id": 19, "offset": 10000}

# Seek back to last committed offset
consumer.seek(partition, 10000)

# Re-process those 250 messages + 250 more
# Complete batch 20, write checkpoint
# {"batch_id": 20, "offset": 10500}

Result: No data loss, no duplicates in final Parquet files

HDFS Storage Structure

hdfs://boss:9000/data/
├── partition-0-batch-0.parquet   (500 records)
├── partition-0-batch-1.parquet   (500 records)
├── partition-0-batch-2.parquet   (500 records)
├── partition-1-batch-0.parquet   (500 records)
├── partition-1-batch-1.parquet   (500 records)
├── partition-2-batch-0.parquet   (500 records)
├── partition-2-batch-1.parquet   (500 records)
├── partition-3-batch-0.parquet   (500 records)
└── partition-3-batch-1.parquet   (500 records)

Analytics Query Pattern

Downstream analytics systems should read data safely:

# Read checkpoint to find last complete batch
with open('partition-0.json') as f:
    checkpoint = json.load(f)
    last_batch = checkpoint['batch_id']

# Only read complete batches
files = [
    f'partition-0-batch-{i}.parquet'
    for i in range(last_batch + 1)
]

# Compute analytics across all partitions
df = pd.concat([pd.read_parquet(f) for f in files])
average_price = df.groupby('ticker')['price'].mean()

Safety Guarantee: Never reads incomplete/corrupted files

Deployment

Build Images

docker build -f Dockerfile.hdfs -t stock-streaming-hdfs .
docker build -f Dockerfile.kafka -t stock-streaming-kafka .
docker build -f Dockerfile.namenode -t stock-streaming-nn .
docker build -f Dockerfile.datanode -t stock-streaming-dn .
docker build -f Dockerfile.mysql -t stock-streaming-mysql .

Start Services

export PROJECT=stock-streaming
docker compose up -d

Generate Protobuf Stubs

docker exec -w /src stock-streaming-kafka-1 \
  python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. report.proto

Launch Pipeline

# Start price generator
docker exec -d -w /src stock-streaming-kafka-1 python3 price_generator.py

# Start producer
docker exec -d -w /src stock-streaming-kafka-1 python3 producer.py

# Monitor with debug consumer (optional)
docker exec -it -w /src stock-streaming-kafka-1 python3 debug.py

# Start consumers (one per partition)
docker exec -d -w /src stock-streaming-kafka-1 python3 consumer.py 0
docker exec -d -w /src stock-streaming-kafka-1 python3 consumer.py 1
docker exec -d -w /src stock-streaming-kafka-1 python3 consumer.py 2
docker exec -d -w /src stock-streaming-kafka-1 python3 consumer.py 3

Performance Characteristics

Throughput

  • Producer: ~100-200 messages/second
  • Consumer: ~500 records/batch write
  • Latency: Sub-second end-to-end (MySQL → HDFS)

Scalability

  • Horizontal: Add more partitions for higher throughput
  • Consumer Parallelism: Independent processes per partition
  • Storage: HDFS scales to petabytes

Reliability

  • Message Durability: All-replicas acknowledgment
  • Consumer Fault Tolerance: Checkpoint-based recovery
  • Data Integrity: Atomic writes prevent corruption

Implementation Highlights

Producer Reliability

  • Retry Logic: 10 attempts before failure
  • Acknowledgment: Waits for all in-sync replicas
  • Idempotence: Prevents duplicate messages on retry

Consumer Patterns

  • Consumer Groups: Automatic load balancing (debug consumer)
  • Manual Assignment: Fine-grained control (main consumers)
  • Offset Management: Manual commits with checkpointing

Storage Optimization

  • Columnar Format: Parquet for analytics efficiency
  • Batched Writes: Reduces HDFS operations
  • Atomic Operations: Temporary files with rename

Error Handling

  • Connection Retries: MySQL and Kafka reconnection
  • Graceful Shutdown: Flush buffers on termination
  • Checkpoint Validation: JSON parsing with error handling

Skills Demonstrated

  • Stream Processing: Kafka architecture, topics, partitions
  • Message Reliability: Exactly-once semantics, idempotence
  • Fault Tolerance: Checkpointing, crash recovery, atomic writes
  • Data Engineering: ETL pipelines, protobuf serialization
  • Distributed Storage: HDFS integration, Parquet optimization
  • Consumer Patterns: Groups vs. manual assignment
  • Database Integration: SQLAlchemy, incremental reads
  • Container Orchestration: Multi-service Docker deployment

Technologies

  • Apache Kafka (streaming platform)
  • Apache Hadoop HDFS (distributed storage)
  • Apache Parquet (columnar format)
  • Protocol Buffers (serialization)
  • MySQL (source database)
  • Python 3.x (kafka-python, pyarrow, SQLAlchemy, pandas)
  • Docker & Docker Compose

A production-ready demonstration of real-time data streaming with fault tolerance and exactly-once guarantees.

Vyaya

  1. Aranyaka wildlife-image application interface

    Fullstack

    Aranyaka

  2. DermaNet hackathon prototype showing classification output and a generated explanation

    ML & AI

    DermaNet

  3. AI-generated concept illustration of source documents connected to retrieved evidence and an answer

    ML & AI

    Private-Data RAG

  4. Before It Breaks dashboard separating real weather from synthetic equipment telemetry and estimated costs

    ML & AI

    Before It Breaks

  5. Architecture diagram of simulated stock records flowing through Kafka to HDFS

    Distributed SystemsViewing

    Stock Streaming Engine

  6. Vyaya dashboard with clearly labeled synthetic demonstration data

    Fullstack

    Vyaya

  7. Study assistant interface with retrieved source context

    ML & AI

    Fine-Tuning vs. RAG

  8. Architecture diagram of NOAA data, a Cassandra cluster and a query service

    Distributed Systems

    Distributed Weather Data System

  9. Distributed Property Lookup Service project image

    Distributed Systems

    Distributed Property Lookup Service

  10. Architecture diagram connecting MySQL, a gRPC service and HDFS storage

    Distributed Systems

    HDFS & SQL Pipeline

  11. Architecture diagram of Spark processing, SQL and analysis

    Distributed Systems

    Spark Analytics

  12. Multi-Source ELT Pipeline project image

    Data Engineering

    Multi-Source ELT Pipeline

  13. Wisconsin schools in their geographic context — explanatory architecture diagram

    Data Engineering

    GCP Geospatial Pipeline

  14. Chinook Music Store SQL Analytics project image

    Data Engineering

    Chinook Music Store SQL Analytics

  15. Yelp Geospatial Analytics project image

    Data Engineering

    Yelp Geospatial Analytics

  16. E-Commerce Semantic Search Engine project image

    Data Engineering

    E-Commerce Semantic Search Engine

  17. Retail Sales Forecasting project image

    ML & AI

    Retail Sales Forecasting

  18. Making tool dependencies inspectable — explanatory architecture diagram

    ML & AI

    AI Agent Tool Dependency Graph

  19. From public records to operational questions — explanatory architecture diagram

    Data Engineering

    Palantir Foundry and AIP Build Challenge

  20. StudyFlow study-session dashboard

    Fullstack

    StudyFlow

  21. Habit Command Center planning dashboard

    Fullstack

    Habit Command Center

  22. Interfaces across web and mobile — explanatory architecture diagram

    Fullstack

    React Web & Mobile

  23. Startup Scout project image

    Fullstack

    Startup Scout

  24. Inventory-Based Recipe Planner project image

    Fullstack

    Inventory-Based Recipe Planner

  25. Concurrent Word Count and Performance Benchmarking project image

    Data Engineering

    Concurrent Word Count

  26. Data structures matched to their assumptions — explanatory architecture diagram

    Computing Foundations

    Algorithm Design

  27. Campus Navigator project image

    Fullstack

    Campus Navigator

  28. i-Songly: Song Management Engine project image

    Fullstack

    i-Songly

  29. An experiment that can resume — explanatory architecture diagram

    ML & AI

    CIFAR-100 with LeNet

  30. Fashion-MNIST Neural Network Classifier project image

    ML & AI

    Fashion-MNIST Classifier

  31. PCA Eigenfaces and Image Reconstruction project image

    ML & AI

    PCA Eigenfaces

  32. Two routes to the same regression objective — explanatory architecture diagram

    ML & AI

    Lake Mendota Ice Regression

  33. Bayesian Language Detection project image

    ML & AI

    Bayesian Language Detection

  34. Explanatory diagram of the video-fingerprint benchmark: 25 sources, 75 transformed variants and 1,875 comparisons.

    ML & AI

    Video-Fingerprint Benchmark

All projects