0% found this document useful (0 votes)
8 views57 pages

Cassandra Data Modeling Best Practices

The document discusses best practices for modeling data in Cassandra, particularly in the context of high-scale systems like eBay. It emphasizes the importance of understanding the non-relational nature of Cassandra, proper key design, and the use of composite columns over super columns. The document also outlines strategies for optimizing read and write performance, including data denormalization and the importance of query patterns in data modeling.

Uploaded by

thomasperez1276
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views57 pages

Cassandra Data Modeling Best Practices

The document discusses best practices for modeling data in Cassandra, particularly in the context of high-scale systems like eBay. It emphasizes the importance of understanding the non-relational nature of Cassandra, proper key design, and the use of composite columns over super columns. The document also outlines strategies for optimizing read and write performance, including data denormalization and the importance of query patterns in data modeling.

Uploaded by

thomasperez1276
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

May 6, 2013 DataStax Cassandra South Bay Meetup

Cassandra Modeling
Best Practices and Examples

Jay Patel
Architect, Platform Systems
@pateljay3001
That’s me
Technical Architect @ eBay
Passion for building high-scale systems
Architecting eBay’s next-gen data platform
Prototyped first version of eBay’s cloud platform
Built various social applications at IBM & eBay
Implemented telecom softwares at an early startup
Pursuing machine-learning at Stanford
Entrepreneurial minded

http://www.jaykumarpatel.com
2
eBay Marketplaces
$75 billion+ per year in goods are sold on eBay
112 million active users
2 billion+ page views/day
Petabytes of data
400+ million items for sale
Thousands of servers

Multiple Datacenters Big Data


turning over a TB every second
24x7x365 Billions of SQLs/day
Always online
99.98+% Availability Near-Real-time
eBay Site Data Infrastructure
A heterogeneous mixture

Thousands of nodes
> 2K sharded logical host Hundreds of nodes
Hundreds of nodes
> 16K tables > 50 TB
> 250 TB provisioned
> 27K indexes > 2 billion ops/day
(local HDD + shared SSD)
> 140 billion SQLs/day
> 6 billion writes/day
> 5 PB provisioned
> 5 billion reads/day

Hundreds of nodes Dozens of nodes


Thousands of nodes
> 40 billion SQLs/day
The world largest cluster
with 2K+ nodes
Cassandra at eBay
Billions Terabytes
(per day) writes
offline reads
7 storage capacity
350 online reads
6
300
5
250
4
200
3
100
2
50
1

May, 2013
Aug, 2011

Aug, 2012

Doesn’t predict
5
business
eBay’s Real Time Big Data on Cassandra
 Social Signals on eBay Product & Item pages
 Mobile notification logging and tracking
 Tracking for fraud detection
 SOA request/response payload logging
 Metrics collections and real-time reporting for thousands of severs
 Personalization Data Service
 NextGen Recommendation System with real-time taste graph for eBay users
 Cloud CMS change history storage
 Order Payment Management logging
 Shipment tracking
 RedLaser server logs and analytics

More in upcoming Cassandra summit..


6
Cassandra Modeling

The more I read,


The more I’m confused!

Thanks to CQL for confusing further..

7
Intro to Cassandra Data Model
Non-relational, sparse model designed for high scale distributed storage

Relational Model Cassandra Model


Database Keyspace
Table Column Family (CF)
Primary key Row key
Column name Column name/key
Column value Column value
8
Data Model – Example Column Families

Static column family

Dynamic column family


(aka, wide rows)

9
Data Model – Super & Composite column

Grouping using
Super Column

Grouping using
Composite column
name

Note – make sure to read practice 18 later in the slides. 10


Don’t think of a relational table

Instead, think of a nested, sorted map data structure

SortedMap<RowKey, SortedMap<ColumnKey, ColumnValue>>

Why?
• Physical model is more similar to sorted map than relational
How?
• Map gives efficient key lookup & sorted nature gives efficient scans
• Unbounded no. of column keys
• Key can itself hold value
Each column has timestamp associated. Ignore it during modeling
11
1
Refinement - Think of outer map as unsorted

Map<RowKey, SortedMap<ColumnKey, ColumnValue>>

