Capacity, conditional writes and streams
Estimate capacity before cost
On-demand mode charges for request units; provisioned mode allocates read/write capacity per second, with scaling options. For a single standard read, round item size upward in 4 KiB steps (4096 bytes); eventual consistency uses half the strong read units, transactional reads twice. Standard single-item writes round upward in 1 KiB steps (1024 bytes); transactional writes double that. Example: a 4500-byte item needs 2 strong read units, 1 eventual unit and 5 standard write units. An update charges based on the larger old/new item size. These are unit estimates, not dollars. Query, batch, failed conditions and index updates have additional accounting rules. Pricing depends on Region, table class, mode, storage, indexes, backups, replication, stream consumers and caching. Review current rates before any deployment; no live lab is needed.
Protect inventory with conditions
Suppose stock is 3 and two shoppers each request 2. A read followed by an unconditional write can use stale data. UpdateItem can atomically test a condition and change one item. The sketch below reduces stock only when enough remains. Use expression attribute names and values to bind data; validate a positive quantity. The first request leaves 1, the second fails without reducing stock. A conditional failure can still consume capacity. This condition prevents overselling in this single-item example, but a retried successful purchase could deduct twice if enough stock remains. Store a durable order/request identifier and make its creation conditional in the same TransactWriteItems operation as the decrement when an order and inventory must change together. Transactions are atomic within their supported scope, not across arbitrary Regions.
UpdateItem (illustrative expression, not a complete request)ConditionExpression: #stock >= :qtyUpdateExpression: SET #stock = #stock - :qtyNames: {"#stock": "stock"}Values (document notation): {":qty": 2}Before: 3; after first request: 1; second request: rejectedHandle expiry and change events
TTL uses a numeric Unix timestamp in seconds to mark an item for later deletion. Expiration is not immediate; filter expired records in application reads where required. DynamoDB Streams records item changes for up to 24 hours; consumers can use them to update a read model. Do not assume one global order across keys or exactly-once processing by your consumer. Make side effects idempotent and monitor lag, retries and failures. Streams is not a substitute for backups.
Plan scale, recovery and global access
Global tables replicate data across Regions. MREC uses eventual cross-Region consistency; MRSC offers strong consistency with different topology and feature limits (TTL is not supported there). Verify regional support before choosing either. DAX is a managed cache for suitable read-heavy workloads, not a fix for write contention or a strong-read cache. Enable and test backups according to recovery requirements: point-in-time recovery restores to a new table, so plan validation and cutover. Use least-privilege IAM for the application, encryption and operational metrics; investigate throttling by key distribution, capacity and index pressure. Practice: explain how to handle a duplicate order event and a failed stock condition without silently retrying an invalid purchase.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Capacity, conditional writes and streams — knowledge check
Apply DynamoDB concepts to fictional shopping workloads.
How many standard write units does a new 4500-byte item require?
Stock is 3; two requests for 2 use the atomic stock condition. What remains?
How do you prevent a repeated order from deducting stock twice?
An item expired one second ago but still appears. Why?
Which feature is suitable for restoring an earlier table state?