- Web scraper
- crawled URLs are added to a table using DynamoDB.
- The partition key is an ID for the current crawl. So when a crawl starts an ID is generated that is applied to any URLs found in that crawl. It’s a way of grouping all the URLs that came in during a particular crawl ‘batch’.
- Need to be able to check whether a URL has already been seen/queued within that same Crawl ID
- There are a lot of read and write requests with the same crawl ID, in a sudden burst, when a crawl is happening. I’ve got a local buffer for the URL queue that helps throttle, but I’m still running into hot key issues.
Any time a big crawl is running, we get hit with hot key messages. It makes sense - the key *is* hot for a while. Any thoughts on how to solve this? I wanted to keep everything in DynamoDB for simplicity of the architecture, but I’m scratching my head as to how to solve this hot key problem given the other requirements.
I may have rubber duck’d my own question here. I think I can make the URL part of the partition key and still suit my use case. However, in case that doesn’t actually work, please add any responses or ideas!
It depends what other queries you need. If you still need the crawler id as the pk, you could add a gsi with a pk of # and query that so see if the url has already been crawled. Bear in mind GSIs are eventually consistent so you’ll need to allow for the query occasionally saying the url hasn’t been crawled when it actually has.
I’m using the .consistent() method in Dynamoose to overcome the consistency issue, and I also have a local buffer of URLs to check first (they’re eventually batched up to DyanmoDB when the local queue gets too big).
I *think* that’ll give me safety on the consistency issue. It’s also not really a problem, aside from wasting a little bandwidth and a few processing cycles, if a URL is crawled twice; the alternative of it not getting crawled would be a problem.
I think adding the URL to the PK will work, although I feel like that’s how I had it set up initially and changed it for some reason. However, I’m not currently able to think of a reason I can’t do that since I should *always* have the crawl ID and an associated URL. Thanks for the response!
You could have a look at Dynamodb accelerator ?
Depends on how hot you’re talking. Dynamo’s adaptive capacity and ability to both increase and reduce the number of shards these days makes hot key issues quite rare. Are you expecting more than 2000-3000 reads per second consistently per hot key?
GSIs are always eventually consistent, no matter what. There is nothing Dynamoose can do to change that. I haven’t looked at the Dynamoose docs but that setting most likely just enables consistent reads which do not actually exist for GSIs.
For clarity, you’re using a table with composite primary key, so each crawled URL result is its own item with partition key attribute set to the crawl ID, and the sort key is the URL? If so, I agree that increasing the cardinality of the primary key will help a lot - use a simple primary key (partition key but no sort key) and set that key to be a combination of crawl ID and the URL. You can still do the same point lookups. On the other hand, if you ever do have a need to pull all the URLs for a given crawl ID (or a sorted range of them) you need to stick with the composite key that you are using now. One thing to look at in that case is the ordering of the URLs as they are processed and written into the item collection - if they are being processed in alphabetical order, there’s nothing that DDB can do to help spread your write throughput over multiple partitions. On the other hand, if you randomize the order (not matching the natural sort order) DDB may be able to do some fancy splitting on the fly to help cover the load - this can take 15mins or so, but depending on your timing it could work out well. If you can start out the load slowly and steadily increase it, this gives DDB time to do that splitting before you get to the point of throttling.
That method may be doubling your read costs for no benefit.
Caching helps with read throttling, not write throttling.
It sounds to me like it’s the writes that are throttling, not reads.
Fair point. Depends on the item size as well of course, but 1500 writes/second per partition key is still a decent amount if it’s results from crawling a website unless you’re shoving huge json blobs in to dynamo. But it is sounding like a data design issue at the end of the day.
I’m actually confused about the note from the DynamoDB docs:
Read operations such as GetItem, Query, and Scan provide an optional ConsistentRead parameter. If you set ConsistentRead to true, DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful. Strongly consistent reads are only supported on tables and local secondary indexes. Strongly consistent reads from a global secondary index or a DynamoDB stream are not supported.
What does it mean to say it’s supported on “tables” if that doesn’t include the PK and SK? My architecture has a partition key, sort key, and LSIs on the queue table, but no GSIs.
it’s definitely not at that level in terms of # reads/writes . Dynamo does respond and adaptive when the key gets hot, but I’m not currently handling that condition and re-queuing the item (because it seemed like I can solve this with schema adjustments). And the data itself is tiny; it’s just metadata about the crawled page (headers, response code, etc).
You just reminded me that I *do* need to be able to search on just the crawl ID in certain circumstances. So I think I’m going to still update the main PK to include the URL and then do a GSI with the crawl ID
Yeah ok, are you using provisioned capacity or on-demand? on-demand essentially acts as though each partition is maximum capacity and handles bursts super well - but the adaptive capacity should do alright if you have enough provisioned capacity as well to fill up the token bucket to handle the bursts.
True I may have been too quick with my answer .