<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krishna Nayak</title>
    <description>The latest articles on DEV Community by Krishna Nayak (@krishna-nayak).</description>
    <link>https://dev.to/krishna-nayak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F815242%2F15b6dbe8-7c00-4738-9d92-582c48bc4eb2.jpg</url>
      <title>DEV Community: Krishna Nayak</title>
      <link>https://dev.to/krishna-nayak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krishna-nayak"/>
    <language>en</language>
    <item>
      <title>Why Exposing Persistence Entities in APIs Is a Dangerous Shortcut</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sun, 28 Dec 2025 07:48:38 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/why-exposing-persistence-entities-in-apis-is-a-dangerous-shortcut-3oim</link>
      <guid>https://dev.to/krishna-nayak/why-exposing-persistence-entities-in-apis-is-a-dangerous-shortcut-3oim</guid>
      <description>&lt;p&gt;I recently revisited one of my old backend projects.&lt;/p&gt;

&lt;p&gt;Everything was working fine. No errors. No failing APIs.&lt;/p&gt;

&lt;p&gt;But while reading the code, one design decision immediately stood out — I had &lt;strong&gt;exposed persistence entities directly in API requests and responses&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At the time, it felt like a smart move. Looking back now, I realize it was a shortcut with hidden costs.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Shortcut That Seemed Like a Smart Move
&lt;/h2&gt;

