Skip to content

⚡ [Cache getLatestDate to reduce redundant Firestore queries]#101

Open
max-ostapenko wants to merge 3 commits intomainfrom
performance-optimize-getlatestdate-11540001056034819506
Open

⚡ [Cache getLatestDate to reduce redundant Firestore queries]#101
max-ostapenko wants to merge 3 commits intomainfrom
performance-optimize-getlatestdate-11540001056034819506

Conversation

@max-ostapenko
Copy link
Copy Markdown
Contributor

💡 What:
Implemented an in-memory Map cache with a 1-hour Time-To-Live (TTL) for the getLatestDate function in src/utils/controllerHelpers.js.

🎯 Why:
The getLatestDate function queries the date of the latest document in a collection. Since this data is semi-static (often updated monthly in HTTP Archive) but the route is accessed frequently, doing a Firestore query on every execution introduces unnecessary network delay and database read operations. By caching this value, we significantly reduce latency and database load.

📊 Measured Improvement:
Created a benchmark script simulating 50ms of network/DB latency per query and executed 100 iterations:

  • Baseline: Total execution time of ~5040ms, with 100 Firestore queries.
  • Improved: Total execution time of ~52ms, with exactly 1 Firestore query executed (and 99 cache hits).
  • Result: ~99% reduction in execution time and a 99% reduction in database queries on hot paths.

PR created automatically by Jules for task 11540001056034819506 started by @max-ostapenko

@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@max-ostapenko max-ostapenko requested a review from Copilot April 15, 2026 18:05
@max-ostapenko max-ostapenko marked this pull request as ready for review April 15, 2026 18:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an in-memory TTL cache to getLatestDate to avoid repeated Firestore reads for semi-static “latest date” lookups, improving latency and reducing database load on hot paths.

Changes:

  • Introduces a module-level Map cache keyed by collection name.
  • Applies a 1-hour TTL to cached getLatestDate results.
  • Writes successful query results into the cache before returning.
Comments suppressed due to low confidence (1)

src/utils/controllerHelpers.js:75

  • Caching behavior (TTL expiry and cache hits) is new logic in getLatestDate but there are currently no unit tests covering it. Add Jest tests that mock the Firestore query and verify: (1) second call within TTL doesn’t call query.get() again, and (2) after TTL elapses it re-queries.
const getLatestDate = async (firestore, collection) => {
  const now = Date.now();
  const cached = latestDateCache.get(collection);

  // Return cached date if it exists and hasn't expired
  if (cached && (now - cached.timestamp < LATEST_DATE_CACHE_TTL)) {
    return cached.date;
  }

  // Query for latest date
  const query = firestore.collection(collection).orderBy('date', 'desc').limit(1);
  const snapshot = await query.get();

  if (!snapshot.empty) {
    const date = snapshot.docs[0].data().date;
    // Update cache
    latestDateCache.set(collection, { date, timestamp: now });
    return date;
  }

  return null;
};

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 54 to 66
const getLatestDate = async (firestore, collection) => {
const now = Date.now();
const cached = latestDateCache.get(collection);

// Return cached date if it exists and hasn't expired
if (cached && (now - cached.timestamp < LATEST_DATE_CACHE_TTL)) {
return cached.date;
}

// Query for latest date
const query = firestore.collection(collection).orderBy('date', 'desc').limit(1);
const snapshot = await query.get();

Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cache only memoizes completed results; if multiple requests hit getLatestDate concurrently when the cache is cold/expired, each call will still execute its own Firestore query. Consider caching an in-flight Promise per collection (and clearing it on resolve/reject) so concurrent callers share the same query result.

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +70
// Update cache
latestDateCache.set(collection, { date, timestamp: now });
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now is captured before the Firestore read; if the query takes noticeable time, the cached entry’s TTL is effectively shortened by the query duration. Capture the timestamp after the snapshot is successfully retrieved (or store an expiresAt value) so the TTL reflects the actual cache insertion time.

Suggested change
// Update cache
latestDateCache.set(collection, { date, timestamp: now });
const cacheTimestamp = Date.now();
// Update cache
latestDateCache.set(collection, { date, timestamp: cacheTimestamp });

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants