a3kov

a3kov

Stamp is a fast and flexible Snowflake-flavored ID generator based on 8-byte integers with optional encoding.

The original Snowflake supported BigTech-scale numbers (e.g. 1024 workers each inserting up to 4,096,000 records per second). With BEAM favoring vertical (rather than horizontal) scaling and hardware getting more powerful every year it is reasonable to assume that we can leverage the bits in the ID more effectively than Twitter did back in 2010, especially if the application never reaches Twitter’s scale.

Features

  • works both with and without Ecto
  • BlazingFast™ sequences on top of process-free atomic counters
  • no global configuration - uses per-field configuration and runtime values
  • user-configurable number of bits for every part of the ID
  • some parts can be disabled, freeing up the bits for other parts
  • optional encoding of the integers to string versions
  • optional Stripe-style prefix to easily distinguish IDs of different models/schemas
  • optional range-based partitioning and sharding (experimental)

How stamps look

Type Stamp
Raw, no partitioning 5406450851512320
Base62 "Ol6XKJ8oy"
Base62 + prefix "u_Ol6XKJ8oy"
Integer codec (always a string) "5406450851512320"
Integer + prefix "u_5406450851512320"
Raw, 256 partitions, p255 (worst case) 9187364417633648640
256 partitions, p255, Base62 "AwgE2gA8mq8"

Stamp vs Alternatives

UUID fields have become more popular recently, but they are expensive for use in indexes and especially primary keys. UUIDv7 improves upon v4 and other versions in some ways, but the size hasn’t changed. There are also exotic alternatives like KSUID and ULID, but they share some issues of UUID.

Stamp Serial/Identity UUID
generated in the application :white_check_mark: :cross_mark: :white_check_mark:
globally unique with distributed generation :cross_mark: :cross_mark: :white_check_mark:
compact in terms of storage and RAM :white_check_mark: :white_check_mark: :cross_mark:
composite PK/indexes without bloat :white_check_mark: :white_check_mark: :cross_mark:
looks good and is short both raw/encoded :white_check_mark: :white_check_mark: :cross_mark:
efficient BTree index operations :white_check_mark: :white_check_mark: :white_check_mark: in v7
may remove the need for extra time field :white_check_mark: :cross_mark: :white_check_mark: in v7
bulk inserts :eight_spoked_asterisk: sequence nuances[1] :white_check_mark: :white_check_mark:
range partitioning/sharding (not by time) :white_check_mark: :cross_mark: :cross_mark:
keeps creation time secret :cross_mark: :eight_spoked_asterisk: guessable[2] :cross_mark: not in v7
keeps number of records secret :white_check_mark: :cross_mark: barely[3] :white_check_mark:
next id is unpredictable :eight_spoked_asterisk: depends[4] :cross_mark: :white_check_mark:
easy to set up and use :red_question_mark: planning, setup[5] :white_check_mark: :white_check_mark:

[1] If an insert overflows the sequence, time goes into the future. This does not limit the insertion,
but forces timestamp accuracy trade-offs. In the worst case scenario you can have more accurate version of creation time in a separate field. See Important details and caveats section in the docs for more info.

[2] Because there is only 1 sequence correlated with creation, one could roughly estimate the
creation time of a record by comparing the number with other records, where the time may be known.

[3] Under normal conditions 1 used sequence number corresponds to 1 record. If we can observe
sequence growth, we can estimate or even know precisely how many records are created in a
period of time.
With total number of records it’s a bit different. Some applications set initial sequence
number high to help with the issue, but it’s only a half measure. If it’s possible to observe random ids inside the application, one can notice the gap and guess that it’s empty.

[4] Stamp is monotonic for a combination of partition, node number, sequence id. If you can generate ids for this combination, you can predict next id. Some randomness can be added by randomly picking from a pool of node numbers on each generation. In general, Stamp is definitely not as unpredictable as UUID simply because of the size difference.

[5] The library provides good defaults tuned for “average project scale” rather than “Twitter
scale”, but learning about the available configuration parameters to get the most benefits is still encouraged.

Project on Hex
Docs

Showing Posts 1 to 2

dimitarvp

dimitarvp

One thing that would help your post here is to substantiate the “looks good” claim with examples.

Also most people want UUIDs because next ID is unpredictable. Can you clarify what does “it depends” means here?

a3kov

a3kov OP

Sure! I’ll add examples.

Total keyspace is big enough, but it’s monotonic for a combination of partition, node, sequence id. If you can generate ids for this combination, you can predict next id. So it really depends on the generator params, parallel generations, partitioning etc. It’s definitely not as unpredictable as UUID. It’s also better than serial/identity (which is just 1 sequence).

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews