Modeling chat rooms and messages in Firestore

PCD By islever Editorial Last updated

Storing and accessing data. Integrating applications with data and storage services.

Short answer

C Nest a subcollection under each room document with one document per message, so history grows without hitting the room document's size limit.

Question

You are designing a chat room application that will host multiple rooms and retain the message history for each room. You have selected Firestore as your database. How should you represent the data in Firestore?
  1. Create a collection for the rooms. For each room, create a document that lists the contents of the messages
  2. Create a collection for the rooms. For each room, create a collection that contains a document for each message
  3. Create a collection for the rooms. For each room, create a document that contains a collection for documents, each of which contains a message. Correct Answer
  4. Create a collection for the rooms, and create a document for each room. Create a separate collection for messages, with one document per message. Each room’s document contains a list of references to the messages.

Key Takeaways

For a Firestore chat application, model each room as a document and store its messages as a nested subcollection with one document per message. Option C wins because Firestore allows subcollections under documents, keeping the room document small while message history grows unbounded. Flat alternatives either exceed the 1 MiB document limit or require composite indexes for room-scoped queries.

Expert Analysis

C. Pub/Sub is designed for asynchronous messaging with at-least-once delivery and message retention, ideal for chat applications. Cloud Tasks (A) is for task distribution. Cloud Scheduler (B) triggers on cron. GCS notifications (D) are storage-triggered, not chat messaging.

The shipped commentary addresses a different scenario — asynchronous messaging services such as Pub/Sub, Cloud Tasks, Cloud Scheduler and GCS notifications — and none of those services appears in this chat room design, so it cannot be reused here. The design question stands on its own terms: given Firestore as the chosen database, which document and collection layout best represents multiple rooms plus retained per-room message history?

Option C is the intended structure. It nests a subcollection under each room document, with one document per message. Firestore supports this hierarchy natively — a document may contain subcollections — and the nesting gives each room a bounded scope for its messages. The room document itself stays small (metadata such as title or participant count), while the potentially unbounded message list lives in its own subcollection rather than inside a single document. Option C is also the only choice that combines per-room grouping with per-message documents in that nested form, which is what the key selects.

Option A fails because it puts the message contents directly into each room's document. Firestore documents have a 1 MiB size limit and a write is atomic across a document, so appending chat messages into one document would eventually exceed the limit and force rewrites of the whole history on every new message. That is the opposite of what a chat log needs.

Option B is a genuine alternative model — a top-level messages collection filtered by room ID — but it is not what the key selects, and it has a practical drawback here: a room-scoped subcollection keeps messages physically grouped under the room, whereas a flat messages collection must be queried with an equality filter plus ordering on a timestamp, and without a composite index the two together will be rejected. Option D is the weakest: one document per message in a shared collection is workable, but storing an array of message references inside the room document reintroduces the 1 MiB ceiling, since the reference array grows with every message and must be rewritten on each append.

One Firestore behaviour the options assume but never state is that a document's size limit applies to its stored value, not to its subcollections: the room document in option C stays fixed-size because its messages are child documents in a separate collection group scope, not fields inside the room document. That is precisely why option C can retain an unbounded chat history while options A and D cannot. For this data-modeling item the correct answer is therefore C.

Official Reference

https://firebase.google.com/docs/firestore/data-model

This link opens the official vendor documentation in a new tab.

Exam Strategy

When a question asks how to model data in Firestore, reject any option that stores an unbounded, growing list inside a single document — Firestore's 1 MiB per-document limit makes that a trap. Prefer nesting: a parent document for the entity (room) and a subcollection for its children (messages). Then check that each child is its own document, since arrays of references or inline message content reintroduce the size ceiling.

Practice All PCD Questions

Access 84 questions with complete answers and detailed explanations.

View Full PCD Practice Test →

← Back to PCD Study Guide