<?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: Monesh B</title>
    <description>The latest articles on DEV Community by Monesh B (@monesh_b_24cd798d8de84819).</description>
    <link>https://dev.to/monesh_b_24cd798d8de84819</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%2F1556088%2Fd1ea2044-43b5-4105-a5fd-c5e0dc6836b2.jpg</url>
      <title>DEV Community: Monesh B</title>
      <link>https://dev.to/monesh_b_24cd798d8de84819</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/monesh_b_24cd798d8de84819"/>
    <language>en</language>
    <item>
      <title>Mastering Rate Limiting in Java Spring Boot with Bucket4j</title>
      <dc:creator>Monesh B</dc:creator>
      <pubDate>Sun, 22 Dec 2024 15:58:36 +0000</pubDate>
      <link>https://dev.to/monesh_b_24cd798d8de84819/mastering-rate-limiting-in-java-spring-boot-with-bucket4j-1ai8</link>
      <guid>https://dev.to/monesh_b_24cd798d8de84819/mastering-rate-limiting-in-java-spring-boot-with-bucket4j-1ai8</guid>
      <description>&lt;p&gt;In today's digital landscape, APIs are the backbone of many applications, enabling seamless communication between different services. However, with great power comes great responsibility. As your application scales, you may encounter issues like server overload, abuse, or even denial-of-service attacks. This is where rate limiting comes into play. In this blog, we will explore how to implement rate limiting in a Java Spring Boot application using the Bucket4j library, making it both effective and easy to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Rate Limiting?&lt;/strong&gt;&lt;br&gt;
Rate limiting is a technique used to control the amount of incoming requests to a server within a specified time frame. It helps to ensure that your application remains responsive and available, even under heavy load. By limiting the number of requests a user can make, you can prevent abuse and protect your resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-World Example&lt;/strong&gt;&lt;br&gt;
Imagine you have an e-commerce application where users can view customer information. If a malicious user tries to scrape customer data by sending thousands of requests per second, it could overwhelm your server, leading to downtime or degraded performance. Rate limiting can help mitigate this risk by restricting the number of requests a user can make in a given time period.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use Bucket4j?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bucket4j&lt;/strong&gt; is a lightweight and efficient Java library for token-bucket-based rate limiting. It offers features like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Easy integration with Java applications.&lt;/li&gt;
&lt;li&gt;Thread safety and high performance.&lt;/li&gt;
&lt;li&gt;Customizable rate limiting rules.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Bucket4j in Spring Boot&lt;/strong&gt;&lt;br&gt;
Let's dive into the code to see how we can implement rate limiting using Bucket4j in a Spring Boot application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Add Dependency&lt;/strong&gt;&lt;br&gt;
First, you need to add the Bucket4j dependency to your pom.xml:&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%2F94ffx2740mbctvapkfcx.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%2F94ffx2740mbctvapkfcx.png" alt="Image description" width="507" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create the Rate Limiter Service&lt;/strong&gt;&lt;br&gt;
Next, we will create a service that will handle the rate limiting logic. Here’s how you can implement it:&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%2Fmdfzczn404utuakfj0yy.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%2Fmdfzczn404utuakfj0yy.png" alt="Image description" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, we define a rate limit of 5 requests per minute. The tryConsume(1) method attempts to consume one token (request) from the bucket. If successful, it means the request is allowed; otherwise, it indicates that the rate limit has been exceeded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Implement the Controller&lt;/strong&gt;&lt;br&gt;
Now, let’s implement the controller that will use the RateLimiterService to enforce rate limiting on the /customers/view endpoint:&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%2Fyvbka9y6m3y5yawlsky0.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%2Fyvbka9y6m3y5yawlsky0.png" alt="Image description" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Before processing a request, the controller checks if a token is available in the bucket.&lt;/li&gt;
&lt;li&gt;If no token is available, a &lt;strong&gt;429 Too Many Requests&lt;/strong&gt; response is sent.&lt;/li&gt;
&lt;li&gt;If a token is consumed successfully, the request is processed as usual.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Testing and Debugging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To ensure your rate limiter works as expected:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use tool like Postman to send multiple requests.&lt;/li&gt;
&lt;li&gt;Monitor responses to verify the 429 Too Many Requests status code.&lt;/li&gt;
&lt;/ol&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%2Fckgk3gxhx92bswqzeki8.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%2Fckgk3gxhx92bswqzeki8.png" alt="Image description" width="800" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rate limiting is a critical feature for modern applications. With Bucket4j, you can effortlessly add this functionality to your Spring Boot project. By following the example above, you’ll not only protect your backend but also improve your application’s reliability and user experience.&lt;/p&gt;

