I have the table named as message_tbl
in dynamodb for messaging system.
For the purpose to fetch all the message items related to particular conversation_id, i designed the table like this:
The attributes are:
Primary Hash key => conversation_id
Primary Sort key => date_time
Other attributes => sender_id
, message
conversation_id date_time sender_id message
123456 2016-12-27 06:00:39 pm 10 hai how are you..?
123456 2016-12-27 06:01:00 pm 11 I am fine
123456 2016-12-27 06:01:12 pm 10 ok
123456 2016-12-27 06:01:12 pm 14 Hai man. How are you.?
The last two entries which is send by sender_id
=> 10 & 14 in same time can have chances to happen right.?
If it happens, will replace the attribute values with the same primary key and the data loss will occur.
Shall i use an unique random 6 digit string as secondary local index, i can get escape from this..?
While put item into db with same conversation_id and date_time can accept if i set secondary index as sort key (unique random string).? If i am doing wrong, prescribe me to design the table.
Note: I am using PHP Codeigniter MVC framework.
Yes, with DynamoDB if you your
Hash Key
isconversation_id
and yourSort Key
isdate_time
and you get those 2 messages:conversation_id date_time sender_id message 123456 2016-12-27 06:01:12 pm 10 ok 123456 2016-12-27 06:01:12 pm 14 Hai man. How are you.?
The last message will overwrite it.
What you could do is have a
message_id
. It's essentially the unique random string you were already thinking as theSort Key
. So this would not be overwritten:conversation_id message_id date_time sender_id message 123456 1123 2016-12-27 06:01:12 pm 10 ok 123456 3213 2016-12-27 06:01:12 pm 14 Hai man
Then if you want to query by
conversation_id
and sort things bydate_time
, like you had before, you could create a Local Secondary Index.