The hi/lo algorithm allows a server to control id generation by handing out a range of ids that can be assigned to objects as they are created.
Having to request a lot of ids all at the same time, or each time an object is created is expensive so the idea is for the algorithm to provide a single id, which allows the creation of a range of ids.
How does it work?
The id generated is made up of two parts; a Hi part and a Lo part.
- The Hi part of the id is controlled and generated by the database
- Once a Hi part is generated the client only needs to know the maximum number of Lo entries.
- The id can be generated by the following
(Current Hi * Maximum Lo) + Current Lo
- To generate the next id the client just increments the current Lo part
- When all Lo parts are used a new Hi part is requested.