Bird
Raised Fist0
Firebasecloud~20 mins

Server timestamps in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Server Timestamp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Understanding Firebase Server Timestamps Behavior

When you set a field in a Firestore document to firebase.firestore.FieldValue.serverTimestamp(), what will be the value of that field immediately after the write operation completes on the client?

AThe exact server time at the moment the write was processed by Firestore.
BA placeholder value that will be replaced asynchronously with the server timestamp once confirmed.
CThe client's local time at the moment the write was initiated.
DNull, because the server timestamp is not available on the client.
Attempts:
2 left
💡 Hint

Think about how Firestore handles latency and synchronization between client and server.

❓ Configuration
intermediate
2:00remaining
Using Server Timestamps in Firestore Document Updates

You want to update a Firestore document's lastModified field to the current server time. Which of the following update commands correctly sets the lastModified field to the server timestamp?

Firebase
const docRef = firestore.collection('users').doc('user123');

// Which update command is correct?
AdocRef.update({ lastModified: firebase.firestore.FieldValue.serverTimestamp() });
BdocRef.update({ lastModified: new Date() });
CdocRef.set({ lastModified: firebase.firestore.FieldValue.serverTimestamp() });
DdocRef.update({ lastModified: firebase.firestore.Timestamp.now() });
Attempts:
2 left
💡 Hint

Consider which method updates only the specified fields and which value represents the server timestamp.

❓ Architecture
advanced
2:30remaining
Designing a Firestore Schema with Server Timestamps for Audit Logs

You are designing an audit log system in Firestore that records user actions with timestamps. You want to ensure timestamps are consistent and trustworthy. Which architecture choice best achieves this?

AStore timestamps as strings formatted on the client side for readability.
BUse client-generated timestamps with <code>new Date()</code> and trust client clocks.
CUse <code>firebase.firestore.FieldValue.serverTimestamp()</code> for all timestamp fields to ensure server time consistency.
DUse a Cloud Function to add timestamps after writes, ignoring client timestamps.
Attempts:
2 left
💡 Hint

Think about trustworthiness and consistency of timestamps in distributed systems.

❓ security
advanced
2:30remaining
Securing Firestore Writes with Server Timestamps

You want to prevent clients from spoofing timestamps in Firestore writes. Which Firestore security rule best enforces that the createdAt field is set only to the server timestamp?

Aallow write: if request.resource.data.createdAt == request.time;
Ballow write: if request.resource.data.createdAt == request.resource.data.createdAt;
Callow write: if request.resource.data.createdAt == request.auth.token.timestamp;
Dallow write: if request.resource.data.createdAt != null;
Attempts:
2 left
💡 Hint

Consider how Firestore security rules access server time and request data.

✅ Best Practice
expert
3:00remaining
Handling Missing Server Timestamps in Offline Firestore Clients

When a Firestore client is offline and writes a document with a serverTimestamp() field, what is the best practice to handle the timestamp field in your app UI before the client reconnects?

ASet the timestamp field to null and never update it.
BShow the client's local time as the timestamp immediately after the write.
CHide the timestamp field completely until the client reconnects.
DDisplay a loading indicator or placeholder until the server timestamp is available.
Attempts:
2 left
💡 Hint

Think about user experience and data consistency when server data is delayed.

Practice

(1/5)
1. What is the main purpose of using firebase.firestore.FieldValue.serverTimestamp() in Firestore?
easy
A. To get the current time from the user's device
B. To store the exact time from the server when the data is written
C. To create a random timestamp for testing
D. To convert a timestamp to a readable string

Solution

  1. Step 1: Understand server timestamp usage

    The method serverTimestamp() sets the field to the current time according to the server, not the client device.
  2. Step 2: Compare with other options

    Options B, C, and D do not describe server-based time storage. Only To store the exact time from the server when the data is written correctly describes storing server time when data is written.
  3. Final Answer:

    To store the exact time from the server when the data is written -> Option B
  4. Quick Check:

    Server timestamp = accurate server time [OK]
Hint: Server timestamp always uses server time, not client time [OK]
Common Mistakes:
  • Thinking it uses client device time
  • Confusing with formatting timestamps
  • Assuming it generates random times
2. Which of the following is the correct syntax to set a Firestore document field to the server timestamp?
easy
A. docRef.set({ createdAt: firebase.firestore.FieldValue.serverTimestamp() })
B. docRef.set({ createdAt: new Date() })
C. docRef.set({ createdAt: Date.now() })
D. docRef.set({ createdAt: firebase.firestore.Timestamp.now() })

