Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeing very high latency for local fetches from firestore on device db #6673

Open
raghavvijind opened this issue Feb 5, 2025 · 5 comments
Open

Comments

@raghavvijind
Copy link

[READ] Step 1: Are you in the right place?

We are facing a very high latency upto 140ms to 200ms when trying to fetch firestore's local database. We understand that remote fetches can take a higher time but local fetches taking upto 150ms is not acceptable.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: Android Studio Meerkat | 2024.3.1 Canary 6
  • Firebase Component: Firestore
  • Component version: 33.7.0

[REQUIRED] Step 3: Describe the problem

We are facing a very high latency upto 140ms to 200ms when trying to fetch firestore's local database. We understand that remote fetches can take a higher time but local fetches taking upto 150ms is not acceptable.

Steps to reproduce:

Any fetches from local database are latent, takes upto 150ms

Relevant Code:

private suspend fun executeDocumentTask(pageName: String?, fireStoreTask: Task, source: String, stylesList: String = EMPTY, tracer : Tracer? = null): DocumentSnapshot? = suspendCancellableCoroutine { coroutine ->
val startTimeMs = System.currentTimeMillis()
try {
tracer?.log("fireStoreManager.executeDocumentTask.addOnCompleteListener.start")
fireStoreTask.addOnCompleteListener {
tracer?.log("fireStoreManager.executeDocumentTask.inOnCompleteListener.result")
if (it.isSuccessful) {
application.trackInNewRelic(eventName = "firestore_fetch_success", attributes = mutableMapOf(
"reason" to "Exception - ${it.exception.toString()} || Reason - ${it.exception?.message.toString()}",
"source" to source,
"duration" to System.currentTimeMillis() - startTimeMs
))
if (it.result.exists() && "server".equals(source, true)) {
application.trackInNewRelic(eventName = "firestore_fetch_no_items", attributes = mutableMapOf(
"stylesNotFound" to stylesList,
"page" to (pageName ?: EMPTY)
))
}
if (coroutine.isActive) coroutine.resume(it.result)
} else {
application.trackInNewRelic(eventName = "firestore_fetch_failed", attributes = mutableMapOf(
"reason" to "Exception - ${it.exception.toString()} || Reason - ${it.exception?.message.toString()}",
"source" to source,
"duration" to System.currentTimeMillis() - startTimeMs
))
if (coroutine.isActive) coroutine.resume(null)
}
}
} catch (exception: Exception) {
application.trackInNewRelic(eventName = "firestore_fetch_failed", attributes = mutableMapOf(
"reason" to "Exception - ${exception.toString()} || Reason - ${exception.message.toString()}",
"source" to source,
"duration" to System.currentTimeMillis() - startTimeMs
))
if (coroutine.isActive) coroutine.resume(null)
}
}

val widgetPageDocumentFromCache = withContext(mainDispatcher) {
tracer?.log("getWidgetUrlFromPage.widgetPageDocumentFromCache.main.start")
val data = executeDocumentTask(pageName, documentRef.get(Source.CACHE), "cache", tracer =tracer)
tracer?.log("getWidgetUrlFromPage.widgetPageDocumentFromCache.main.end")
data
}

@cherylEnkidu
Copy link
Contributor

Hi @raghavvijind ,

Thank you so much for reporting this. Can you provide us some more information to investigate this issue? For example, does this behaviour only show up after SDK upgrade to a certain version? Which Android device and what operation system version you are using?

@raghavvijind
Copy link
Author

raghavvijind commented Feb 19, 2025 via email

@cherylEnkidu
Copy link
Contributor

We recently fixed a performance issue in the Web SDK, but this bug should not affect the Android and iOS SDKs.

In your case, enabling auto cache index creation might help:
Configure Offline Query Indexes.

By default, the SDK's cache system scans all documents in a collection to find the target. With cache indexing enabled, queries can locate results more efficiently.

@cherylEnkidu
Copy link
Contributor

We would like your feedbacks for using this feature if possible :) Right now we set a threshold which creates cache indexes conservatively. If you would like to try more, I can explain how to change the SDK to create more cache indexes.

@raghavvijind
Copy link
Author

raghavvijind commented Mar 11, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants