I’ve been using DynamoDB for many years, and I’ve occasionally run into situations where the lack of consistent reads on global secondary indexes is a pain. Since Global Secondary Indexes are effectively separate tables, which have writes replicated to them from the main table, it makes sense that they cannot support consistent reads.
In November 2018, DynamoDB introduced transactions. This means that instead of using a GSI, I could create my own GSI-like structure for a table by adding a new ‘secondary’ table with a different hash and sort key. I could update both of these tables in a single transaction, and I have now created a GSI which supports consistent reads.
So if I can manually implement a ‘secondary index table’ that supports consistent reads, why do we still not consistent reads for GSIs offered by Amazon?
Is there something I am missing about TransactWriteItems and read consistency on a table?
I’m unfamiliar with the specific implementation, but going only from what you’ve said. Perhaps it’s not considered an acceptable performance/cost for advertising. What I mean by that is if you’re managing multiple writes in a transaction to ensure consistency between the two tables with strong consistency you’re doubling your write cost and increasing the chance you hit high peak latency. While halving expected advertised iops. Would concerns about amplifying hot spotting be an issue in this scenario as well?
Maybe they just didn’t want to introduce and manage the customer expectation of pricing and latency guarantees to be like “If you enable strongly consistent GSI’s you’re doubling cost?”
The cost (in $ that is) of writing to a GSI is the same as the cost of writing to a secondary table. We already pay to write to GSIs as after all, they’re just a table and we must provision them as necessary. The cost of a consistent read on a GSI (if supported) would be priced exactly the same as any other DynamoDB consistent read.
There are also no concerns about halving advertised iops or amplifying hotspotting. A GSI is already simple a secondary table with exactly the same iops and hotspotting behaviour as a table.
I think everyone using DynamoDB understands that we pay double for a consistent read.
It all boils down to performance. You can’t hit near as many ops/sec using the transaction API as you can the put operation and allowing DynamoDB to update those GSIs. Granted it would be nice to have a flag on the operation that you could set to true and have the entire update be transactional, but I’m sure there are reasons that isn’t possible yet.
Is there something I am missing about TransactWriteItems and read consistency on a table?
Not about TransactWriteItems or read consistency, but about performance.
Performance in DynamoDB is fundamentally driven by having data and operations evenly distributed across partitions. When you use a GSI, you’re essentially asking DynamoDB to store a copy of your data with a different partitioning scheme.
If you want writes to the main table to be synchronous with the update to the GSI, then the maximum performance you could achieve would be the lowest between the two. The design of GSI is so that the operations on the main table shouldn’t be negatively affected by the presence of a GSI, so they are async. That’s why you have that consistency model.
In fact, in your example, your application’s use of DynamoDB will have its performance limited at the lowest performance between the “main” table and the “secondary” table you created. Which is perfectly fine! But it’s just not how GSI is designed.
I wouldn’t be surprised if whatever design they’re using under the hood to replicate data into GSIs was too difficult to work with their transaction features.
E.g if GSIs were internally built on something like DynamoDB streams it would be infeasible to support atomic writes and they’d need to rewrite all of their GSI handling.
I’m pretty sure this is the reason. OP’s question presumes that eventual consistency is inherently bad, but it’s not. It’s a conscious performance trade-off and they don’t see a problem with that.