Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
What is the best solution to allocate a number from a very large range of numbers?

Requirement description

There is a pool of number 1 - 160 000 000.

When create a obj, need to allocate one number to the obj. There is some rules

  1. the number within the pool
  2. the number not taken by other obj

Also user sometimes will specify one number to use for the obj creation.

Below are some solutions, each have its own problems, So I expect some better solution

Please note that we use mongo DB here. I do not want to change database because this one issue.

Solution 1

Generate a big table(collection) with 160,000,000 items. The structure of the collection is

number,allocated

When allocate number, use find_one_and_update method to update one record,change the allocated from false to true

issue

problem for this solution is that generate a collection of 160,000,000 is too heavy

Solution 2

Similar to solution 1 except we do not generate 160,000,000 at one time. Instead we generate 1000 each time. When this 1000 records is run out, we generate another 1000

issue

The problem is that user can specify number sometimes. For example, we generate 1000 records in the collection, but use want to use number 5000 instead. So this is the problem now because we did not generate it

Solution 3

Each time we create an obj, we generate a random number within 1-160,000,000 to this obj and save it in the db.

issue

It is hard to avoid that the random number you generated is not used previously

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The usual way to do this is to have a (Sharded) Atomic counter. The counter initially has a value of zero. When there is need of an index, an API should be called which will atomically increment this counter and give out its old value.

While this will likely be much faster than the approaches you have mentioned, this may still not be quick enough depending on your needs. The bottleneck in the above situation is the single lock typically used while making the increment atomic. This is not ideal in some distributed situations.

Using Sharded Counters:

The usual way to increase performance in such distributed scenarios is to have sharded counters:

  1. Shard the counter (partition the range of values 1..160,000,000 into N disjoint ranges).
  2. Have the same atomic increment service running in N threads / processes / entities / machines with N different locks.
  3. Based on some property (maybe address of object or hash of an object), choose one of the ranges (in a distributed system you could use distributed hashing)
  4. Ask the appropriate service mentioned in (2) for the next index.

The above will increase performance N-fold and will likely scale to your application needs.

Some interesting reading on sharded counters is at this link.

Note that if you want to use a random number generation (Solution 3), you could optimize looking for existence of a key using Bloom Filters. This may be sufficient depending on your performance needs.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda