Section 3: Data Operations & Internals

Cassandra Hash Functions

Master the cryptographic magic that powers Cassandra's data distribution! Deep dive into Murmur3, token generation, consistent hashing, and the mathematical foundations of distributed databases.

📖 The Story: The Library's Magic Filing System

Imagine the World's Largest Library with 10 billion books. How do you organize them so any book can be found in milliseconds?

❌ The BAD System: Alphabetical Order

Scenario: Store books by title, alphabetically

"A Tale of Two Cities" → Shelf A
"Alice in Wonderland" → Shelf A
"Animal Farm" → Shelf A
"Brave New World" → Shelf B
...
"Zen and the Art..." → Shelf Z

Problems:

  • Shelf A is OVERLOADED! 40% of books start with A-D!
  • Shelf X, Y, Z mostly empty! Wasted space!
  • Sequential scanning: Finding "Anna Karenina" = check 10,000 books in Shelf A!
  • Hot spots: Everyone looking for "A" books → Shelf A crowded!

✅ The BRILLIANT System: Magic Hash Number

The Magic Formula: Run book title through a "magic math function" that spits out a unique number (0 to 999)

MAGIC_HASH_FUNCTION:

"A Tale of Two Cities" → 🔮 → 347 → Shelf 347
"Alice in Wonderland" → 🔮 → 891 → Shelf 891
"Animal Farm" → 🔮 → 12 → Shelf 12
"Brave New World" → 🔮 → 534 → Shelf 534
"Zen and the Art..." → 🔮 → 238 → Shelf 238

Why It's BRILLIANT:

  • Perfect Distribution: Books evenly spread across all 1000 shelves!
  • Instant Lookup: Run title through formula → Get shelf number → Find book in 1 second!
  • No Hot Spots: Similar titles ("Anna Karenina", "Animal Farm") end up in completely different shelves!
  • Predictable: Same title ALWAYS gives same number!

🎯 This is EXACTLY What Cassandra Does!

  • Books = Your Data Rows
  • Shelves = Cassandra Nodes
  • Magic Formula = Murmur3 Hash Function
  • Magic Number = Token (64-bit integer)
  • Book Title = Partition Key (user_id, order_id, etc.)
CASSANDRA MURMUR3:

user_id="alice_2024" → 🔐 Murmur3 → -3,847,293,847,293 → Node 1
user_id="bob_2024" → 🔐 Murmur3 → 5,123,456,789,012 → Node 3
user_id="charlie_2024" → 🔐 Murmur3 → -8,901,234,567,890 → Node 2

The library's magic filing system = Cassandra's hash function!
Same genius, different scale!

🔐 What is a Hash Function?

The mathematical foundation of Cassandra's data distribution.

Simple Definition

Hash Function: A mathematical algorithm that converts input data (any size) into a fixed-size numeric output called a "hash" or "token".

The Magic Properties:

  • Deterministic: Same input ALWAYS produces same output
  • Fast: Computes in microseconds
  • Uniform Distribution: Outputs spread evenly across range
  • Avalanche Effect: Tiny input change → Completely different output
  • One-Way: Cannot reverse (hash → original input)

Hash Function Anatomy

Hash Function: Input → Output Transformation INPUT (Any Size) "alice_2024" 10 bytes ASCII: 97 108 105 99 101... Feed In HASH FUNCTION (Murmur3 Algorithm) Step 1: Convert to bytes [97, 108, 105, 99, 101...] Step 2: Mix & Rotate (Complex bit operations) Step 3: Finalize (Avalanche mixing) ⚡ Processing: ~10 nanoseconds Output TOKEN (Fixed Size) -3,847,293,847,293 64-bit integer Range: -2^63 to 2^63-1 ✅ PROPERTIES • Deterministic • Fast (3 GB/sec) • Uniform distribution • Avalanche effect • Low collision rate • One-way function • Non-cryptographic 🎯 USES IN CASSANDRA • Partition key → Token • Node assignment • Data distribution • Load balancing • Replica placement • Query routing • Ring organization

Key Insight

Hash functions are the secret sauce of distributed systems!

Without hash functions:

  • ❌ No way to evenly distribute data
  • ❌ Hotspots everywhere (some nodes overloaded)
  • ❌ Linear search required (slow!)
  • ❌ Cannot scale horizontally

With hash functions:

  • ✅ Perfect even distribution
  • ✅ O(1) lookup time (instant!)
  • ✅ Linear scalability (add nodes easily)
  • ✅ No manual partitioning needed

Simple Example: Understanding Hash Output

1
Input: Partition Key

Let's hash a user_id:

user_id = "alice_2024"
2
Convert to Bytes

String becomes byte array (ASCII values):

[97, 108, 105, 99, 101, 95, 50, 48, 50, 52]
(a=97, l=108, i=105, c=99, e=101, _=95, ...)
3
Apply Murmur3 Algorithm

