How Many Cloud Datastore Writes Are Incurred? Exact Formula for StringProperty, StringListProperty & Composite Index Exclusions
Direct Answer (GCP Professional Data Engineer & Cloud Architect Reference): Every Cloud Datastore (or Firestore in Datastore Mode) entity put() incurs 2 base writes (1 for the entity + 1 for the built-in __key__ index) plus 2 writes per indexed scalar property (asc + desc single-property indexes) plus 2 × M writes for an M-element StringListProperty, plus 1 write per composite index entry (or M composite writes when a composite index includes an M-element list property without ancestor, multiplied by (A + 1) ancestor levels if ancestor: yes is enabled).
1. Exact Datastore Index Write Cost Table
Suppose you persist a new entity containing one indexed scalar StringProperty, one indexed StringListProperty with M = 4 values, and one custom composite index over (category_scalar, tags_list) (ascending only, no ancestor):
| Index Component | Formula (New Entity Put) | Example (1 StringProperty + 4-Item StringListProperty + 1 Composite Index) |
|---|---|---|
| Entity + Built-in Key Index | 2 |
2 writes |
Single-Property Index: StringProperty |
2 × 1 (asc + desc) |
2 writes |
Single-Property Index: StringListProperty (M items) |
2 × M (asc + desc per element) |
2 × 4 = 8 writes |
Composite Index: (StringProperty, StringListProperty) |
1 × M (cartesian fan-out) |
1 × 4 = 4 writes |
| Total Index + Entity Writes Incurred | 2 + 2 + 3M |
16 writes for a single entity put()! |
If two StringListProperty fields of lengths M and K appear in the same composite index, Datastore generates M × K composite index entries (the notorious exploding index problem, capped at 40,000 index entries per entity).
2. Eliminating Unnecessary Index Writes in Python (google-cloud-datastore)
By explicitly passing exclude_from_indexes on unqueried JSON blobs, embeddings, descriptions, and audit payloads—and disabling unneeded single-property indexes—you cut Datastore write amplification and monthly GCP storage/write bills by 60–85%:
from google.cloud import datastore
def build_optimized_order_entity(
client: datastore.Client,
tenant_id: str,
order_id: str,
status: str,
tags: list[str],
raw_audit_json: str,
) -> datastore.Entity:
key = client.key("Tenant", tenant_id, "Order", order_id)
# Exclude large or unqueried properties right at Entity construction
entity = datastore.Entity(
key=key,
exclude_from_indexes=("raw_audit_json", "notes", "line_items_snapshot"),
)
entity.update(
{
"status": status, # Indexed scalar StringProperty (2 single-prop writes)
"tags": tags[:10], # Bounded StringListProperty (2*M single-prop writes)
"raw_audit_json": raw_audit_json, # 0 index writes! Can exceed 1,500 bytes safely
"notes": "Idempotent settlement snapshot",
}
)
return entity
def calculate_incurred_datastore_writes(
indexed_scalars: int,
list_lengths: list[int],
composite_fanouts: list[int],
) -> int:
"""Deterministic write-unit estimator for GCP capacity & FinOps reviews."""
base_writes = 2
scalar_writes = 2 * indexed_scalars
list_single_prop_writes = sum(2 * m for m in list_lengths)
composite_writes = sum(composite_fanouts)
return base_writes + scalar_writes + list_single_prop_writes + composite_writes
3. Declarative Composite Index Governance (index.yaml & Firestore Datastore Mode)
Audit every composite index in index.yaml before running gcloud datastore indexes create index.yaml and prune stale indexes via gcloud datastore indexes cleanup index.yaml:
indexes:
- kind: Order
ancestor: yes
properties:
- name: status
direction: asc
- name: tags
direction: asc
Notice that setting ancestor: yes multiplies composite index writes by the depth of the entity's key path (e.g., Tenant -> Order has 2 key path elements, doubling composite index writes from M to 2 × M). Only enable ancestor: yes when strong-consistency ancestor queries are strictly required.
4. Work With a Certified GCP Professional Cloud Architect & Data Engineer
Need to eliminate exploding Datastore/Firestore write costs, migrate high-throughput workloads to BigQuery/Spanner/AlloyDB, or tune distributed Python & Go backends? Explore my core engineering guides or book a fixed-scope sprint:
- GCP Professional Cloud Architect Guide (Cloud Run, GKE, Spanner & FinOps)
- GCP Professional Data Engineer Guide (BigQuery, Dataflow & CDC)
- Designing Composite Ancestor Keys & datastore-indexes.xsd Schemas
- Book a $100 USD / Rp 1,000,000 1-Day Engineering Sprint