&lt;p&gt;In the initial version, my controller looked something like this:&lt;br&gt;
(Here, BookItem is a persistence entity class used for creating book records.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/books"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;BookItem&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;BookItem&lt;/span&gt; &lt;span class="n"&gt;bookItem&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bookService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookItem&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why did this feel right?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The entity already had all the fields&lt;/li&gt;
&lt;li&gt;No extra classes to write&lt;/li&gt;
&lt;li&gt;Faster development&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DTOs felt like unnecessary boilerplate. For a small project, this approach &lt;em&gt;worked&lt;/em&gt; — and that’s exactly why it was misleading.&lt;/p&gt;




&lt;h2&gt;
  
  
  The First Problem: Clients Can Send Too Much
&lt;/h2&gt;

&lt;p&gt;When an entity is used directly as a request body, &lt;strong&gt;every field becomes writable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ftc75ttbj6lh2hb6jxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ftc75ttbj6lh2hb6jxf.png" alt="client-request" width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means a client can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set database IDs manually&lt;/li&gt;
&lt;li&gt;Modify fields they should never control&lt;/li&gt;
&lt;li&gt;Send values meant only for internal logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The framework doesn’t know intent — it simply maps fields. Without a boundary, &lt;strong&gt;the API trusts the client far too much&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Second Problem: Leaking Sensitive Data
&lt;/h2&gt;

&lt;p&gt;Entities are designed for persistence, not for exposure.&lt;br&gt;
When returned directly in API responses, they often include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Internal flags&lt;/li&gt;
&lt;li&gt;Audit timestamps&lt;/li&gt;
&lt;li&gt;Fields irrelevant or unsafe for clients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5003g0850gv3d3lm212b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5003g0850gv3d3lm212b.png" alt="client-response" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once clients start consuming these fields, they become part of the public contract — even if you never intended them to be.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Real Cost: Change Becomes Dangerous ‼️
&lt;/h2&gt;

&lt;p&gt;This is the most critical problem — and the one that usually appears &lt;strong&gt;too late&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  A Realistic Example
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Initial Entity
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookItem&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  API Response
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Clean Code"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Robert Martin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;464&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Clients build their logic around this response.&lt;/p&gt;
&lt;h4&gt;
  
  
  Later: A Normal Business Change
&lt;/h4&gt;

&lt;p&gt;You decide that &lt;code&gt;pages&lt;/code&gt; is inaccurate and replace it with &lt;code&gt;wordCount&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookItem&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You didn’t touch the controller. You didn’t change the API intentionally.&lt;/p&gt;

&lt;p&gt;But the response now becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Clean Code"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Robert Martin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"wordCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;150000&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  What Just Happened?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pages&lt;/code&gt; disappeared&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wordCount&lt;/code&gt; appeared&lt;/li&gt;
&lt;li&gt;Clients break&lt;/li&gt;
&lt;li&gt;Frontend crashes&lt;/li&gt;
&lt;li&gt;Mobile apps fail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n9egh4sv0psgw66e5o4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n9egh4sv0psgw66e5o4.png" alt="Business Change, Prod Fail" width="800" height="1188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This happened because &lt;strong&gt;the entity was acting as the API&lt;/strong&gt;.&lt;br&gt;
Every internal refactor became a breaking change.&lt;br&gt;
That is why this design is dangerous.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why DTOs Solve This Problem
&lt;/h2&gt;

&lt;p&gt;DTOs create a &lt;strong&gt;stable boundary&lt;/strong&gt; between internal models and external consumers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bh9h872kz4p1awx7di2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bh9h872kz4p1awx7di2.png" alt="DTO solution" width="800" height="1392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With DTOs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clients can send only allowed fields&lt;/li&gt;
&lt;li&gt;Responses expose only safe data&lt;/li&gt;
&lt;li&gt;Entities can change freely&lt;/li&gt;
&lt;li&gt;APIs remain stable&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Using entities directly in APIs often feels productive at first.&lt;/p&gt;

&lt;p&gt;But the real cost appears later — when requirements change and refactoring becomes risky.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An entity is not an API.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;DTOs may add a few extra classes, but they protect your system from silent breakage and future pain.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
      <category>java</category>
    </item>
    <item>
      <title>Token Bucket - Rate Limiter</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sat, 09 Aug 2025 03:20:11 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/token-bucket-rate-limiter-3fb1</link>
      <guid>https://dev.to/krishna-nayak/token-bucket-rate-limiter-3fb1</guid>
      <description>&lt;p&gt;Token Bucket rate limiter is one of the popular rate-limiter algorithms use to control number of requests sent to server. It work by maintaining a bucket which hold fixed number tokens, which will be refilled at a constant rate time.&lt;/p&gt;

&lt;p&gt;Each Request consume one token to processed.&lt;br&gt;
If the bucket don't have tokens left, the request is either delayed or droped, depends on implementation.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxoz1guofhuk37o4e0c3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxoz1guofhuk37o4e0c3.png" alt="token bucket rate limiter" width="800" height="919"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let have a high level understanding.&lt;br&gt;
The bucket is full, holding a maximum number of tokens (let say, capacity is 5). Every time when a request comes in, it check whether there's a token in bucket. if yes, a token is out the request is allow to proceed. if no token is available, the request is denied/delayed, depending on implementation.&lt;/p&gt;

&lt;p&gt;Meanwhile, system refill bucket with new token at a fixed rate(like 3 token/sec). But bucket can't hold more than it bucket capacity - so any extra tokens beyond that are discarded.&lt;/p&gt;

&lt;p&gt;Let consider a scenario where, within the a second there were requests are made, say 3 request. Before allow these request to proceed, rate limiter mechanism will checks system bucket tokens. Since tokens are available, all three requests are allowed, and three tokens are consumed from the bucket. However, within that same second another 5 requests arrive, so again rate limiter mechanism, will check for availability of tokens. At this point, only contain 2 tokens remain, so only the first two requests are allowed and those token are consumed. The remaining three requests are denied or delayed due to insufficient tokens. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfhmrpvywgqwobxctv95.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfhmrpvywgqwobxctv95.png" alt="token bucket mechanisms within a second" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, no additional requests will permite until the bucket is refilled, which happen every one second, based on the defined refill rate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5op5v1ktno26wqhky8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5op5v1ktno26wqhky8o.png" alt="token-refill-mechanism" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At 01.00 second, the refill mechanism kicks in, adding 3 new tokens to the bucket. Immediately after that, 1 new request arrives, which is allowed by consuming 1 of the newly added tokens. Since tokens were available, the request proceeds, and no denial occurs. Moving to 02.00 seconds, no new requests arrive, so while the refill logic adds more tokens, none are consumed. The bucket now reaches its maximum capacity of 5 tokens. Again, no requests are denied, simply because none were received. At 03.00 seconds, the system attempts another refill, but since the bucket is already full, no new tokens are added. No requests arrive at this point either, so no tokens are consumed and no requests are denied. This idle period demonstrates how the token bucket patiently accumulates tokens during inactivity, preparing the system to handle upcoming bursts of traffic efficiently.&lt;/p&gt;



&lt;p&gt;Now that we have developed concept of token bucket rate limiter behavior, it's time to shift our focus to how this &lt;strong&gt;implementation in code&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Core Components of the Token Bucket Algorithm in Code
&lt;/h4&gt;

&lt;p&gt;Before we jump into the Java implementation, let’s break down what needs to be built:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Token Bucket Capacity&lt;/strong&gt; – This defines how many tokens the bucket can hold at any time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refill Rate&lt;/strong&gt; – Determines how many tokens to add per second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Consumption&lt;/strong&gt; – Each request checks if a token is available; if so, it’s consumed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refill Logic&lt;/strong&gt; – The bucket must be topped up based on time elapsed since the last refill.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The class starts with two key configuration parameters: &lt;code&gt;capacity&lt;/code&gt; and &lt;code&gt;refillRate&lt;/code&gt;. The capacity represents the maximum number of tokens the bucket that can hold, while refillRate defines how many tokens should be added per second.&lt;/p&gt;

&lt;p&gt;Internally, the class maintains a &lt;code&gt;tokens&lt;/code&gt; counter to track the number of currently available tokens. Since all access to this variable is handled within synchronized methods, thread safety is ensured. It also keep tracks of last time bucket was refilled using lastRefillTimestamp, which stores the timestamp in nanoseconds (via &lt;code&gt;System.nanoTime()&lt;/code&gt;), allowing for precise time-based calculations. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;refill()&lt;/code&gt; method, whenever a request comes in, this method calculates how much time has passed since the last refill. If one or more full seconds have passed, it computes how many tokens should be added (&lt;code&gt;tokensToAdd = secondsPassed * refillRate&lt;/code&gt;). Then, it updates the token count, ensuring it doesn't exceed the bucket's maximum capacity using &lt;code&gt;Math.min(capacity, currentTokens + tokensToAdd)&lt;/code&gt;. After that, &lt;code&gt;lastRefillTimestamp&lt;/code&gt; is also updated with elapsed seconds to accurately track the time for the next refill cycle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;allowRequest()&lt;/code&gt; method is synchronized to ensure thread-safety. When a request arrives, it first refills the bucket. Then, if there's at least one token available, it decrements the token count and allows the request. Otherwise, the request is denied. This mechanism ensures that only a limited number of requests are allowed over time, enforcing both burst and sustained rate limits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// TokenBucketRateLimiter.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TokenBucketRateLimiter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;refillRate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// tokens per second&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;lastRefillTimestamp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;TokenBucketRateLimiter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;refillRate&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;refillRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;refillRate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastRefillTimestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nanoTime&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;refill&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nanoTime&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;elapsedTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastRefillTimestamp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;secondsPassed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elapsedTime&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1_000_000_000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondsPassed&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;tokensToAdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondsPassed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;refillRate&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;tokensToAdd&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;lastRefillTimestamp&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;secondsPassed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1_000_000_000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;allowRequest&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;refill&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see the Token Bucket rate limiter in action, we will simulate a sequence of requests using the Main class. In this simple example, we create an instance of the TokenBucketRateLimiter with a bucket capacity of 5 tokens and a refill rate of 3 tokens per second. We then simulate 10 consecutive requests in a loop, each separated by a 100-millisecond of pause using Thread.sleep(100). This delay helps mimic how requests might arrive in a real-world application over a short period of time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Simulation of request&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;TokenBucketRateLimiter&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TokenBucketRateLimiter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allowRequest&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Request "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" allowed? "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// simulate requests over time&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it’s your turn - try it out and see for yourself how the Token Bucket rate limiter behaves in different scenarios. You can tweak the capacity, change the &lt;code&gt;refillRate&lt;/code&gt;, or even modify the request intervals in the Main class to observe how the system reacts under various traffic patterns.&lt;/p&gt;

&lt;p&gt;If you have any additional insights, know alternative approaches, or spot potential improvements in this implementation, feel free to share them in the comments.&lt;/p&gt;

&lt;p&gt;Thanks for reading — and happy coding! 🚀&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>java</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understand Rate Limiting – A Beginner-Friendly Overview</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sun, 20 Jul 2025 07:19:44 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/rate-limiter-302d</link>
      <guid>https://dev.to/krishna-nayak/rate-limiter-302d</guid>
      <description>&lt;p&gt;Whenever we visit a website or call API too many times in short span. We might get blocked, or request get rejected. That's due to Rate Limiter.&lt;br&gt;
But why it is important?&lt;/p&gt;

&lt;p&gt;Let's understand with real life scenario.&lt;/p&gt;

&lt;p&gt;Think of Coffee shop.&lt;br&gt;
Only &lt;strong&gt;5 customers&lt;/strong&gt; can be served in every &lt;strong&gt;minute&lt;/strong&gt; because that fast the coffee-maker can make coffee. &lt;br&gt;
Now, if 10 customers come at once, chaos happens. So to deal with that shop introduce a &lt;strong&gt;token system&lt;/strong&gt; - only 5 customers are allowed in each minute. The rest will wait.&lt;/p&gt;

&lt;p&gt;That the rate limiter - in real life. &lt;/p&gt;




&lt;h3&gt;
  
  
  What is Rate Limiter in System Design?
&lt;/h3&gt;

&lt;p&gt;Based on example, you might understood that, rate limiting in system design is &lt;strong&gt;a technique used to control the number of requests of users or client, within a time period&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;we will check benefits of using a API rate Limiter:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Prevent resource starvation caused by Denial of Service (DoS) attack.
&lt;/h4&gt;

&lt;p&gt;Let understand with an example/scenario, imagine of login api. &lt;code&gt;POST /login&lt;/code&gt;.&lt;br&gt;
Now an attacker write a script that sends 10,000 login requests per second to your API.&lt;/p&gt;

&lt;p&gt;What will happens without rate Limiter?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;server CPU spikes.&lt;/li&gt;
&lt;li&gt;Database connections max out (because each request hits the user table).&lt;/li&gt;
&lt;li&gt;Genuine users can't log in.&lt;/li&gt;
&lt;li&gt;The login service crashes due to resource starvation (too many open threads, memory usage, DB locks).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a rate limiter prevents DoS attacks, either intentional or unintentional, by blocking the excess call. &lt;/p&gt;

&lt;h4&gt;
  
  
  2. Reduce Cost
&lt;/h4&gt;

&lt;p&gt;Limiting excess request means fewer servers and allocating more resources to high priority APIs. Rate limiting is extremely important for companies that use paid third party APIs. &lt;br&gt;
For example, company charged on per-call basis for that following external APIs. so Limiting the number of calls is essentials to reduce costs. &lt;/p&gt;

&lt;h4&gt;
  
  
  3. Prevent servers from being overload.
&lt;/h4&gt;

&lt;p&gt;To reduce server load a rate limiter is used to filter out the excess requests caused by bots or user's misbehaviour.&lt;/p&gt;




&lt;h3&gt;
  
  
  Algorithms/Strategies for Rate Limiting
&lt;/h3&gt;

&lt;p&gt;Rate limiting can be implemented in various algorithms/strategies, each of the strategies have it own pros and cons. We will overview most common algorithms/strategies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Token Bucket&lt;/strong&gt;: This strategies is widely used for rate limiting. The concept is simple, imagine a bucket that get filled with token rate over time - Let's say, one token one second.
Each token represents a persimmon to make a request. So, whenever the client/user request to service, the system first checks if there is token available on bucket.

&lt;ul&gt;
&lt;li&gt;if token available, it's consumed and the request is allowed. &lt;/li&gt;
&lt;li&gt;if bucket is empty, meaning the token is been already been used - then the request get &lt;strong&gt;denied or delayed&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6spky0ro2dhf2gipyhb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6spky0ro2dhf2gipyhb.png" alt="Token Bucket" width="601" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Leaking Bucket&lt;/strong&gt;: The Leaking Bucket is another popular strategies for rate limiting. Just like a real bucket with small hole at the bottom, it process request by "leaking" them in steady and fixed rate, regardless of how many requests come in at once.Incoming requests are added to bucket (queue), and if the bucket is full, excess requests are either dropped or delayed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzs19colll5z5n48ohhk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzs19colll5z5n48ohhk1.png" alt="Leaking Bucket" width="523" height="758"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fixed Window Counter&lt;/strong&gt;: The Fixed Window Counter strategies is basic rate limiting methods where we define the time window (suppose, 1 minute) and maximum number of request (Let's say 100). it keep the counter of each time window,and every request increases the count. If the number of requests exceeds the limit within that time window, further requests are blocked until the window resets. Once the minute is over, the counter resets back to zero, and a new time window begins.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqxj3socpwzc7hfqfqb8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqxj3socpwzc7hfqfqb8.png" alt="Fixed Window Counter" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Rate limiting is small topic but big in impact. It helps your system breathe during high traffic, stops abuse, and saves money on cloud bills.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Beginner’s Guide to the Singleton Pattern</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sat, 26 Oct 2024 17:45:19 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/beginners-guide-to-the-singleton-pattern-5904</link>
      <guid>https://dev.to/krishna-nayak/beginners-guide-to-the-singleton-pattern-5904</guid>
      <description>&lt;p&gt;The &lt;u&gt;&lt;em&gt;Singleton Pattern&lt;/em&gt;&lt;/u&gt; is one of the most foundational concepts to grasp. It is classified as a &lt;strong&gt;creational design pattern&lt;/strong&gt; and &lt;u&gt;&lt;em&gt;it is used to control the creation of objects, ensuring that only one instance of a particular class exists throughout the lifetime of an application.&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;In simpler terms, it ensures that a class is responsible for managing only &lt;u&gt;one version of itself, and it gives global access to that single instance.&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;So when/why to use Singleton Pattern, this is the one of the most common question?&lt;/p&gt;

&lt;p&gt;Before diving into the when/why using Singleton patterns, it’s important to understand the problem statements. Each design pattern is created in response to specific challenges faced by developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement: Database Connection&lt;/strong&gt;&lt;br&gt;
We want to manage a single connection to a database throughout our application. Creating multiple connections can lead to conflicts and resource exhaustion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// first instance&lt;/span&gt;
&lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="n"&gt;dataConnection1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//Second instance&lt;/span&gt;
&lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="n"&gt;dataConnection2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, creating two instances of &lt;code&gt;DataConnection&lt;/code&gt; can cause resource issues. Each instance may perform operations independently, leading to conflicts and unpredictable behavior if they attempt to access the same database resource simultaneously.&lt;br&gt;
&lt;strong&gt;To solve this problem, we need the Singleton pattern.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  When Should Do We Use Singleton Pattern?
&lt;/h3&gt;

&lt;p&gt;The Singleton Pattern is indeed a popular, especially in software design, as it's a powerful tool but can be easily misused. Let have understanding of when it’s appropriate to use the Singleton Pattern:&lt;/p&gt;
&lt;h3&gt;
  
  
  When to Use the Singleton Pattern
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When Only One Instance of a Class is Needed Globally&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
If we need a class that should have only one instance across the entire application, the Singleton Pattern is a best choice 👍 . This ensures that all parts of the application are working with the same instance, &lt;strong&gt;maintaining consistency&lt;/strong&gt; and &lt;strong&gt;preventing duplicate instances&lt;/strong&gt; that might waste memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When We Need Centralized Management of Shared Resources&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;u&gt;Singletons are beneficial when there are shared resources&lt;/u&gt;, like &lt;strong&gt;configuration settings&lt;/strong&gt;, &lt;strong&gt;caches&lt;/strong&gt;, or &lt;strong&gt;logging tools&lt;/strong&gt;, which are needed by multiple parts of the application. Centralized access and management simplify resource handling, especially for classes that need to be globally accessible and modifiable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When We Want to Control Access to a Resource&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Singletons allow controlled access to a single resource by encapsulating it within a class. This makes them helpful when only one connection or resource should be used at a time, like managing a file writer to ensure synchronized access or a shared network connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In Multithreaded or Parallel Applications&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In applications with multiple threads, a Singleton can ensure that critical sections or shared resources aren’t accessed by more than one thread at a time. A properly implemented Singleton can provide thread-safe access, reducing the likelihood of race conditions and ensuring that only one instance of a shared resource is accessed concurrently.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, we understand about problem and when to use, let see how to address this problem.&lt;/p&gt;
&lt;h3&gt;
  
  
  UML Diagram of Singleton Pattern
&lt;/h3&gt;

&lt;p&gt;The UML diagram typically includes the following components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Singleton Class&lt;/strong&gt;: This is the class that restricts instantiation. It usually has a private static instance variable to hold the single instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constructor&lt;/strong&gt;: The constructor is private to prevent direct instantiation from outside the class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Public Static Method&lt;/strong&gt;: This method provides access to the instance. If the instance does not exist, it creates one; otherwise, it returns the existing instance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s a simple representation of the UML diagram for the Singleton pattern:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbao7q13vpykoeie3ssiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbao7q13vpykoeie3ssiw.png" alt="Singleton description" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt;: Private access modifier (indicating that the instance variable and constructor are not accessible outside the class).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt;: Public access modifier (indicating that the getConnection() method can be accessed publicly).&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Java Implementation
&lt;/h4&gt;

&lt;p&gt;Here’s an example of Singleton pattern implemented in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

      &lt;span class="cm"&gt;/*
       * This private keyword in constructor 
       * is to prevent direct instantiation from
       * outside the class.
       */&lt;/span&gt;
      &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;DataConnetion&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

      &lt;span class="cm"&gt;/*
       * static method so that we can call 
       * this method outside the class without
       * instantiation object
       */&lt;/span&gt;
      &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="nf"&gt;getConnection&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                  &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;showMessage&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Database is connected"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we want to get the instance of Database Connection, we call its static methods like this: &lt;code&gt;DataConnection dataConnection = DataConnection.getConnection();&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/*
             * We can't directly initialize DataConnection
             * due to its private constructor.
             *
             * DataConnection dataConnection = new DataConnection();
             *
             * This would cause an error.
             *
             */&lt;/span&gt;

            &lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="n"&gt;dataConnection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Example output: 112810359&lt;/span&gt;

            &lt;span class="nc"&gt;DataConnection&lt;/span&gt; &lt;span class="n"&gt;dataConnection1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DataConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataConnection1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Example output: 112810359&lt;/span&gt;

            &lt;span class="c1"&gt;// Both variables, dataConnection and dataConnection1, refer to the same instance in memory.&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The Singleton Pattern is a foundational design pattern that ensures only one instance of a particular class exists across an application, By restricting the instantiation process and providing global access to a single instance, the Singleton pattern helps manage shared resources efficiently, such as database connections, configuration settings, and loggers. This pattern is especially useful in scenarios where consistency and controlled access to resources are crucial.&lt;/p&gt;

&lt;p&gt;For more information and best practices on implementing the Singleton Pattern effectively, refer to &lt;a href="https://www.digitalocean.com/community/tutorials/java-singleton-design-pattern-best-practices-examples" rel="noopener noreferrer"&gt;this guide on DigitalOcean&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>learning</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding UML Class Diagrams: A Beginner’s Guide</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Fri, 18 Oct 2024 20:30:24 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/understanding-uml-class-diagrams-a-beginners-guide-2omj</link>
      <guid>https://dev.to/krishna-nayak/understanding-uml-class-diagrams-a-beginners-guide-2omj</guid>
      <description>&lt;p&gt;Designing systems clearly and accurately is crucial, and one of the most commonly used tools for this purpose is UML (Unified Modeling Language) diagrams.&lt;br&gt;
Among various types of UML diagrams, &lt;strong&gt;Class Diagrams&lt;/strong&gt; are essential for visualizing the structure of a system.&lt;br&gt;
So our first question should be what is UML Class Diagram?&lt;/p&gt;


&lt;h3&gt;
  
  
  UML Class Diagram
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;UML Class Diagram&lt;/strong&gt; is, basically a blueprint that describes the structure of a system by showing its classes, attributes, operations (or methods), and relationships among objects.&lt;br&gt;
In simpler terms, it a visual representation of how objects and classes within a system interact with each other.&lt;/p&gt;

&lt;p&gt;Now that we have an understanding of what a UML Class Diagram is, the next step is to know about &lt;strong&gt;Key Elements of a Class Diagram&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Key Elements of a Class Diagram
&lt;/h2&gt;

&lt;p&gt;The Key Elements of class diagram include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class&lt;/li&gt;
&lt;li&gt;Attributes&lt;/li&gt;
&lt;li&gt;Methods/Operations&lt;/li&gt;
&lt;li&gt;Relationships&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Class &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The Class is the most fundamental element in a class diagram. It represents a blueprint for objects. Each class has three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class Name&lt;/strong&gt;: The name of the class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attributes&lt;/strong&gt;: The data stored in the class, often variables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt; (Methods): Functions or actions that the class can perform.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgg8y8skpimut9zwf5w8f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgg8y8skpimut9zwf5w8f.jpg" alt="Class description" width="800" height="675"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Attributes &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The properties or data of a class, typically shown with their visibility (public, private, etc.).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;visibility&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;name:&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;multiplicity&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;defaultValue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: The name of the attribute.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;type&lt;/code&gt;: The data type of the attribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Visibility&lt;/strong&gt; defines the access level of attributes and methods within a class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Public (+)&lt;/code&gt;: The attributes and methods accessible from outside the class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Private (-)&lt;/code&gt;: The attributes and methods accessible only within the class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Protected (#)&lt;/code&gt;: The attributes and methods accessible within the class and its subclasses.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Package (~)&lt;/code&gt;: The attribute or method is accessible within the same package.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Multiplicity&lt;/strong&gt; define, how many objects can participate in a relationship, such as "one-to-many" or "one-to-one."&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1..*&lt;/code&gt; means one or more objects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0..1&lt;/code&gt; means zero or one object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; Exactly one instance&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0..1&lt;/code&gt; Zero or one instance &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1..*&lt;/code&gt; One or more instances &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0..*&lt;/code&gt;: Zero or more instances &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;default Value&lt;/strong&gt;: (Optional) The initial value of the attribute.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5xdsp5rmldz1z3qtgzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5xdsp5rmldz1z3qtgzr.png" alt="Attributes description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Methods/Operations &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Methods (or Operations) define the behavior of a class. They are the actions that the class can perform. In simple terms, it is the functions of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;visibility&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameterList&lt;/span&gt;&lt;span class="o"&gt;):&lt;/span&gt; &lt;span class="n"&gt;returnType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: The name of the method/operation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;parameterList&lt;/code&gt;: A comma-separated list of parameters, each specified as name: type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;returnType&lt;/code&gt;: The data type returned by the method.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Relationships in UML Class Diagrams  &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Relationships show how classes are connected and interact with one another within a system.&lt;/p&gt;

&lt;p&gt;There are six main types of relationships between classes: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Association &lt;/li&gt;
&lt;li&gt;Aggregation &lt;/li&gt;
&lt;li&gt;Composition &lt;/li&gt;
&lt;li&gt;Inheritance &lt;/li&gt;
&lt;li&gt;Implementation &lt;/li&gt;
&lt;li&gt;Dependency.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm7jicrmmibmu1gpcxfo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm7jicrmmibmu1gpcxfo.png" alt="Relationships in UML Class description" width="800" height="799"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Association
&lt;/h4&gt;

&lt;p&gt;Association represents a general relationship between two classes where objects of one class interact with objects of another. It’s just like saying, "these two things are connected in some way."&lt;/p&gt;

&lt;h4&gt;
  
  
  Aggregation
&lt;/h4&gt;

&lt;p&gt;Aggregation is a special type of association that represents a "whole-part" relationship. This is used when one class (the whole) is made up of other classes (the parts) that can exist &lt;em&gt;independently&lt;/em&gt;. In other words, the parts (other classes) can exist without the whole (that one class), so removing the whole doesn’t destroy the parts.&lt;/p&gt;

&lt;h4&gt;
  
  
  Composition
&lt;/h4&gt;

&lt;p&gt;Composition is a stronger form of aggregation and represents a "whole-part" relationship as well. However, in composition, the parts cannot exist without the whole. This means that if the whole is deleted, the parts are also deleted because they depend on the whole for their existence.&lt;/p&gt;

&lt;h4&gt;
  
  
  Inheritance (Generalization)
&lt;/h4&gt;

&lt;p&gt;Inheritance, also known as &lt;strong&gt;Generalization&lt;/strong&gt;, like a “parent-child” relationship between two classes. In this relationship, one class (the child or subclass) inherits attributes and behaviors from another class (the parent or superclass). This helps reuse code and avoid duplication, as the child class automatically gets the characteristics of the parent class.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementation
&lt;/h4&gt;

&lt;p&gt;Implementation is a relationship between a class and an interface. An interface defines abstract methods that a class must implement, but it doesn’t provide the actual code. When a class &lt;em&gt;implements&lt;/em&gt; an interface, it agrees to provide specific functionality defined by that interface.&lt;/p&gt;

&lt;h4&gt;
  
  
  Dependency
&lt;/h4&gt;

&lt;p&gt;Dependency is a relationship where one class relies on another class to function. It’s often temporary, meaning the dependency exists only when the operation is happening and doesn’t form a permanent relationship between classes.&lt;/p&gt;

&lt;h5&gt;
  
  
  Summary Table for Relationship in UML Class Diagrams:
&lt;/h5&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Relationship&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Notation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Association&lt;/td&gt;
&lt;td&gt;General connection between classes, without ownership.&lt;/td&gt;
&lt;td&gt;Solid line&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Aggregation&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;&lt;sup&gt;#&lt;/sup&gt;Whole-part relationship&lt;/strong&gt; where parts can exist independently of the whole.&lt;/td&gt;
&lt;td&gt;Dashed line with an open diamond on the “whole” side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composition&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;&lt;sup&gt;#&lt;/sup&gt;Whole-part relationship&lt;/strong&gt; where parts cannot exist independently of the whole.&lt;/td&gt;
&lt;td&gt;Solid line with a filled diamond on the “whole” side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inheritance&lt;/td&gt;
&lt;td&gt;Parent-child relationship where the child inherits properties and behaviors from parent.&lt;/td&gt;
&lt;td&gt;Solid line with an filled arrow to the parent class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementation&lt;/td&gt;
&lt;td&gt;Class-interface relationship, where the class agrees to fulfill the interface's actions.&lt;/td&gt;
&lt;td&gt;Dashed line with a filled arrow to the interface&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dependency&lt;/td&gt;
&lt;td&gt;Temporary relationship where one class depends on another for a specific action.&lt;/td&gt;
&lt;td&gt;Dashed line with an arrow from the dependent class&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;sup&gt;#&lt;/sup&gt;  &lt;u&gt;&lt;strong&gt;Whole&lt;/strong&gt; refer to one class&lt;/u&gt; and &lt;u&gt;&lt;strong&gt;Part&lt;/strong&gt; refer to the made up of other class/classes&lt;/u&gt;. For example, A &lt;code&gt;House class&lt;/code&gt; contains a &lt;code&gt;Room class&lt;/code&gt; in a composition relationship — if the House is demolished, the Room is also demolished.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;UML Class Diagrams also support some special elements like &lt;code&gt;Enums&lt;/code&gt;, &lt;code&gt;Interfaces&lt;/code&gt;, and &lt;code&gt;Abstract Classes&lt;/code&gt;. These are denoted as show in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlebyymdjgiwhtl5m1f4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlebyymdjgiwhtl5m1f4.png" alt="special elements" width="800" height="550"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Combined Example [library system UML class]
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frq3vbb1mqo0ueiq87hw3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frq3vbb1mqo0ueiq87hw3.png" alt="library system UML class description" width="800" height="686"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Element&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Relationships&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;User&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Generalization: &lt;strong&gt;Member&lt;/strong&gt; and &lt;strong&gt;Librarian&lt;/strong&gt; are specialized versions of &lt;strong&gt;User&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Member&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Implements &lt;strong&gt;Borrowable&lt;/strong&gt; interface.&lt;br&gt; - Aggregation with &lt;strong&gt;Book&lt;/strong&gt; (A member can borrow multiple books).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Librarian&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Association with &lt;strong&gt;Library&lt;/strong&gt; (A librarian manages one library).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Book&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Aggregation with &lt;strong&gt;Genre&lt;/strong&gt; (Each book belongs to a specific genre).&lt;br&gt; - Association with &lt;strong&gt;Author&lt;/strong&gt; (Each book has one or more authors).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Author&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Association with &lt;strong&gt;Book&lt;/strong&gt; (An author writes one or more books).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Library&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Composition with &lt;strong&gt;Catalog&lt;/strong&gt; (A library contains a catalog).&lt;br&gt; - Aggregation with &lt;strong&gt;Book&lt;/strong&gt; (A library contains many books).&lt;br&gt; - Aggregation with &lt;strong&gt;Member&lt;/strong&gt; (A library can have multiple members).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Catalog&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;- Composition with &lt;strong&gt;Library&lt;/strong&gt; (The catalog is part of the library and can't exist without it).&lt;br&gt; - Aggregation with &lt;strong&gt;Book&lt;/strong&gt; (The catalog manages a collection of books).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Borrowable&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Interface&lt;/td&gt;
&lt;td&gt;- Implemented by &lt;strong&gt;Member&lt;/strong&gt; (A member can borrow books).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Genre&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Enumeration&lt;/td&gt;
&lt;td&gt;- Aggregation with &lt;strong&gt;Book&lt;/strong&gt; (Each book has a genre).&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As become more familiar with UML Class Diagrams, we can gain a toolset to design more complex systems and communicate these designs effectively with other developers. With the user UML, &lt;strong&gt;we can create a blueprint that is clear and organized, making it easier to adapt in the future and supporting software that can grow and be maintained over time.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>uml</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Advanced Query Techniques in Spring Data JPA</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sat, 05 Oct 2024 00:30:00 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/advanced-query-techniques-in-spring-data-jpa-2j27</link>
      <guid>https://dev.to/krishna-nayak/advanced-query-techniques-in-spring-data-jpa-2j27</guid>
      <description>&lt;p&gt;We have explored the basics of Spring Data JPA and how method naming conventions make querying simple. If not, I highly recommend you to follow that blog first. In this second part, we'll dive deeper into more advanced query techniques, allowing you to leverage powerful combinations, sorting, pagination, and string-specific operations for better control over your data retrieval.&lt;/p&gt;

&lt;p&gt;We are taking the same example as of Book Entity, having the attribute of the &lt;code&gt;bookName&lt;/code&gt;, &lt;code&gt;authorName&lt;/code&gt;, &lt;code&gt;isbn&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;, &lt;code&gt;publishedDate&lt;/code&gt;, &lt;code&gt;published&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Freanv25nlnf5zqn7hm0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Freanv25nlnf5zqn7hm0v.png" alt="Book Entity description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Combining Conditions with Keywords
&lt;/h3&gt;

&lt;p&gt;Spring Data JPA allows combining multiple conditions using the &lt;code&gt;And&lt;/code&gt; and &lt;code&gt;Or&lt;/code&gt; keywords to create more complex queries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;And&lt;/code&gt;: Combines conditions using the &lt;code&gt;AND&lt;/code&gt; operator.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameAndAuthorName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This above method will execute a query that retrieves a book from the database where both the &lt;code&gt;bookName&lt;/code&gt; and &lt;code&gt;authorName&lt;/code&gt; match the values provided in the method's parameters.&lt;/p&gt;

&lt;p&gt;If we search for the book title, i.e, &lt;code&gt;bookName&lt;/code&gt;, "The Great Book" and the author is "A. qwerty". If such a book exists, it will return that record. Otherwise, it will return &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The equivalent SQL query that the method would execute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'The Great Book'&lt;/span&gt; 
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'A. qwerty'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;Or&lt;/code&gt;: Combines conditions using the &lt;code&gt;OR&lt;/code&gt; operator.&lt;/em&gt;
The &lt;code&gt;OR&lt;/code&gt; operator in method names allows us to retrieve results where at least one of the conditions is met.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameOrAuthorName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method returns a list of Book objects where either the bookName or authorName matches the specified parameters. It does not require both conditions to be true. If a book matches either the name or the author, it will be included in the result list.&lt;/p&gt;

&lt;p&gt;The corresponding SQL query for this method would look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'The Great Book'&lt;/span&gt;
&lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Albert Hero'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query will return all books where either the title is "The Great Gatsby" or the author is "George Orwell". If a book matches either condition, it will be part of the result set.&lt;/p&gt;




&lt;h3&gt;
  
  
  Sorting and Pagination
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Sorting&lt;/code&gt;: This allows us to control the order in which records are returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;OrderBy&lt;/code&gt;: We can use &lt;code&gt;OrderBy&lt;/code&gt; in our method name to specify sorting. For instance, if we want to get books sorted by their name in ascending order (A to Z), we can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByAuthorNameOrderByBookNameAsc&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what each part means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;findByAuthorName&lt;/code&gt;: This retrieves books based on the author’s name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OrderByBookNameAsc&lt;/code&gt;: This sorts the books by their name in ascending order (A to Z).
If we want to sort in descending order (Z to A), we can replace &lt;code&gt;Asc&lt;/code&gt; with &lt;code&gt;Desc&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Pagination&lt;/code&gt;: This helps us to break down large sets of data into smaller chunks or "pages" to load more efficiently. This is useful when dealing with long lists of records, such as &lt;code&gt;products&lt;/code&gt;, &lt;code&gt;search results&lt;/code&gt;, or &lt;code&gt;blog posts&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pageable&lt;/strong&gt;: Spring JPA provides a Pageable interface that allows us to retrieve a specific page of data with a defined number of records. It supports both pagination and sorting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByAuthorName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Pageable&lt;/span&gt; &lt;span class="n"&gt;pageable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method will return a page of books, filtered by the author’s name.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to Use Pageable:
&lt;/h4&gt;

&lt;p&gt;To paginate and sort data, we are passing a Pageable object when calling the method. For instance, if we want to get the second page with 10 books per page, sorted by book name in ascending order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Pageable&lt;/span&gt; &lt;span class="n"&gt;pageable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PageRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Sort&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;by&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bookName"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;ascending&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bookRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByAuthorName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Lost Hero"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pageable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PageRequest.of(1, 10, Sort.by("The Lost Hero").ascending())&lt;/code&gt;: This creates a pageable object that requests the second page (page index starts from 0) with 10 books per page, sorted by the bookName in ascending order.&lt;/p&gt;




&lt;h3&gt;
  
  
  Comparison Keywords
&lt;/h3&gt;

&lt;p&gt;These are used to compare numeric values, dates, or other comparable data types. They help us filter data based on specific comparison criteria.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;IsBefore&lt;/code&gt; / &lt;code&gt;IsAfter&lt;/code&gt;: These are used for comparing date or time fields.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByPublishedDateAfter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LocalDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;publishedDate&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'2024-01-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;LessThan&lt;/code&gt; / &lt;code&gt;GreaterThan&lt;/code&gt; / &lt;code&gt;Between&lt;/code&gt;:These are used for numeric comparisons.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByPriceBetween&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;startPrice&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;endPrice&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  String-Specific Operations
&lt;/h3&gt;

&lt;p&gt;These operations are used to perform pattern matching and comparisons on string fields, similar to SQL's &lt;code&gt;LIKE&lt;/code&gt; clause.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Containing&lt;/strong&gt;: Used to perform a &lt;code&gt;LIKE %hunted%&lt;/code&gt; operation, i.e., it look for partial matches&lt;/em&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameContaining&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;keyword&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like, if the &lt;code&gt;keyword&lt;/code&gt; value is &lt;code&gt;hunted&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%hunted%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;StartingWith&lt;/strong&gt;: This is used to check if a string field starts with a specific prefix.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameStartingWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like, if the &lt;code&gt;prefix&lt;/code&gt; value is &lt;code&gt;The Lost&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'The Lost%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;EndingWith&lt;/strong&gt;: This is used to check if a string field ends with a specific suffix.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameEndingWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like, if the &lt;code&gt;suffix&lt;/code&gt; value is &lt;code&gt;Hero&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Hero'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Boolean Fields
&lt;/h3&gt;

&lt;p&gt;Spring JPA provides simple keywords for querying boolean fields directly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;IsTrue&lt;/code&gt; / &lt;code&gt;IsFalse&lt;/code&gt;: This is used to query boolean fields where the value is either true or false.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByPublishedIsTrue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByPublishedIsFalse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;TRUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  In and NotIn
&lt;/h3&gt;

&lt;p&gt;These keywords are used to check if a field matches any value in a collection or does not match any value in a collection.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;In&lt;/code&gt;: This is used to find entities where the value of a field is included in a collection of values.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameIn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like, if the list of the String names are these, &lt;code&gt;'The Lost Hero'&lt;/code&gt;, &lt;code&gt;'Alice in Wonderland'&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'The Lost Hero'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alice in Wonderland'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;NotIn&lt;/code&gt;: This is used to find entities where the value of a field is not included in a collection of values. Just an opposite of the &lt;code&gt;In&lt;/code&gt; operation.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByBookNameNotIn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding SQL query for this method would look like, if the list of the String names are these, &lt;code&gt;'The Lost Hero'&lt;/code&gt;, &lt;code&gt;'Alice in Wonderland'&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'The Lost Hero'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alice in Wonderland'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Top and First
&lt;/h3&gt;

&lt;p&gt;These keywords are used to limit the number of records returned. We can retrieve the Top N or First N results, typically in combination with sorting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Top&lt;/code&gt;:This is used to retrieve the top N records from a result set.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findTop3ByOrderByPublishedDateDesc&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;publishedDate&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;First&lt;/code&gt;: This is used to retrieve the first N records from a result set, often sorted by a specific field.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findFirst5ByOrderByBookNameAsc&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets take an example of this method &lt;code&gt;findFirst5ByOrderByBookNameAsc()&lt;/code&gt; and break it down into different parts to make it easier to understand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;find&lt;/code&gt;: This is the core part of the method, which tells Spring JPA that we want to retrieve data from the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;First5&lt;/code&gt; / &lt;code&gt;First&amp;lt;Number&amp;gt;&lt;/code&gt;: This part of the method specifies that we want to get the first 5 records. We can replace the 5 with any number depending upon the choices or requirement. This is useful when we don’t need all the results, just a small number.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;By&lt;/code&gt;: This keyword help to connects the "find" action with the field we want to search by. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;OrderByBookName&lt;/code&gt;: This tell Spring JPA that we want to sort the results based on the bookName field. So, it will organize the books alphabetically (by their names) before choosing the first 5.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Asc&lt;/code&gt;: This stands for "ascending" order, which means sorting from A to Z (alphabetically). Suppose we wanted to sort from Z to A, we would use &lt;code&gt;Desc&lt;/code&gt; (for "descending" order) instead.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Summary, this method will find books in the database, sort them by their names in ascending order (A to Z), and return the first 5 records that meet this sorting criteria.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this second part, we explored how Spring Data JPA allows us to create more advanced and flexible queries through method naming conventions. Combining conditions, pagination, sorting, comparison operators, and other powerful features provide the ability to construct complex queries without writing SQL. These conventions follow the "&lt;strong&gt;&lt;em&gt;convention over configuration&lt;/em&gt;&lt;/strong&gt;" principle, saving our time while offering immense flexibility.&lt;/p&gt;

&lt;p&gt;When working with strings, numbers, dates, booleans, or collections, Spring Data JPA makes querying our database simpler and more intuitive. &lt;em&gt;When our queries become too complex for method conventions, we can always fall back on custom queries using the &lt;code&gt;@Query&lt;/code&gt; annotation&lt;/em&gt;. But for most use cases, these conventions are all we need to handle queries efficiently.&lt;/p&gt;

</description>
      <category>java</category>
      <category>database</category>
      <category>springboot</category>
      <category>developer</category>
    </item>
    <item>
      <title>Spring Data JPA Method Naming Conventions: Build Queries Without Writing SQL</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Sun, 29 Sep 2024 03:38:57 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/spring-data-jpa-method-naming-conventions-build-queries-without-writing-sql-23o5</link>
      <guid>https://dev.to/krishna-nayak/spring-data-jpa-method-naming-conventions-build-queries-without-writing-sql-23o5</guid>
      <description>&lt;p&gt;Spring Data JPA has become one of the most popular frameworks for simplifying database access. It offers out-of-the-box solutions that allow developers to query databases with minimal effort-without writing complex SQL. One of the key features of Spring Data JPA is its method naming conventions, which allow developers to define repository methods that automatically generate queries based on the method signature.&lt;br&gt;
We will explore the powerful naming conventions Spring Data JPA provides, and how they can be used to create simple and complex queries effortlessly.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Does It Work?
&lt;/h2&gt;

&lt;p&gt;When we define a method of &lt;strong&gt;&lt;code&gt;findByBookName(String name)&lt;/code&gt;&lt;/strong&gt; in a Spring Data. JPA repository, we’re not writing the actual SQL query. Instead Spring Data JPA uses the method name to automatically infer the query. &lt;em&gt;It follows a set of well-defined conventions to generate queries at runtime&lt;/em&gt;, which means we don’t need to write manual code for basic operations.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic Query Method Keywords
&lt;/h2&gt;

&lt;p&gt;Spring Data JPA has several method prefixes that form the foundation for query generation. Some of the most commonly used are:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbmzewvf0wah0uj1b4c4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbmzewvf0wah0uj1b4c4f.png" alt="Query Method Keyword description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;findBy&lt;/code&gt;, &lt;code&gt;queryBy&lt;/code&gt;, and &lt;code&gt;readBy&lt;/code&gt; are the functionally identical in Spring Data JPA. They all retrieve entities based on a field and return &lt;code&gt;null&lt;/code&gt; if no match is found. The difference lies in naming preference—developers can choose between them for better code readability or clarity, but their act of behavior remains the same.&lt;/p&gt;

&lt;p&gt;Let suppose there is a &lt;strong&gt;Book&lt;/strong&gt; entity, having the attribute of the &lt;code&gt;bookName&lt;/code&gt;, &lt;code&gt;authorName&lt;/code&gt;, &lt;code&gt;isbn&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;, &lt;code&gt;publishedDate&lt;/code&gt;, &lt;code&gt;published&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Freanv25nlnf5zqn7hm0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Freanv25nlnf5zqn7hm0v.png" alt="Book Entity description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;BookRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="nf"&gt;findByBookName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;bookName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring Data JPA takes care of the implementation under the hood, creating the necessary SQL query. If we need more complex queries, we can still define custom queries using the &lt;code&gt;@Query&lt;/code&gt; annotation or a custom repository.&lt;/p&gt;

&lt;p&gt;In this, Spring uses the field name &lt;code&gt;bookName&lt;/code&gt; to match it to a property in our Book entity. This means the method &lt;strong&gt;&lt;code&gt;findByBookName&lt;/code&gt;&lt;/strong&gt; translates into a query like &lt;code&gt;SELECT b FROM Book b WHERE b.bookName = :name&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By simply defining methods in a repository interface, we can effortlessly generate complex queries based on the method names and field attributes. Understanding the nuances of keywords like &lt;code&gt;findBy&lt;/code&gt;, &lt;code&gt;getBy&lt;/code&gt;, &lt;code&gt;readBy&lt;/code&gt;, and others can help us to create readable, maintainable, and efficient data access layers in our application. With this minimal effort, Spring Data JPA simplifies the process of interacting with databases, reducing boilerplate code, and improving development speed.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>springboot</category>
      <category>java</category>
      <category>learning</category>
    </item>
    <item>
      <title>CAP Theorem</title>
      <dc:creator>Krishna Nayak</dc:creator>
      <pubDate>Tue, 02 May 2023 08:57:18 +0000</pubDate>
      <link>https://dev.to/krishna-nayak/cap-theorem-52h7</link>
      <guid>https://dev.to/krishna-nayak/cap-theorem-52h7</guid>
      <description>&lt;p&gt;Let have brief understanding about, What is a CAP Theorem? The CAP theorem is a &lt;strong&gt;concept in computer science that describes the trade-offs that exist in building &lt;a href="https://www.geeksforgeeks.org/what-is-a-distributed-system/" rel="noopener noreferrer"&gt;distributed systems&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The CAP Theorem revolve around the three concept which is from the CAP acronym, i.e. &lt;strong&gt;Consistency&lt;/strong&gt;, &lt;strong&gt;Availability&lt;/strong&gt; and &lt;strong&gt;Partition Tolerance.&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Consistency
&lt;/h3&gt;

&lt;p&gt;When a request is made. The system node always returns up-to-date information. This ensures that the information provided is accurate and reliable whenever a request is made. This makes the system trustworthy and reduces error risks. As a result, users are always provided with the latest version of the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Availability
&lt;/h3&gt;

&lt;p&gt;The system always returns information, regardless of whether it is stable or not. It ensures that no matter what the circumstances are, users always have access to their data. Even though it provides inaccurate data, it is available to the public. But if it cannot handle any query it is unavailable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partition Tolerance
&lt;/h3&gt;

&lt;p&gt;The system can continue operating during network partition. This means that the system can keep functioning even if there is a disruption in the network. For example, a server is down or a link may be down. This ensures that the system can continue to serve its users during a network partition.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is CAP Theorem?
&lt;/h2&gt;

&lt;p&gt;Now, We have basic idea about the CAP acronym. Let comeback to the CAP Theorem.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The CAP theorem states that you have &lt;strong&gt;only two properties in a distributed system but not all three.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, it is not possible to achieve consistency, availability, and partition tolerance in a distributed system simultaneously. However, it is possible to prioritise two of these properties while sacrificing the third.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfokalu34ikaj7rtz5j3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfokalu34ikaj7rtz5j3.png" alt="CAP Theorem" width="800" height="722"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Consistency and availability
&lt;/h3&gt;

&lt;p&gt;It is possible for a system to ensure consistency and availability, but no partition tolerance is possible. In other words, if the network has no partition, we can guarantee consistency and availability. This generally makes sense because when a network partition isn't happening the system is running normally and thus can have both consistency and availability because there's no need to make trade-offs. This means it can always respond to requests with the latest information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consistency and partition tolerances
&lt;/h3&gt;

&lt;p&gt;Achieving both consistency and partition tolerance can be challenging, as these properties can sometimes be at odds with each other. Consistency requires that all nodes in a distributed system see the same data at the same time. Partition tolerance allows the system to remain operational in the event that some nodes fail or become inaccessible. Balancing the two is key to ensuring successful distributed operations. To maintain this balance, distributed systems must have rules in place to prevent data inconsistency and maintain availability in the event of a failure. Additionally, they must have mechanisms in place to ensure nodes stay in sync.&lt;/p&gt;

&lt;h2&gt;
  
  
  Availability and partition tolerances
&lt;/h2&gt;

&lt;p&gt;Availability refers to the percentage of time a system is operational. Partition tolerance refers to the ability of a system to continue functioning despite the failure of one or more of its components. High availability and partition tolerance are important characteristics of distributed systems. Together, they enable systems to remain available even in the face of component or network failure. This ensures that distributed systems are robust and reliable, and can continue to function even in the event of an outage or disruption. This makes distributed systems more resilient and better able to handle large workloads.&lt;/p&gt;




&lt;p&gt;The CAP theorem states that it is impossible for a distributed system to simultaneously guarantee all three of the following characteristics: Consistency, Availability, and Partition tolerance. This theorem implies that a distributed system can only achieve two of the three characteristics, as one must be sacrificed in order to meet the other two. This has important implications for the design of distributed systems, as developers must choose the two characteristics they wish to prioritise.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
