Firebase push uniqueness scope

后端 未结 2 1808
醉话见心
醉话见心 2021-01-14 19:25

The docs say Firebase push generates a unique key, I want to know if this key is unique across my whole database (or even a GuId), or just in the node where it was pushed on

2条回答
  •  青春惊慌失措
    2021-01-14 20:01

    It is statistically nearly impossible to get duplicate push-Ids. For it to happen:

    1. The first 48 bit piece of the id, a time stamp generated by the client, would need to be generated in the same millisecond
    2. The next 72 randomly generated bits would need to be the same. So you could have duplicate push ids, but there are 2^72 possible random permutations ( over 4 sextillion which is 4 times the estimated grains of sand on all the beaches in the world ). So you would need a lot of push requests in the same millisecond.

    One of the core developers wrote a blog post describing how they are generated: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

    "A push ID contains 120 bits of information. The first 48 bits are a timestamp, which both reduces the chance of collision and allows consecutively created push IDs to sort chronologically. The timestamp is followed by 72 bits of randomness, which ensures that even two people creating push IDs at the exact same millisecond are extremely unlikely to generate identical IDs. One caveat to the randomness is that in order to preserve chronological ordering if a client creates multiple push IDs in the same millisecond, we just 'increment' the random bits by one."

提交回复
热议问题