KatInterview

Architecting Real-Time Data Sync with React and Firestore

System DesignModel answer
EasyModel answerSystem Designreactfirebasefirestore

Last updated

Architect a high-performance, cost-effective data retrieval strategy for a live fundraising application using React and Firestore.

Imagine you are building the front-end data layer for a high-traffic crowdfunding platform. The main campaign view displays a real-time progress bar and a total amount raised metric that needs to update dynamically as supporters donate. The application uses React on the client and Google Cloud Firestore as its database.

Explain your strategy for handling data fetching, lifecycle management, and secure back-end operations. In your response, address the following three areas:

  1. onSnapshot vs. getDoc: Compare real-time listeners (onSnapshot) against one-time queries (getDoc). How do cost, network overhead, user experience, and scalability influence your choice?
  2. React Subscription Lifecycle: How do you correctly manage database listeners within React functional components? What steps prevent memory leaks, improper re-renders, and race conditions during route transitions or unmounting?
  3. Secure Back-End Processing: How do you execute secure operations (such as issuing receipts, updating payment ledgers, or applying business rules) without putting sensitive logic or write access on the client side?

Hints

Hint 1

Avoid summing individual donation documents on the client side; think about how pre-aggregated summary documents simplify reads.

Hint 2

Remember that Firestore listeners return an unbind callback function that integrates directly with React's effect cleanup mechanism.

Hint 3

Consider using server-side triggers (e.g., Cloud Functions) and keeping write permissions for financial balances restricted to Admin SDK environments.