Firestore write batch with 400 docs takes multiple minutes to complete

Firestore write batch with 400 docs takes multiple minutes to complete



I am using the Firestore database on Android and found that when I do a batched write over 300-400 items (but below the limit of 500), it can take very long to complete and the success listener to kick in (i.e. 3-5 minutes). Sometimes the listener doesn't react at all.



In the latter case (when the listener doesn't react), neither the OnSuccess or OnFailure listener is being called. I'm checking with logs.
So it feels like the commit is stuck somewhere?



The document writes I make are with POJOs. Each POJO/Document has approximately 60 indices. Could the amount of indices be the problem?
The documents are nested in a subcollection.



Here is my code:


for (int i = 0; i < listOfPojos.size(); i++)

Pojo pojo= listOfPojos.get(i);


//set/create the document in subcollection. Use its date as name.
DocumentReference document = docRef.collection(pojo.getDevice_id()).document(pojo.getDate().toString());
batch.set(document, pojo);



batch.commit().addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)

//do stuff
//log here doesn't kick in



).addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)

//do stuff
//log here doesn't kick in


);



I have searched the forums quite a bit but haven't found a question with this problem. Hope that someone could point in the direction of what I'm doing wrong?




1 Answer
1



Sorry to post an answer myself, but I just figured something out.



Instead of having one batch of 400 writes, it's much faster to have 20 batches of 20 documents.



I'm not sure if that is only the case for me, but having smaller write batches seems to be almost exponentially faster than one large batch, even if that batch is clearly under the limit.



So my solution is just to have a hard cap of 20 documents per batch write, and now it's working much faster (I still need 20 seconds for 400 documents writes though).



I'll leave this question up, it might help someone. Or maybe someone who knows more about Firestore can chime in and confirm/refute my observation.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)