Hi/Lo algorithm
algorithm explanation
number generation
programming
software development

What's the Hi/Lo algorithm?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The Hi/Lo algorithm is a method used for generating unique identifiers in database systems. These identifiers, known as primary keys, play a critical role in maintaining the integrity of the database by ensuring that each record can be uniquely identified. The Hi/Lo algorithm is commonly employed in systems where a balance is needed between generating a large number of identifiers efficiently and distributing the work across multiple nodes or instances.

Overview of the Hi/Lo Algorithm

The core concept of the Hi/Lo algorithm revolves around splitting the responsibility of ID generation between two components, the "high" and "low" components. This strategy helps optimize the generation of unique keys by reducing the contention typically associated with sequential ID generation in distributed systems.

How It Works

  1. High Component (Hi):
    • The high component is generated less frequently. Typically, a counter at the database level is incremented and fetched whenever a new batch of IDs is required.
    • This high value acts as a prefix or segment identifier for a specific range of low values.
  2. Low Component (Lo):
    • The low component is used within the application instance. It is a local counter that starts at zero and goes up to a maximum predefined limit before the next high value is fetched.
    • The combination of the high and low components results in a unique identifier.

Calculation

The Hi/Lo algorithm can be expressed mathematically as follows:

  • Usually, a composite key is generated in the form of ID = Hi * n + Lo, where:
    • Hi: The current high value from the database.
    • n: The size of the low range.
    • Lo: The current low value.

Whenever the low value exceeds its limit, the high value is incremented, and the local low counter is reset.

Implementation Example

Let’s assume we need to generate identifiers with a low range of 1000. Here’s how this could be implemented:

python
1class HiLoGenerator:
2    def __init__(self, db, low_max):
3        self.db = db
4        self.low_max = low_max
5        self.high_value = self._get_next_high_value()
6        self.low = -1
7        
8    def _get_next_high_value(self):
9        # Simulate fetching and incrementing high value from a database
10        high = self.db.execute("SELECT nextval('high_seq')")
11        return high
12    
13    def get_next_id(self):
14        self.low += 1
15        if self.low >= self.low_max:
16            self.low = 0
17            self.high_value = self._get_next_high_value()
18        return self.high_value * self.low_max + self.low
19
20# Example Usage
21# Assuming 'db' is a database connection with a sequence 'high_seq'
22generator = HiLoGenerator(db, 1000)
23new_id = generator.get_next_id()
24print(f"Generated ID: {new_id}")

Advantages and Disadvantages

Advantages

  • Scalability: The algorithm enables the distribution of key generation amongst multiple nodes, reducing the need for excessive lock contention.
  • Batch Key Allocation: By allocating keys in batches (via the high component), the system reduces frequent requests to the database, which enhances performance.

Disadvantages

  • Complexity: Implementing the Hi/Lo algorithm may introduce additional complexity into the system as it requires careful synchronization.
  • Wasted IDs: If the application crashes or stops without using all low values in the current batch, those IDs are effectively wasted.

Use Cases

  • Distributed systems: Especially where multiple instances need to generate keys consistently without frequent coordination.
  • High volume applications: Where performance and reduced pressure on a centralized key generator are critical.

Summary

The Hi/Lo algorithm is a powerful mechanism for generating unique identifiers efficiently in distributed systems. By employing separate high and low components, it achieves a balance between reducing database load and synchronization overhead. However, as with any algorithm, understanding its trade-offs and ensuring proper implementation is crucial for maximizing its benefits.

ComponentDescription
HiObtained less frequently, acts as a batch prefix.
LoGenerated locally, within a high component batch.
ID FormulaID=Hi×n+LoID = Hi \times n + Lo
ScalabilityProper management can improve scalability without bottlenecking on the database.
ComplexityMore complex than simple auto-increment strategies but efficient in scale.

In conclusion, when implemented correctly, the Hi/Lo algorithm can significantly enhance the performance and scalability of systems requiring unique identifiers in a distributed environment.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.