AWS DynamoDB - keshavbaweja-git/guides GitHub Wiki

DynamoDB is

  • Fully managed
  • NoSQL
  • Scalable
  • Cloud native
  • High performance
  • Distributed

Offload database administrative tasks to AWS

  • Hardware provisioning
  • Setup and configuration
  • Patching and upgrades
  • Replication
  • Scaling
  • Backup & restore
  • Encryption at rest

High availability and durability

Data is stored on solid state disks and automatically replicated across multiple availability zones in an AWS region.

DynamoDB Global tables provide capability to host data in sync across multiple AWS regions.

Core concepts

Each table has a primary key, a primary key uniquely identifies an item in the table. A primary key is composed of a mandatory partition key and an optional sort key.

Partition key is used to determine the partition (physical storage location) in which the item will be stored.

Sort Key enables range queries on sort key attributes for a given partition key. DynamoDB stores items with same partition key together sorted by sort key.

Each primary key attribute must be scalar. Only data types allowed for primary key are string, number or binary.

Secondary indexes allow queries against an alternate key, in addition to queries against primary key.

Global secondary index An index with partition key and sort key that can be different from those on table Each table has a quota of 20 global secondary indexes.

Local secondary index An index with same partition key as defined on the table but with a different sort key. Each table has a quota of 5 local secondary indexes.

Every secondary index belongs to a table, known as base table of the index.

DynamoDB maintains indexes automatically once defined.

When you create an index you specify what attributes from base table will be copied or projected to index. At a minimum, DynamoDB projects primary key attributes from the base table into the index.

DynamoDB Streams Near-real time, ordered stream of change data capture events from a DynamoDB table.

Each event is represented by a stream record. Stream records have a lifetime of 24 hours, they are automatically removed from stream after that.

DynamoDB stream can act as event source for Lambda invocation.

DynamoDB streams enable

  • Implementation of triggers by invoking Lambda functions
  • Cross region replication
  • Materialized views

API

Control plane

  • CreateTable

  • DescribeTable

  • ListTables

  • UpdateTable

  • DeleteTable

  • ListStreams

  • DescribeStream

Data plane

PartiQL (a SQL compatible query language)

  • ExecuteStatement (read multiple, write one)
  • BatchExecuteStatement (read/update/write multiple items)
  • ExecuteTransaction (multiple items within and across tables)

Classic API

  • PutItem

  • BatchWriteItem (write up to 25 items)

  • GetItem

  • BatchGetItem (retrieve up tp 100 items)

  • Query (retrieve all items for a partition key)

  • Scan (retrieve all items in a table or index)

  • TransactWriteItems

  • TransactGetItems

  • GetShardIterator

  • GetRecords

Read consistency

DynamoDB read operations are eventually consistent by default. It is possible to perform strongly consistent reads against DynamoDB.

Possible issues with strongly consistent reads -

  • Might fail in cases of network delay or outages
  • Higher latency than eventually consistent reads
  • Higher throughput capacity consumption
  • Not supported on global secondary indexes

Read/Write capacity mode

On demand mode

  • Flexible, pay-per-request pricing model
  • Automated scale up and down managed by DynamoDB
  • Good fit for unknown workloads, unpredictable traffic
  • Request rate is limited by DynamoDB throughput default table quotas

Read request unit

# Read request unit # Read Consistency mode Item size (max)
1 2 Eventually consistent 4 KB
1 1 Strongly consistent 4 KB
2 1 Transactional read 4 KB

Write request unit

# Write request unit # Write Mode Item size (max)
1 1 Default 1 KB
2 1 Transactional 1 KB

Provisioned mode

Suitable for predictable application traffic, when traffic ramps gradually.

Read capacity unit RCU

# Read capacity unit # Read Consistency mode Item size (max)
1 2 Eventually consistent 4 KB
1 1 Strongly consistent 4 KB
2 1 Transactional read 4 KB

Write capacity unit WCU

# Write capacity unit # Write Mode Item size (max)
1 1 Default 1 KB
2 1 Transactional 1 KB

Provisioned throughput is the maximum amount of capacity that an application can consume from a table or index. If your application exceeds your provisioned throughput capacity on a table or index, it is subject to request throttling.

DynamoDB Auto Scaling

DynamoDB auto scaling actively manages throughput capacity for tables and global secondary indexes. With auto scaling, you define a range (upper and lower limits) for read and write capacity units. You also define a target utilization percentage within that range. DynamoDB auto scaling seeks to maintain your target utilization, even as your application workload increases or decreases.

Reserved capacity

With reserved capacity, you pay a one-time upfront fee and commit to a minimum provisioned usage level over a period of time. Your reserved capacity is billed at the hourly reserved capacity rate. By reserving your read and write capacity units ahead of time, you realize significant cost savings compared to on-demand or provisioned throughput settings. Any capacity that you provision in excess of your reserved capacity is billed at standard provisioned capacity rates.