DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters...

79
@doanduyhai Cassandra nice use-cases and worst anti- patterns DuyHai DOAN, Technical Advocate

Transcript of DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters...

Page 1: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Cassandra nice use-cases and worst anti-patterns DuyHai DOAN, Technical Advocate

Page 2: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Shameless self-promotion!

2

Duy Hai DOAN Cassandra technical advocate •  talks, meetups, confs •  open-source devs (Achilles, …) •  technical point of contact

[email protected] •  production troubleshooting

Page 3: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Agenda!

3

Anti-patterns •  Queue-like designs •  CQL null values •  Intensive updates on same column •  Design around dynamic schema

Page 4: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Agenda!

4

Nice use-cases •  Rate-limiting •  Anti Fraud •  Account validation •  Sensor data timeseries

Page 5: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Data Model Crash Course!

Page 6: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

6

jdoe age name

33 John DOE

INSERT INTO users(login, name, age) VALUES(‘jdoe’, ‘John DOE’, 33);

#partition

Page 7: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

jdoe age (t1) name (t1)

33 John DOE

7

INSERT INTO users(login, name, age) VALUES(‘jdoe’, ‘John DOE’, 33);

auto-generated timestamp (μs)

.

Page 8: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

8

UPDATE users SET age = 34 WHERE login = jdoe;

jdoe age (t1) name (t1)

33 John DOE jdoe

age (t2)

34

SSTable1 SSTable2

Page 9: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

9

DELETE age FROM users WHERE login = jdoe;

jdoe age (t3)

ý

tombstone

jdoe age (t1) name (t1)

33 John DOE jdoe

age (t2)

34

SSTable1 SSTable2 SSTable3

Page 10: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

10

SELECT age FROM users WHERE login = jdoe;

? ? ?

SSTable1 SSTable2 SSTable3

jdoe age (t3)

ý jdoe

age (t1) name (t1)

33 John DOE jdoe

age (t2)

34

Page 11: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Last Write Win (LWW)!

11

SELECT age FROM users WHERE login = jdoe;

✓ ✕ ✕

SSTable1 SSTable2 SSTable3

jdoe age (t3)

ý jdoe

age (t1) name (t1)

33 John DOE jdoe

age (t2)

34

Page 12: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Compaction!

12

SSTable1 SSTable2 SSTable3

jdoe age (t3)

ý jdoe

age (t1) name (t1)

33 John DOE jdoe

age (t2)

34

New SSTable

jdoe age (t3) name (t1)

ý John DOE

Page 13: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Simple Table!

13

CREATE TABLE users ( login text, name text, age int, … PRIMARY KEY(login));

