Everything You’ve Always Wanted to Know About DynamoDB But Didn’t Know to Ask
Tags: aws, database
Published: 2020-06-04 01:06:00

## What are the benefits to DynamoDB early in a project?

1. Instant setup and near instant ability to create new tables and add columns

1. **No charge unless break out of the generous free tier**

1. No need to pay attention to scaling — the relatively new [pay per request](https://aws.amazon.com/blogs/aws/amazon-dynamodb-on-demand-no-capacity-planning-and-pay-per-request-pricing/) billing mode handles everything

1. [DynamoDB streams](https://aws.amazon.com/blogs/database/dynamodb-streams-use-cases-and-design-patterns/) are very powerful compared to a relational DB’s triggers

## What are the costs of DynamoDB early in a project?

1. Eventually consistent requires thinking about long term scaling problems immediately. You can use [consistent reads](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html) but they are much slower and not available on [global secondary indexes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html) (GSI, an index other than the main key structure of your table).

So for instance say you do this query (written in [PynamoDB](https://pynamodb.readthedocs.io/en/latest/)):

    comments = CommentModel.market_id_index.query(hash_key=market_id, 
    attributes_to_get=[CommentModel.id,CommentModel.investible_id,                                                                                     
    CommentModel.comment_type,CommentModel.created_by,                                                                                
    CommentModel.updated_at,CommentModel.resolved,                                                                        
    CommentModel.version,CommentModel.uploaded_files])

That won’t get you all the comments — just all the comments currently in the market_id_index GSI. Eventually all comments will be in that GSI but not until DynamoDB has propagated the latest changes to all nodes.

2. Using DynamoDB streams means you will early on tackle having asynchronous APIs. Plus streams are not actually FIFO but just FIFO [per shard](https://aws.amazon.com/blogs/compute/new-aws-lambda-scaling-controls-for-kinesis-and-dynamodb-event-sources/) and shards are determined by your hash keys.

3. Speaking of keys you will need to [choose your keys carefully](https://aws.amazon.com/blogs/database/choosing-the-right-dynamodb-partition-key). You get a hash key and a range key that must combine to something unique and random enough to guarantee even distribution across shards. Unlike a relational database you also want to maximize data stored in a table — so long as your keys are useful you can have your columns be sparsely populated. Then the table will “heat up” and so be warm for retrieval. See [adjacency graph patterns](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html).

## What are the benefits to DynamoDB as your usage scales?

1. The simple SQL queries that were coded ignoring overall usage of the relational database and data growth of any one table will eventually require an entire operational team to simulate the out of the box behavior of DynamoDB.

1. The push for eventually consistent and asynchronous processing now pays off as you don’t have APIs promising synchronous results that increasingly you cannot deliver.

1. Upgrades are much easier as you don’t have to play tricks replicating data between database instances and unless [DynamoDB transactions](https://aws.amazon.com/blogs/aws/new-amazon-dynamodb-transactions/) were used your tables are likely more independent. A SQL “join” is the opposite of a services based solution.

1. Your transactional and reporting databases are probably already separate so you don’t need to painstakingly split them. Using NoSQL and DynamoDB streams you most likely copied the data over into an entirely different reporting or search database.

We will be covering how [Uclusion](https://www.uclusion.com/?utm_source=devto&utm_medium=blog&utm_campaign=devdynamodb) coded for eventual consistency and asynchronous APIs in later blogs.

## Why is MySQL still so much more popular than DynamoDB?

See for instance [this discussion](https://www.reddit.com/r/aws/comments/fchhac/eli5_when_should_you_use_rds_over_dynamodb), I believe that MySQL remains the devil developers know. Pay per request, transactions, encryption at rest and point in time recovery for DynamoDB only came out in 2018. Like a lot of AWS the decision to use DynamoDB relies as much on where the technology is going as where it is now. In this sense there is really no such thing as avoiding database vendor lock in — your choice of database technology will determine a lot of your application’s current architecture and your roadmap and your database’s roadmap will be on the same map.