Mobile App with Amazon DynamoDB

Geethanjali V
4 min readJul 6, 2021

(AWS Hands-on experiments)

AWS DynamoDB

Overview:

Designing a mobile application (Idea based on Instagram), users can upload photo on application and share it with friends.

The app has following features:

1. Users can find and follow friends.

2. Receive notifications of the new photos when uploaded.

3. Message friends in the network.

4. Browse all photos of user in the order of date uploaded.

5. Users can react to a photo with one of four emojis — a heart, a smiley face, a thumbs up, or a pair of sunglasses.

6. Users should be able to see the number of each type of reaction a photo has received.

We want to build Dynamo DB table to handle all the access patterns focusing on data modelling and transaction feature.

Why Dynamo DB?

Dynamo DB is designed for high-scale use cases where scalability is the primary factor to ensure there is no performance degradation. It handles upgrades, backups, and other administrative tasks so that resources can be efficiently directed towards developing the application.

1. Setting up AWS Cloud9: For this application we use t2.micro EC2 instance, Once set up you will see the following screen;

Run the following command in the terminal to unpack and list the python scripts required.

2. Planning the Data Model further:

Data Modelling is nothing but, designing how the data is stored inside the give database.

Dynamo DB is a NoSQL database that can process high volume data with speed and efficiency, the performance will not degrade as in case of relational database when we scale up.

In Relational database we plan E-R diagrams, define entities and create foreign key to define relationships, once implemented we use SQL to retrieve data in forms we require.

But, in case of Dynamo DB, we decide how we are going to access the data and model data accordingly.

We need to jot down all the requirements, how to read & write data, understanding how the data needs to flow through the application, this will give us an idea how we can optimize the access patterns.

Each access pattern should require only a single request to DynamoDB because network requests are slow, and this limits the number of network requests you will make in your application.

3. Building E-R diagram for the application:

Define the Entities required for the app,

i) User

ii) Photo

iii) Reaction

iv) Friendship

4. User profile access patterns

As per application requirement we need these 3 patterns to begin with.

i) Create user profile (Write)

ii) Update user profile (Write)

iii) Browse another user profile (Read)

5. Photo access patterns

Now that we have drafted what we need t=for user profile we can focus on photo access patterns.

i) Upload photo of users (Write)

ii) View recent updates (Read)

iii) React to a photo (Write)

iv) View photo and reactions (Read)

6. Access patterns for friendship

i) Follow user (Write)

ii) View list of followers (Read)

iii) View list of users followed (Read)

Designing primary key:

This design requires a composite primary key with both Hash & Range, i.e., Partition key & Sort Key.

As you can see one-to-one entity like User requires only a range value

Please refer to Hash & Range for each entity for a detailed explanation of this table.

Creating a table:

Entities are stored in a single table so we cannot define keys like “User” hence we use general terms like (PK-Partition Key) and (SK-Sort Key). We then define capacity modes, in this case, we are using provisioned (Exact amount of reading and write), advantage of using this mode is that we know the cost estimation beforehand and do not spend too much time on capacity planning. The on-demand option is to pay as you go option that is best in the case where we have unpredictable workloads which are better as in the case of provisioned option cost per request is high.

Loading sample data (JSON format) into the table:

We load data into quick-photos from items.json,

Sample Data (AWS hands-on)

Verify using the scan command if all the data has been loaded.

Code to load data

Retrieving data about a user:

As we fetch information about User we will actually be retrieving 2 entities (User and photos uploaded by user)

Fetch User information

Conclusion:

This is just the first step to design a basic model, this can be improvised further. Below is the link for the AWS tutorial that details all the steps and provides the source code. It also provides documentation about DynamoDB that will give you more insight to get started.

design-a-database-for-a-mobile-app-with-dynamodb

Happy Learning :)

--

--