500 likes | 660 Views
Marcelo Olivas @mfolivas. Start With “ WHY ”. Scale Out...seriously tho. Linear scaling Real multi-datacenter support “ Fix it Monday ” fault tolerance. Data Model. key/value pair. C. MARCELO. NAME. 1298000486821. timestamp. Column. ROW KEY. C. C. C. C. MOBILE. FNAME. LNAME.
E N D
Marcelo Olivas • @mfolivas
Scale Out...seriously tho • Linear scaling • Real multi-datacenter support • “Fix it Monday” fault tolerance
key/value pair C MARCELO NAME 1298000486821 timestamp Column
ROW KEY C C C C ... ... ... ... MOBILE FNAME LNAME EMAIL 1298000486821 1298000486833 1298000486835 1298000486839 COLUMNS Row
Row • A row is a set of orderable columns
C C C C C C C C C C C C C COLUMN COLUMN COLUMN COLUMN COLUMN COLUMN COLUMN COLUMN COLUMN KEY KEY KEY KEY Column Family
GOOG Price: 589.55 Name: Google APPL Price: 401.76 Name: Apple NFLX Price: 78.73 Name: Netflix Exchange: NYSE Static Column Family
Dynamic Column GOOG 10/25/11=583.16 10/24/11=596.42 10/23/11=590.49 APPL 10/25/11=397.77 10/24/11=405.77 10/23/11=392.87 NFLX 10/25/11=77.37 10/24/11=118.14 10/23/11=117.23 Prematerialized Queries Store it how you read it
Column Family • A column family is a set of orderable rows
Keyspace COLUMN FAMILY COLUMN FAMILY COLUMN FAMILY
Cluster KEYSPACE KEYSPACE
Data Modeling • Column Families are not tables • Column Families can be wide • Column Families can be narrow • The database does not join • You only get one index*
Secondary Index • Index any column value • Performant for low-cardinality columns • Opposite of most relational indexes • Well-supported
Patterns • Entity • Column Family as Index • Materialized View • Time Series • Event Sourcing
Entities • Single Primary Key • Fairly Consistent Columns Names • Fairly Narrow Rows • Feels Like Relational Database • Schemaless
Column Family as Index • Rows are Indexed by Row Key • Secondary Indexes Want Low Cardinality • We Have to Build Our Own • Query-First Data Modeling • Row Key Becomes Index Value • Column Values are Entity Row Keys
Materialized View • Based on Entity Data • A Separate Column Family • Entity Data Organized by Index Key
Time Series • Row Key is Time Identifier • Column Values are Events • Columns Values are Measurements • Rows Can be Very Wide
Event Sourcing • A Martin Fowler idea • Persist state change events • Do not persist present state • All writes are immutable • Play back the tape to rehydrate the app
CQL Missing old friends
CQL over JDBC Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver"); Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9170/Keyspace1"); String query = "UPDATE Test SET a=?, b=? WHERE KEY=?"; PreparedStatement statement = con.prepareStatement(query); statement.setLong(1, 100); statement.setLong(2, 1000); statement.setString(3, "key0"); statement.executeUpdate(); statement.close();
Create Keyspace CREATE KEYSPACE Payables; CREATE KEYSPACE MasterClass WITH strategy_class = 'SimpleStrategy' AND strategy_options:replication_factor = 3;
Create Column Family CREATE COLUMNFAMILY Contacts (KEY uuid PRIMARY KEY); CREATE COLUMNFAMILY Monkey (KEY text PRIMARY KEY, id_tag long, emotion text) WITH comment='Simian Emotional States' AND read_repair_chance = 0.5;
Write UPDATE Contacts SET name='Tim Berglund' WHERE KEY = B70DE1D0-9908-4AE3-BE34-5573E5B09F14; UPDATE Monkeys USING CONSISTENCY EACH_QUORUM SET emotion = 'Angry', name = 'Baby Boss' WHERE KEY = 'OREILLY:8827';
READ SELECT * FROM Contacts WHERE KEY = B70DE1D0-9908-4AE3-BE34-5573E5B09F14; SELECT FIRST 1000 FROM Temperatures WHERE KEY = 7627748986; SELECT emotion, id_tag FROM Monkeys USING CONSISTENCY ONE WHERE KEY = 'OREILLY:8827';
Scaling • How a dynamo-style distributed hash table works
2000 0000 E000 C000 4000 A000 6000 8000
2000 0000 E000 C000 4000 A000 6000 8000 3D97: “ANGRY” 9C4F: “MONKEY”
2000 0000 E000 C000 4000 A000 6000 8000 3D97? 3D97: “ANGRY”
2000 0000 E000 C000 4000 A000 6000 8000 9C4F? 9C4F: “MONKEY”
Replication • Replication factor (N) • default is 1 but 3 is conventional • Replica placement strategy - there more than these • Simple • Network topology strategy
Simple Strategy 2000 0000 E000 C000 4000 A000 6000 8000 3D97: “ANGRY” N=3 3D97: “ANGRY” 3D97: “ANGRY”
Network Topology DC1 DC2 4000 2000 6000 8000 C000 E000 A000 0000 9C4F: “MONKEY” 9C4F: “MONKEY” 9C4F: “MONKEY” 9C4F: “MONKEY”
Client Connection 2000 0000 E000 C000 4000 A000 6000 8000 ? CLIENT
Client Connection 2000 0000 E000 C000 4000 A000 6000 8000 3D97? CLIENT
Write Replica 2000 0000 E000 C000 4000 A000 6000 8000 14C7? CLIENT
Hinted “hand off” --- 0000 E000 C000 4000 A000 6000 8000 X CLIENT 14C7? Coordinator stores a “hint”
Write Consistency • ANY • At least one node (hinted handoffs allowed) • ONE • At least one node (no hints allowed) • QUORUM • Written to (N+1)/2 replicas • ALL
Reading Data 2000 0000 E000 C000 4000 A000 6000 8000 (N=3) 9C4F? 9C4F:”MONKEY” TODAY 9C4F:”MONKEY” TODAY 9C4F:”MONKEY” TODAY
Reading Data 2000 0000 inconsistent E000 C000 4000 A000 6000 8000 (N=3) 9C4F? 9C4F:”MONKEY” TODAY 9C4F:”LEMUR” YESTERDAY 9C4F:”MONKEY” TODAY
Read Consistency • ONE • Get one response from the closest replica • QUORUM • Get (n+1)/2 responses, then return the most recent timestamp (at least two) • ALL • Wait until N replicas respond. Fail if they don’t all answer
Writes Commit Log -> Memtable -> SSTable