Multiple Firestore changes with batch vs cloud functions










0















In a chat app, if I add a new message to the messages collection, I also need to update that particular chat's document in another collection to show the last message and the time when it was sent. Right now I am triggering a cloud function every time a new message comes, in order to update the metadata for the chat. Am I doing the right thing or would it be more appropriate to use Batched writes instead?










share|improve this question




























    0















    In a chat app, if I add a new message to the messages collection, I also need to update that particular chat's document in another collection to show the last message and the time when it was sent. Right now I am triggering a cloud function every time a new message comes, in order to update the metadata for the chat. Am I doing the right thing or would it be more appropriate to use Batched writes instead?










    share|improve this question


























      0












      0








      0








      In a chat app, if I add a new message to the messages collection, I also need to update that particular chat's document in another collection to show the last message and the time when it was sent. Right now I am triggering a cloud function every time a new message comes, in order to update the metadata for the chat. Am I doing the right thing or would it be more appropriate to use Batched writes instead?










      share|improve this question
















      In a chat app, if I add a new message to the messages collection, I also need to update that particular chat's document in another collection to show the last message and the time when it was sent. Right now I am triggering a cloud function every time a new message comes, in order to update the metadata for the chat. Am I doing the right thing or would it be more appropriate to use Batched writes instead?







      firebase google-cloud-firestore google-cloud-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 14:34









      Frank van Puffelen

      233k29380406




      233k29380406










      asked Nov 14 '18 at 9:35









      romin21romin21

      18013




      18013






















          1 Answer
          1






          active

          oldest

          votes


















          2














          There is a difference that you might be aware of when using one approach vs. the other. When using a batch write, according to the official documentation:




          You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.




          This means that those simultaneous updates that are made in this atomic way, either all updates succeed or all updates fail.



          In case you are using a function that is triggered once a message is sent, it means that you are performing two separate actions. The first one is to send a message and the second one is to update some metadata once the message is successfully sent. In this case, you can send a message but your function may fail, according to the official documentation:




          By default, without retries enabled, the semantics of executing a background function are "best effort." This means that while the goal is to execute the function exactly once, this is not guaranteed.




          This are the reasons why background functions fail to complete:




          On rare occasions, a function might exit prematurely due to an internal error, and by default the function might or might not be automatically retried.



          More typically, a background function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:



          • The function contains a bug and the runtime throws an exception.

          • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.

          • The function intentionally throws an exception (for example, when a parameter fails validation).

          • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.



          The workaround in this case, is to use retry to handle transient errors.






          share|improve this answer

























          • So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

            – romin21
            Nov 14 '18 at 15:22






          • 1





            In this case, you should go ahead with the batch writes.

            – Alex Mamo
            Nov 14 '18 at 15:24










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296987%2fmultiple-firestore-changes-with-batch-vs-cloud-functions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          There is a difference that you might be aware of when using one approach vs. the other. When using a batch write, according to the official documentation:




          You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.




          This means that those simultaneous updates that are made in this atomic way, either all updates succeed or all updates fail.



          In case you are using a function that is triggered once a message is sent, it means that you are performing two separate actions. The first one is to send a message and the second one is to update some metadata once the message is successfully sent. In this case, you can send a message but your function may fail, according to the official documentation:




          By default, without retries enabled, the semantics of executing a background function are "best effort." This means that while the goal is to execute the function exactly once, this is not guaranteed.




          This are the reasons why background functions fail to complete:




          On rare occasions, a function might exit prematurely due to an internal error, and by default the function might or might not be automatically retried.



          More typically, a background function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:



          • The function contains a bug and the runtime throws an exception.

          • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.

          • The function intentionally throws an exception (for example, when a parameter fails validation).

          • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.



          The workaround in this case, is to use retry to handle transient errors.






          share|improve this answer

























          • So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

            – romin21
            Nov 14 '18 at 15:22






          • 1





            In this case, you should go ahead with the batch writes.

            – Alex Mamo
            Nov 14 '18 at 15:24















          2














          There is a difference that you might be aware of when using one approach vs. the other. When using a batch write, according to the official documentation:




          You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.




          This means that those simultaneous updates that are made in this atomic way, either all updates succeed or all updates fail.



          In case you are using a function that is triggered once a message is sent, it means that you are performing two separate actions. The first one is to send a message and the second one is to update some metadata once the message is successfully sent. In this case, you can send a message but your function may fail, according to the official documentation:




          By default, without retries enabled, the semantics of executing a background function are "best effort." This means that while the goal is to execute the function exactly once, this is not guaranteed.




          This are the reasons why background functions fail to complete:




          On rare occasions, a function might exit prematurely due to an internal error, and by default the function might or might not be automatically retried.



          More typically, a background function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:



          • The function contains a bug and the runtime throws an exception.

          • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.

          • The function intentionally throws an exception (for example, when a parameter fails validation).

          • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.



          The workaround in this case, is to use retry to handle transient errors.






          share|improve this answer

























          • So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

            – romin21
            Nov 14 '18 at 15:22






          • 1





            In this case, you should go ahead with the batch writes.

            – Alex Mamo
            Nov 14 '18 at 15:24













          2












          2








          2







          There is a difference that you might be aware of when using one approach vs. the other. When using a batch write, according to the official documentation:




          You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.




          This means that those simultaneous updates that are made in this atomic way, either all updates succeed or all updates fail.



          In case you are using a function that is triggered once a message is sent, it means that you are performing two separate actions. The first one is to send a message and the second one is to update some metadata once the message is successfully sent. In this case, you can send a message but your function may fail, according to the official documentation:




          By default, without retries enabled, the semantics of executing a background function are "best effort." This means that while the goal is to execute the function exactly once, this is not guaranteed.




          This are the reasons why background functions fail to complete:




          On rare occasions, a function might exit prematurely due to an internal error, and by default the function might or might not be automatically retried.



          More typically, a background function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:



          • The function contains a bug and the runtime throws an exception.

          • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.

          • The function intentionally throws an exception (for example, when a parameter fails validation).

          • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.



          The workaround in this case, is to use retry to handle transient errors.






          share|improve this answer















          There is a difference that you might be aware of when using one approach vs. the other. When using a batch write, according to the official documentation:




          You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.




          This means that those simultaneous updates that are made in this atomic way, either all updates succeed or all updates fail.



          In case you are using a function that is triggered once a message is sent, it means that you are performing two separate actions. The first one is to send a message and the second one is to update some metadata once the message is successfully sent. In this case, you can send a message but your function may fail, according to the official documentation:




          By default, without retries enabled, the semantics of executing a background function are "best effort." This means that while the goal is to execute the function exactly once, this is not guaranteed.




          This are the reasons why background functions fail to complete:




          On rare occasions, a function might exit prematurely due to an internal error, and by default the function might or might not be automatically retried.



          More typically, a background function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:



          • The function contains a bug and the runtime throws an exception.

          • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.

          • The function intentionally throws an exception (for example, when a parameter fails validation).

          • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.



          The workaround in this case, is to use retry to handle transient errors.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 10:53

























          answered Nov 14 '18 at 10:48









          Alex MamoAlex Mamo

          42k72859




          42k72859












          • So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

            – romin21
            Nov 14 '18 at 15:22






          • 1





            In this case, you should go ahead with the batch writes.

            – Alex Mamo
            Nov 14 '18 at 15:24

















          • So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

            – romin21
            Nov 14 '18 at 15:22






          • 1





            In this case, you should go ahead with the batch writes.

            – Alex Mamo
            Nov 14 '18 at 15:24
















          So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

          – romin21
          Nov 14 '18 at 15:22





          So I think in my scenario using batch writes is the right way to go right? I don't want inconsistencies of the type: The message is sent but the metadata aren't updated because a function call failed.

          – romin21
          Nov 14 '18 at 15:22




          1




          1





          In this case, you should go ahead with the batch writes.

          – Alex Mamo
          Nov 14 '18 at 15:24





          In this case, you should go ahead with the batch writes.

          – Alex Mamo
          Nov 14 '18 at 15:24

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296987%2fmultiple-firestore-changes-with-batch-vs-cloud-functions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線