The result
A partitioned stream-to-storage workflow with a concrete restart and checkpoint path.
Stream-to-storage recovery with explicit checkpoints
Academic project · simulated market data
Made stream recovery inspectable through saved partition offsets and predictable batch files, carrying typed, simulated stock records from Kafka into HDFS and Parquet.
From the repository
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.
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.

Data Flow:
// 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.)
}
Topic: stock_prices
{
"batch_id": 19,
"offset": 10000
}
Simulates real-time stock market data:
stock_prices tableStreams 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:
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}
Production consumer with fault tolerance:
# Consume partition 0
python3 consumer.py 0
# Consume partition 2 (independent process)
python3 consumer.py 2
Implementation Highlights:
partition = TopicPartition('stock_prices', partition_id)
consumer = KafkaConsumer(bootstrap_servers='localhost:9092')
consumer.assign([partition])
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
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
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)
Initial State:
// partition-0.json
{"batch_id": 19, "offset": 10000}
Consumer Processing:
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://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)
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
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 .
export PROJECT=stock-streaming
docker compose up -d
docker exec -w /src stock-streaming-kafka-1 \
python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. report.proto
# 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
A production-ready demonstration of real-time data streaming with fault tolerance and exactly-once guarantees.
Continue exploring · 34 projects

Fullstack

ML & AI

ML & AI

ML & AI
Distributed SystemsViewing

Fullstack

ML & AI
Distributed Systems

Distributed Systems
Distributed Systems
Distributed Systems

Data Engineering
Data Engineering

Data Engineering

Data Engineering

Data Engineering

ML & AI
ML & AI
Data Engineering

Fullstack

Fullstack
Fullstack

Fullstack

Fullstack
Data Engineering
Computing Foundations

Fullstack

Fullstack
ML & AI
ML & AI

ML & AI
ML & AI
ML & AI
ML & AI