Wednesday, 15 September 2021

Amazon DynamoDB

Amazon DynamoDB Indexes

Primary Key

There are two kinds of primary key:

1. Partition key (aslo called hash attribute) only

2. Partition key and sort key. This kind of primary key is also called composite primary key.

The name of partition key comes from the fact that items have the key is mapping to a physical address in storage. For example, in composite primary key case, items which have the same partition key and different sort key are stored together with sorted order by sort key.

Secondary Indexes

1. Global secondary index. Can be created on items which have different partition key. Basically, can use this index to search whole table.

2. Local secondary index. Items need to have the same partition key and different sort key. Most time, this index is useless.

Secondary Index is also a table. You also need to specify primary key(partition key only or composite primary key). Also need to specify projected attributes from the base table. The primary key of the base table will be projected automatically.

Noted: In a DynamoDB table, each key value must be unique. However, the key values in a global secondary index do not need to be unique.

Using Global Secondary Indexes in DynamoDB

Amazon Dynamodb Schema

For attributes used in primary key and indexes, you need to define these attributes. However, you can only define these attributes. In fact, if you define other attributes and try to create a table, you will get error that said attributes do not match. This is because Dynamodb is a schemaless database and you can save attributes you do not define.

This is a good explanation about DynamoDB query how to query DynamoDB

Create Dynamodb table in AWS

One option is to create a cloudformation template for this table. Then deploy template


aws cloudformation deploy --template /path_to_template/my-template.json --stack-name my-new-stack --parameter-overrides Key1=Value1 Key2=Value2 

PHP code snippet to query DynamoDB

//define expression attribute names and expression attribute values. It is a key/value map
//in PHP, we use associate array as a map
//For expression attribute names, need to start with pound sign(#)
//For expression attribute values, need to start with colon(:). Also need to define type.
//Here use marshalValue to add a type. e.g {":agelimit": {"N": 21} }
$attributeNames["#typeAndStatus"] = "typeAndStatus";
$attributeValues[":typeAndStatus"] = self::$marshaller->marshalValue($typeAndStatus);
$attributeValues[":now"] = self::$marshaller->marshalValue(time());

//pass the above expression attribute names and expression attributes values in query
$queryAttributes['ExpressionAttributeNames'] = $attributeNames;
$queryAttributes['ExpressionAttributeValues'] = $attributeValues;

//Therefore, for KeyConditionExpression, we can use expression attribute names and expression attribute values
$queryAttributes['KeyConditionExpression'] = "#typeAndStatus = :typeAndStatus and nextRunTime < :now"

//query the Dynamodb client
$result = self::$client->query($queryAttributes);

DynamoDB

You can set up a DynamoDB local using Dockfile provided by AWS. After set up, you can admin db

Calculation of WCU and RCU

It is speed of size

  • One WCU is 1 KB per second. RCU is 4 KB per second. Also RCU counts on strongly consistent read
  • Item size will be round up
  • For RCU: one strongly consistent read. (== 2 eventaully consistent read)

Global Table

  • Active to Active replication between multiple region
  • Can take up to 2 seconds to do replication
  • aws:rep:updateregion contains the region in which the update was made

See ReplicationLatency Metrics

  • Go to Cloudwatch Service
  • click all metrics tab
  • Choose DynamoDB
  • Choose ReceivingRegion, TableName
  • Find Table and check replicationLatency
  • It will show up in the above chart

No comments:

Post a Comment