Sunday, April 10, 2016

Firebase: cloud DB's unique IDs

Firebase Logo.png

Firebase - Build Extraordinary Apps
"A powerful platform for your mobile or web application"Firebase - Wikipedia, the free encyclopedia
"Firebase provides a realtime database and backend as a service. The service provides application developers an API that allows application data to be synchronized across clients and stored on Firebase's cloud... Firebase is a cloud services provider and backend as a service company. The company was acquired by Google in October 2014.

course: Firebase Fundamentals | Pluralsight

"A core concept of the Firebase database is that every piece of data has its own URL"

Firebase is a "real-time" "key-value" NoSQL store,
that maps to JSON structured data for all subkeys based on URL.
That creates interesting benefits and challenges in designing data structures.
Since JSON includes all "subkeys" (by URL) it is an imperative to minimize nesting.
Objects have internal identifiers, and those identifiers are used as pointers to other objects.
The identifiers have form of random strings like this: "-JRMxm5cFfp-TeUG1JE_"
(20 character long strings resemble GUID encoded in base64)
This "JSON pointers" technique also resembles the way Netflix exposes data to APIs)

The 2^120 Ways to Ensure Unique Identifiers - Firebase
"Firebase’s push IDs, they are the chronological, 20-character unique IDs generated when you write lists of data to Firebase from any of our client libraries."{
    "messages": {
        "-JhLeOlGIEjaIOFHR0xd": "Hello there!",
        "-JhQ76OEK_848CkIFhAq": "Push IDs are pretty magical.",
        "-JhQ7APk0UtyRTFO9-TS": "Look a white rabbit!"

"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.
To turn our 120 bits of information (timestamp + randomness) into an ID that can be used as a Firebase key, we basically base64 encode it into ASCII characters, but we use a modified base64 alphabet that ensures the IDs will still sort correctly when ordered lexicographically (since Firebase keys are ordered lexicographically).
Since the timestamp is generated client-side, it’s at the mercy of the client’s local clock, which could be incorrect. We make an attempt to compensate for these ‘skewed’ clocks to some degree. When a Firebase client establishes a connection, the Firebase server sends an accurate timestamp to the client so it can use that to correct its incorrect clock when generating push IDs.

Web Quickstart - Firebase
Tutorial - Firebase
Web Development Guide - Firebase
Structuring Data - Firebase
Firebase App Dashboard (demo data in dashboard)

Anant Narayanan - Building Realtime Apps With Firebase and Angular - NG-Conf 2014 - YouTube

JavaScript tools, Promises & Observables

kriskowal/q: A tool for creating and composing asynchronous promises in JavaScript @ GitHub