Scenario
advanced
Python Service Crashing with OOM in Kubernetes
Diagnose and fix a Python service being OOM-killed in Kubernetes by profiling memory growth and switching to streaming JSON parsing.
The Situation
Senior Python developer / backend engineering interviews
Context: Your Python service deployed in Kubernetes (256 MB memory limit) is being OOM-killed every few hours. The service reads and processes JSON payloads from a message queue.
Diagnosis approach:
# Step 1 — Add memory tracking
import tracemalloc
import resource
import logging
def log_memory():
usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
logging.info(f"Memory: {usage / 1024:.1f} MB")
# Step 2 — Profile what's growing
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
# ... process messages ...
snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot1, 'lineno')
for stat in top_stats[:5]:
print(stat)
# Common cause: loading entire JSON payload into memory
import json
# SLOW — loads entire file into memory
def process_large_json(path: str):
with open(path) as f:
data = json.load(f) # Entire 500MB JSON in RAM!
for record in data["records"]:
process(record)
# FIX — use ijson for streaming JSON parsing
import ijson
def process_large_json(path: str):
with open(path, "rb") as f:
for record in ijson.items(f, "records.item"):
process(record) # One record at a time — minimal memory
# FIX 2 — for message queues, process and acknowledge immediately
async def consume_messages(queue):
async for message in queue:
await process_single_message(message.body)
await message.ack() # Don't batch large amounts in memory
del message # Help GC reclaim memory immediately
What You Learned
- Diagnosing OOM kills with tracemalloc
- Streaming JSON parsing with ijson
- Processing message queues without buffering in memory
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form