DEV Community

Vishwa
Vishwa

Posted on Originally published at Medium

Choosing the Right Database on GCP: Ask What the App Does With the Data

Orders in Cloud SQL, the cart in Redis or Firestore, reports in BigQuery, and why one database is rarely enough

Picture a shopping app. A customer adds shoes to the cart on her phone and expects to see them on the laptop a second later. She pays, and stock must drop by exactly one. That evening, a manager asks for sales by category for the whole month.

These are three different questions, and I would not answer all of them with the same database.

The problem

The easy choice is "put everything in one database". It works on day one. Then it breaks in quiet ways:

  • The month-end sales report scans millions of order rows on the same primary that is taking payments, and checkout slows down.
  • Cart updates, which happen constantly and matter only for a short time, compete with orders for the same connections.
  • A product catalog where a phone has RAM and a shirt has a size gets forced into a table full of empty columns.

The real question is not "which database is popular". It is "what does the app do with this data?"

The solution: choose by the read

Here is how I split the shopping app:

Data Store Why
Orders, payments, stock Cloud SQL (Spanner if global) Money and stock must change together or not at all: transactions
Live cart Firestore One key per user, live sync to every open screen
Cart / session cache Redis (Memorystore) cart:user42 with a 30-minute TTL, so abandoned carts clean themselves up
Product catalog MongoDB Atlas or Firestore Flexible documents, different attributes per product
Clicks and views Bigtable A firehose of writes, read back by key
Sales reports BigQuery SQL scans over history

Orders in Cloud SQL. Placing an order is one transaction: insert the order and its items, reduce stock. If any step fails, everything rolls back, so there is no order without stock and no charge without an order. Cloud SQL fits one region. If I needed one globally consistent stock count across regions, that is where Spanner would replace it.

Cart in Firestore or Redis. These two solve slightly different needs. Firestore gives real-time listeners, so the cart change is pushed to the other open screens with no polling. Redis is the fast option when the cart is a short-lived cache next to the app, with a TTL. Either way, the cart is not the source of truth for money; the checkout re-reads price and stock from Cloud SQL.

Analytics in BigQuery. Reports never read the app database. Order changes are sent as small JSON messages through Pub/Sub, Dataflow validates, removes duplicates and enriches them, and writes them into date-partitioned BigQuery tables. A query for one day reads only that partition, and since BigQuery stores data by column, it reads only the columns the query uses.

CAP helps break ties. When the network splits, some data must stay correct and some must stay available. A bank balance or stock count should refuse a write rather than risk a conflict (CP). A view counter should keep counting and merge later (AP).

Trade-offs and limits

  • More databases means more to run. Every extra store adds backups, monitoring and a skill the team must have. For a small product I would start with Cloud SQL plus a Redis cache and add the others only when a real access pattern needs them.
  • Copies are eventually consistent. The BigQuery report is a copy, so it can be a little behind. That is fine for a sales dashboard and wrong for a payment screen.
  • Pipelines deliver at least once. Pub/Sub can deliver the same message twice, so the pipeline needs a dedupe key (here order_id + table + ts) and a dead-letter table for bad rows.
  • Bigtable has no SQL. You read by row key or key range, so the key must be designed around the main read before any data goes in. Getting it wrong means full scans or one hot node.
  • Cart in two places is a trap. If I use both Firestore and Redis for the cart, I must be clear which one is the truth. Usually I would pick one.

Takeaway

Ask what the app does with the data, then pick the store.

Orders in Cloud SQL, live cart in Firestore or Redis, history in BigQuery.

Keep the app database as the truth and treat everything else as a copy.

Top comments (0)