Why?
• Row keys are sorted in natural order only if OPP is used. OPP
is not recommended!

Think of a below visual!

12
1
How about super column?

Map<RowKey, SortedMap<SuperColumnKey,SortedMap<ColumnKey,
ColumnValue>>>

Super column is the past! Instead, use Composite column names:

Think of a below visual!

e.g., <state|city>: <CA|San Diego>, <CA|San Jose>, <NV|Las Vegas>, <NV|Reno>


13
CQL should not influence modeling

Best is to forget CQL during modeling exercise

• It’s a relational-style interface on top of non-relational model.


• Don’t think in relational or CQL-way while designing model!

Or, make sure to understand how CQL maps to physical structure.

14
Storing value in column name is perfectly ok

Leaving ‘column value’ empty (Valueless Column) is also ok

• 64KB is max for column key. Don't store long text fields, such
as item descriptions!
• 2 GB is max for column value. But limit the size to only a few
MBs as there is no streaming.

15
Use wide row for ordering, grouping and filtering

• Since column names are stored sorted, wide rows enable ordering of data and
hence efficient filtering.
• Group data queried together in a wide row to read back efficiently, in one
query.
• Wide rows are heavily used with composite columns to build custom indexes.

Example: Store time series event log data & retrieve them hourly.

16
4
But, don’t go too wide!

Because a row is never split across nodes

Traffic:
All of the traffic related to one row is handled by only one
node/shard (by a single set of replicas, to be more precise).

Size:
Data for a single row must fit on disk within a single node in the
cluster.

17
Choose proper row key – It’s your “shard key”

Or you’ll end up with hot spots, even with Random Partitioner

Example:
Bad row key: “ddmmyyhh”

Better row key: “ddmmyyhh|eventtype”

18
Make sure column key and row key are unique

Otherwise, data could get accidentally overwritten


• No unique constraint enforcement, of course.
• CQL INSERT and UPDATE are semantically the same – UPSERT

Example:
• Timestamp alone as a column name can cause collisions
• Use TimeUUID to avoid collisions.

19
Define correct comparator & validator

Don’t just use the default BytesType comparator and validator

• Inappropriate comparator can store data(column names) in


inappropriate order.
• Costly or impossible to do column slice/scans later.
• Can’t change comparator once defined, without data migration.
• Validator helps to validate column value & row key. Can change later.

Validator - Data type for a column value or row key.


Comparator - Data type for a column keys & defines sort order of column keys.

20
Favor composite column over super column

Composite column supports features of super columns & more

Concerns with Super column:


• Sub-columns are not indexed. Reading one sub-column de-
serializes all sub-columns.
• Built-in secondary index does not work with sub-columns.
• Can not encode more than two layers of hierarchy.

21
Order of sub-columns in composite column matters

Order defines grouping


Example: Items sold by seller per state per city

<state|city>
Ordered by State first and then by City. Cities will be grouped by state
physically on the disk.
<city|state>
The other way around, which you don’t want.
22
9
Order affects your queries
Example: User activities

Efficient to query data for a given time range.

Efficient to query data for a given activity type and time range.
Also, efficient for only activity type. But not efficient for only time range.
23
9
It’s like compound index!

Assume,
CF with composite column name as <subcolumn1 | subcolumn2 | subcolumn3>

Not all the sub-columns needs to be present. But, can’t skip also.
Query on ‘subcolumn1|subcolumn2’ is fine. But not only for ‘subcolumn2’.

Sub-columns passed after the sliced (scanned) sub-column are ignored.


Query on ‘subcolumn1|slice of subcolumn2|subcolumn3’ will ignore subcolumn3.

Correct order of sub-columns ultimately depends on your query patterns.

24
10 Model column families around query patterns

But start your design with entities and relationships, if you can

• Not easy to tune or introduce new query patterns later by simply creating
indexes or building complex queries using join, group by, etc.
• Think how you can organize data into the nested sorted map to satisfy
your query requirements of fast look-
up/ordering/grouping/filtering/aggregation/etc.

 Identify the most frequent query patterns and isolate the less frequent.
 Identify which queries are sensitive to latency and which are not.

