Inserting, Deleting, and Fetching multiple items at a time with batchGet and batchWrite

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

batchWrite and batchGet are a way to get the parallelism of sending multiple writes or gets in multiple threads regardless of whether or not your language supports threads. This is particularly interesting when implementing GraphQL node resolvers.

Instructor: [0:00] Now that we've covered get, put and delete, we can cover BatchWrite and BatchGet. BatchWrite allows us to send multiple put request in combination with multiple delete request to multiple different tables or the same table.

[0:14] In this case, we have a set of parameters whose request items all operate on the same table. If we wanted to add another table here, we could specify a different table name as the key. Each request is either a put request or a delete request. Put requests take an item, while delete requests just take a key.

[0:29] Note that there's a fundamental difference between BatchWrite and regular put request in that we can't put conditional expressions on each of these put requests, but we will be able to if we ran them individually.

[0:40] In this case, we have three items, all for user number one, todo1, todo2 and todo3. At the bottom, we have our machinery to run the batch write command.

[0:49] Note that we get a key back called unprocessed items. This key will contain any items that were unable to be written to the table. They could be not written to the table for a variety of reasons, including going over the provisioned capacity for the table.

[1:02] If you're using batch write, it's good to have an exponential back off algorithm to reattempt a batch write for the items that haven't been processed. If we look at our data in the DynamoDB console, we can see that we have three items for user number one with a sort key of todo1, 2, or 3 and the appropriate text for each one.

[1:18] A single BatchWrite call can write up to 16 MB of data, which can include as many as 25 put or delete requests. Individual items are still restricted by the 400 KB DynamoDB item size limit. Also remember that I've only talked about put and delete requests. You cannot put an update request inside of a BatchWrite.

[1:40] Now that we have successfully batch written to the table, let's try to use BatchGet to get all the items out. In this case, in request items, we specify the table that we want to operate on. Remember that this is an object, so we can specify multiple tables and get multiple items from multiple different tables. The keys for the table for the get request, we have the PK and SK for each item that we want.

[2:01] If we run this, we can see that we get the responses back to the testing table and each of the items is returned with all of its data. Note also that we have an unprocessed keys object that comes back similar to before.

[2:12] BatchWrite and BatchGet can be very useful for large background processing, such as pulling data from another system into or out of DynamoDB. In addition, BatchWrite and BatchGet can be good if you're building a GraphQL API that might have to fetch arbitrary nodes at any given time, as a BatchGet can allow you to efficiently fetch using a data loader for multiple tables with multiple primary keys.

[2:35] Finally, note that you cannot use a BatchWrite that has a put request for the same item as a delete request.

egghead
egghead
~ 26 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today