skip to content

As the lead on an Android app built on Firebase, how do you decide which writes go straight from the client under Security Rules and which go through Cloud Functions?

level: principalimportance: should knowfreq 30%

answer

  1. who can be trusted to compute it
  2. Admin SDK writes skip rules
  3. offline queue versus a network call
  4. callables gatekeep, triggers react
  5. one authority per field

basics

~20 s

Let the client write directly where Security Rules can validate the change from the request alone; route a write through a callable when it needs secrets, hidden data or cross-document invariants, and deny that direct path in rules. Triggers handle consequences.

solid answer

~50 s

Start from what each layer can do. Direct writes under Security Rules are cheap, work offline and update the UI at once, but rules see one request with limited lookups and no secrets. A callable runs with the Admin SDK, so it can check coupons, prices or external services and answer with an `HttpsError`, but it needs the network, costs an invocation and bypasses rules, so rules must deny clients the fields it owns. A trigger runs after the write commits, at least once and unordered, so it can derive data and send email but never reject a write. My default is direct writes for user-owned data whose validity the request proves, callables for anything a modified client could abuse, and triggers for follow-on work, with each field's authority written down and tested in the Emulator Suite.

go deeper

for a junior

Recall that Security Rules check client writes, callables run trusted code on request, and triggers run after a write has happened.

for a middle

Explain why Admin SDK writes skip rules and why a trigger cannot reject a write.

for a senior

Show how you would lock a server-owned field: deny it in rules, write it in a callable, and test a hostile client in the emulator.

for a principal

Own the split per collection, weighing offline behaviour, invocation cost, client version lifetime and review burden, and keep one authority for each field.

## Three places logic can live A Firebase app can enforce a rule in three places, and each has a different contract: | | Direct write under Security Rules | Callable (`onCall`) | Event trigger (`onDocumentCreated` and others) | |---|---|---|---| | Runs | on each client request, before the write | when the app calls it | after the write has committed | | Can reject the write | yes | yes, it performs the write itself | no, it can only compensate | | Works offline | yes, queued and synced later | no | not applicable | | Can use secrets or external APIs | no | yes | yes | | Cost per write | rules evaluation only | one invocation and a round trip | one invocation | | Delivery | synchronous | synchronous, 70-second client timeout by default | at least once, unordered | ## What rules carry well, and where they stop - Rules are the right gate for **ownership and shape**: the writer owns the document, required fields are present, a status only moves forward. - They judge one request with a limited number of lookups into other documents, cannot call external services and cannot hold secrets. The rules language itself is covered with Firebase Security Rules. - A direct write keeps Firestore's offline queue and its immediate local update, which is real product value on a phone with a poor connection. ## When a callable earns its cost - The outcome depends on **data the client must not see or control**: coupon validity, prices, another user's inventory. - It needs a **secret** or an external call: payments, email, a fraud check. - It enforces an **invariant across many documents** that rules cannot express within their lookup limits. - The app needs a definite answer now, as an `HttpsError` code it can show to the user. - The team wants **one audited place** for a sensitive change, with the caller's `uid` in the function's logs, instead of reconstructing it from document history. - Abuse matters: a callable can require App Check with `enforceAppCheck: true` and apply its own rate limits before touching data. The function writes with the Admin SDK, which Security Rules do not govern, so the rules must also **deny** client writes to the fields and collections the server owns. Otherwise a modified client simply writes `discountPct` itself and skips the callable. ## Triggers are for consequences, not gatekeeping - A trigger sees a write only after other clients may already have read it, so it cannot keep bad data from being seen. - Use triggers for **derived work**: decrement stock, maintain counters, send the confirmation email, send an FCM message. - Because delivery is at least once and unordered, every trigger must be idempotent and tolerate late events. ## A decision procedure for a team 1. List each collection and server-relevant field, and name its single **authority**: the client under rules, or the server. 2. Default to direct writes under rules when the request alone carries everything needed to validate it. 3. Move a write behind a callable when it needs secrets, hidden data, cross-document invariants or an external call, and deny the direct path in rules. 4. Put follow-on work in triggers and make each one idempotent. 5. Test rules and functions together in the Emulator Suite, including a hostile client that writes server-owned fields directly. 6. Revisit the split when offline use, invocation cost or latency targets change. ## What the choice costs - **Everything behind callables** turns Firebase into a hand-written API: offline writes stop working, each write costs an invocation and a round trip, cold starts show up as latency, and installed app versions keep calling a function's name and region for as long as they stay installed. - **Everything in the client** puts all the risk in rules that few people review, with no place for secrets or cross-document checks. - **A per-collection split** is where most teams land. The lead's job is to keep it explicit, written down, enforced by rules that deny the server's paths, and tested. For the order app, placing an order is a direct write validated by rules (the owner, the line format), applying a coupon is a callable that reads the coupon and writes the discount, and stock and the confirmation email are triggers.

  • Why not put every write behind a callable to be safe?
    You lose Firestore's offline queue and local latency compensation, pay an invocation and a round trip for every write, inherit cold-start latency and the client's 70-second timeout, and installed app versions start depending on each function's name and region. You still need rules for reads and to deny direct writes. Reserve callables for writes a modified client could abuse.
  • Can an onDocumentCreated trigger reject an order with a forged total?
    No. The order has already committed and other clients may have read it before the trigger runs, and delivery is at least once and possibly late. The trigger can only compensate, by marking or deleting the order. An invariant that must hold at write time belongs in rules, if the request proves it, or in a callable that performs the write.

  • A Firestore trigger can block an invalid write before other clients see it.
  • Writes from a Cloud Function are still checked by Security Rules.
  • Routing every write through callables costs only a little latency.
  • Security Rules can call an external service to validate a coupon.
  • Once a callable exists, the client can no longer write those fields directly.