DynamoDb with AWS SDK Go v2 — (Part 2— CRUD Operations)

Emre Ayberk Kaymaz
Delivery Hero Tech Hub
4 min readNov 30, 2021
Mic out for DynamoDb

Before starting this part some basic knowledge is essential which I mentioned previous part and in this part for DynamoDB and Go SDK v2, where I’m writing the crud operation for Go SDK v2, we’re going to see basic operations which to create, read, update and delete an item and other complex operations like transaction and pagination.

Now Let’s Start Coding

  • Create-Table

The most important thing to do to create a table is to choose the right PK. PK must be created when creating a table.

As seen in the example, PK and SK fields should be defined as attributes and then defined in the key schema.

If we want to add GSI or LSI as we mentioned in the introduction.

— For GSI

First of all, GSI must be defined as an attribute.

Then GSI values must be defined.

The point to be considered here is that GSI should be defined as the Primary Key when creating a GSI. Then the desired attribute can be set as the Sort Key. In addition, the entered throughput value must be equal to or greater than the throughput value of our existing table.

— For LSI

To create a table with LSI, LSI must be defined as an attribute.

Then LSI definition should be made.

The thing to consider when defining LSI is that LSI is not assigned as a primary key.

Totally, anyone wants to create a table with both GSI and LSI the code should be like this

I didn't find how to apply auto-scaling so if anyone wants auto-scaling enabled, s/he can use terraform. I leave a link below about terraform to create a table with indexes.

Create Table With Terraform

  • InsertItem / PutItem

Put operation creates a new element or replaces an old element with a new element if the same key is used.

  • GetItem

The Get operation can be used if the exact PK (and SK if you’re using) is known for the item you want to retrieve from the DynamoDB table.

  • GetItem By Index

DynamoDB allows getting not only on the main table index but also on LSIs and GSIs.

  • Get All Items

This operation Scan through the whole table, returning a collection of items and their attributes. The end result of a scan operation can be narrowed down using FilterExpressions.

  • UpdateItem

If an item with the specified key does not exist, UpdateItem creates a new item. Otherwise, it modifies an existing item’s attributes. To use update expression the attributes that wanted to modify and new values should be specified. Within the update expression, expression attribute values are used as placeholders for the actual values.

  • DeleteItem

DeleteItem deletes the item with the specified key.

Let’s Make Some Transactional Action

With Amazon DynamoDB transactions, multiple actions can be grouped together and submitted as a single all-or-nothing action. There are two types of transaction operations.

  • Transaction-Read

Thanks to TransactGetItems synchronous reading operation, it is possible to perform a maximum of 25 operations in a single operation. These 25 transactions must consist of unique items in one or more tables within the same AWS region. The aggregate size of the items in the transaction can’t exceed 4 MB. The example

  • Transaction-Write

Thanks to TransactWriteItems synchronous write operation, it is possible to perform a maximum of 25 operations in a single operation. These 25 transactions must consist of unique items in one or more tables within the same AWS region. The aggregate size of the items in the transaction can’t exceed 4 MB. It contains 3 types of action and these are Put, Update, and Delete. The example;

Key Notes About Transaction

— It is necessary to avoid grouping operations in one transaction unless necessary. For example, let’s imagine a transaction with 5 operations, dividing these 5 operations into separate operations without affecting the accuracy of the application will increase the efficiency and the probability of the transactions being successful.

— It may cause conflicts when trying to update the same item with more than one transactional operation. In order to prevent this situation, best practices should be followed for DynamoDb data modeling and such conflict situations should be minimized.

— Bulk Insert operations should be avoided. TransactWriteItems only works with Put, Update, and Delete operations but if we want to use Bulk Insert so we need BatchWriteItem. So use it out of the transaction operation if it's necessary.

  • Pagination

The limitation of both Query and Scan operations is 1MB of items. To get more records Paginators should be used. So we can get more items and separate them into pages.

--

--