DynamoDB - ecommerce filtering access patterns causing skyrocketing costs

I am building a price comparison site that will have around 50,000 products. I have built product pages, price tracking. Up until this point my access patterns have allowed DynamoDB to be read heavy (good for costs). However, I have reached the point where I want to implement filtering of products and create my product catalogue (listing) pages.

I have 2 critical criteria:

  1. Prices must be correct (they are updated daily)
  2. I must not go bankrupt

I am struggling to create partition keys and sort keys that do both.

Access patterns:

AP1: Get product by category

AP2: Get product by category and sub-category

AP3: Get product by category, sub-category, rating and price

My initial idea was to have duplicate items for category products with a 1 day TTL to ensure price freshness.

AP1 Partition Key:

CATEGORY##

AP1 Sort Key:

PRODUCTGROUP###PRODUCT#

AP2 Partition Key:

CATEGORY###

AP2 Sort Key:

PRODUCTGROUP###PRODUCT#

AP3 Partition Key:

CATEGORY####RATING####PRICE##

AP3 Sort Key:

PRODUCTGROUP###PRODUCT#

Each item also contains updatedAt, lowestCurrentPrice, highestCurrentPrice, regularPrice, rating1, rating2, rating3, ratingScore, valueForMoney attributes and GSIs to allow for ordering in different ways.

AP1 and AP2 would have an estimated cost of $41 per month. However, when you introduce AP3 with its large number of duplicates, costs skyrocket to $94000 per month.

Each Rating has 5 options, price filtering is chunked into $50 sections which is 40 options. This is unfortunately sometimes ending up with 5 * 5 * 5 * 40 = 5000 filter variations. Which is 5000 duplicates for 1 product, writing every day.

Does anyone have a better way of doing partition keys and sort keys for product catalogues that would for for filtering or am I missing something?

Edit 1: Clarification on product number - there are 50000 products currently each with (on average) 8 size variations, 5 colour variations, 3 suppliers. Around 6 million DDB items before you add in historic prices, price changes, on a per variation-supplier level.

Everything was making sense to me until your AP3 scenario. You need to flatten out the keys as much as possible.

Why are rating and prices combined in the same access pattern? Why are there three different ratings fields?

Thanks for your reply Rowanu, I agree, there is definitely something wrong with the AP3 design.

Could you expand on what you mean by “You need to flatten out the keys as much as possible”.
Could you also expand on what you mean by "Why are rating and prices combined in the same access pattern?".

There are three different rating fields as each product has three different ratings for different qualities of that product. Three different sliders. This is open to be changed if it’s not feasible. Here is an image to show you what I mean: Ratings - Album on Imgur

Recovered from imgur album XcymONm (the album’s full listing is gone, but this item survived).

My initial idea was to have duplicate items for category products with a 1 day TTL to ensure price freshness.

Just to check you know there is only a best effort guarantee on ttl right?

And even then that best effort is ~48 hours. There is no guarantee to have a record deleted within 24 hours of a TTL expiring.

Why can’t your AP3 just be the same query as AP2, but with filtering on the ranges of values?

Also, it appears you are duplicating rows rather than building GSIs on the same data?

Edit: Sorry, it seems you are designing this to only use ‘get’ operations? Have you looked at the query operation?

Do you know about the NoSQL work bench?

It’s a good tool for building out schemas based on access patterns.

Ah, thanks for the extra info.

Your problem is the combinatorial complexity is blowing out your costs. When I say “flatten” I mean remove as many fields from the key as possible. For example, AP1 and AP2 only have 2-3 fields, but AP3 has 8. This means you might end up with more APs (and hence GSIs, which have a limit of 20 by default, but can be increased), but less combinations (which is then multiplied by your number of products) for each AP.

Could you just go with the first few APs to start with, and add the others if needed later?

The other option is for you to create a single feed which includes all the information you want to sort on (your application will have to compose and insert it itself), and then you can (from a DDB point of view) sort on a single field, instead of 3 ratings and min/max price.

The best answer depends on knowing more about the desired UX.

Option 1: Assuming a user navigates their way through AP1-2, most modern clients (browsers and mobile apps) can easily handle filtering large JSON product payloads client-side.

This let’s the client slice-and-dice products by price, ratings, any data point.

Option 2: Generate facets for each category so the user knows how many products are $100+ and rated “5” (for example). Users will generally click on subsets of data with facet counts that match their expectations, thus preventing a “click and hunt” UX pattern across several sub-categories.

Option 3: Personally I would build such a cache in Redis (Elasticache) and pre-compute the sub-categories and leaderboards for fast retrieval.

With that small of a data set that is only updated once a day, why bother with Dynamo? I would just stick it in S3 and call it a day.

When you grab the object and deserialize it, you should always check the TTL and it if < Utc Now

It sounds like you are reinventing the wheel. Dynamo may not be the best option. Have you thought about using something like Algolia?

I’m going to be “that guy” and ask, if you only have 50000 products, why are you using something more complicated than a HashMap in your web server code? Is DynamoDB really the best way to access read-heavy data that isn’t updated frequently?

Or if the updates are quite frequent and you have multiple web servers, have you considered using a plain old SQL relational database?

This. I double him going at least to run a proof of concept using a simple rds with postgre or something like this, your data is pretty “sqlizable”

Yeah, you should check the ttl in your code as well. The SLA is 48h, in my experience it’s typically within 10-30 seconds though.

So it’s nice for cleanup, but you can’t use it as a guarantee; hence the client-side filter if you use it and then you’re fine.

Thank you for your reply menge101.

In regards to “with filtering on the ranges of values”, I interpreted that as using filter expressions with a query. I have considered this, but as Alex DeBrie says “Filter Expressions won’t save your bad DynamoDB table design”. I understand this to be a bad habit of people that come from an SQL background. More info on it here: https://www.alexdebrie.com/posts/dynamodb-filter-expressions/

In regards to “it appears you are duplicating rows rather than building GSIs on the same data?” Could you give an example of the GSIs that would work for filtering rating and price? You may be correct about this, I have been quite focused on overloading the partition key.

To clarify on your question “Have you looked at the query operation?”. These are all queries, they are listing pages (catalogue pages) and need to show 15 results per page, therefore I would be retrieving all of the items that have a specific partition key with a limit of 15.

Yes I use NoSQL work bench thank you Menge101. Good tool!

I will be validating the TTL using DDBs filter expressions to check they are still valid. Thank you for pointing this out. It’s worth knowing.

+1. Move it to SQL. I love Document databases, but if you have relations your going to have a bad time. You can make workarounds.

Use the right tool for the job.

Thank you b0untryk1ll3r, there are 50000 products currently each with (on average) 8 size variations, 5 colour variations, 3 suppliers. Around 6 million DDB items before you add in historic prices, price changes, on a per variation-supplier level. I believe it warrants a database.