DynamoDB Magic Numbers: Optimize your DynamoDB spend with simple math
Learn some surprisingly simple tricks to save money on DynamoDB.
DynamoDB is magical! There is no other service where you can just show up, turn on load at tens of millions of TPS, and things just work. It is also addictive. Once teams start to use DynamoDB, they use it for everything. DynamoDB is so effective that it just blends into the background – you don’t even realize it’s there because it’s just not in the way. This is a dual-edged sword. In fact, DynamoDB works so well that over time, teams accrue untapped optimization opportunities because it just keeps working.
What if we were to tell you that these built-up optimization opportunities aren’t complicated to act on? What if you could wave a wand and activate cost and performance benefits? Well, you kind of can. To quote Alex DeBrie, “One of the things I love most about DynamoDB is how you can actually do the math to figure things out,” – and it’s simple math at that. It almost feels like magic.
So we’ve put on a little show – not to confuse you with sleight of hand, but to reveal some simple tricks to help you get the most out of DynamoDB. This blog is a breakdown of what we covered in our first lunch and learn on the topic and is focused on cost optimizations.
We also hosted a second lunch and learn diving into even more numbers. We’ll post a link to that recording and corresponding blog here when it’s available. But for now – read on!
6.94: Is DynamoDB provisioned throughput or on-demand right for you?
DynamoDB has two billing modes: provisioned throughput or on-demand. With provisioned throughput, you specify how much capacity you want ahead of time, whereas with on-demand you are charged for each operation without having to specify capacity ahead of time.
DynamoDB on-demand costs 6.94x the cost of provisioned capacity. But there is a catch! This assumes that you are consuming 100% of your provisioned capacity. In practice, 100% utilization of your IOPS is nearly impossible. Tight autoscaling rules and predictable traffic patterns like a diurnal cycle can get you a little north of 50%. However, if you don’t have the time or desire to mess with autoscaling rules or if you have particularly spiky traffic, on-demand IOPS may actually save you a ton of money.
How do you tell? It’s all there in CloudWatch – but here is how you can piece it together. There are two metrics that can help you learn your utilization: ConsumedWriteCapacityUnits (CWCU) and ProvisionedWriteCapacityUnits (PWCU).
Unfortunately, you cannot simply divide these numbers with each other to get the utilization. CWCU is an aggregate of all the capacity you consumed during a period of time, while PWCU is a per-second rate provisioned over 5-minute windows. Therefore, if you were running at 100% utilization, you would have to consume PWCU x 300 (300 is the number of seconds in a typical 5-minute period).
To determine your utilization over a 30-day period, you simply sum up your PWCU over 30 days, multiply it by 300, and divide it by the sum of CWCU over the same 30-day period. 30 days is a good span because it gives you longer-term data, allows you to assess the efficacy of auto-scaling, and blurs out high peaks. If the result is greater than 6.94, you may be better off with on-demand!
You could also reframe this as “what is the utilization of my IOPS?” To calculate that, simply flip the numerator and denominator in the above equation. If the result is below 14.4%, go on-demand. But remember that this doesn’t include your global secondary indexes (GSIs)!
2.4: Could you be saving money with DynamoDB Infrequent Access storage?
DynamoDB recently started offering an Infrequent Access (IA) storage class. IA gives you a whopping 60% discount on your DynamoDB storage ($0.10/GB vs $0.25/GB) in exchange for a mere 25% increase on your IOPS cost. As a thought exercise, if you spend the same on your storage as you do on your IOPS, moving to IA is a slam dunk! But it may still be a win as long as your IOPS cost less than 2.4x your storage cost.
An important caveat is that many tables don’t start this way, but as they collect more data over time, they cross the 2.4 threshold and become eligible for an IA win.
Let’s derive the 2.4 number. Suppose Jamie has a Standard DynamoDB table and her monthly bill is A for storage and B for IOPS each month. Her total monthly DynamoDB bill would be:
If she were to move to IA, her new bill would be:
And for IA to be a good deal:
TL;DR: If you are spending less than 2.4x storage on your IOPS, you are better off using IA.
To compute your IOPS cost, you just add your write IOPS and read IOPS cost together. The formula is exactly the same for both on-demand and provisioned capacity.
The magic show is just beginning!
There are many more simple tricks to maximize DynamoDB where these came from. We (Alex and Khawaja) have been sharing about them on X/Twitter and there’s a whole slew of them beyond cost optimization that we shared in another lunch and learn on this topic. We’ll update this blog when the recording link is available.