Skip to content

Instantly share code, notes, and snippets.

View rebaz94's full-sized avatar

Rebaz Raouf rebaz94

View GitHub Profile
class StateHolder<T> {
T? internalValue;
T get value => internalValue!;
}
class CounterWidget extends StatefulWidget {
// State reference holder
final StateHolder<CounterState> $ = StateHolder();
@rebaz94
rebaz94 / README.md
Created May 11, 2022 12:22 — forked from colophonemes/README.md
Postgres Distributed Key Generation

Postgres Distributed Key Generation

This SQL creates a Postgres function to generate sequential, numeric, unique IDs in a consistent format across services. Useful for database sharding or microservices.

Draws heavily on Instagram's ID generator, via Rob Conery, with minor modifications.

The main changes are that the unique number resolution is per-second rather than per-millisecond. This is to reduce key size below 2^53^-1 so that generated IDs that are under Javascripts Number.MAX_SAFE_INTEGER limit . This is important if you're using these on a Node.js server (e.g. our use case is an Express API using Hashids).

Max IDs are in the order of 51 bits, broken down as follows:

  • 31 bits for the timestamp difference