Consistency across all copies of data is usually reached within a second
Best for read perforamnce
A strongly consistent read always reflects all successful writes. Writes are reflected across all 3 locations at once.
Best for read consistency
DynamoDB Transactions provide the ability to perform ACID Transactions (Atomic, Consistent, Isolated, Durable) Read or write multiple items across multiple tables as an all or nothing operations.
Goo for payment process.
A unique attribute.
When partition key is not unique.
For example: Forum posts, Users post multiple messages.
Combination of:
2. A Unique Combination
Items in the table may have the same partition key, but they must have a differnet sort key.
3. Storage
All items with the same partition key are stored together and then sorted according to the sort key value.
You can also use a special IAM Condtion to restrict user access to only their own records.
IAM condition paramter dynamodb:LeadingKeys allow users to access only the itmes where partition key value matches their User_ID
Query based on an attribute that is not the primary key.
DynamoDB allows you to run a query on non-primary key attributes using global secondary indexes and local secodnary index.
1. Create table with Partition key and sort key
2. Add Local secondary Indexes (LSI)
Recall that for LSI, the partition key is the same as Primary key‘s partiion key, when this can only be created during creating new table.
Remeber to check the checkbox for LSI
3. After creating table, we can create Global secondary index
Recll that GSI can be created any time, the partiion key can be different from Primary key‘s partition key.
4. GSI can ONLY be used by CLI / SDK, not possible yet with GUI.
Therefore when you do Query, you can only see the LSI and Primary key.
A query find items in a table based on the primary key attribute and a distinct value to search for.
A scan operation examines every item in the table. By default, it returns all data attributes.
Use the ProjectionExpression parameter to refine the scan to only return the attributes you want.
DynamoDB Provisioned Throughput is measured in Capacity Units.
When you create your table, you specify your requirements in terms of Read Capacity Units and Write Capacity Units.
1 * 1KB write per second
1 * 4KB read per second
2 * 4KB per second
Your application needs to read 80 items per second, each item is 3 KB in size. You need strongly consistent reads. How many read capacity units will you need?
1. Calculate the size of each item / 4 KB -> 3KB/4KB = 0.75, round to 1, so need 1 read capacity unit
2. Multiply by the number of read operations per second -> 80 * 1 = 80
How about eventually consistent read?
Stongly consistent read / 2 -> 80 / 2 = 40
You want to write 100 items per scond. Each item is 512 bytes in size, how many write capacity units do you think you will need?
512byte/1KB = 0.5
round to 1
100 * 1 = 100
Doc: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html
DynamoDB provide SDK for many programming language, but if there is no such SDK for your project using langauge, you can use low-level API to using DynamoDB
The Amazon DynamoDB low-level API is the protocol-level interface for DynamoDB. At this level, every HTTP(S) request must be correctly formatted and carry a valid digital signature.
The low-level DynamoDB API uses JavaScript Object Notation (JSON) as a wire protocol format.
Requet Format:
POST / HTTP/1.1 Host: dynamodb.<region>.<domain>; Accept-Encoding: identity Content-Length: <PayloadSizeBytes> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.0 Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>, Signature=<Signature> X-Amz-Date: <Date> X-Amz-Target: DynamoDB_20120810.GetItem { "TableName": "Pets", "Key": { "AnimalType": {"S": "Dog"}, "Name": {"S": "Fido"} } }
Authorization
header contains information required for DynamoDB to authenticate the request. Need to sign the request using AWS access keys and Signature Version 4X-Amz-Target
header contains the name of a DynamoDB operation: GetItem
.GetItem
operation, the parameters are TableName
and Key
.
It is important to know about what apis you can use for DynamoDB.
Such as GetItem, BatchGetItem, PutItem, UpdateItem, ConditionalWrites, ConditionalDelete
Here we just memetion Conditional write/delete:
GSI:
GSI creates a NEW Table, when creating the GSI, you need to define RCU / WCU as well.
Solve the Hot Key Problem (too many reads)
TTL for 5 mins
DynamoDB Accelerator (or DAX) is a fully managed, clustered in-memory cache for DynamoDB.
Delivers up to 10x read performance improvement. Microsecond performance for millions of requests per second.
DAX is a write-through caching service. Data is written to the cache and the backend store at the same time.
This allows you to point your DynamoDB API calls at the DAX cluster first.
If the item you are query is in the cache (cache hit), DAX return the result to application.
If the item is not available (cache miss), then DAX performs an eventually consistent GetItem operation against DynamoBD and returns the result of the API call.
Caters for eventually consistent reads only.
Defines an expiry time of your data. Expired itmes marked for deletion in next 48 hours.
Good for trigger Lambda event.
Recoreds are not retroactively populated in a stream after enabling it. Only the future data after enabled stream.
Atomic is mainly increase / descrese by the number
Using DynamoDB, max size is 400KB for each item.
If larger than 400KB, you need to save the item into S3.
Then save the metadata into DynamoDB table.
Then clinet will get object from S3.
If we save itme into S3, we lost the search functionality. Way to solve it is trigger a Lambda function, write metadata into DynamoDB then we can do the searching in DynamoDB table.
Global Secondary indexes has a limit 20.
You need use filter, by default there is no filter.
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
Only `GetItem`, `UpdateItem`, `DeleteItem` for single item
And `BatchGetItem`, `BatchExecuteStatement`, `BatchWriteItem`.
(Item size / read size) * itemPreSecond / 2 (if eventually consistent)
[AWS - DA - Guru] DynamoDB Exam Tips
原文:https://www.cnblogs.com/Answer1215/p/14667218.html