{"id":59784,"date":"2020-08-18T07:25:08","date_gmt":"2020-08-18T07:25:08","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=85834"},"modified":"2020-08-18T07:25:08","modified_gmt":"2020-08-18T07:25:08","slug":"what-is-the-hi-lo-algorithm","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/08\/18\/what-is-the-hi-lo-algorithm\/","title":{"rendered":"What Is the Hi\/Lo Algorithm?"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data- data-id=\"introduction-3\">1. Introduction<\/h2>\n<div class=\"bd-anchor\" id=\"introduction-3\"><\/div>\n<p>In this tutorial, we&#8217;ll explain the\u00a0Hi\/Lo Algorithm. It is used mostly as a\u00a0<strong>database identifier generation strategy<\/strong>.<\/p>\n<p>We&#8217;ll start with the algorithm overview. Then, we&#8217;ll show a practical example based on the Hibernate framework. Finally, we&#8217;ll discuss the algorithm&#8217;s use cases, its benefits, and its drawbacks.<\/p>\n<h2 data- data-id=\"overview-7\">2. Hi\/Lo\u00a0Algorithm Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview-7\"><\/div>\n<h3 data- data-id=\"definition\">2.1 Definition<\/h3>\n<div class=\"bd-anchor\" id=\"definition\"><\/div>\n<p>The main purpose of the Hi\/Lo algorithm is to <strong>create a range of numbers that can be safely used as database identifiers<\/strong>. In order to do that, it uses three number variables commonly called <em>high, low<\/em>, and <em>incrementSize<\/em>.<\/p>\n<p>The <em>incrementSize<\/em> variable holds the maximum number of identifiers that can be generated in one batch. It should be treated as a constant value defined at the beginning of the algorithm. Any runtime modification might cause serious problems in environments where multiple clients use the same Hi\/Lo configuration to persist entries.<\/p>\n<p>The <em>high<\/em> variable is usually assigned from a database sequence. In that case, we&#8217;re sure that no one will get the same number twice.<\/p>\n<p>The <em>low<\/em> variable holds the currently assigned number in the range [0<em>, incrementSize<\/em>).<\/p>\n<p>Given these points, the Hi\/Lo algorithm generates values in range [(<em>hi <\/em>&#8211; 1)<em> * incrementSize <\/em>+ 1<em>, <\/em>(<em>hi * incrementSize<\/em>)).<\/p>\n<h3 data- data-id=\"pseudocode\">2.2 Pseudocode<\/h3>\n<div class=\"bd-anchor\" id=\"pseudocode\"><\/div>\n<p>Let\u2019s take a look at the steps for generating a new value using the Hi\/Lo algorithm:<\/p>\n<ul>\n<li>if <em>low<\/em> is greater than or equal to\u00a0<em>incrementSize<\/em>, assign a new value to <em>high<\/em> and reset <em>low<\/em>\u00a0to 0<\/li>\n<li>generate a new value with the formula: (<em>high<\/em> &#8211; 1) * <em>incrementSize<\/em> + <em>low<\/em><\/li>\n<li>increment <em>low<\/em>\u00a0by 1<\/li>\n<li>return the generated value<\/li>\n<\/ul>\n<h2 data- data-id=\"example\">3.\u00a0Practical Example<\/h2>\n<div class=\"bd-anchor\" id=\"example\"><\/div>\n<p>Let&#8217;s see the Hi\/Lo algorithm in action. To do that, we&#8217;ll use the Hibernate framework and its Hi\/Lo implementation.<\/p>\n<p>First, let&#8217;s define a database entity to work with:<\/p>\n<pre><code class=\"language-java\">@Entity\r\npublic class RestaurantOrder {\r\n    @Id\r\n    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = \"hilo_sequence_generator\")\r\n    @GenericGenerator(\r\n      name = \"hilo_sequence_generator\",\r\n      strategy = \"sequence\",\r\n      parameters = {\r\n        @Parameter(name = \"sequence_name\", value = \"hilo_seqeunce\"),\r\n        @Parameter(name = \"initial_value\", value = \"1\"),\r\n        @Parameter(name = \"increment_size\", value = \"3\"),\r\n        @Parameter(name = \"optimizer\", value = \"hilo\")\r\n      }\r\n    )\r\n    private Long id;\r\n}<\/code><\/pre>\n<p>It&#8217;s a simple restaurant order with one\u00a0<em>id<\/em> field. To correctly define the Hi\/Lo algorithm in Hibernate, in the definition of the <em>id<\/em> field, we must choose a <em>sequence<\/em> strategy \u2013 <em>hilo<\/em> optimizer \u2013 and specify the <em>increment_size<\/em> parameter.<\/p>\n<p>To show the Hi\/Lo algorithm in action, we&#8217;ll persist nine restaurant orders in a loop:<\/p>\n<pre><code class=\"language-java\">public void persist() {\r\n    Transaction transaction = session.beginTransaction();\r\n    for (int i = 0; i &lt; 9; i++) {\r\n        session.persist(new RestaurantOrder());\r\n        session.flush();\r\n    }\r\n    transaction.commit();\r\n}<\/code><\/pre>\n<p>According to the specified increment size in the entity, we should have only three calls to the database for the next <em>high<\/em> value. Assuming the database sequence starts from 1, the first batch of generated identifiers will be in the range [1,3].<\/p>\n<p>When the Hi\/Lo algorithm returns 3 and Hibernate asks for the next identifier&#8217;s value, the value of the <em>low<\/em> variable is equal to the <em>incrementSize<\/em> constant. In that case, the next call to the database for the new <em>high<\/em> value must be made. Having 2 as the new <em>high<\/em> value, the algorithm generates values in the range [4,6].<\/p>\n<p>Finally, the last call to the database for the next <em>high<\/em> value is made, and values in the range [7, 9] are assigned to the entities.<\/p>\n<p>Hibernate logs captured during the execution of the<em> persist()<\/em> method confirm those values:<\/p>\n<pre><code class=\"language-bash\">Hibernate: call next value for hilo_seqeunce\r\norg.hibernate.id.enhanced.SequenceStructure  - Sequence value obtained: 1\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 1, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 2, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 3, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\nHibernate: call next value for hilo_seqeunce\r\norg.hibernate.id.enhanced.SequenceStructure  - Sequence value obtained: 2\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 4, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 5, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 6, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\nHibernate: call next value for hilo_seqeunce\r\norg.hibernate.id.enhanced.SequenceStructure  - Sequence value obtained: 3\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 7, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 8, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator\r\norg.hibernate.event.internal.AbstractSaveEventListener  - Generated identifier: 9, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator<\/code><\/pre>\n<h2 data- data-id=\"banefits_and_drawbacks\">4. Algorithm Benefits and Drawbacks<\/h2>\n<div class=\"bd-anchor\" id=\"banefits_and_drawbacks\"><\/div>\n<p>The main advantage of the Hi\/Lo algorithm is the <strong>reduced number of database calls<\/strong> for the next sequence values. Increasing the value of <em>incrementSize<\/em> decreases the number of round-trips to the database. Obviously, that means a performance gain in our application. In addition to that, the Hi\/Lo algorithm is a <strong>preferred choice in environments with a weak Internet connection<\/strong>.<\/p>\n<p>On the other hand, the Hi\/Lo algorithm <strong>isn&#8217;t the best choice in environments where multiple different clients persist data to the same table in a database<\/strong>. Third-party applications might be unaware of the Hi\/Lo strategy we&#8217;re using to generate identifiers. As a result, they might use entity ids from the generated range of numbers used currently in our application. In that case, when persisting data, we may encounter errors that are difficult to fix.<\/p>\n<h2 data- data-id=\"conclusion-9\">5. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-9\"><\/div>\n<p>In this tutorial, we discussed the Hi\/Lo algorithm.<\/p>\n<p>First, we explained how it works and discussed its pseudocode implementation. Then, we showed a practical example using Hibernate&#8217;s algorithm implementation. Finally, we listed Hi\/Lo benefits and drawbacks.<\/p>\n<p>As always, the code shown in this article is available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/persistence-modules\/hibernate5\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/633810883\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/633810883\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/hi-lo-algorithm-hibernate#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/hi-lo-algorithm-hibernate\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn how to the use Hi\/Lo algorithm as a&nbsp;database identifier generation strategy.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/633810883\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/633810883\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/hi-lo-algorithm-hibernate#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/hi-lo-algorithm-hibernate\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":214,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"What Is the Hi\/Lo Algorithm? - ITTeacherITFreelance.hk","description":"Learn how to the use Hi\/Lo algorithm as a&nbsp;database identifier generation strategy. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"},"footnotes":""},"categories":[6],"tags":[4792,4791],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59784"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=59784"}],"version-history":[{"count":2,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59784\/revisions"}],"predecessor-version":[{"id":60272,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59784\/revisions\/60272"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=59784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=59784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=59784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}