DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB vs Cassandra: Data Model, Consistency, Scaling, and Cost

DynamoDB and Cassandra are both distributed, horizontally scalable NoSQL databases. They share a common heritage (both influenced by Amazon's Dynamo paper), but their implementations and operational models differ significantly.

Data Model

DynamoDB

DynamoDB uses tables with items (rows) and attributes (columns). Each item must have a partition key and optionally a sort key:

// DynamoDB table: Users

// Partition key: user_id (String)

// Sort key: created_at (Number)

{

"user_id": "user_42",

"created_at": 1717000000,

"email": "alice@example.com",

"name": "Alice",

"address": {

"city": "New York",

"zip": "10001"

}

}

Key design rules:

  • The partition key determines which partition stores the item.

  • Items with the same partition key are stored together, ordered by sort key.

  • Query operations require the partition key; optional sort key conditions.

  • Secondary indexes can be local (same partition key, different sort key) or global (different partition key).

DynamoDB query

import boto3

client = boto3.client('dynamodb')

response = client.query(

TableName='Users',

KeyConditionExpression='user_id = :uid',

ExpressionAttributeValues={

':uid': {'S': 'user_42'}

}

)

Cassandra

Cassandra uses tables with rows and columns, but the data model is designed around query patterns. The PRIMARY KEY defines partitioning and clustering:

CREATE TABLE users_by_email (

email TEXT PRIMARY KEY,

user_id UUID,

name TEXT,

address TEXT

);

CREATE TABLE orders_by_user (

user_id UUID,

order_id UUID,

total DECIMAL,

created_at TIMESTAMP,

PRIMARY KEY (user_id, created_at, order_id)

) WITH CLUSTERING ORDER BY (created_at DESC);

Key design rules:

  • The first column in PRIMARY KEY is the partition key.

  • Subsequent columns are clustering columns that define sort order within a partition.

  • You model tables around your access patterns (query-first design).

  • Denormalization is expected and encouraged.

Cassandra query

from cassandra.cluster import Cluster

cluster = Cluster(['127.0.0.1'])

session = cluster.connect('mykeyspace')

rows = session.execute(

"SELECT * FROM orders_by_user WHERE user_id = %s",

(user_id,)

)

Consistency

DynamoDB Consistency Levels

| Level | Description | Cost | |-------|-------------|------| | Eventual | Reads may return stale data | 0.5 RCU per read | | Strong | Returns most up-to-date data | 1 RCU per read | | Transactional | Serial isolation for reads/writes | 2 RCU/WCU per operation |

DynamoDB offers tunable consistency at the request level:

Eventually consistent read (cheaper)

response = client.get_item(

TableName='Users',

Key={'user_id': {'S': 'user_42'}},

ConsistentRead=False

)

Strongly consistent read

response = client.get_item(

TableName='Users',

Key={'user_id': {'S': 'user_42'}},

ConsistentRead=True

)

Cassandra Consistency Levels

Cassandra's consistency is tunable per query using the consistency level (CL):

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- QUORUM: majority of replicas must respond

SELECT * FROM users WHERE email = 'alice@example.com'

CONSISTENCY QUORUM;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- ONE: fastest, weakest guarantee

SELECT * FROM users WHERE email = 'alice@example.com'

CONSISTENCY ONE;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- ALL: strongest guarantee, slowest

SELECT * FROM use


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)