Complex mixing operations (bit rotations, XOR, multiplication):

h = seed
for each block of 4 bytes:
  k = block * c1
  k = rotate_left(k, r1)
  k = k * c2
  h = h XOR k
  h = rotate_left(h, r2)
  h = h * m + n
finalize(h)
4
Output: 64-bit Token

Final hash value (token):

Token: -3,847,293,847,293,847,289

In binary (64 bits):
1100101010110011... (showing first few bits)

This number determines which node stores the data!

🎯 Murmur3: Deep Dive into Cassandra's Hash Algorithm

Why Murmur3 became the industry standard for distributed systems.

📚 The History of Murmur3

Created by: Austin Appleby in 2008

Why "Murmur"? It does "multiply and rotate" operations (MUR-MUR sound)

Version 3: Released in 2011, fixed some issues in v2

🏆 Murmur3's Achievements

  • 6x faster than MD5
  • 20x faster than SHA-256
  • 99.97% uniformity in distribution tests
  • < 0.0001% collision probability
  • SMHasher test: Passed all tests

Adopted by:

  • Apache Cassandra (2012)
  • Redis
  • Memcached
  • Nginx
  • Hadoop
  • Elasticsearch

Murmur3 Algorithm Breakdown

🔢

Constants

Magic numbers carefully chosen for optimal mixing:

c1 = 0x87c37b91c82f8339
c2 = 0x4cf5ad432745937f
r1 = 31 (rotate bits)
r2 = 27 (rotate bits)
m = 5 (multiplier)
n = 0x52dce729 (constant)

These values maximize avalanche effect and minimize collisions.

⚙️

Core Operations

Three key operations mix the bits thoroughly:

  • Multiplication: Spreads bits across entire range
  • Rotation: Moves bits to different positions
  • XOR: Combines bits in non-linear way

Why these operations?

They're FAST in CPU (single cycle) and create maximum "mixing"

💥

Avalanche Effect

Change 1 bit in input → ~50% output bits flip:

Input: "alice"
Hash: 0x1234567890ABCDEF

Input: "alicf" (one char)
Hash: 0x9876543210FEDCBA

Completely different!

Expert Note: Why Not MD5 or SHA?

Cryptographic vs Non-Cryptographic Hash Functions:

