Member-only story
Fixing a Performance Anti-pattern With Amazon DynamoDB and Lessons Learned
My first steps with Amazon DynamoDB
I’ve recently been working on a personal project where I’ve decided to use and learn more about DynamoDB. While I’ve used Dynamo a bit for small things in the past, the majority of my past experience has been with relational databases, and it’s taken me some time to get my head around working with NoSQL.
What is DynamoDB?
DynamoDB is Amazon Web Services’ fully managed NoSQL database.
Unlike relational databases, which store their data in structured rows and columns, Dynamo data is stored in key-value pairs. This means that the data you store in Dynamo is unstructured compared to what you’d usually see in a relational database. The only schema that you have to define is your table keys: the partition key and the sort key. Beyond that, your data can be schemaless (although your application still has to know how to handle it).
My Mistake
In my project, I’m using Python and accessing Dynamo via the boto3
library. This is running on AWS Lambda. In my code, I have a data model, collection
, which I needed to do basic CRUD (Create, Read, Update, Delete) operations on.
My code looked something like this:
Do you see my mistake?
With every database operation I was doing, I was re-initializing the
boto3
resource and the Dynamo Table.
It was easy to do (especially with GitHub Copilot basically writing these functions for me), and I didn’t really think about it at the time. However, while reviewing some code, my fiancé, Myles Loffler, pointed out to me that this is actually an anti-pattern when working with boto3
clients, particularly on Lambda.
Note: While I hit this anti-pattern while working with Dynamo, due to the nature of the CRUD operations — you can run into it any time you’re re-initializing the same boto3
…