Search code examples
cassandrahbasemessagekey-value-store

how can I store a record with 2 keys in a key value store?


This is a semi theoretical question, Im studying system design and I havent worked with KV stores. The question is about designing a chat app.

I have schema like this:

Message
PK | channel_id   bigint
   | message_id   bigint
_______________
   | user_id
   | content 
   | created_at
...

how would I organize this information to store in a Key - Value Store like Hbase, or Cassandra ?


Solution

  • Cassandra supports composite keys
    So you can do a key of both channel_id + message_id

    Example structure for your schema:

    CREATE TABLE message ( 
       channel_id bigint, 
       message_id bigint, 
       user_id bigint, 
       content text,
       created_at date, 
       PRIMARY KEY (channel_id, message_id) 
    );
    

    However, cassandra keys are a bit harded then this.
    With PRIMARY KEY (channel_id, message_id) you only define how to partition your data,
    but there's also CLUSTERING KEY option to define sorting

    In this case it cound be
    PRIMARY KEY ((channel_id, message_id), created_at)

    Which would mean partition by channel_id + message_id AND sort a partition based on created_at.

    Further read on keys in cassandra:
    Difference between keys in cassandra