Cryptographic (MD5, SHA-256):

  • Designed for security (can't reverse or find collisions)
  • SLOW (100-500 MB/sec)
  • Complex algorithms (many rounds)
  • Overkill for data distribution

Non-Cryptographic (Murmur3):

  • Designed for speed and distribution
  • FAST (3000 MB/sec)
  • Simple, efficient operations
  • Perfect for data partitioning

Cassandra doesn't need cryptographic security - it just needs even distribution and speed. That's why Murmur3 is perfect!

Visualizing Murmur3 Bit Operations

Murmur3: Bit-Level Operations Initial Input (32 bits shown) 01100001 01101100 01101001 01100011 ("alic" in binary) Step 1: Multiply by c1 10110111 11001100 10101010 11110000 × c1 Step 2: Rotate Left 31 bits 01111000 00101101 11011011 11110011 ROL 31 Wrap
Around Step 3: Multiply by c2 11001010 01010110 11110000 10101010 × c2 Step 4: XOR with hash state 01010111 10101100 00111111 11010101 XOR h Final Token (after more mixing) -3,847,293,847,293,847,289 Note: Actual Murmur3 uses 128-bit operations, simplified here for visualization

🎫 Token Generation: From Partition Key to Node Assignment

The complete journey of how Cassandra determines where your data lives.

1
Extract Partition Key from Query
CREATE TABLE users (
  user_id TEXT,
  name TEXT,
  email TEXT,
  PRIMARY KEY (user_id)
);

INSERT INTO users (user_id, name, email)
VALUES ('alice_2024', 'Alice', 'alice@example.com');

Partition Key extracted: "alice_2024"
2
Serialize to Byte Array

Convert the partition key to bytes based on data type:

DataType: TEXT (UTF-8 encoding)
"alice_2024" →

Bytes: [97, 108, 105, 99, 101, 95, 50, 48, 50, 52]
Hex: 0x616C6963655F32303234

10 bytes total

Different data types serialize differently:

  • INT: 4 bytes, big-endian
  • BIGINT: 8 bytes, big-endian
  • UUID: 16 bytes
  • TEXT: Variable, UTF-8 encoded
3
Apply Murmur3 Hash Algorithm

Run bytes through Murmur3 (128-bit version, take lower 64 bits):

hash128 = Murmur3_128([97, 108, 105, 99, 101, 95, 50, 48, 50, 52])

Full 128-bit hash:
0xF9876543210FEDCBA1234567890ABCDEF

Take lower 64 bits:
0xA1234567890ABCDEF

Convert to signed 64-bit integer:
Token: -3,847,293,847,293,847,289
4
Map Token to Token Ring

Token falls into a range owned by a specific node:

Token Ring (4 nodes, simplified):

Node 1: -9,223,372,036,854,775,808 to -4,611,686,018,427,387,904
Node 2: -4,611,686,018,427,387,903 to -1
Node 3: 0 to 4,611,686,018,427,387,903
Node 4: 4,611,686,018,427,387,904 to 9,223,372,036,854,775,807

Our token: -3,847,293,847,293,847,289

Falls in Node 2's range! ✅
5
Apply Replication Factor

With RF=3, data replicates to 3 nodes clockwise:

Replication Factor: 3
Strategy: NetworkTopologyStrategy

Primary Replica: Node 2 (owns the token)
Replica 2: Node 3 (next clockwise)
Replica 3: Node 4 (next clockwise)

Data written to Nodes: [2, 3, 4]
6
Write/Read Operations

Coordinator routes requests to correct nodes:

WRITE:
Coordinator sends to Nodes [2, 3, 4]
Waits for QUORUM (2 out of 3 ACKs)
Returns SUCCESS to client

READ:
Coordinator queries Nodes [2, 3, 4]
Gets data from fastest responder
Validates with digest from others
Returns data to client

Total time: 2-5ms ⚡

Important: Composite Partition Keys

When you have composite partition keys, ALL components are hashed together:

CREATE TABLE events (
  user_id TEXT,
  event_date DATE,
  event_id TIMEUUID,
  PRIMARY KEY ((user_id, event_date), event_id)
);

Partition key: (user_id, event_date)

Hash input: serialize(user_id) + serialize(event_date)
Example: "alice_2024" + "2024-01-15" → combined bytes → single token

Different date = Different token = Different node!

💥 The Avalanche Effect: Why Tiny Changes Matter

Change 1 bit in input → ~50% of output bits flip. This is critical for even distribution!

❌

Bad Hash (No Avalanche)

hash(x) = x * 17

"user_001" → 1 × 17 = 17
"user_002" → 2 × 17 = 34
"user_003" → 3 × 17 = 51

Sequential inputs = Sequential outputs!
All cluster on same node!
✅

Good Hash (Avalanche Effect)

Murmur3

"user_001" → -5,234,567,890
"user_002" → 7,891,234,567
"user_003" → -2,345,678,901

Completely scattered!
Perfect distribution!

Visual Demonstration: Bit Flipping

Input: "alice"

0
1
1
0
0
0
0
1
...
1
0
1
0

Hash: 0x1A2B3C4D5E6F7890

Change ONE letter: "alice" → "alicf"

Input: "alicf" (one character different)

1
0
0
1
1
1
1
0
...
0
1
0
1

Hash: 0x9F8E7D6C5B4A3210

Result: 32 out of 64 bits flipped (50%)!

This ensures similar inputs map to completely different nodes!

🖥️ Interactive Hash Laboratory

Experiment with real Murmur3 hashing and see token generation in action!

Murmur3 Hash Simulator
🔬 Welcome to the Hash Laboratory!
Enter a partition key and click "Generate Hash" to see the complete token generation process.

Try these examples:
• alice_2024
• bob_2024
• user_12345
• Click "Compare" to see avalanche effect!

⚡ Performance: Why Murmur3 is Lightning Fast

Real-world benchmarks and performance analysis.

Hash Function Speed (GB/sec) Latency (ns) Distribution Use Case
Murmur3 3.0 GB/s ~10 ns 99.97% Cassandra, Redis
XXHash 5.0 GB/s ~7 ns 99.95% Newer alternative
CityHash 2.5 GB/s ~12 ns 99.90% Google projects
MD5 0.5 GB/s ~60 ns 99.99% Legacy systems
SHA-256 0.15 GB/s ~200 ns 99.99% Cryptography

Real-World Impact

At Netflix scale: 1 million writes/second

With Murmur3 (3 GB/s):

  • Hash time: 10 ns × 1M = 10 milliseconds CPU time/sec
  • CPU usage: ~1% per core
  • Result: Negligible overhead! ✅

With SHA-256 (0.15 GB/s):

  • Hash time: 200 ns × 1M = 200 milliseconds CPU time/sec
  • CPU usage: ~20% per core
  • Result: Significant overhead! ❌

📊 Hash Function Comparison Matrix

Expert-level comparison of popular hash functions.

🏆

Murmur3

Cassandra's Choice

Strengths:

  • Perfect speed/quality balance
  • Industry-proven (10+ years)
  • Excellent distribution
  • Low collision rate

Weaknesses:

  • Not cryptographically secure
  • Slightly slower than XXHash
⚡

XXHash

Fastest Non-Crypto

Strengths:

  • Fastest (5 GB/s)
  • Excellent quality
  • Modern design (2012)
  • AVX2/SSE optimized

Weaknesses:

  • Less battle-tested
  • Newer (adoption risk)
🔒

MD5/SHA

Cryptographic

Strengths:

  • Cryptographically secure
  • Can't reverse engineer
  • Very low collisions
  • Industry standard

Weaknesses:

  • SLOW (10-20x slower)
  • Overkill for partitioning
  • Higher CPU cost

💼 Interview Questions & Expert Answers

Master these questions to demonstrate deep Cassandra expertise!

1 What is a hash function and why is it critical for Cassandra? ▼

Answer:

A hash function converts input data into a fixed-size numeric output (token). It's the foundation of Cassandra's distributed architecture.

Why Critical:

  • Data Distribution: Determines which node stores each row
  • Load Balancing: Ensures even distribution across nodes
  • Query Routing: Enables O(1) lookups (instant!)
  • Scalability: Allows adding nodes without rehashing all data

Without hash functions: Cassandra couldn't distribute data, would need manual partitioning, would have hotspots, and couldn't scale linearly.

2 Why does Cassandra use Murmur3 instead of MD5 or SHA-256? ▼

Answer:

Murmur3 is optimized for speed and distribution quality, not cryptographic security.

Performance Comparison:

  • Murmur3: 3 GB/sec, ~10 nanoseconds per hash
  • MD5: 0.5 GB/sec (6x slower)
  • SHA-256: 0.15 GB/sec (20x slower)

Why Speed Matters:

At Netflix scale (1M writes/sec), hash function is called 1 million times per second. With Murmur3, this takes 1% CPU. With SHA-256, it would take 20% CPU!

Key Point: Cassandra doesn't need cryptographic security for data partitioning - just fast, uniform distribution. Murmur3 provides this perfectly.

3 Explain the avalanche effect and why it's important ▼

Answer:

The avalanche effect means changing even one bit in the input causes approximately 50% of the output bits to flip.

Why It's Critical:

  • Prevents Clustering: Similar keys (user_001, user_002) don't end up on same node
  • Even Distribution: Sequential inputs produce random outputs
  • No Hotspots: Time-based keys don't cluster on one node

Example:

"alice" → Token: -3,847,293,847
"alicf" → Token: 7,891,234,567

One character change → Completely different token!
These users end up on different nodes!

Real-World Impact: Without avalanche effect, all timestamps from same second would go to same node, creating massive hotspot!

4 What is the token range in Cassandra and why those specific numbers? ▼

Answer:

Token range: -2^63 to 2^63-1

In numbers: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Why These Numbers?

  • 64-bit signed long: Standard Java/computer data type
  • Efficient: Fits in single CPU register
  • Fast operations: Native CPU arithmetic (no library needed)
  • Massive space: 18.4 quintillion possible tokens
  • Balanced: Equal negative and positive space

Why Not 0 to 2^64?

Unsigned integers aren't native in Java. Signed long is faster and more natural for JVM operations.

5 Can two different partition keys generate the same token? (Hash collision) ▼

Answer:

Theoretically YES, practically NO. Hash collisions are mathematically possible but astronomically unlikely.

The Math:

Token space: 2^64 = 18.4 quintillion possible values

Birthday paradox collision probability:
- 1 billion keys: ~0.00003% chance
- 1 trillion keys: ~0.03% chance

Reality: Most clusters < 100 billion keys
Collision probability: Effectively ZERO

If Collision Occurred:

  • Both keys map to same node (by design)
  • Still separate rows (partition key distinguishes them)
  • No data loss or corruption
  • Minimal performance impact

Real-World: In 10+ years of Cassandra usage across billions of deployments, zero reported cases of hash collisions affecting operations. Not a practical concern!

🎓 Chapter Summary: Master Hash Functions

Congratulations! You now deeply understand Cassandra's hash functions!

Key Concepts Mastered:

  • Hash Functions: Convert any input → fixed-size token
  • Murmur3: 3 GB/sec, 99.97% distribution quality
  • Token Range: -2^63 to 2^63-1 (18.4 quintillion)
  • Avalanche Effect: Tiny input change → 50% output change
  • Distribution: Perfect load balancing across nodes

The Library Analogy Recap:

Remember the library's magic filing system? Instead of alphabetical (clustered), use a magic formula that distributes books evenly across all shelves. Same book title always gives same shelf number, but similar titles go to completely different shelves!

Production Insights:

  • ✅ Hash function called for every single read/write operation
  • ✅ Speed is critical at scale (millions of ops/sec)
  • ✅ Murmur3's 10ns latency = negligible overhead
  • ✅ Collision probability is effectively zero
  • ✅ Avalanche effect prevents hotspots

🚀 You understand the cryptographic magic powering distributed databases!

Advertisement

Responsive Ad