[erlang-questions] how: mnesia: id/generator in a cluster

Scott Lystig Fritchie <>
Sun Nov 11 20:54:55 CET 2007


>>>>> "uw" == Ulf Wiger (TN/EAB) <> writes:

uw> I once wrote the following code, which I've never used in any real
uw> product. The idea is that you have a counter per erlang node in
uw> your cluster, and then assign each node an index; the combination
uw> of node index and counter value becomes your unique id (unique to
uw> your cluster, that is).

I've used a similar scheme, to great success.  A single Mnesia
key/value pair keeps track of the next unused value.  Any counter
client that wants exclusive rights to a range of values above that
counter, say C through C + 999, will use a Mnesia transaction simply
increment the key's value by 1000.  (*Not* using the dirty counter
mechanism.)  Then that client can use the values C..(C+999) in any way
it sees fit, without touching Mnesia again.

"Gaps" in counter assignment aren't a problem for this app, and
strictly monotonically increasing counters weren't required.
Uniqueness and speed were.  If strictly monotonically increasing
counters aren't necessary, do not go to the effort of trying to make
them.  Laziness is good.  :-)

-Scott



More information about the erlang-questions mailing list