The hi/lo algorithm

Introduction

In my previous post I talked about various database identifier strategies, you need to be aware of when designing the database model. We concluded that database sequences are very convenient because they are both flexible and efficient for most use cases.

But even with cached sequences, the application requires a database round-trip for every new the sequence value. If your applications demand a high number of insert operations per transaction, the sequence allocation may be optimized with a hi/lo algorithm.

The hi/lo algorithm

The hi/lo algorithms split the sequences domain into “hi” groups. A “hi” value is assigned synchronously. Every “hi” group is given a maximum number of “lo” entries, that can by assigned off-line without worrying about concurrent duplicate entries.

  1. The “hi” token is assigned by the database, and two concurrent calls are guaranteed to see unique consecutive values
  2. Once a “hi” token is retrieved we only need the “incrementSize” (the number of “lo” entries)
  3. The identifiers range is given by the following formula:
  4. [(hi -1) * incrementSize) + 1, (hi * incrementSize) + 1)

    and the “lo” value will be taken from:

    [0, incrementSize)

    starting from

    [(hi -1) * incrementSize) + 1)

  5. When all “lo” values are used, a new “hi” value is fetched and the cycle continues

Here you can have an example of two concurrent transactions, each one inserting multiple entities:

hi_lo_algorithm

Continue reading “The hi/lo algorithm”

Advertisements

A beginner’s guide to natural and surrogate database keys

Types of primary keys

All database tables must have one primary key column. The primary key uniquely identifies a row within a table therefore it’s bound by the following constraints:

  • UNIQUE
  • NOT NULL
  • IMMUTABLE

When choosing a primary key we must take into consideration the following aspects:

  • the primary key may be used for joining other tables through a foreign key relationship
  • the primary key usually has an associated default index, so the more compact the data type the less space the index will take
  • a simple key performs better than a compound one
  • the primary key assignment must ensure uniqueness even in highly concurrent environments

When choosing a primary key generator strategy the options are:

  1. natural keys, using a column combination that guarantees individual rows uniqueness
  2. surrogate keys, that are generated independently of the current row data

Continue reading “A beginner’s guide to natural and surrogate database keys”

What I learned at Topconf Bucharest

Introduction

I’ve got back from Topconf Romania, a developer to developer conference that emerged in Tallinn and for the first time this year it was also held in Bucharest .

As an architect I assumed I’d be after technical speeches but I got really impressed by some management related presentations as well.

Lessons learned

A conference is a great learning experience. New technologies are being advertised and software paradigms get dissected and questioned by both the speakers and the attenders. There were some great ideas I came back with and I’ll share with you as follows:

It’s all about feed-back

Feed-back is the tool of wise people. Every action has an associated reaction and the feed-back is a reinforcing factor you should never ignore.

Nothing is perfect but feed-back can help you get better. Feed-back is probably the only suitable learning technique in the ever changing environment of software development.

We inherently use feed-back to build better relationships, to shape our personalities or understand a problem space whose function depends on way too many variables to think of any formula that can always give you the right result.

Continue reading “What I learned at Topconf Bucharest”

A beginner’s guide to Hibernate Types

The basic mapping concepts

When learning Hibernate, many like to jump to ParentChild associations without mastering the object relation mapping basics. It’s very important to understand the basic mapping rules for individual Entities before starting modelling Entity associations.

Hibernate types

A Hibernate type is a bridge between an SQL type and a Java primitive/Object type.

Continue reading “A beginner’s guide to Hibernate Types”

The minimal configuration for testing Hibernate

Introduction

In my previous post I announced my intention of creating a personal Hibernate course. The first thing to start with is a minimal testing configuration. The examples are relevant for Hibernate 4.

You only need Hibernate

In a real production environment you won’t use Hibernate alone, as you may integrate it in a Java EE or Spring container. For testing Hibernate features you don’t need a full-blown framework stack, you can simply rely on Hibernate flexible configuration options.

Continue reading “The minimal configuration for testing Hibernate”