I am trying to wrap my head around how to structure my data for firebase realtime database. I read the docs and some other questions on SO finding following advices:
- data should be as flat as possible
- be expensive on writes to have cheap reads
- avoid nesting data
- duplication of data may be okay
Keeping this in mind let me describe my specific use case. Frist there is user with following attributes:
- Firstname
- Lastname
- Profile picture (big)
- Profile picture (small)
A user may create a story, that consist of following attributes:
- User
- Text
- Timestamp
The visual representation of a story may look like this:
My question is, how would you associate user information (firstname, lastname, small profile picture) with a story?
What I thought about:
put a user_id in the story that contains the foreign id to the specific user. To load the story we would have to make two request to the database, one to get the story and one for the user.
{ user_id : 'XYZ', text: 'foobar', timestamp: ... }
put firstname, lastname and small profile picture in the story. Only one request would be necessary to display the story. But we would have to update each user's story, when e.g. the profile picture changes.
{ user_id : 'XYZ', firstname: 'sandra', lastname: 'adams', smallProfilePicutre: '...', text: 'foobar', timestamp: ... }
So when there are few stories created and most of the time there are just reads, approach 1. would be expensive, because we pay for two reads to display a story. Approach 2. would be more cost efficient.
I would like to here your thoughts and ideas on this.