What DynamoDB is
TrapDon't say DynamoDB lacks strong reads or transactions. It has both, when you ask for them.
GetItem takes the whole key, and Query takes one partition key. A Scan reads every item.ConsistentRead=True . A global secondary index (GSI) updates asynchronously, so its reads are always eventually consistent.GetItem or Query.TrapDon't say DynamoDB lacks strong reads or transactions. It has both, when you ask for them.
docker run -p 8000:8000 amazon/dynamodb-local Or run the free download with Java.endpoint_url="http://localhost:8000" The SDK still needs keys and a Region, but any values work.TrapDynamoDB Local ignores provisioned throughput and has no IAM. Test capacity, throttling and permissions on the real service.
Asked in interviews
AskedWhat are the differences between DynamoDB Local and DynamoDB in AWS?KeyType="HASH"KeyType="RANGE"HASH is the partition key, and RANGE is the sort key.TrapYou can't change a table's primary key later. A new key means a new table and a copy of the data.
S, Number N, Binary B, Boolean BOOL and Null NULL.L and Map M nest values. Sets are SS, NS and BS.{"id": {"S": "123"}}Each value names its type.TrapKey attributes must be String, Number or Binary. There's no date type, so store an ISO string or an epoch number.
Asked in interviews
AskedWhat are the main data types supported by DynamoDB?BatchGetItem reads 100 items or 16 MB. BatchWriteItem writes 25 items.TrapAttribute names count toward the 400 KB. Long names on every item cost storage and capacity.
Asked in interviews
AskedWhat are the DynamoDB service limits you should be aware of?get_item(Key=key) put_item(Item=item) It creates the item, or replaces one with the same key.SET price = :pAn UpdateItem expression. It creates a missing item too, so both calls upsert.SET #v = if_not_exists(#v, :zero) + :oneAn atomic counter. A retried call can count twice.delete_item(Key=key) batch_write_item(RequestItems=req) Retry the UnprocessedItems it returns.Tipviews is a reserved word. Write #v in the expression and map it with Expression.
TrapPutItem drops every attribute you leave out. Use UpdateItem to change only some.
Asked in interviews
AskedWhat is the difference between PutItem and UpdateItem in DynamoDB?PK = :pkA Query key condition. Items come back in sort key order.begins_with(SK, :prefix)ScanIndexForward=False scan()Its cost grows with the table, not with the result.FilterExpression="age > :a"DynamoDB applies it after the read.TipProjection returns fewer attributes, but you still pay to read the whole item.
TrapA filter doesn't lower the cost. Items it drops still use read capacity.
Asked in interviews
AskedExplain the difference between Query and Scan operations.Limit=25It counts items read, not items returned.ExclusiveStartKey=last_key Set it to the last response's LastEvaluatedKey . A response holds at most 1 MB.TrapWith a filter, a page can be empty while more items remain. Stop only when Last is missing.
SELECT * FROM Orders WHERE PK = 'U#1'It reads fast because WHERE names the partition key, with = or IN.BatchExecuteStatement . They must be all reads or all writes.TrapA SELECT without the partition key scans the whole table. An IAM policy can deny those scans.
Asked in interviews
AskedWhat is PartiQL and how does it work with DynamoDB?ConsistentRead=True It costs twice the read units. It works on tables and LSIs only.ReturnValues="ALL_NEW" Use the item the update returns, instead of reading it back.TrapA GSI query is always eventually consistent, and Consistent on it returns an error. Retry a missed new item with backoff.
attribute_not_exists(PK) attribute_exists(PK) version = :expectedOptimistic locking. Set version one higher in the same write.ConditionalCheckFailedException Nothing was written. Read the item again and retry.TipPass a condition as Condition. DynamoDB checks it and writes in one atomic step.
TrapA failed condition still uses write capacity.
Asked in interviews
AskedWhat are conditional writes and how do they work?transact_write_items(TransactItems=items) Across tables in one Region. Condition checks count toward the 100.transact_get_items(TransactItems=items) ClientRequestToken=token The token makes it idempotent for 10 minutes.TipFor bulk loads, use BatchWriteItem. It costs less, but some items can fail.
TrapDon't say 25 items. That limit rose to 100 in September 2022.
Asked in interviews
AskedWhat are DynamoDB transactions and when should you use them?TipPick a GSI for most new access patterns. An LSI fits only when you need strong reads in another sort order.
Asked in interviews
AskedWhat's the difference between GSI and Local Secondary Index (LSI)?GlobalSecondaryIndexUpdates Pass a Create action in UpdateTable.IndexName="ByStatus" KEYS_ONLYINCLUDEALLA GSI query can't fetch the others.TrapA GSI short of write capacity throttles writes to its base table too.
Asked in interviews
AskedWhat are Global Secondary Indexes (GSI) and when would you use them?premium_expires only on premium items, and index it.REMOVE premium_expiresDynamoDB drops it from the index.TipA sparse index holds fewer items, so it costs less to store and to query.
Asked in interviews
AskedWhat are sparse indexes and how are they useful?GetItem or Query.TrapA new access pattern later can mean a new GSI or a data migration. This design trades flexibility for speed.
Asked in interviews
AskedWhat are DynamoDB best practices for data modeling?Query on PK = USER#42.TrapDon't force it. If the app rarely reads entity types together, one table per entity is simpler.
ORDER# plus the date as sort key.PK and SK swapped, and query it on the course.TipFor queries that walk many hops of a graph, a graph database such as Amazon Neptune fits better.
TrapCopying a course title into each enrollment saves a read. But a title change must then update every copy.
SK BETWEEN :start AND :endDevice ID as partition key, an ISO 8601 time as sort key.delete_table(TableName=name) TrapNever make a date rounded to the day or hour the whole partition key. That period's writes all hit one partition.
Asked in interviews
AskedHow would you model time-series data in DynamoDB?chunk#0, chunk#1. Add a metadata item.Binary attribute. You can't filter or query on it then.TrapS3 and DynamoDB share no transaction. Clean up S3 objects that a failed write leaves behind.
Asked in interviews
AskedHow do you handle large items in DynamoDB?TipDynamoDB rounds item size up to the next 4 KB for reads and 1 KB for writes. Transactions use twice the units.
Asked in interviews
AskedWhat are Read Capacity Units (RCU) and Write Capacity Units (WCU)?BillingMode="PAY_PER_REQUEST" On-demand. AWS now calls it the default for most workloads.BillingMode="PROVISIONED" Provisioned, billed by the hour. Add auto scaling, and reserved capacity for steady loads.TableClass="STANDARD_INFREQUENT_ACCESS" Standard-IA. Storage costs less, but reads and writes cost more.TrapOn-demand still throttles. Traffic that more than doubles its previous peak within 30 minutes can be throttled.
ProvisionedThroughputExceededException TrapLarge items throttle sooner, because each request uses more capacity units.
user_id.status, or one value far busier than the rest.TipFor a key that gets heavy reads, cache it with DAX or in the app.
TrapDynamoDB can't split one partition key's items across partitions when the table has an LSI.
UpdateTimeToLive with the name of the expiry attribute."expires_at": int(time.time()) + 3600A Number, in Unix epoch seconds. TTL ignores other types.expires_at > :nowAdd it as a filter expression.TipTTL deletes reach the table's stream as service deletions, so a consumer can archive them.
TrapDynamoDB deletes an expired item within a few days, not at once. Reads still return it until then.
Asked in interviews
AskedHow do you implement Time to Live (TTL) in DynamoDB?StreamViewType="NEW_AND_OLD_IMAGES" The other view types are KEYS_ONLY, NEW_IMAGE and OLD_IMAGE.TipUse streams for cache invalidation, audit trails, analytics and copies to another store.
TrapA stream keeps records for 24 hours. A consumer that falls further behind loses them.
Asked in interviews
AskedWhat are DynamoDB Streams and how are they used?TipThe DAX client has DynamoDB's API, so switching takes few code changes.
TrapWrites that bypass DAX leave its cache stale until each entry's TTL runs out. Writes never update its query cache.
Asked in interviews
AskedWhat is DynamoDB Accelerator (DAX) and when should you use it?MREC confirms a write before other Regions have it, so a Region failure can lose a few seconds. MRSC waits for another Region, so writes are slower.
TipEach replica uses write capacity for every write. Read capacity can differ per replica.
TrapUnder MREC, two Regions can write the same item, and the last writer wins. Give each item one home Region.
Asked in interviews
AskedWhat are Global Tables and when would you use them?PointInTimeRecoveryEnabled=True Point-in-time recovery (PITR). You choose a window of 1 to 35 days.create_backup(TableName=t, BackupName=name) An on-demand backup.TipBackups use no table capacity, so they don't slow the table.
TrapA restore brings back the data and indexes, but not TTL, streams or auto scaling. Set those again.
Asked in interviews
AskedWhat are the different backup and restore options in DynamoDB?DeletionProtectionEnabled=True TrapReplication isn't a backup. A bad delete reaches every Region, so keep point-in-time recovery on.
Asked in interviews
AskedHow do you design for high availability and disaster recovery?dynamodb:LeadingKeys Fine-grained access control. This IAM condition matches the partition key to the user's ID.TipIAM policies decide who calls which actions on which tables. CloudTrail logs every call.
Asked in interviews
AskedHow do you secure DynamoDB tables?ConsumedReadCapacityUnits ConsumedWriteCapacityUnits ReadThrottleEvents WriteThrottleEvents WriteKeyRangeThroughputThrottleEvents Or turn on CloudWatch Contributor Insights.SuccessfulRequestLatency TrapThrottled and SystemErrors carry an operation dimension. An alarm on the table name alone never gets data.
Nothing matches. Try a shorter word, or clear the search.
With Pro, you can:
Already have Pro? Sign in