Design DynamoDB Streams (in-house)
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-05-08
Problem Statement
Your team uses a large-scale NoSQL key-value store (like DynamoDB). Business teams need real-time event notifications whenever data in a table changes (insert/update/delete). For example:
•A downstream cache needs invalidation when rows update.
•An analytics pipeline needs to process new events.
•A microservice needs to trigger workflows on inserts.
Instead of relying on AWS DynamoDB Streams, you are asked to design an in-house DynamoDB Streams system that captures table change events reliably and exposes them to consumers.
✅ Functional Requirements
1.Change Capture
•Capture changes (INSERT, UPDATE, DELETE) from DynamoDB tables.
•Provide event details: old values, new values, metadata (timestamp, partition key).
2.Streaming Semantics
•Guarantee ordered delivery per partition key.
•Support at-least-once delivery (avoid data loss).
3.Scalability
•Handle streams for millions of updates/sec across multiple tables.
•Partition events to support parallel processing.
4.Retention
•Store events for a limited duration (e.g., 24 hrs) so consumers can replay.
5.Consumer API
•Allow multiple consumers (e.g., Lambda-like workers, analytics jobs).
•Support checkpointing to track read progress.
•Pull-based API (like GetRecords()) or push-based subscription.
6.Reliability
•Ensure events are not lost even if nodes fail.
•Handle retries and deduplication gracefully.
❌ Non-Functional Requirements
•Low Latency: <1 second from table write → stream availability.
•High Availability: Survive node failures.
•Durability: Events must be persisted until consumed/expired.
•Multi-Tenancy: Multiple apps can consume same stream independently.
🧱 System Components
1.Change Capture Layer
•WAL (Write Ahead Log) replication or commit-log hook inside DynamoDB write path.
•Each write produces a change event.
2.Stream Storage Layer
•Append-only event log (like Kafka/Pulsar/Kinesis).
•Partitioned by table + partition key for ordering.
•Retention window (e.g., 24 hrs).
3.Stream Shards / Partitions
•Partition streams by table + hash key.
•Events in the same partition delivered in order.
4.Consumer Layer
•API to fetch records (GetShardIterator, GetRecords).
•Consumers maintain checkpoints (offsets) to resume after crashes.
•Support fan-out (many consumers per stream).
5.Monitoring & Scaling
•Auto-scale partitions/shards based on throughput.
•Expose metrics: lag, throughput, error rates.
🔄 Example Flow
1.App inserts {userId=123, action=login} into UsersActivity table.
2.Change Capture Layer appends event → UsersActivity_Stream partitioned by userId.
3.Event stored durably in Stream Log Storage (partition = shard-7).
4.Consumer (analytics job) calls GetRecords(shard-7) → gets the event.
5.Consumer checkpoint updated (offset = last read event).
6.Event expires after 24 hrs retention if not consumed.
⚖️ Trade-offs / Design Considerations
•Delivery Guarantees: At-least-once (simple) vs exactly-once (complex, requires dedup).
•Partitioning: By hash key (ordered but uneven load) vs round-robin (balanced but no ordering).
•Storage: In-house Kafka/Pulsar vs custom append-only logs.
•Fan-out Model: Shared stream with multiple checkpoints per consumer vs replicated streams per consumer.
•Retention Policy: Configurable (e.g., 24 hrs hot storage, archival for longer).
🧪 Open Discussion Points
•How to handle hot partitions (e.g., a few keys dominate traffic)?
•Should consumers support real-time push (webhooks) in addition to polling?
•How to integrate with downstream systems (Lambda-style triggers, ETL pipelines)?
•Do we allow schema evolution in events (new fields)?
👉 This problem tests distributed logs, consistency, ordering guarantees, scaling partitions, and consumer APIs — very similar to designing Kafka + DynamoDB Streams hybrid.