What is DynamoDb

Sajjad Salaria
The Startup
Published in
7 min readAug 14, 2019

Let’s learn about an effective NoSQL database.

DynamoDB is a fully managed, Internet scalable, easily administrated and cost-effective NoSQL database.

https://jrebel.com/wp-content/uploads/2015/04/image00.jpg

Benefits of DynamoDB:

Scalability and Performance

Using Amazon DynamoDB, Developers can combine incremental scalability and high performance with the ease of cloud administration, reliability and table data model and thus can meet the customer demand. It can scale the table assets to a large number of servers on various Availability Zones for addressing storage need. In addition, there is no specific limit to the quantity of data that a table can store. As a result, any amount of data can be stored and retrieved and Dynamo DB shared data across more servers with the increase of data stored in a table.

Cross-region Replication

It enables you to maintain identical copies as replicas of a DynamoDB master table in one or more AWS regions. Once you enable cross-region replication for a table, identical copies of the table are created in other AWS areas. Any mode of changes in the table will be consequently propagated to all replicas.

TTL (Time to Live)

TTL is a process that gives you an opportunity to set a specific timestamp to delete expired data from your tables. Once the timestamp expires, the relating item is marked as expired and is subsequently deleted from the table. By using this functionality, you don’t need to track expired data and delete it manually. TTL can help you reduce storage usage and reduce the cost of storing data that is no longer important.

Fine-grained Access Control

Fine-Grained Access Control gives a DynamoDB table owner a high level of control over data in the table. In particular, the table owner can specify who can access which data or attributes of the table and perform what actions such as read-write or update. Fine-Grained Access Control is used in combination with AWS Identity and Access Management, which manages the security credentials and the related permissions.

Stream

Using the DynamoDB Streams, developers can update and receive the item-level data before and after data are changed. DynamoDB Streams provides a time-ordered sequence of data changes made in a table in the last 24 hours. You can access a stream with a simple API call and use it to keep other data stores updated with the latest changes and take actions based on it.

Tables, Items, and Attributes

https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2018/09/10/dynamodb-partition-key-1.gif

The following are the basic DynamoDB components:

  • Tables — Similar to other database systems, DynamoDB stores data in tables. A table is a collection of data. For example, see the example table called People that you could use to store personal contact information about friends, family, or anyone else of interest. You could also have a Cars table to store information about vehicles that people drive.
  • Items — Each table contains zero or more items. An item is a group of attributes that is uniquely identifiable among all of the other items. In a People table, each item represents a person. For a Cars table, each item represents one vehicle. Items in DynamoDB are similar in many ways to rows, records, or tuples in other database systems. In DynamoDB, there is no limit to the number of items you can store in a table.
  • Attributes — Each item is composed of one or more attributes. An attribute is a fundamental data element, something that does not need to be broken down any further. For example, an item in a People table contains attributes called PersonID, LastName, FirstName, and so on. For a Department table, an item might have attributes such as DepartmentID, Name, Manager, and so on. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.

Note the following about the People table:

  • Each item in the table has a unique identifier, or primary key, that distinguishes the item from all of the others in the table. In the People table, the primary key consists of one attribute (PersonID).
  • Other than the primary key, the People table is schema-less, which means that neither the attributes nor their data types need to be defined beforehand. Each item can have its own distinct attributes.
  • Most of the attributes are scalar, which means that they can have only one value. Strings and numbers are common examples of scalars.
  • Some of the items have a nested attribute (Address). DynamoDB supports nested attributes up to 32 levels deep.

The following is another example table named Music that you could use to keep track of your music collection.

Note the following about the Music table:

  • The primary key for Music consists of two attributes (Artist and SongTitle). Each item in the table must have these two attributes. The combination of Artist and SongTitle distinguishes each item in the table from all of the others.
  • Other than the primary key, the Music table is schema-less, which means that neither the attributes nor their data types need to be defined beforehand. Each item can have its own distinct attributes.
  • One of the items has a nested attribute (PromotionInfo), which contains other nested attributes. DynamoDB supports nested attributes up to 32 levels deep.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/images/HowItWorksStreams.png

When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.

DynamoDB supports two different kinds of primary keys:

  • Partition key — A simple primary key, composed of one attribute known as the partition key.
  • DynamoDB uses the partition key’s value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored.
  • In a table that has only a partition key, no two items can have the same partition key value.
  • The People table described in Tables, Items, and Attributes is an example of a table with a simple primary key (PersonID). You can access any item in the People table directly by providing the PersonId value for that item.
  • Partition key and sort key — Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.
  • DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key are stored together, in sorted order by sort key value.
  • In a table that has a partition key and a sort key, it’s possible for two items to have the same partition key value. However, those two items must have different sort of key values.
  • The Music table described in Tables, Items, and Attributes is an example of a table with a composite primary key (Artist and SongTitle). You can access any item in the Music table directly if you provide the Artist and SongTitlevalues for that item.
  • A composite primary key gives you additional flexibility when querying data. For example, if you provide only the value for Artist, DynamoDB retrieves all of the songs by that artist. To retrieve only a subset of songs by a particular artist, you can provide a value for Artist along with a range of values for SongTitle.

DynamoDB can have three data types

  1. Scalar
  2. Set
  3. Document
https://image.slidesharecdn.com/srv404deepdiveonamazondynamodb-170731162610/95/srv404-deep-dive-on-amazon-dynamodb-14-638.jpg?cb=1501518430

A scalar can be String, Number, and Binary, Boolean, Null.

A set can be:

  • String Set : [“Jane”, “Doe”,”Kevin”],
  • Number Set: [42.2, -19, 7.5, 3.14]
  • Binary Set: [“uKASDB”, “ASDDSFFF”]

A set can only be of one Scalar type, Each value must be unique in the Set

Document:

A document can be a List — [“Jane”, 122, 2.30]. There are no restriction on data type.A document can be a map — It is an unordered collection of name value pairs - {Day: ‘Monday’, Emails: 34}

These indexes allow you to query table data with an alternate key. Though DynamoDB does not force their use, they optimize querying.

DynamoDB uses two types of secondary indexes −

  • Global Secondary Index − This index possesses partition and sort keys, which can differ from table keys.
  • Local Secondary Index − This index possesses a partition key identical to the table, however, its sort key differs.

--

--

Sajjad Salaria
The Startup

You can go a long way with a smile. You can go a lot further with a smile and a gun. - Al Capone