👨‍💻 DynamoDB for every developer
Tags: aws, python, datascience
Published: 2021-12-20 09:21:20

#### Understanding the databases is important for every developer, and the arising cloud services bring the best in class tools and technologies for every developer to build applications in hours. 
#### Database is a critical component for any application. NoSQL database has become a widely popular choice for various solutions from web to machine learning applications.
#### Let's understand one of the most popular cloud database services called DynamoDB. 

## ❓What is DynamoDB? 

DynamoDB is a fully managed NoSQL database from Amazon Web Services (AWS). It is a document database which means the data is stored in key/value pairs. The  DynamoDB stores the data across 3 different availability zones in a region. 

Since it is a fully managed service, DynamoDB is well suited for various workloads with any amount of data that require auto-scaling from small to large and anywhere in between. 

## 📌 Key Benefits of using DynamoDB 

- ⚡ Elasticity and Performance 

Generally scaling databases is hard and risky, but DynamoDB gives you the ability to auto-scale based on the usage which allows your system to adjust according to the traffic and other needs.

- 🦾 Security 

Access control policies and encryptions are available for your tables, so it makes the database more secure and accessible to the appropriate users without any bottlenecks. 

-  🏁 Real-time event streaming 

DynamoDB streams enable developers to access the data when it happens which makes the DynamoDB suitable for many complex solutions. 


-  🧰 Database Management
 
DynamoDB creates and keeps the backup of your data safely on the cloud, so in the event of any occurrence, you can safely retrieve the data. 

-  ⏲️ Time to Live

TTL allows you to define when to expire the items in a table so they get deleted automatically from the database. 

### ✅ Considerations  

Some of the important guidelines to assess your suitability of using DynamoDB are as follows:

- DynamoDB is well suitable for any business-critical applications which require higher scalability and is predominantly used for transactions processing (OLTP)

- DynamoDB is not suitable for data warehousing and OLAP purposes.

## ⭐ **Read and Write Consistency**

- DynamoDB supports two types of reading such as **Eventually Consistent Read** and **Strongly Consistent Read**, By default, it is set to Eventually Consistent Read. 

- In the Eventually consistent read, data may not be consistent as the copies of data will generally be available in 1 second. Whereas, a Strongly consistent read will always be up-to-date since it fetches the straight from the leader node. 

- DynamoDB does not support strongly consistent reads across AWS regions 

## 🌋Throughputs and Capacity Management  

- DynamoDB offers two capacity modes, **Provisioned** and **On-Demand**. In the Provisioned mode, the developer would need to define the read and write capacity of the database. 

- On-demand mode is a flexible way to provision your DynamoDB tables to serve thousands of requests without doing any capacity planning.  

- Switching between provisioned and on-demand modes can be done once every 24 hours.


## 👨‍🔬 Calculating Read and Write Capacity Units 

### Read Capacity Units (RCU)

Read capacity units represents one strongly consistent read per second or two eventually consistent reads per second for an item of up to 4KB.

Let's see how we calculate RCU for strongly consistent read: 

- 1 read RCU = 1 strongly consistent read of up to 4kb, So let's take an example of 50 reads at 40KB: 

= 50 X 40/4 = 500 RCUs 

- For eventually consistent reads, it's two reads of up to 4KB. As an example of the same 50 reads at 40 KB:

= 50/2 X 40/4 = 250  RCUs

### Write Capacity Units (WCU)

One write capacity unit represents one write of 1 KB per second, so for example, if we need 50 writes of 40 KB per item:

= 50 X 40 = 2000 WCUs

## 📑 Partitions

The partitions in DynamoDB slice the table as multiple chunks of data for faster reads. It automatically creates partitions on every 10GB of data or when your application exceeds 3000 RCUs or 1000 WCUs in a single partition. Generally, DynamoDB will try to evenly distribute the RCU and WCU capacities across the partitions. 

## 🔑 Primary Key (HASH) and Sort Key (RANGE) 

The primary key determines the partition to store the data, so it is important to get the primary key correct to be able to distribute the data across the partitions for better performance. 

DynamoDB can have two keys 

- Primary Key
- Sort Key 

**Primary Key** can either be simple or composite keys. Composite keys consist of both a partition and a sort key. 

**Sort Key** helps to organize the data in DynamoDB. So sort keys logically group the data in one place for efficient querying purposes. 


## 🤖 Secondary Indexes 

DynamoDB allows your application to query the table using an alternate key, in addition to queries against the primary key. The two types of indexes are as follows: 

- Global Secondary Index (GSI) 
An index with a partition key and sort key that can be different. It only supports eventually consistent reads, and it can be created or modified at any time.

- Local Secondary Index (LSI) 
An index that has the same partition key as the table, but a different sort key. It supports strongly or eventual consistent reads, and it can be created only during the initial table creation. 

## 🚀 DAX 

- DynamoDB Accelerator (DAX) is a fully managed, highly available, in-memory cache for DynamoDB. 

- DAX can reduce the response times to microseconds. 

- DAX only supports the server-side encryption 

- DAX is not ideal for write-intensive workloads 


## ⛑️ Backups and Monitoring 

- DynamoDB provides on-demand backup capability as well as enable point-in-time recovery for your DynamoDB tables. 

- Backups includes the data, indexes, streams and provisioned capacity of RCU's and WCU's 

- DynamoDB usage can be monitored using CloudWatch and CloudTrail.