partition key (#partition)

Page 14: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Clustered table (1 – N)!

14

CREATE TABLE sensor_data ( sensor_id text, date timestamp, raw_data blob, PRIMARY KEY((sensor_id), date));

partition key clustering column (sorted)

unicity

Page 15: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Sorted on disk!

sensor_id date1(t1) date2(t2) date3(t3) date4(t4) date5(t5) …

… … … … …

SSTable1

Page 16: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Worst anti-patterns!

Queue-like designs!CQL null!

Intensive updates on same column!Design around dynamic schema!

!

Page 17: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Failure level!

17

☠☠

☠☠☠

☠☠☠☠

Page 18: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

18

Adding new message ☞ 1 physical insert

Page 19: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

19

Adding new message ☞ 1 physical insert Consuming message = deleting it ☞ 1 physical insert (tombstone)

Page 20: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

20

Adding new message ☞ 1 physical insert Consuming message = deleting it ☞ 1 physical insert (tombstone) Transactional queue = re-inserting messages ☞ physical insert * <many>

Page 21: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

21

A

FIFO queue

{ A }

Page 22: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

22

A B

FIFO queue

{ A, B }

Page 23: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

23

A B C

FIFO queue

{ A, B, C }

Page 24: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

24

A B C A

FIFO queue

{ B, C }

Page 25: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

25

A B C A D

FIFO queue

{ B, C, D }

Page 26: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

26

A B C A D B

FIFO queue

{ C, D }

Page 27: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

27

A B C A D B C

FIFO queue

{ D }

Page 28: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

28

A A A A A A A A A A

FIFO queue, worst case

{ }

Page 29: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Failure level!

29

☠☠☠

Page 30: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Queue-like designs!

30

Solution: event-sourcing •  write ahead, never delete •  read = move a cursor forward (or backward in time for history)

A B C D A E

Write cursor

Read cursor. Next read will give {A, E}

Page 31: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

CQL null semantics!

31

Reading null value means •  value does not exist (has never bean created) •  value deleted (tombstone)

SELECT age FROM users WHERE login = jdoe; à NULL

Page 32: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

CQL null semantics!

32

Writing null means •  delete value (creating tombstone) •  even though it does not exist

UPDATE users SET age = NULL WHERE login = jdoe;

Page 33: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

CQL null semantics!

33

Seen in production: prepared statement

UPDATE users SET age = ?, … geo_location = ?, mood = ?, … WHERE login = ?;

Page 34: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

CQL null semantics!

34

Seen in production: bound statement

preparedStatement.bind(33, …, null, null, null, …);

null ☞ tombstone creation on each update …

jdoe age name geo_loc mood status

33 John DOE ý ý ý

Page 35: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Failure level!

35

Page 36: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update!

36

Context •  small start-up •  cloud-based video recording & alarm •  internet of things (sensor) •  10 updates/sec for some sensors

Page 37: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

37

Data model

sensor_id value

45.0034

CREATE TABLE sensor_data ( sensor_id long, value double, PRIMARY KEY(sensor_id));

Page 38: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

38

Updates

sensor_id value (t1)

45.0034

UPDATE sensor_data SET value = 45.0034 WHERE sensor_id = …; UPDATE sensor_data SET value = 47.4182 WHERE sensor_id = …; UPDATE sensor_data SET value = 48.0300 WHERE sensor_id = …;

sensor_id value (t13)

47.4182 sensor_id

value (t36)

48.0300

Page 39: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

39

Read

SELECT sensor_value from sensor_data WHERE sensor_id = …;

read N physical columns, only 1 useful … (until compaction)

sensor_id value (t1)

45.0034 sensor_id

value (t13)

47.4182 sensor_id

value (t36)

48.0300

Page 40: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Failure level!

40

☠☠

Page 41: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

41

Solution 1: leveled compaction! (if your I/O can keep up)

sensor_id value (t1)

45.0034 sensor_id

value (t13)

47.4182 sensor_id

value (t36)

48.0300

sensor_id value (t36)

48.0300

Page 42: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

42

Solution 2: reversed timeseries & DateTiered compaction strategy

CREATE TABLE sensor_data ( sensor_id long, date timestamp, value double, PRIMARY KEY((sensor_id), date)) WITH CLUSTERING ORDER (date DESC);

Page 43: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Intensive update on same column!

43

Data cleaning by configuration the strategy (base_time_seconds)

SELECT sensor_value FROM sensor_data WHERE sensor_id = … LIMIT 1;

sensor_id date3(t3) date2(t2) date1(t1) ...

48.0300 47.4182 45.0034 …

Page 44: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

44

Customer emergency call •  3 nodes cluster almost full •  impossible to scale out •  4th node in JOINING state for 1 week •  disk space is filling up, production at risk!

Page 45: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

45

After investigation •  4th node in JOINING state because streaming is stalled •  NPE in logs

Page 46: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

46

After investigation •  4th node in JOINING state because streaming is stalled •  NPE in logs Cassandra source-code to the rescue

Page 47: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

47

public class CompressedStreamReader extends StreamReader { … @Override public SSTableWriter read(ReadableByteChannel channel) throws IOException { … Pair<String, String> kscf = Schema.instance.getCF(cfId); ColumnFamilyStore cfs = Keyspace.open(kscf.left).getColumnFamilyStore(kscf.right);

NPE here

Page 48: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

48

The truth is •  the devs dynamically drop & recreate table every day •  dynamic schema is in the core of their design Example:

DROP TABLE catalog_127_20140613; CREATE TABLE catalog_127_20140614( … );

Page 49: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

49

Failure sequence

n1

n2

n4

n3

catalog_x_y

catalog_x_y

catalog_x_y

catalog_x_y

1 4

2

3

5

6

Page 50: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

50

Failure sequence

n1

n2

n4

n3

catalog_x_y

catalog_x_y

catalog_x_y

catalog_x_y

1 4

2

3

5

6

catalog_x_z

catalog_x_z

catalog_x_z

catalog_x_z

Page 51: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

51

Failure sequence

n1

n2

n4

n3

1 4

2

3

5

6

catalog_x_z

catalog_x_z

catalog_x_z

catalog_x_z

catalog_x_y ????

Page 52: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Design around dynamic schema!

52

Nutshell •  dynamic schema change as normal prod operation is not

recommended •  schema AND topology change at the same time is an anti-pattern

Page 53: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Failure level!

53

☠☠☠☠

Page 54: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Q & R

! " !

Page 55: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Nice Examples!

Rate limiting!Anti Fraud!

Account Validation!

Page 56: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

56

Start-up company, reset password feature

1) /password/reset

2) SMS with token A0F83E63DB935465CE73DFE….

Phone number Random token

3) /password/new/<token>/<password>

