I have a web based instant messenger application written by PHP. Recently I migrated to couchdb from mysql and i think that it's generally a good idea, it's working perfectly so far. I don't need views etc. Basicly I fetch document by ID.
However I've some doubts about performance. A conversation between two users is stored in a single document. For example between A and B, I've a document, between C and B I've another document etc.
I never delete logs and when a new conversation is initiated between those two users, I decode stored document with json_decode, print out the recent conversation history two the users. When one of them write something new, I add the new chat line at the end of the array (that I obtained from the document), reencode array to json and finally update document.
Do I right? What's the best practice about storing such large arrays in no-sql databases?
I'd model it differently; Use a document for each thing said. {"from":"foo","to":"bar","text":"hey there"}. Thus you only ever make new documents and every document stays very small.
Add a timestamp and then use a view keyed on that timestamp to reconstruct the conversation.
You'll want to use the server's time to ensure the proper ordering, so I'd recommend you update via an update handler (http://wiki.apache.org/couchdb/Document_Update_Handlers) and add the timestamp there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With