Search code examples
firebasefirebase-cloud-messagingretry-logicexponential-backoff

FCM: Retry-after and exponential backoff


As I understand, when a message fails to be delivered, the Retry-After header is sometimes included in the response and sometimes not. But what happens if I first receives an error response with Retry-After included, resends the message and receives another error response but without Retry-After? I know I should use exponential backoff but how does that work when the previous waiting time was from the Retry-After header?

Imagine this sequence of requests and responses:

Request 1: No waiting
Response 1: Error without Retry-After
Request 2: Wait 2 seconds
Response 2: Error with Retry-After included (let's say 120 seconds)
Request 3: Wait 120 seconds
Response 3: Error without Retry-After
Request 4: How long should I wait? 

How long should I wait before sending request 4? 8 seconds? Or start from the beginning with 2 seconds?


Solution

  • AFAIK, there isn't really a standard way or interval for an exponential backoff.

    However, what is usually followed is that for every consecutive unsuccessful request, the waiting interval should be longer than the previous one, but if it is the first error response (e.g. previous request was successful), it should revert to the default value (a value you set, e.g. 2 seconds).

    An example would be something like this.

    Request 1: No waiting
    Response 1: Error without Retry-After
    Request 2: Wait 2 seconds // assume 2seconds is your default
    Response 2: Error with Retry-After included (let's say 120 seconds)
    Request 3: Wait 120 seconds
    Response 3: Error without Retry-After
    Request 4: 180 // you use the previous value of 120 x 1.5 (sample value increment)
    Request 5: ...
    Response 5: Success!
    Request 6: 1 second (assume default waiting interval per success response)
    Response 6: Error without Retry-After included
    Request 7: Wait 2 seconds (using the default)
    

    There are also some behaviors where it doesn't have to a precise interval, some mix in a random value for the increment.

    You should have a look at the following: