Design a User Profile Ingestion System: Batch vs. Streaming
System DesignModel answerLast updated
Design an ingestion and processing pipeline for JSON user profiles, comparing batch file arrivals against a real-time event streaming paradigm.
You are designing an ingestion platform that processes complex user profile records emitted by upstream application services. The records arrive as JSON objects containing personal details, physical addresses, and nested payment instrument arrays.
An example payload is shown below:
{
"id": "u-001",
"name": "Sample User",
"address": {
"street": "1 Main St",
"city": "Springfield",
"postal_code": "00000"
},
"payment_methods": [
{
"type": "card",
"brand": "visa",
"last4": "1111",
"expires": "2029-08"
}
]
}
Compare and outline the architectural design for processing this data under two distinct ingestion paradigms:
- Batch Ingestion: Upstream systems periodically deposit files containing collections of these JSON records into object storage.
- Streaming Ingestion: Upstream systems push real-time change events to a message bus as users update their profiles or payment options.
Your architecture should address data storage, processing steps (flattening, validation, PII handling), idempotency/deduplication, out-of-order data handling, and schema validation.
Hints
Hint 1
Focus on how idempotency is guaranteed differently in file-based batch loads versus event-driven streaming.
Hint 2
Consider security and privacy: how are nested PII and payment data sanitized or tokenized during ingestion?
Hint 3
Think about ordering guarantees—how do you ensure late-arriving stream events do not overwrite newer state?
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
High-Level Architecture Overview
Processing semi-structured user records requires an architecture capable of sanitizing sensitive information (PII/PCI), handling nested fields (addresses, payment methods), ensuring data quality, and maintaining idempotency regardless of data delivery mechanics.
+-------------------+ +----------------------+ +----------------------+
| Batch Ingestion | ---> | Object Storage S3 | ---> | Batch Engine |
| (Scheduled Files)| | (Raw Landing Zone) | | (Apache Spark/Glue) |
+-------------------+ +----------------------+ +----------------------+
|
v
+-------------------+ +----------------------+ +----------------------+
| Streaming Source | ---> | Event Streaming Bus | ---> | Stream Engine |
| (Real-time CDC) | | (Kafka/Kinesis) | | (Spark Streaming) |
+-------------------+ +----------------------+ +----------------------+
|
v
+----------------------+
| Target Data Layer |
| (Iceberg / Snowflake|
| / Dead-Letter Q) |
+----------------------+
Core Processing Logic (Shared Across Both Paths)
To prevent code duplication, core transformations are encapsulated in a reusable pipeline library:
- Validation & Dead-Letter Queue (DLQ):
- Schema validation against a defined JSON schema.
- Malformed payloads or records missing mandatory primary keys (e.g.,
id) are redirected to an S3/Kafka DLQ for manual inspection or alert-triggered re-processing.
- PII and PCI Masking/Hashing:
- Names and addresses are hashed or tokenized if downstream consumers (e.g., product analysts) do not require raw identities.
- Credit card attributes are validated to ensure only non-sensitive tokens (e.g.,
last4,brand,expires) are present.
- Data Flattening & Modeling:
- JSON fields are exploded or modeled into relational/columnar-friendly tables:
- Users Table:
user_id,name,street,city,postal_code,updated_at. - User Payment Methods Table:
payment_id,user_id,type,brand,last4,expires.
- Users Table:
- JSON fields are exploded or modeled into relational/columnar-friendly tables:
Part 1: Batch Ingestion Architecture
1. Ingestion & Storage
- Landing Zone: Upstream jobs write compressed JSON files to an immutable S3 bucket (
s3://company-landing/user_records/YYYY/MM/DD/). - Manifests & Signals: Upstream emits an
_SUCCESSfile or an SQS/S3 Event notification once a file upload completes to prevent reading partial writes.
2. Processing & Transformation
- Orchestration: Apache Airflow triggers a daily or hourly Apache Spark job upon file arrival.
- Transformations: Spark reads the batch, applies validation, flattens
addressandpayment_methods, and hashes sensitive user attributes.
3. Idempotency & Failure Handling
- Partition Overwrites / Atomic Upserts: Data is stored in ACID storage formats like Apache Iceberg or Delta Lake.
- Atomic Merge: Upserts use
MERGE INTO target USING staging ON target.user_id = staging.user_iddriven by an upstream timestamp field (updated_at). - Retry Safety: Re-running a batch for date
YYYY-MM-DDproduces identical results without creating duplicate profile records.
Part 2: Streaming Ingestion Architecture
1. Transport & Partitioning
- Message Broker: Upstream services produce change events to Apache Kafka or AWS Kinesis.
- Partition Key: Messages are partitioned by
user_id. This guarantees that all updates for a specific user land in the same Kafka partition and are processed strictly sequentially by a single consumer thread.
2. Processing Engine
- Engine: Apache Flink or Spark Structured Streaming reads from Kafka with checkpointing enabled (e.g., every 10 seconds) to ensure exactly-once processing semantics.
3. Handling Out-of-Order & Late Data
- Stateful Deduplication & Upsert: Stream events may arrive out of order due to network retries.
- Timestamp Comparisons: The processing layer compares the event's internal
updated_attimestamp with the existing warehouse record.MERGE INTO analytics.users AS target USING stream_stage AS source ON target.user_id = source.user_id WHEN MATCHED AND source.updated_at > target.updated_at THEN UPDATE SET * WHEN NOT MATCHED THEN INSERT * - Compaction: Kafka topic log compaction is enabled for long-term retention of the latest state per key.
Key System Comparison
| Metric / Dimension | Batch Ingestion | Streaming Ingestion |
|---|---|---|
| Latency | Operational delay (Hours/Days) | Sub-second to seconds |
| Processing Engine | Spark SQL / Snowflake Tasks | Spark Structured Streaming / Flink |
| Idempotency Strategy | Atomic table partition merges | Watermarking & State-store deduplication |
| Ordering Guarantee | Implicitly handled per batch | Partitioning by user_id key |
| Infrastructure Cost | Low (Ephemeral clusters) | Higher (Continuous streaming nodes) |
| Failure Recovery | Simple re-run of failed partition | Checkpoint rollback + offset replay |
Common Pitfalls & Considerations
- Schema Drift: Upstream adding fields to
payment_methodswithout warning. Mitigation: Schema registry enforcement (e.g., Confluent Schema Registry / Glue Schema Registry). - Duplicated Array Records: Flattening arrays like
payment_methodswithout unique identifiers leads to duplicate rows on re-runs. Mitigation: Generate deterministic composite primary keys (e.g.,hash(user_id + last4 + brand)). - GDPR / Right-to-be-Forgotten: In continuous append-only logs, compliance deletes must propagate across both raw object storage and downstream tables via targeted tombstone events or vacuum commands.