JP Ventura
JP Ventura

Reputation: 5742

Is Firebase Realtime Database key generation reliable to clock change on Android/iOS devices? #AskFirebase

TL; DR

Firebase Realtime Database uses a push ID with a chronological prefix, thus new objects are always sorted by its creation timestamp.

Given offline capabilities is enable on Android and iOS app, if the user has disabled the automatic clock synchronization, will the SDK:

  1. Detect the change and adjust the prefix when the connection is recovered?
  2. Obey the clock change and create an ID with a bad prefix?

Details

The Bolsonaro's Daylight Saving Time Bug

In 2019 Brazilian government canceled Daylight Saving Time without giving the proper time to carriers and internet providers get prepared to it. Consequently clocks were automatically advanced, crashing several distributed systems in Brazil due timestamp inconsistency. This phenomena is popular known as Bolsonaro's Bug.

Desperate because they were no longer receiving orders from customers, they turned off the automatic clock adjustment and set it manually, users who depends on their mobile devices for work (ex. taxi drivers, deliverer professionals, etc) turned off the automatic clock update on their devices and manually turned their block back in 1 hour. Thus:

  1. If you live in Rio de Janeiro, the GMT-3 would be updated to GMT-2 during daylight saving time, but UTC would not change (so far, so good).
  2. Turning of the automatic update and manually subtracting 1 hour to fix the bug messed with the device UTC, i.e. it is 1 hour behind the real UTC.

Question

The scenario is problematic when the user save data offline (with a bad clock) and hours later tries to sync with the server.

Since Firebase Realtime Database has offline capabilities and push IDs are chronologically sorted, in this very specific scenario, would it be possible to:

  1. Inform the SDK about this delay to the correct UTC time?
  2. Help the SDK to fix the push IDs generated offline and update their timestamp prefix?

Upvotes: 1

Views: 190

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

An inaccurate local clock could cause inaccurate push IDs, in terms of time ordering. If you need accurate times for the purpose of sorting, I wouldn't rely too heavily on the ordering of push IDs.

You might want to read the documentation on clock skew for more information on what you can do to deal with possible time differences:

While firebase.database.ServerValue.TIMESTAMP is much more accurate, and preferable for most read/write operations, it can occasionally be useful to estimate the client's clock skew with respect to the Firebase Realtime Database's servers. You can attach a callback to the location /.info/serverTimeOffset to obtain the value, in milliseconds, that Firebase Realtime Database clients add to the local reported time (epoch time in milliseconds) to estimate the server time. Note that this offset's accuracy can be affected by networking latency, and so is useful primarily for discovering large (> 1 second) discrepancies in clock time.

DatabaseReference offsetRef = FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");
offsetRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        double offset = snapshot.getValue(Double.class);
        double estimatedServerTimeMs = System.currentTimeMillis() + offset;
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.w(TAG, "Listener was cancelled");
    }
});

Upvotes: 2

Related Questions