25
11 De-normalize and duplicate for read performance

But don’t de-normalize if you don’t need to.


It’s all about finding the right balance.

Normalization in Relational world:


Pros: less data duplication, fewer data modification anomalies, conceptually
cleaner, easier to maintain, and so on.
Cons: queries may perform slowly if many tables are joined, etc.
The same holds true in Cassandra, but the cons are magnified

Next few slides illustrate practice 10 & 11 through the example.

26
Example 1 “Likes” relationship between User & Item

• Get user by user id


• Get item by item id
• Get all the items that a particular user has liked
• Get all the users who like a particular item

27
Example 1
Option 1: Exact replica of relational model

Note: timestamp column is dropped for simplicity.

There is no easy way to query:


- Items that a particular user has liked
- Users who liked a particular item
The worst way to model for this use case. 28
Example 1
Option 2: Normalized entities with custom indexes

• Normalized entities except user and item id mapping stored twice


• What if we want to get the titles in addition to item ids, and username in
addition to user ids.
– How many queries to get all usernames who liked a given item with
like-count of 100? 29
Option 3: Normalized entities with Example 1

de-normalization into custom indexes

• ‘Title’ and ‘Username’ are de-normalized now.


• What if we want:
- Given a item id, get all item data along with user names who liked the item.
- Given a user id, get all user data along with item titles liked by that user.

How many queries in the current model? Can it increase further if user becomes
active or item becomes hot? 30
Example 1
Option 4: Partially de-normalized entities

• Looks messy. Just to save one query?


• If User and Item are highly shared across domains, I would prefer option 3
at a constant cost of one additional query.

31
Example 1
Best Option for this use case – Option 3

Introduced timestamp (when user


liked item) back as part of column
name, to have chronological order.

32
Example 2 Semi-structured event log data

Collecting time series event log, and doing real-time aggregation & roll ups

• Data is never updated so duplication/de-normalization won’t hurt


• Entities & relationships may not matter much, like in earlier use case.

33
Example 2
Example Cassandra model

Many ways to model. The best way


depends on your use case & access
patterns.

34
12 Keep read-heavy data separate from write-heavy

So can benefit from caching read-heavy data

Irrespective of caching, it’s always a good practice to keep read-


heavy data separate from write-heavy since they scale differently.

Row cache caches the whole row. Be cautious before enabling for wide rows.

35
13 Isolate more frequent from the less frequent

Split hot & cold data in separate column families


Example: Seller Attribute data

36
14 Manual sharding can help compaction

But do use-case level splitting, first


• Compaction can run faster, in parallel.
• Better than built-in multi threaded compaction.
• Keep number of shards proportional to number of CPU cores.
• Don’t have to match number of shards to number of nodes.
• Consider when lots of data in CF with high writes & reads.

It’s painful, so don’t pursue if you don’t need to.

37
15 Design such that operations are idempotent

Unless, can live with inaccuracies or inaccuracies can be corrected eventually.


• Perceived write failure can result in successful write eventually.
• It’s recommended to retry on write failure.

Retry on write failure can yield unexpected result if model isn’t update idempotent.

38
15
But may not be efficient

• Counting users requires reading all user ids (million?) - Can’t scale.
• Can we live with approximate count for this use case? - Yes.
• If needed, counter value can be corrected asynchronously by counting the
user ids from update idempotent CF.

Cassandra write operations to regular CF is always idempotent.

Idempotency of Use case level operation(e.g. like/dislike item) is


also useful, in case of eventual consistency.

39
16 Keep column name short

Because it’s stored repeatedly


• If column name holds actual data (in case dynamic CF), then
that's great.
• But, in case of static CF, keep column name short!

For example:
Favor ‘fname’ over ‘firstname’, and ‘lname’ over ‘lastname’.

40
17 Favor built-in composite type over manual

Because manual construction doesn’t always work


Avoid using string concatenation to create composite column names

• Won’t work as expected when sub-columns are of different types.


<State|ZipCode|TimeUUID> won’t be sorted in type aware fashion.

• Can’t reverse the sort order on components in the type


<String | Integer> with the string ascending, and the integer descending.

41
18 Keep all data in a single CF of the same type

At least for row key & column key

In other words, favor static composite types over dynamic

Better to break into multiple column families.

42
19 Don’t use the Counter Column for surrogate keys

Because it’s not intended for this purpose

• Holds distributed counters meant for distributed counting.


• You can receive duplicate sequence numbers!
• ASK: Do I really need strictly sequential numbers?
• Prefer TimeUUID (type-1 uuid) as surrogate keys.

43
20 Indexing is not an afterthought, anymore

Think about query patterns & indexes from beginning

 Primary (Row key) Index


 Build-in secondary index
 Custom secondary index

44
20.1 Primary (or Row key) Index

• Built-in index, always used.


• Very efficient – Know which shard to hit!
• Good for equality predicate queries
(e.g., where rowkey = 'key1’)

• Not useful for range queries


(e.g., rowkey > 'key1' and rowkey < 'key2')

Order Preserving Partitioner is almost never used. So, no range


scans on row keys.

45
20.2 Built-in Secondary Index

• From Cassandra 0.7 & later.


• Each index as a separate hidden Column Family per node.
• A query based on secondary index field will be sent to all the
nodes, sequentially.

• It's an index on the column values, and not on the column keys.
• Column keys are always indexed & stored physically sorted.

46
20.2
Best used when

• Indexed Column is low cardinality, or read load is low.


• No scan (<, <=, =>, >) required. No ‘order by’ required.

Atomic index update Yes


Is efficient? No
In-equality predicate support No
(<, >, <=, =>)
Order by No
(return results in sorted order)
Maintenance overhead No
47
20.3 Custom Secondary Index

• Column keys are stored physically sorted and indexed.


• This property of column keys is exploited along with
composite columns to build custom secondary indexes.
• Custom secondary index is just yet another column family!

You actually build model as if you’re building custom indexes!

48
20.3
Best used when

• Read load is high & indexed column is high cardinality.


• Range scan and/or ‘order by’ required, and has at least one
equality predicate.

Atomic index update No


Is efficient? Yes
In-equality predicate support Yes, if at least one quality predicate
(<, >, <=, =>) (row key) is present
Order by Yes
(return results in sorted order)
Maintenance overhead Yes

Note that Index updates can be hard.


49
20.3
Custom Indexing Examples

Let’s create indexes for the below static ‘Item’ CF.

50
20.3
Select Title from Item where Seller = 'sellerid1'

Better: Materialize ‘title’ in the index.

51
20.3
where Seller = 'sellerid1' order by Price

What if seller changes price of the item? How to update index?

Delete <old_price|itemid> columns & insert <new_price|itemid>.

But,
- How to get old price in order to delete? Read before write?
- Any race condition? What if Consistency Level is eventual?
- Repair on read?
52
where Seller='sellerid1' and ListingDate > 10-11-2011
20.3
and ListingDate < 11-12-2011 Order by Price

Won’t work 
Data won’t be ordered by ‘Price’ across dates.

What to do then?

53
Key Takeaways

• Don’t think of a relational table


– Think of a nested sorted map, instead.
• Model column families around query patterns
– But start with entities & relationships, if you can.
• De-normalize and duplicate for read performance
– But don’t de-normalize if you don’t need to.
• Many ways to model data in Cassandra
– The best way depends on your use case and query patterns.
• Indexing is not an afterthought, anymore
– Think about query patterns & indexes upfront.
• Think of physical storage structure
– Keep data accessed together, together on the disk.
54
There is more..

eBay Tech Blog:


Best Practices Part 1:
http://www.ebaytechblog.com/2012/07/16/cassandra-data-modeling-best-practices-part-1/

Best Practices Part 2:


http://www.ebaytechblog.com/2012/08/14/cassandra-data-modeling-best-practices-part-2/

Cassandra at eBay:
http://www.slideshare.net/jaykumarpatel/cassandra-at-ebay-13920376

55
Meet our operations:

Feng Qu
Principle DBA @ eBay
Cassandra Prod. operations expert

Baba Krishnankutty
Staff DBA @ eBay
Cassandra QA operations expert

56
Are you excited? Come Join Us!

Thank You
@pateljay3001

57

You might also like