Page 57: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

57

Problem 1 •  account created with premium phone number

Page 58: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

58

Problem 1 •  account created with premium phone number •  /password/reset x 100

Page 59: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

59

« money, money, money, give money, in the richman’s world » $$$

Page 60: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

60

Problem 2 •  massive hack

Page 61: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

61

Problem 2 •  massive hack •  106 /password/reset calls from few accounts

Page 62: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

62

Problem 2 •  massive hack •  106 /password/reset calls from few accounts •  SMS messages are cheap

Page 63: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

63

Problem 2 •  ☞ but not at the 106/per user/per day scale

Page 64: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

64

Solution •  premium phone number ☞ Google libphonenumber

Page 65: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting!

65

Solution •  premium phone number ☞ Google libphonenumber •  massive hack ☞ rate limiting with Cassandra

Page 66: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Cassandra Time To Live!

66

Time to live •  built-in feature •  insert data with a TTL in sec •  expires server-side automatically •  ☞ use as sliding-window

Page 67: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting in action!

67

Implementation •  threshold = max 3 reset password per sliding 24h per

user

Page 68: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Rate limiting in action!

68

Implementation •  when /password/reset called •  check threshold •  reached ☞ error message/ignore

•  not reached ☞ log the attempt with TTL = 86400

Page 69: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Rate Limiting Demo

Page 70: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Anti Fraud!

70

Real story •  many special offers available •  30 mins international calls (50 countries)

•  unlimited land-line calls to 5 countries •  …

Page 71: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Anti Fraud!

71

Real story •  each offer has a duration (week/month/year) •  only one offer active at a time

Page 72: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Anti Fraud!

72

Cassandra TTL •  when granting new offer

INSERT INTO user_special_offer(login, offer_code, …) VALUES(‘jdoe’, ’30_mins_international’,…) IF NOT EXISTS USING TTL <offer_duration>;

Page 73: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Anti Fraud Demo

Page 74: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Account Validation!

74

Requirement •  user creates new account •  sends sms/email link with token to validate account •  10 days to validate

Page 75: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Account Validation!

75

How to ? •  create account with 10 days TTL

INSERT INTO users(login, name, age) VALUES(‘jdoe’, ‘John DOE’, 33) USING TTL 864000;

Page 76: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Account Validation!

76

How to ? •  create random token for validation with 10 days TTL

INSERT INTO account_validation(token, login, name, age) VALUES(‘A0F83E63DB935465CE73DFE…’, ‘jdoe’, ‘John DOE’, 33) USING TTL 864000;

Page 77: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

@doanduyhai

Account Validation!

77

On token validation •  check token exist & retrieve user details

SELECT login, name, age FROM account_validation WHERE token = ‘A0F83E63DB935465CE73DFE…’;

•  re-insert durably user details without TTL INSERT INTO users(login, name, age) VALUES(‘jdoe’, ‘John DOE’, 33);

Page 78: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Q & R

! " !

Page 79: DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - NoSQl matters Barcelona 2014

Thank You @doanduyhai

[email protected]

https://academy.datastax.com/