A customer was considering leaving #dynamodb due to costs.
@pj_naylor and I helped them save half a million dollars per year with one simple, magical, optimization.
Want to save DynamoDB costs like a pro? Let’s start with some background…
1\ DynamoDB writes are 20x more expensive than reads. If you have 5 GSIs, you may be doing 6 writes every time you write a single item into a DynamoDB table.
The 20x is not a typo. More on that soon.
2\ The read & write request units are a bit complex. Let’s simplify, @momentohq style.
DynamoDB writes are charged in 1KB increments.
If you write 100 bytes, you pay for 1KB. 1.1KB => 2KB.
Writing exactly 1KB items costs $1.25/GB (on-demand), but….👇
2b\ 100 byte item cost $12.5/GB (on-demand) to write to DynamoDB. 1.1KB items cost $2.27/GB to write.
100 byte item with 1 GSI costs $25/GB to write (on-demand).
Moral: item size has a profound, under appreciated, impact on your DynamoDB costs.
3\ Reads are measured in 4KB increments. This is why even though write requests are 5x more expensive than read requests, perfectly sized reads (4KB) are 20x cheaper than perfectly sized writes (1KB).
4\ To help our customer save half a million / year, the biggest optimization was to reduce their 1.2KB objects to be reliably under 1KB.
This is easier than it sounds!
1. Use binary data types + compression
2. Use numbers / bytes to express dates
3. Use shorter attribute names
5\ There are lots of ways to easily reduce your dynamodb item size.
The key is to recognize that dropping your item size by 10% can have a meaningfully larger impact on your total DynamoDB bill - especially if you have a write heavy workload!
6\ Reducing the number of attributes projected in your GSI can reduce the GSI item sizes to under 1KB too.
1. Optimize write costs first. If you’re read heavy, try @momentohq .
2. Reduce item sizes to be closer to 1KB increments.
3. Reduce unnecessary GSIs / reduce item sizes
7\ Next up, you may be tempted to go to provisioned capacity to save money over on-demand.
Is provisioned capacity actually cheaper? It comes down to one thing: utilization.
8\ DynamoDB provisioned capacity is sold in WCUs and RCUs (Write/Read Capacity Units). A WCU can write a 1KB object per second & costs $0.00065/hr
Let’s simplify, @momentohq style. Provisioned writes cost $.18/GB to write data, which is a steal over $1.25/GB on-demand. Right?
9\ Yup! DynamoDB provisioned capacity is 1/7th the cost of on-demand.
But you pay for this capacity whether you use it or not.
For provisioned capacity to be cheaper, utilization needs to be at least 15% - every single second.
This is harder than it sounds for spiky traffic!
10\ If your traffic follows a predictable pattern, you could use autoscaling to help right-size your provisioned capacity.
Setting up autoscaling is tedious - & you have to keep an eye on it… but, if your traffic patterns are stable, it is magical for improving utilization.
11\ If your load is not stable, you are likely better off using on-demand.
As counter intuitive as it may sound, I have seen numerous customers *save* money by going on-demand! It scales faster too - and it makes your system more robust.
12\ But wait. You can make your provisioned capacity billing even cheaper!
3 year reservations on WCU save 87% on IOPS! 80% for 1 year.
If you commit the capacity, you get much larger discounts than EC2 RIs on DynamoDB.
But is it worth it? Comes down to utilization 👇
13\ The reserved capacity is sold in the increments of 100s of WCUs/RCUs. The utilization here is even harder to get right!
You have to utilize all 100 of the WCUs/RCUs that you purchase for the year AND you have to utilize each second of throughput throughput the year, but…
14\ Reserved throughput that is consistently provisioned may end up being meaningfully cheaper and meaningfully simpler compared to complicating provisioned capacity w/ autoscaling!
If your usage is predictable, you can skip the autoscaling.
DynamoDB rocks! You can tune it to cost way less.
Have more questions? Want to optimize even more? Reach out to me or @pj_naylor.
Or just post your question on twitter. Lots of DynamoDB fans & experts ready to help you make the most of a great service.
Paging @Alexbdebrie
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
