Firestore Schema Design for Unbounded Comments
Short answer
A Store each comment in a subcollection of the article.
Question
Key Takeaways
This CRISC question addresses designing a scalable schema in Google Cloud Firestore for handling an unknown number of comments. Storing comments in a subcollection of the article document is correct because it bypasses the 1 MiB document size limit and supports efficient hierarchical reads.
Expert Analysis
The decisive factor in this schema design is the requirement to handle an unknown and potentially unbounded volume of comments while maintaining efficient access patterns for both reading and posting. Firestore’s document size limit of 1 MiB makes storing all comments in a single structure impossible, necessitating a hierarchical data model that leverages subcollections to bypass these constraints. Option A correctly implements this by treating each article as a parent document and its associated comments as a subcollection, which naturally scales with the number of articles and comments without hitting storage limits.
Subcollections Bypass Document Size Constraints
Firestore enforces a strict maximum document size of 1 MiB (megabyte). This constraint is absolute; any attempt to store more data than this within a single document will result in a write failure. In the context of a comment system where users can post unlimited comments on news articles, storing every comment directly within the article's document would quickly exceed this limit. By utilizing a subcollection, Firestore allows you to create an unlimited number of documents under a specific parent document path. Each comment becomes its own document within the comments subcollection of the articles/{articleId} document. This architectural choice effectively decouples the storage capacity from the single-document limit, allowing the application to scale horizontally as more comments are added to any given article. The parent document can still hold metadata about the article (title, summary, author) while the heavy payload of comments resides in the scalable subcollection structure.
Hierarchical Access Patterns Enable Efficient Reads
The mechanism of subcollections aligns perfectly with the access pattern described: displaying comments for a specific article. When a user views an article, the application needs to retrieve the list of comments associated with that specific article ID. With the schema defined in Option A, the application can execute a simple query against the /articles/{articleId}/comments subcollection. This query returns only the documents relevant to that specific article, ensuring efficient read performance. If comments were stored in a flat collection or an array, retrieving them would require either scanning the entire database or complex filtering logic that degrades performance. The hierarchical structure allows Firestore to optimize queries based on the parent-child relationship, making it ideal for scenarios where data is accessed via a known parent identifier.
Array Properties and Alternative Stores Fail at Scale
Option B suggests adding each comment to an array property on the article. This approach fails because Firestore arrays have practical limitations; while they can grow, updating large arrays is expensive due to the need to rewrite the entire document, and they do not support querying individual elements within the array efficiently. Furthermore, as the array grows, the document size approaches the 1 MiB limit, leading to eventual write failures. Option C proposes storing comments in documents but adding keys to an array on the article. This introduces unnecessary complexity and redundancy; the array of keys serves no purpose if the comments are already accessible via the subcollection, and it duplicates data without providing a benefit. Option D stores keys in the user profile, which incorrectly associates comments with the wrong entity scope; comments belong to articles, not users, making retrieval for article display inefficient and semantically incorrect. Other options like Cloud SQL or Bigtable, while valid databases, are not the primary focus of Firestore-specific schema design questions unless real-time sync is explicitly required, which is implied by the nature of comment systems.
Conditions for Alternative Designs
A different schema would be appropriate if the comments needed to be queried globally across all articles or if they required complex relational joins beyond Firestore's capabilities. For instance, if the requirement was to find all comments made by a specific user across all articles, storing them in a user-centric subcollection might be considered, though even then, a global index or separate collection is often better. However, since the prompt specifies displaying comments for news articles, the article-centric hierarchy is optimal. If the volume of comments per article was guaranteed to be small and fixed, an array might suffice, but the "unknown number" constraint rules this out. Therefore, the subcollection approach remains the only viable solution for scalable, unbounded comment storage in Firestore.
Exam Strategy
When designing NoSQL schemas, always check for hard storage limits like Firestore's 1 MiB document cap. If data volume is unbounded, use subcollections to create hierarchical structures that scale infinitely. Avoid arrays for growing datasets as they become inefficient to update and query. Map your access patterns first: if you query by parent ID, make the child data a subcollection of that parent.
Related Analysis
Practice All PCD Questions
Access 84 questions with complete answers and detailed explanations.
View Full PCD Practice Test →