Understanding your DynamoDB Single Table Performance
Tags: aws, serverless, dynamodb, observability
Published: 2021-09-16 07:54:03

---
title: Understanding your DynamoDB Single Table Performance
published: true
description: In depth monitoring insights into your single-table design patterns with DynamoDB.
tags: aws, serverless, dynamodb, observability
cover_image: https://www.sensedeep.com/images/sensedeep/table-single.png
canonical_url: https://www.sensedeep.com/blog/posts/stories/single-table-dynamodb-monitoring.html
---

Best practices for DynamoDB have evolved to favor single-table design patterns where one database table serves the entire application and holds multiple different application entities.

This design pattern offers greater performance by reducing the number of requests required to retrieve information and lowers operational overhead. It also greatly simplifies the changing and evolving of your DynamoDB designs by uncoupling the entity key fields and attributes from the physical table structure.

>However, managing single-table data and performance can often feel like you are peering at `Assembly Language`. Composite keys with prefixed and mapped attribute names are single-table design techniques but they can make just reading a single-table item quite difficult.

What is needed are tools that "understand" the single-table schema and can present and organize your data logically according to your application entities.

To meet this need we've created the [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) library which calculates and emits detailed single-table performance metrics for DynamoDB.

This post looks at our libraries [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics), [OneTable](https://www.npmjs.com/package/dynamodb-onetable) and the [SenseDeep](https://www.sensedeep.com) platform that understand your single-table design schema and can create and present detailed metrics to graphically show how your single-table designs are performing.

## Single Table Monitoring

So what are the kinds of questions that [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) can answer?

* Which single-table entity/model is causing the most load and is consuming the most RCU or WCU?
* Which customer tenant is causing the most load and how much should they be billed?
* Which app or function is causing what percentage of load on DynamoDB and is consuming the most RCU or WCU?
* Which queries are the most inefficient (items vs scanned) and by which app or model?
* Which operations are being used the most?
* Which entity is using performing scans or other operations?

These questions and others can be answered by using detailed metrics for DynamoDB that profile performance at an application entity/model level.

![Single Table](https://www.sensedeep.com/images/sensedeep/table-single.png)

## DynamoDB Metrics Features

The [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) library is an NPM module for Node applications that captures and emits detailed DynamoDB metrics. It has the following features:

* Creates detailed CloudWatch metrics for Tables, Indexes, Apps/Functions, Entities and DynamoDB operations.
* Emits metrics using CloudWatch EMF for zero-latency metric creation.
* Supports AWS V2 and V3 SDKs.
* Simple easy integration.
* Very low CPU and memory impact.
* Clean, readable small code base (<400 lines).
* Full TypeScript support.
* No dependencies.
* Optionally integrates with [SenseLogs the Serverless Logger](https://www.npmjs.com/package/senselogs) for dynamic control of metrics.
* Supported by the free [SenseDeep Developer Plan](https://www.sensedeep.com/) for graphical DynamoDB single-table monitoring.

## Metrics Galore

DynamoDB Metrics captures detailed statistics across 5 dimensions:

* Table &mdash; Per table metrics
* Tenant &mdash; Per tenant metrics
* Source &mdash; Per application, module or function identification
* Index &mdash; Primary or global secondary index
* Model &mdash; Application single-table entity / model name
* Operation &mdash; DynamoDB low-level operation: GetItem, PutItem, etc.

You can drill-down to see metrics aggregated by table, tenant, source, index, model or operation. This enables you to pin-point exactly where performance issues may be lurking.

For each of these dimension combinations, DynamoDB Metrics emits the following metrics:

* read — Read capacity units consumed
* write — Write capacity units consumed
* latency — Aggregated request latency in milliseconds
* count — Count of items returned
* scanned — Number of items scanned
* requests — Number of API requests issued

With these metrics, you can see precisely who is consuming read and write capacity, which requests are running long, which requests are inefficient and are scanning the table.


## How to get DynamoDB Metrics

There are two ways to get these wonderful single-table metrics for DynamoDB.

1. The [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) NPM library can be used by any Node application using DynamoDB. It is configured as AWS SDK middleware and efficiently captures request details with minimal overhead.

2. Alternatively, you can use the [OneTable](https://www.npmjs.com/package/dynamodb-onetable) library that has this support built-in and get all the other benefits of OneTable.


## DynamoDB Metrics

To configure DynamoDB Metrics, load the library and pass your DynamoDB client instance to the Metrics constructor. The other parameters tell Metrics how to understand your index and key structure.

```javascript
import Metrics from 'dynamodb-metrics'
const metrics = new Metrics({
    client: client,
    indexes: {primary: {hash: 'pk', sort: 'sk'}},
    separator: '#',
})
```

You can read more about how to configure Metrics at [DynamoDB Metrics README](https://github.com/sensedeep/dynamodb-metrics/blob/main/README.md).


## OneTable Support

To enable DynamoDB Metrics using OneTable, just add `metrics` to your OneTable constructor and specify the name of your application or Lambda function.

```javascript
const table = new Table({
    metrics: {source: 'acme:launcher'}
})
```

OneTable uses your defined OneTable schema to understand your key structure.


## How to View DynamoDB Single Table Metrics

You can view DynamoDB single-table metrics using CloudWatch or the SenseDeep Serverless Platform.

### Viewing via SenseDeep

SenseDeep has pre-configured dashboards and graphs to assist in visualizing your DynamoDB metrics. You can drill down and view metrics at the table, tenant, source, index, model or operation dimension level for any desired time period.

It is easy to see which application or function is consuming read/write capacity and how your app data entities are using DynamoDB.

![Single Table](https://www.sensedeep.com/images/sensedeep/table-single.png)

SenseDeep also provides intuitive capacity planning and provisioning assistance.

![Provisioning Table](https://www.sensedeep.com//images/sensedeep/table-provisioning.png)

## CloudWatch

Using [CloudWatch Metrics](https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#metricsV2:graph=~()), you can see cards for the DynamoDB Metrics dimension combinations.

![CloudWatch Dimensions](https://www.sensedeep.com/images/blog/cloud-watch-dimensions.png)

The metrics are created under the `SingleTable/Metrics.1` namespace. Clicking on a card provides a list of dimension combinations to graph.

![CloudWatch Metrics](https://www.sensedeep.com/images/blog/cloud-watch-graph-metrics.png)

## Under the Hood

The DynamoDB Metrics library emits metrics using the [CloudWatch EMF](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html) log-based metrics format. This permits zero-latency creation of metrics without impacting the performance of your Lambdas. EMF allows metrics to be emitted without blocking as would be the case with a normal blocking API.

DynamoDB Metrics will only emit metrics for dimension combinations that are active. If you have many application entities and indexes, you may end up with a large number of metrics. If your site uses all these dimensions actively, your CloudWatch Metric costs may be high. You will be charged by AWS CloudWatch for the total number of metrics that are active each hour at the rate of $0.30 cents per hour.

If your CloudWatch costs are too high, you can minimize your charges by reducing the number of dimensions. The dimensions emitted can be modified via the `dimensions` constructor property. Alternatively, you can dynamically enable and disable metrics via the LOG_FILTER parameter.

DynamoDB Metrics are buffered and aggregated to minimize the load on your system. If a Lambda function is reclaimed by AWS Lambda, there may be a few metric requests that are not emitted before the function is reclaimed. This should be a very small percentage and should not significantly impact the quality of the metrics. You can control this buffering via the Metrics constructor.


## Summary

Gaining insight into single-table design patterns is the new frontier. Previously, single-table designs with DynamoDB has been a black box and it has been difficult to peer inside and see how the components of your design are operating.

[DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) provides an easy way to instrument your code and gain these insights. SenseDeep provides a free developer plan so you can view and analyze these metrics with graphical dashboards.

## More?

Download [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics) from NPM at [DynamoDB Metrics](https://www.npmjs.com/package/dynamodb-metrics)

For the most elegant way to create single-table designs, consider [OneTable](https://www.npmjs.com/package/dynamodb-onetable).

And you can get a free developer license for SenseDeep at [SenseDeep App](https://app.sensedeep.com) or learn more at [https://www.sensedeep.com](https://www.sensedeep.com).

You may also like to read:

* [Data Modeling with Single Table Designs](https://www.sensedeep.com/blog/posts/2021/dynamodb-singletable-design.html)
* [DynamoDB OneTable](https://www.sensedeep.com/blog/posts/2020/dynamodb-onetable.html)
* [DynamoDB with OneTable Schemas](https://www.sensedeep.com/blog/posts/2021/dynamodb-schemas.html)

## About SenseDeep

SenseDeep is an observability platform for AWS developers to accelerate the delivery and maintenance of serverless applications.

SenseDeep helps developers through the entire lifecycle to create observable, reliable and maintainable apps via an integrated serverless developer studio that includes deep insights into how your apps are performing.

To try SenseDeep, navigate your browser to: [https://app.sensedeep.com](https://app.sensedeep.com/).

To learn more about SenseDeep please see: [https://www.sensedeep.com/product](https://www.sensedeep.com/product/).

Please let us know what you think, we thrive on feedback. [dev@sensedeep.com](mailto:dev@sensedeep.com).

### Links

* [SenseDeep Web Site](https://www.sensedeep.com/)
* [SenseDeep App](https://app.sensedeep.com/)