Skip to main content

DynamoDB

Basic Concepts

  • Table: A collection of data.
  • Item: A single record in a table.
  • Attribute: A single piece of data in an item.
  • Primary Key: A unique identifier for an item in a table.

Note: DynamoDB has several reserved words that cannot be used for attribute name. See the full list here.

Primary Key

You can have simple or composite primary keys.

  • Simple Primary Key: Consists of a single attribute (the partition key).
  • Composite Primary Key: Consists of two attributes (the partition key and the sort key).

Query vs. Scan

  • Query: Retrieves items based on primary key.
    • Allows for comparison operators
    • Fast
  • Scan: Scans the entire table
    • Slow, looks at all items
    • Filters applied to refine results after the scan
    • Not effective (or feasible) for large tables

Consistency

  • Eventually Consistency Read (default): Reads may not reflect the latest write.
  • Strongly Consist Read: Reads reflect the latest write.

Throughput

  • Read Capacity Units (RCUs): The number of reads per second.
  • Write Capacity Units (WCUs): The number of writes per second.

Per the AWS DynamoDB Documentation

RCUs

A single RCU represents one strongly consistent read per second, or two eventually consistent reads per second, for items up to 4 KB in size.

Math (from Nick Garner's course):

Strong reads / second=RCU(item size/4KB)\text{Strong reads / second} = \frac{RCU}{(\text{item size} / 4 \text{KB})} Eventual reads / second=RCU(item size/8KB)\text{Eventual reads / second} = \frac{RCU}{(\text{item size} / 8 \text{KB})}

Or as I'd like to think of it:

1 RCU=(1 strong reads/s)(item size in KB/4 KB)1 \text{ RCU} = (1 \text{ strong reads/s}) (\text{item size in KB} / 4 \text{ KB}) OR\text{OR} 1 RCU=(1 eventual reads/s)(item size in KB/8 KB)1 \text{ RCU} = (1 \text{ eventual reads/s}) (\text{item size in KB} / 8 \text{ KB})

In other words:

1RCU={0.250Sitem readssif strongly consistent0.125Sitem readssif eventually consistent1 RCU = \begin{cases} 0.250 * S_{item} \text{ } \frac{reads}{s} &\textbf{if } \text{strongly consistent} \\ 0.125 * S_{item} \text{ } \frac{reads}{s} &\textbf{if } \text{eventually consistent} \end{cases}

Where SitemS_{item} is the item size in KB.

Therefore, to compute the RCUs needed for a given read rate and item size, we can use the following Python function:

def compute_rcus(reads_per_sec, item_size_kb, consistent=True):
multiplier = 0.250 if consistent else 0.125
return multiplier * (reads_per_sec * item_size_kb)

WCUs

Again, per AWS's DynamoDB documentation:

One WCU can perform one standard write request per second for items up to 1KB in size.

Math:

Writes / second=WCU(item size/1KB)\text{Writes / second} = \frac{WCU}{(\text{item size} / 1 \text{KB})}

Or put differently:

1 WCU=(1writes/s)(item size in KB)1 \text{ WCU} = (1 \text{writes/s}) (\text{item size in KB})

In other words:

1WCU=Sitem writess1 WCU = S_{item} \text{ } \frac{writes}{s}

Where SitemS_{item} is the item size in KB.

Therefore, to compute the WCUs needed for a given write rate and item size, we can use the following Python function:

def compute_wcus(writes_per_sec, item_size_kb):
return writes_per_sec * item_size_kb

Scaling

DynamoDB Auto Scaling is enabled by default.

Secondary Indexes

  • Local Secondary Index (LSI): An index that has the same partition key as the table, but a different sort key.
  • Global Secondary Index (GSI): An index with a partition key and sort key that can be different from those on the table.

Local Secondary Index

TODO

Global Secondary Index

TODO

Useful Resources

Additional Reading