Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

KillrChat: Building Your First Application in Apache Cassandra (English)

1,703 views

Published on

Presentation by: DuyHai Doan, Apache Cassandra Evangelist at DataStax

Published in: Technology
  • Be the first to comment

KillrChat: Building Your First Application in Apache Cassandra (English)

  1. 1. @doanduyhai KillrChat Presentation DuyHai DOAN, Technical Advocate
  2. 2. @doanduyhai KillrChat presentation! 2 What is KillrChat ? •  scalable messaging app Why KillrChat ? •  show real life de-normalization •  provide real application for attendees •  highlight Cassandra eco-system
  3. 3. @doanduyhai Technology stack! 3 AngularJS UI Bootstrap SockJS Spring REST Spring Security Spring Messaging Cassandra Datastax Mapper Module
  4. 4. @doanduyhai Architecture! 4 Jetty Spring C* HTTP Rest Spring {Boot,Security, Rest, Messaging} Cassandra, Mapper Module In-memory broker Embedded/local Cassandra Single node AngularJS, UI Bootstrap, SockJS
  5. 5. @doanduyhai Architecture! 5 Jetty Spring … Broker (RabbitMQ, ZeroMQ, Kafka…) Jety Spring Jetty Spring Scaling out
  6. 6. @doanduyhai Front end layout! 6
  7. 7. @doanduyhai Where can I have it ? ! 7 Clone the Git repository git clone –b CassandraDayParis https://github.com/doanduyhai/killrchat.git Go into the ‘killrchat’ folder and launch tests cd killrchat mvn clean test
  8. 8. User Account Management!
  9. 9. @doanduyhai Scalability! 9 n1 n2 n3 n4 n5 n6 n7 n8 A B C D E F G H jdoe oscar alice hsue bob Scaling by login!
  10. 10. @doanduyhai Data model! 10 CREATE TABLE killrchat.users( login text, pass text, //password is not allowed because reserved word lastname text, firstname text, bio text, email text, chat_rooms set<text>, PRIMARY KEY(login));
  11. 11. @doanduyhai User’s chat rooms! 11
  12. 12. @doanduyhai User’s chat rooms data model! 12 How to store chat rooms for an user ?! CREATE TABLE killrchat.user_rooms( login text, room_name text, PRIMARY KEY((login), room_name)); •  pros: can store huge room count per user (106) •  cons: separated table, needs 1 extra SELECT!
  13. 13. @doanduyhai User’s chat rooms data model! 13 Best choice! •  1 SELECT fetches all data for a given user •  usually, 1 user is not in more that 1000 rooms at a time •  stores only room name CREATE TABLE killrchat.users( login text, … chat_rooms set<text>, //list of chat rooms for this user PRIMARY KEY(login));
  14. 14. @doanduyhai Lightweight Transaction! 14 Avoid creating the same login by 2 different users ? ☞ use Lightweight Transaction! INSERT INTO killrchat.users(room_name, …) VALUES (‘jdoe’, …) IF NOT EXISTS ; Expensive operation! ☞ do you create a new account every day ?!
  15. 15. Demo
  16. 16. Chat Room Data Model!
  17. 17. @doanduyhai Scalability! 17 n1 n2 n3 n4 n5 n6 n7 n8 A B C D E F G H games scala java politics cassandra Scaling by room name!
  18. 18. @doanduyhai Data model! 18 CREATE TABLE killrchat.chat_rooms( room_name text, creation_date timestamp, banner text, creator ???, creator_login text, participants ???, PRIMARY KEY(room_name));
  19. 19. @doanduyhai Room details! 19
  20. 20. @doanduyhai Room participants! 20
  21. 21. @doanduyhai De-normalization! 21 CREATE TYPE killrchat.user( login text, firstname text, lastname text); CREATE TABLE killrchat.chat_rooms( … creator frozen<user>, // de-normalization … participants set<frozen<user>>, // de-normalization PRIMARY KEY(room_name));
  22. 22. @doanduyhai Lightweight Transaction! 22 Avoid creating the same room by 2 different users ? ☞ use Lightweight Transaction! INSERT INTO killrchat.chat_rooms(room_name, …) VALUES (‘games’, …) IF NOT EXISTS ;
  23. 23. Demo
  24. 24. Chat Room Management! Participants management! Room deletion!
  25. 25. @doanduyhai Participant joining! 25 Adding new participant! UPDATE killrchat.chat_rooms SET participants = participants + {…} WHERE room_name = ‘games’; ❓ What if the creator deletes the room at the same time ?
  26. 26. @doanduyhai Concurrent delete/update! 26 UPDATE chat_rooms SET participants = participants + {login: ‘jdoe’, …} WHERE room_name = ‘games’; DELETE FROM chat_rooms WHERE room_name= ‘games’; result games participants creator banner ... {login: ‘jdoe’, …} ∅ ∅ ∅
  27. 27. @doanduyhai Participant joining! 27 Solution ☞ use Lightweight Transaction! UPDATE killrchat.chat_rooms SET participants = participants + {…} WHERE room_name = ‘games’ IF EXISTS;
  28. 28. @doanduyhai Concurrent delete/update! 28 UPDATE chat_rooms SET participants = participants + {login: ‘jdoe’, …} WHERE room_name = ‘games’ IF EXISTS; DELETE FROM chat_rooms WHERE room_name= ‘games’; OK
  29. 29. @doanduyhai Concurrent delete/update! 29 UPDATE chat_rooms SET participants = participants + {login: ‘jdoe’, …} WHERE room_name = ‘games’ IF EXISTS; Room deleted DELETE FROM chat_rooms WHERE room_name= ‘games’;
  30. 30. @doanduyhai Participant leaving! 30 Removing participant (no read-before-write)! UPDATE killrchat.chat_rooms SET participants = participants - {…} WHERE room_name = ‘games’; ❓ What if the creator deletes the room at the same time ? •  we’ll create a tombstone •  tombstone will be garbage-collected by compaction !
  31. 31. @doanduyhai Concurrent delete/update! 31 UPDATE chat_rooms SET participants = participants - {login: ‘jdoe’, …} WHERE room_name = ‘games’; DELETE FROM chat_rooms WHERE room_name= ‘games’; result games participants creator banner ... ∅ ∅ ∅ ∅
  32. 32. @doanduyhai Deleting room! 32 What if participant leaving at the same time ? •  not a problem, tombstone will be garbage What if participant joining at the same time ? ☞ use Lightweight Transaction! Only room creator can delete room, no one else! ☞ use Lightweight Transaction !
  33. 33. @doanduyhai Deleting room! 33 DELETE killrchat.chat_rooms WHERE room_name = ‘games’ IF creator_login = <current_user_login> AND participants = {…}; Solution Condition on creator_login •  current user login coming from security context, no cheating !
  34. 34. @doanduyhai Deleting room! 34 Condition on participants UPDATE chat_rooms SET participants = participants + {login: ‘jdoe’, …} WHERE room_name = ‘games’ IF EXISTS; DELETE FROM chat_rooms WHERE room_name= ‘games’ IF creator_login = ‘xxx’ AND participants = {…}; OK
  35. 35. @doanduyhai Deleting room! 35 Without condition on participants Room KillrChat - created by Patrick - Patrick - Rachel Remove KillrChat from - Patrick’s chat room list - Rachel’s chat room list Christopher joins KillrChat Patrick deletes KillrChat
  36. 36. @doanduyhai Deleting room! 36 LightWeight Transaction is slow …. … but how often do you delete rooms ?
  37. 37. Demo
  38. 38. Chat Messages!
  39. 39. @doanduyhai Scalability! 39 n1 n2 n3 n4 n5 n6 n7 n8 A B C D E F G H Scaling messages by room name! games message1 … messageN … politics message1 … messageN … java message1 … messageN … cassandra message1 … messageN … scala message1 … messageN …
  40. 40. @doanduyhai Data model! 40 CREATE TABLE killrchat.chat_room_messages( room_name text, message_id timeuuid, content text, author frozen<user>, // denormalization system_message boolean, PRIMARY KEY((room_name), message_id) ) WITH CLUSTERING ORDER BY (message_id DESC);
  41. 41. @doanduyhai Data model! 41 Clustering column message_id order by DESC •  latest messages first Improvements •  current data model limits messages count to ≈ 500 ⨉ 106 •  bucketing by day is the right design PRIMARY KEY((room_name, day), message_id) //day format yyyyMMdd
  42. 42. Demo
  43. 43. Q & R ! "!
  44. 44. Thank You @doanduyhai duy_hai.doan@datastax.com https://academy.datastax.com/

×