Solution

  1. Step 1: Identify correct method for server timestamp

    The correct method to set a server timestamp in Firestore is firebase.firestore.FieldValue.serverTimestamp().
  2. Step 2: Check other options for correctness

    docRef.set({ createdAt: new Date() }) uses client time with new Date(). docRef.set({ createdAt: Date.now() }) uses client time with Date.now(). docRef.set({ createdAt: firebase.firestore.Timestamp.now() }) uses current timestamp but from client side. Only docRef.set({ createdAt: firebase.firestore.FieldValue.serverTimestamp() }) uses the server timestamp method.
  3. Final Answer:

    docRef.set({ createdAt: firebase.firestore.FieldValue.serverTimestamp() }) -> Option A
  4. Quick Check:

    Correct syntax = docRef.set({ createdAt: firebase.firestore.FieldValue.serverTimestamp() }) [OK]
Hint: Use FieldValue.serverTimestamp() to get server time [OK]
Common Mistakes:
  • Using client-side Date or Timestamp instead
  • Missing firebase.firestore prefix
  • Using parentheses incorrectly
3. Consider this Firestore update code:
docRef.update({ lastModified: firebase.firestore.FieldValue.serverTimestamp() });

What will be the value of lastModified immediately after this update on the client side?
medium
A. The client device's current time
B. The exact server timestamp value
C. null until the server confirms the update
D. An error because serverTimestamp() cannot be used in update

Solution

  1. Step 1: Understand serverTimestamp behavior on client

    When using serverTimestamp(), Firestore sets the field to null locally until the server responds with the actual timestamp.
  2. Step 2: Evaluate options based on this behavior

    The exact server timestamp value is incorrect because the client does not have the exact server time immediately. The client device's current time is wrong because client time is not used. An error because serverTimestamp() cannot be used in update is wrong because serverTimestamp() can be used in update. null until the server confirms the update correctly states the field is null initially.
  3. Final Answer:

    null until the server confirms the update -> Option C
  4. Quick Check:

    Local value before server response = null [OK]
Hint: Local timestamp is null until server confirms [OK]
Common Mistakes:
  • Expecting immediate server time on client
  • Confusing update with set behavior
  • Assuming client time is used
4. You wrote this Firestore code but get an error:
docRef.set({ createdAt: firebase.firestore.serverTimestamp() });

What is the likely cause of the error?
medium
A. The document reference docRef is undefined
B. Using set instead of update
C. Server timestamps cannot be used in set calls
D. Missing FieldValue in the method call

Solution

  1. Step 1: Check the method call syntax

    The correct method to get a server timestamp is firebase.firestore.FieldValue.serverTimestamp(). The code misses FieldValue.
  2. Step 2: Verify other options

    Using set instead of update is incorrect because set supports server timestamps. Server timestamps cannot be used in set calls is false for the same reason. The document reference docRef is undefined could cause an error but the question implies a syntax mistake.
  3. Final Answer:

    Missing FieldValue in the method call -> Option D
  4. Quick Check:

    Correct method includes FieldValue [OK]
Hint: Always include FieldValue before serverTimestamp() [OK]
Common Mistakes:
  • Omitting FieldValue in method call
  • Confusing set and update restrictions
  • Assuming serverTimestamp is a direct method of firestore
5. You want to store a user's last login time using server timestamps but only update it if the new login time is later than the existing one. Which Firestore approach correctly applies this logic?
hard
A. Use a Firestore transaction to read current timestamp and update with serverTimestamp() if newer
B. Read the current timestamp, compare client time, then conditionally update with serverTimestamp()
C. Use serverTimestamp() directly in update() without checks
D. Set the timestamp field with client time and rely on serverTimestamp() fallback

Solution

  1. Step 1: Understand conditional update needs

    To update only if the new login time is later, you must read the current value and compare before writing.
  2. Step 2: Identify Firestore feature for atomic read-write

    Firestore transactions allow reading a document and conditionally updating it atomically, ensuring no race conditions.
  3. Step 3: Evaluate options

    Use serverTimestamp() directly in update() without checks ignores condition. Read the current timestamp, compare client time, then conditionally update with serverTimestamp() uses client time which is unreliable. Set the timestamp field with client time and rely on serverTimestamp() fallback mixes client and server times incorrectly. Use a Firestore transaction to read current timestamp and update with serverTimestamp() if newer correctly uses a transaction with serverTimestamp.
  4. Final Answer:

    Use a Firestore transaction to read current timestamp and update with serverTimestamp() if newer -> Option A
  5. Quick Check:

    Conditional update needs transaction [OK]
Hint: Use transactions for conditional server timestamp updates [OK]
Common Mistakes:
  • Relying on client time for comparisons
  • Not using transactions for atomic checks
  • Updating without checking existing timestamp
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard