# 3 Creating Elaborate DynamoDB tables

## 3.1 Storage consistency

DDB manages capacity

- 1 WCU (Write Capacity Unit) = 1 write of <=1 kB op per second
  - write sizes are rounded up to 1kB: 1 w/sec of 500B counts as 1 WCU
- 1 RCU (Read Capacity Unit) = 1 or 2 read of <=4 kB ops per second
  - read sizes are rounded up to 4kB: 1 r/sec of 500B counts as 1 RCU
  - 2 types of reads for 1 RCU:
    - eventually consistent: 2 read/sec
    - strongly consistent: 1 read/sec

DDB replicates all writes, and uses eventual consistency:

- eventual consistency uses the most available replicate, and is good for
  - data that may be stale
  - apps that do not query soon after writing
- strong consistency checks all replicas for the most recent update, and is food for
  - data that must be up-to-date
  - applications that have a pattern of reading after writing

![eventual consistency](3-1-eventual-consistency.png)

## 3.2 Calculating throughput capacity

- DDB provides adjustement of WCU/RCU capacity with no downtime, by setting
  minimum and maximum RCU/WCU expected for a table
- To calculate these, we need:
  - size of items in kB
  - quantity of items read/written per second to the table
  - required read consistency
- Example: app reads 10 eventually consistent items/sec around 1kB each
  - Round up item size to next 4kB -> 4kB = 1 Read Unit (RU)
  - RU * rps: 1 * 10 = 10 RCU
  - eventual consistency: divide RCU by 2 -> 5 RCU
- Example: app reads 10 strongly consistent items/sec around 14kB each
  - Round: 14kB -> 16kB = 4 RU
  - RU * rps: 4 * 10 = 40 RCU
  - strong consistency: don't divide -> 40 RCU
- Example: app writes 10 items around (below) 7kB/second
  - Round up : 7 -> 7 WU
  - WU * rps: 7 * 10 = 70 WCU

## 3.3 Table autoscaling

- Apps which exceed capacity will have additional request throttled as fails
- We don't want to over-provision constantly because of the cost
- In that case use on-demand autoscaling instead of provisioned autoscaling

|   Provisioned Autoscaling    |     On-demande Autoscaling      |
|:----------------------------:|:-------------------------------:|
| Predictable traffic patterns | Unpredictable traffic patterns  |
| Pay for provisioned capacity |    Pay for amount of request    |
|   Cap performand and price   | Unlimited performance and price |