&lt;p&gt;Start implementing rate limiting today and make your application robust and scalable!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>java</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
    <item>
      <title>Simplifying Java Development with Lombok in Spring Boot</title>
      <dc:creator>Monesh B</dc:creator>
      <pubDate>Sun, 15 Dec 2024 05:13:32 +0000</pubDate>
      <link>https://dev.to/monesh_b_24cd798d8de84819/simplifying-java-development-with-lombok-in-spring-boot-3g0k</link>
      <guid>https://dev.to/monesh_b_24cd798d8de84819/simplifying-java-development-with-lombok-in-spring-boot-3g0k</guid>
      <description>&lt;p&gt;Java developers often find themselves writing repetitive code, especially when it comes to creating getters and setters for class properties. This boilerplate code can clutter your classes and make them harder to read and maintain. Fortunately, the Lombok library comes to the rescue! In this blog post, we’ll explore how Lombok can streamline your Spring Boot applications by reducing boilerplate code, making your code cleaner and more efficient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Lombok?&lt;/strong&gt;&lt;br&gt;
Lombok is a Java library that helps developers reduce boilerplate code by providing annotations that automatically generate common methods like getters, setters, equals, hashCode, and toString. By using Lombok, you can focus on the business logic of your application rather than getting bogged down by repetitive code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why To Use Lombok?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Less Boilerplate: Lombok eliminates the need for writing repetitive code, allowing you to focus on what really matters.&lt;/li&gt;
&lt;li&gt;Improved Readability: With less clutter in your classes, your code becomes easier to read and maintain.&lt;/li&gt;
&lt;li&gt;Faster Development: By reducing the amount of code you need to write, you can speed up your development process.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Getting Started with Lombok in Spring Boot&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1: Add Lombok Dependency&lt;/strong&gt;&lt;br&gt;
To use Lombok in your Spring Boot project, you need to add the Lombok dependency to your pom.xml file if you are using Maven:&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%2F6d44agswh30vtfyjzdpp.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%2F6d44agswh30vtfyjzdpp.png" alt="Image description" width="421" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Use Lombok Annotations&lt;/strong&gt;&lt;br&gt;
Now that you have Lombok set up, let’s see how to use its annotations in a Spring Boot application.&lt;/p&gt;

&lt;p&gt;Example: Creating a User Entity&lt;br&gt;
Let’s create a simple User  entity class that represents a user in our application.&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%2Fa2lsjmobwcdj91c22wqb.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%2Fa2lsjmobwcdj91c22wqb.png" alt="Image description" width="745" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s Happening Here?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/data"&gt;@data&lt;/a&gt;: This single annotation generates all the boilerplate code for getters, setters, toString, equals, and hashCode methods. You no longer need to write these methods manually.&lt;/li&gt;
&lt;li&gt;@NoArgsConstructor: This annotation generates a no-argument constructor, which is often required by JPA.&lt;/li&gt;
&lt;li&gt;@AllArgsConstructor: This generates a constructor that takes all fields as parameters, making it easy to create instances of the User  class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example: Using the User Entity in a Service&lt;/strong&gt;&lt;br&gt;
Now, let’s create a simple service to manage users.&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%2Fsg6w6st5kkks5xh0x93g.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%2Fsg6w6st5kkks5xh0x93g.png" alt="Image description" width="688" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Creating a User Controller&lt;/strong&gt;&lt;br&gt;
Finally, let’s create a REST controller to expose our user management functionality.&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%2Fyzg79lljusdl3gy9ppwl.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%2Fyzg79lljusdl3gy9ppwl.png" alt="Image description" width="773" height="579"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
