# Firestore, Bigtable and Memorystore — Google Cloud Platform

Source: https://www.skillbyai.com/en/gcp/d-nosql

> Match NoSQL and caching services to access patterns.

## Documents, wide columns and caches

**Firestore** is a serverless document database: collections of documents with real-time listeners and offline support for web and mobile clients, strongly consistent queries, and security rules for direct client access; it scales automatically and you pay per read, write and stored GB. Queries need **indexes**, which Firestore creates for single fields and you define for composite queries. **Bigtable** is a wide-column database for very large, high-throughput workloads such as time series, IoT and ad-tech; data is sorted by a single **row key**, so key design determines performance and avoids hotspots. **Memorystore** offers managed **Redis** and **Valkey** (and Memcached) for caching and sessions, reached over private networking. Pick by access pattern: user-facing documents with simple queries (Firestore), petabyte-scale key-range scans (Bigtable), microsecond lookups of hot data (Memorystore).

## Firestore from a backend

Each document lives at a path; queries combine filters and ordering.

```python
from google.cloud import firestore

db = firestore.Client()

db.collection("customers").document("c-42").collection("orders").document("o-1001").set(
    {"total": 1499, "status": "paid", "createdAt": firestore.SERVER_TIMESTAMP}
)

recent_paid = (
    db.collection_group("orders")
      .where(filter=firestore.FieldFilter("status", "==", "paid"))
      .order_by("createdAt", direction=firestore.Query.DESCENDING)
      .limit(20)
)
for doc in recent_paid.stream():
    print(doc.id, doc.to_dict()["total"])
```

## Sequential keys create hotspots

In Bigtable, a row key starting with a timestamp sends every new write to the same node. Lead with something well distributed, such as a device ID, then the time.

**Quiz:** Which service suits petabyte-scale time-series data read by key ranges?

- [ ] Firestore
- [ ] Memorystore
- [x] Bigtable
- [ ] Cloud Tasks

*Answer:* Bigtable. Bigtable is designed for huge, high-throughput workloads organised around sorted row keys.
