The Non-Relational DB(NoSQL)
Published: 2022-08-28 14:12:00

**What is a Non-Relational DB?** 

As we learned about [The relational dbs](https://dev.to/aws-builders/the-relational-dbs-rdb-1lf2) and their relational model on my earlier article. The Non-relational database also known as NoSQL is a database that does not follow the this model instead follows a key-value structure. 


**How does one know when to choose a Non-Relational DB?** 

If part of your daily tasks requires manipulating and analyzing all sizes of complex and unstructured data with a lot of flexibility, then NoSQL is your best choice. 

**What are some industries Non-Relational DB use cases?** 

- Websites, Ecommerce systems and Apps (Think about customers interaction) 

 - Credit cards (history)
 - Real-time bidding 
 - Shopping cart 
 - Social 
 - Product catalog 
 - Customer preferences
 - High-traffic 

![website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjbw76qrse8e22ter55t.png) 


**Now that we have a general idea of Non-Relational databases let us continue drilling deeper and let us explore and build AWS Non-Relational resources:** 

- Amazon DynamoDB

**What is Amazon Amazon DynamoDB?**
> Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling. 

**Let us see some important characteristics about Amazon DynamoDB** 

``` 

Performance: 

- More than 10 trillion requests per day
- Database tables can store and retrieve any amount of data and serve any level of request traffic
- Provides on-demand backup capability and enable point-in-time recovery of DynamoDB tables
- 20 million requests per second
- Can restore a table to any point in time during the last 35 days
- Global table replication
- Availability to switch between read/write capacity modes once every 24 hours.
- No need to change any code or applications to use or manage encrypted tables.
 
Scalability: 

- Automatic data replication over three availability-zones in a single region
- Scale up or down tables throughput capacity with zero downtime or performance degradation
  

Availability:

 - Maximum size per item is 400KB
 - 2,500 tables per AWS Region
 - Read/write capacity mode: 

 Capacity unit sizes for provisioned tables:

     - One read capacity unit = one strongly consistent read per second, or two eventually consistent reads per second, for items up to 4 KB in size.
     - One write capacity unit = one write per second, for items up to 1 KB in size.
     - Transactional read requests require two read capacity units to perform one read per second for items up to 4 KB.
     - Transactional write requests require two write capacity units to perform one write per second for items up to 1 KB.

 Request unit sizes for on-demand tables:

     - One read request unit = one strongly consistent read, or two eventually consistent reads, for items up to 4 KB in size.
     - One write request unit = one write, for items up to 1 KB in size.
     - Transactional read requests require two read request units to perform one read for items up to 4 KB.
     - Transactional write requests require two write request units to perform one write for items up to 1 KB. 

- Throughput capacity: 

  - On-Demand:
    - Per table
      1. 40,000 read request units and 40,000 write request units

    - Per account
      1. Not applicable
    
    - Minimum throughput for any table or global secondary index
      1. Not applicable
 
  - Provisioned:
    - Per table
      1. 40,000 read request units and 40,000 write request units

    - Per account
      1. 80,000 read capacity units and 80,000 write capacity units
    
    - Minimum throughput for any table or global secondary index
      1. read capacity unit and 1 write capacity unit


Security: 

  - DynamoDB encryption at rest provides enhanced security by encrypting all your data at rest using encryption keys stored in AWS Key Management Service (AWS KMS)
  - AWS owned key – Default encryption type. The key is owned by DynamoDB (no additional charge).
  - AWS managed key – The key is stored in your account and is managed by AWS KMS (AWS KMS charges apply).
  - Customer managed key – The key is stored in your account and is created, owned, and managed by you. You have full control over the KMS key (AWS KMS charges apply).
  - Can switch between the AWS owned key, AWS managed key, and customer managed key at any given time.
  - IAM administrators control who can be authenticated (signed in) and authorized (have permissions) to use Amazon DynamoDB resources.
``` 
_Please note that some of these limitations are adjustable and others are not._  

  
As you can see throughout these characteristics DynamoDB makes a perfect tool for developers :woman_technologist:. 

Oh, right it is building :construction_worker: time let us build this database using Terraform 


![db](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r720rl6gxlzjf8uthip7.png) 

  

[Find the Terraform repo and directions for this project here](https://github.com/ValAug/aws-databases-terraform) 

  

**Terraform code previous:** 

``` 
resource "aws_dynamodb_table" "dynamodb-table" {
  name           = "GameScores"
  billing_mode   = "PROVISIONED"
  read_capacity  = 20
  write_capacity = 20
  hash_key       = "UserId"
  range_key      = "GameTitle"

  attribute {
    name = "UserId"
    type = "S"
  }

  attribute {
    name = "GameTitle"
    type = "S"
  }

  attribute {
    name = "TopScore"
    type = "N"
  }

  ttl {
    attribute_name = "TimeToExist"
    enabled        = false
  }

  global_secondary_index {
    name               = "GameTitleIndex"
    hash_key           = "GameTitle"
    range_key          = "TopScore"
    write_capacity     = 10
    read_capacity      = 10
    projection_type    = "INCLUDE"
    non_key_attributes = ["UserId"]
  }

}
``` 

**Diagram** 

![DDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a161wukwkdnel431byoq.png)
  

**References:** 

``` 
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/identity-and-access-mgmt.html

```