Khawaja Shams Profile picture
Jan 30, 2023 18 tweets 4 min read Read on X
#DynamoDB strongly consistent reads are magical, but not for the reason most of us think.

SC reads have higher latencies, lower availability, and cost more - but worth it if you use them for the right reasons.

🧵👇explains what they are & how to unlock their real power!
0\ Primer: Doug Terry does a phenomenal job explaining the various consistency models through an illustrative exercise:
microsoft.com/en-us/research…

Read this first to become a consistency ninja!
1\ The most common mistake I see with DynamoDB SC reads is assuming it’s fresher than it is. Suppose you want to increment a counter: an SC Read tells you X=2 - you *cannot* set X=3 because another thread may have already changed it in the meantime. Use Conditional Puts instead!
2\ A strongly consistent (SC) read implies the data is at least as fresh as when you issued the request - and NOT when you receive the response or act on it. A lot can happen while the response is in transit.
3\ A good framework to think about this is to assume that your request made it to DynamoDB in zero time - but it took an extra minute to make it back to you. A lot can happen in a minute - but a lot can happen in 1ms too :).
4\ The SC reads are routed to the leader of a partition, which will give you the latest data while it is processing your request. However, all that can change as it serializes your response and some other request updates the underlying data.
5\ The second most common mistake is to assume EC reads are staler than they really are. This leads to superfluous use of an SC when you could have used an EC read. 📣 EC reads in DynamoDB are almost as fresh as SC reads. 👇
6\ DynamoDB leader only acks a write if it AND a 1 follower has gotten the update. So, there is a 66% chance that an SC request lands on the same node as an EC request. Lagging replicas usually catch up within milliseconds, which makes this very hard to observe.
7\ SC read before Conditional Puts is usually overkill too. This has an inconsequential impact on the likelihood of your Conditional Put succeeding. If it gets you more recent data than EC, chances are there is too much going on in your system to be using OCC.
8\ The third most common mistake with DynamoDB SC reads is to overlook their downsides! SC reads have higher latencies, lower availability, and cost double!
9\ DynamoDB SC reads have lower availability because they read from the leader. If the leader is unavailable/getting re-elected, you’ll get an error. They can also only support 1/3rd the throughput - so you are more likely to cause a brown out or throttle on your table with them.
10\ DynamoDB SC reads are slower than EC reads because the request router may have stale data on who the leader really is. “If read requests do not reach the Leader node on the first attempt, then strongly consistent reads may experience a higher latency.”
11\ So why would you use SC reads in DynamoDB? Monotonic reads! 👇
12\ Dynamodb strongly consistent reads ensure that you can only go forward in time (monotonic reads). Consider 2 serial EC reads.The first one goes to the leader - but the second one goes to a follower that’s behind. Time travel backwards can be bad! and creates odd behaviors👇
13\ If a shopping cart goes backwards in time, a customer may not see the item that they just added - OR - they may see an item that they just took out of the cart.

Similarly, my bank balance, chats, or scoreboards for a soccer game should only go forward in time.
14\ The lack of time travel makes SC reads magical. You can be doing an SC read at a to render a dashboard from 100 different lambdas. My screen will never go backwards in time despite the concurrency or lack of stickiness.
15\ You put the same workflow with EC reads - and once in a while, you will see a stale value. This could lead to confusion or erode consumer trust.
Fin.
SC reads are magical if you are using them for the right reasons. Understanding how they work makes you appreciate them even more. Thx @alexbdebrie @pj_naylor @akshatvig @somu_peri for helping w/ this 🧵. Stay tuned for a 🧵 on how to reason about DynamoDB transactions!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Khawaja Shams

Khawaja Shams Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @ksshams

Mar 21
RIP Redis: How Garantia Data pulled off the biggest heist in open source history.

Detailed blog linked - but checkout the thrilling story of this heist in 🧵first 👇

gomomento.co/redisos
Image
Redis launched in 2009 as a labor of love by Antirez. It won the hearts of developers globally - and it is still one of the most loved databases brands in the world.

In 2011, AWS launched ElastiCache Redis, & Garantia Data, an early stage startup, launched managed Redis. Image
Garantia Data had little to do with the development of Redis - and it was basically the same concept as ElastiCache, except ElastiCache grew like wildfire. Garantia - was a scrappy startup - not ready to give up despite the modest growth in comparison 👇 Image
Read 14 tweets
Oct 30, 2023
#DynamoDB is the most magical AWS Service, so @alexbdebrie & I decided to put on a magic show w/ @momentohq! #ddb_magic

Our favorite DynamoDB magic numbers in🧵will help you become an expert table magician by learning key internals!

What's your favorite magic number? Image
6.94
On-Demand IOPS cost 6.94x provisioned iops, but on demand is often cheaper! Why?
1\ 6.94 assumes 100% utilization. Nobody gets 100% utilization IRL!
2\ Autoscaling requires a predictable load cycle & fine tuning
3\ You can mix & match.
#ddb_magic another framing 👇
Image
Image
6.94
Instead of focusing on the price delta being 6.94x for provisioned capacity v On Demand, another framing is “what is the utilization of my IOPS?”
If your utilization is above 14.4%, use provisioned! Otherwise, OD

but #ddb_magic, how do you calculate utilization? 👇 Image
Read 16 tweets
Apr 26, 2023
DynamoDB GSI (Global Secondary Index) are magical if used properly.

A primer 🧵 on how they work to help you make the most of them…

👇

A GSI is basically another DynamoDB table. A table by any other name would have just as many partitions…
0\ You are listening to a DynamoDB Stream on table A. Whenever you see an item with a specific attribute, you insert it into table B with the attribute as the primary key.

👆 this is a GSI in a nutshell! Internalize this - and GSIs will make 💯 more sense..
1\ The only differences b/w what’s illustrated above & a real GSI is that a) GSI is managed for you, b) it gets updated much faster, c) it will prevent you from getting too far behind.

The rest of it is exactly the same. So what does that mean for GSIs?
Read 17 tweets
Apr 20, 2023
Getting started with just 5 lines of code was pretty neat, but how about zero?

Momento Console is here!

@dymxzvf personally worked on this alongside our awesome deveco team. DM us for early access to upcoming features.

#launch #serverless

gomomento.co/3Af3iH8
Getting started on the @momentohq console is as simple as using your existing Google/Github accounts to sign-on! Image
If you are a new customer, you may not have any caches - but not to worry, it takes 10 seconds to make one! Image
Read 4 tweets
Apr 3, 2023
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….👇
Read 17 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(