DynamoDB
Cassandra
data model
NoSQL
database architecture

How do you call the data model of DynamoDB and Cassandra?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Both Amazon DynamoDB and Apache Cassandra are popular NoSQL databases with distinct data models and design goals. While DynamoDB is a managed, high-performance proprietary database service provided by AWS, Cassandra is an open-source distributed database designed for handling large amounts of data across many commodity servers. Understanding their data models is crucial for leveraging each system's strengths effectively.

DynamoDB Data Model

DynamoDB's data model is designed to ensure flexibility and scalability, and it is tightly integrated with AWS's infrastructure. Below are the key components of its data model:

Tables

DynamoDB organizes data into tables, which are collections of items. Unlike relational databases, tables in DynamoDB do not require the specification of a fixed schema apart from the primary key.

Items

An item in DynamoDB is akin to a row in relational databases. Each item is a collection of attributes, which is a key-value pair. The number of attributes in an item can vary, allowing for flexible data structures.

Attributes

Attributes are the pieces of data tied to an item. An attribute can be a scalar value (e.g., string, number), a document (e.g., list, map), or a set (e.g., string set, number set).

Primary Keys

To uniquely identify items, each DynamoDB table must have a primary key, which can either be:

  1. Partition Key: A single attribute that uniquely identifies an item, used to distribute data across partitions.
  2. Composite Key (Partition Key + Sort Key): A combination of two attributes, allowing for logical segregation of related data and efficient query access.

Secondary Indexes

DynamoDB supports two types of secondary indexes to perform efficient queries beyond the primary key:

  • Global Secondary Index (GSI): Allows querying on non-primary key attributes by specifying a different partition key and optional sort key.
  • Local Secondary Index (LSI): Permits the use of alternative sort keys for queries while keeping the same partition key as the main table.

Example

json
1{
2  "Item": {
3    "UserID": { "S": "12345" },
4    "UserName": { "S": "JohnDoe" },
5    "Age": { "N": "30" },
6    "FriendsList": { "L": [ {"S": "Friend1"}, {"S": "Friend2"} ] }
7  }
8}

Cassandra Data Model

Cassandra's data model is geared toward providing high availability and resilience and emphasizes a denormalized approach.

Keyspace

In Cassandra, a keyspace is analogous to a database in other systems. It is the outermost grouping of tables and defines options such as replication.

Tables (Column Families)

Tables in Cassandra are similar to tables in relational databases but with more flexibility. They organize rows and columns, but each can have different columns due to its sparse nature.

Rows and Cells

  • Row: Identified uniquely by a primary key, each row consists of columns.
  • Cell: Each cell represents a specific column value in a row.

Primary Keys

Cassandra requires a primary key that is composed of:

  1. Partition Key: Used to determine the distribution of data across nodes.
  2. Clustering Columns: Define the order of data storage within a partition, which impacts retrieval efficiency.

Secondary Indexes

Cassandra provides the ability to create secondary indexes on columns, allowing for optimized query patterns where non-primary key columns are often filtered.

Example

sql
1CREATE TABLE users (
2  user_id UUID PRIMARY KEY,
3  user_name TEXT,
4  age INT,
5  friends LIST<TEXT>
6);

Key Differences: DynamoDB vs. Cassandra

FeatureDynamoDBCassandra
Managed ServiceYes (AWS service)No (self-managed or managed by third-party solutions)
Schema FlexibilityHighHigh
Primary Key StructureSingle partition key or composite (partition + sort)Composite (partition + clustering)
IndexesGlobal and Local Secondary IndexesSecondary Indexes
Data ConsistencyEventually or strongly consistentTunable consistency
ReplicationAutomatic globally-distributed DBConfigurable replication across data centers

Bonus: Considerations and Use Cases

Use Cases for DynamoDB

  • Applications benefitting from automatic scaling with variable traffic patterns.
  • Use-cases that work within the AWS ecosystem for ease of integration.
  • Scenarios requiring fast performance and low-latency reads/writes.

Use Cases for Cassandra

  • Environments demanding scalable writes and reads with multi-datacenter resilience.
  • Situations that need linear scalability and high fault tolerance.
  • Use cases that involve massive amounts of writes with variable levels of read consistency.

Conclusion

Understanding the data models of DynamoDB and Cassandra enables developers and architects to make informed decisions when choosing a database solution. Whether the requirement is a fully managed service with integration into an existing cloud stack or a self-managed, highly scalable distributed database environment, both DynamoDB and Cassandra offer robust options for modern data needs.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.