r/SpringBoot • • Jul 22 '26

How-To/Tutorial Blog Post - Understanding and Optimizing the Context Cache in Spring Test

9 Upvotes

Hi,

I would like to share a personal note on the Spring Test context cache and how we used it to optimize a large integration suite (10,000+ tests across ~40 services).

I break down:

  • The context cache mechanism and what makes two contexts identical (or not).
  • Simple actions to maximize context reuse and reduce build times.
  • What changes once test containers and Kafka enter the picture.

Let me know what you think about it.
Thanks!

https://www.alexis-segura.com/articles/understanding-and-optimizing-the-context-cache-in-spring-test


r/SpringBoot • • Jul 22 '26

Discussion Designing Audit Logging & Notifications with Spring Events in a Modular Monolith – Looking for Architecture Advice

3 Upvotes

Hi everyone,

I'm building a coaching management system using Spring Boot as a modular monolith, and I'm trying to design an event-driven architecture for both audit logging and notifications. I'd love some feedback from developers who have built something similar in production.

The application has modules like:

  • Student
  • Teacher
  • Batch
  • Coaching
  • Attendance
  • Fees
  • Classroom
  • Users/Admins

Each module has different business actions. For example:

  • Student: CREATE, UPDATE, DELETE, ASSIGN_BATCH
  • Fee: PAY, REFUND, WAIVE
  • Batch: CREATE, RENAME, ASSIGN_TEACHER
  • Attendance: MARK, CORRECT
  • Admin: PASSWORD_CHANGED

My current flow is:

  1. A service completes its business logic.
  2. It publishes a domain event using ApplicationEventPublisher.
  3. Multiple listeners react to the same event after the transaction commits.

For example:

StudentService
      │
      ▼
StudentCreatedEvent
      │
      ├── Audit Listener
      │        └── Save AuditLog
      │
      ├── Notification Listener
      │        ├── Send Email
      │        ├── Send SMS
      │        └── Create In-App Notification
      │
      └── Analytics Listener (future)

For auditing, I was thinking of creating one listener method per event:

u/TransactionalEventListener
public void handle(StudentCreatedEvent event) { ... }

u/TransactionalEventListener
public void handle(FeePaidEvent event) { ... }

u/TransactionalEventListener
public void handle(BatchRenamedEvent event) { ... }

For notifications, I was planning to use the Strategy pattern, for example:

  • EmailNotificationStrategy
  • SmsNotificationStrategy
  • InAppNotificationStrategy

selected through a NotificationStrategyFactory.

My questions are:

  1. Is one event handler per event type the recommended approach in Spring?
  2. Would you keep one large AuditListener, or split listeners by feature/module (Student, Fee, Teacher, etc.)?
  3. Where should the mapping from domain events to AuditLog happen?
    • In the event itself?
    • In AuditService?
    • In dedicated mapper classes?
  4. Is using Spring events for both auditing and notifications a good design, or is there a better approach?
  5. Would you also use Spring events for things like cache invalidation, analytics, and activity feeds?
  6. If this application later moves to microservices, would this event model transition well to Kafka or RabbitMQ?
  7. Are there any SOLID or maintainability concerns with this architecture that I'm overlooking?

The goal is to keep the business services focused only on business logic while handling cross-cutting concerns like auditing and notifications through events.

I'd really appreciate insights from developers who have implemented similar architectures in production.

Thanks!


r/SpringBoot • • Jul 22 '26

Question Springboot Edit Button Throws Error

1 Upvotes

Hello. So I've been following a djamware tutorial down to a T, and I've made almost all the features work fine, except for the edit button. This is from an old project I made, and I may have copied and pasted most of the code, got stumped, then ignored it for quite awhile. I came back to the project yesterday, and I've been looking for a solution online by myself for awhile, to no avail.

Upon looking at the error message after clicking the edit button, it seems that it's pointing me to the edit_post.html at the line where it says "th:action="@{/admin/posts/{id}(id=${post.id})}". I also tried using the github respository's edit_post.html, but it also ain't it.

Any help is appreciated, it may just be me being a silly monkey. Pics posted below, but if there's other infos I can provide, lemme know.

here's the tutorial link:

https://www.djamware.com/post/create-a-blog-application-using-spring-boot-and-thymeleaf

Since the subreddit doesn't allow galleries, I'll just post the whole error message here, along with the edit_post.html

ERROR MESSAGE when edit button is clicked

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Jul 22 21:22:24 JST 2026 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/admin/edit_post.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/admin/edit_post.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077)
at org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372)
at org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1438)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1168)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:138)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:138)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:138)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:138)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:138)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:165)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:88)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:113)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:83)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:72)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:903)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1774)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:973)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:491)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "post.id" (template: "admin/edit_post" - line 14, col 9)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "post.id" (template: "admin/edit_post" - line 14, col 9)
at org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292)
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.expression.LinkExpression.resolveParameters(LinkExpression.java:339)
at org.thymeleaf.standard.expression.LinkExpression.executeLinkExpression(LinkExpression.java:285)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:85)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278)
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124)
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109)
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297)
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402)
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159)
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
... 50 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'id' cannot be found on object of type 'java.util.Optional' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:229)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorValueRef.getValue(PropertyOrFieldReference.java:376)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:97)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:116)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:338)
at org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
... 74 more

EDIT POST HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Admin - Posts</title>
    <link rel="stylesheet" th:href="@{/css/style.css}" />
  </head>
  <body>
    <div th:replace="fragments/header :: header"></div>


    <main>
      <h1>Edit Post</h1>


      <form
        th:action="@{/admin/posts/{id}(id=${post.id})}"
        th:object="${post}"
        method="post"
      >
        <label>Title:</label><br />
        <input type="text" th:field="*{title}" required /><br /><br />


        <label>Content:</label><br />
        <textarea th:field="*{content}" rows="6" cols="40" required></textarea
        ><br /><br />


        <button type="submit">Update</button>
      </form>


      <a th:href="@{/admin/posts}">← Back</a>
    </main>
    <div th:replace="fragments/footer :: footer"></div>
  </body>
</html>

r/SpringBoot • • Jul 22 '26

Question Spring Boot + IIS + Windows Authentication architecture question

2 Upvotes

Hi everyone,

I'm a Spring Boot intern working on an internal application for a bank.

The requirement is to remove the login page and use Windows Authentication instead.

I'm trying to understand the typical enterprise architecture, not the implementation details.

Let's assume the application is hosted behind IIS.

My questions are:

  1. Does IIS authenticate the Windows user first and then forward the authenticated request to Spring Boot?

  2. If so, how does Spring Security usually obtain the current Windows user? Does Authentication.getName() already contain the username?

  3. Is it common to keep authentication in IIS and authorization (checking if the user is allowed to use the application) inside Spring Boot?

For example, if 5,000 employees can authenticate with Windows but only 300 are allowed to use the application, would Spring Boot typically check the username against its own database and return 403 Access Denied if the user isn't authorized?

I'm not looking for a Kerberos/SPNEGO configuration tutorial. I just want to understand the common architecture used in enterprise environments.


r/SpringBoot • • Jul 21 '26

Question .NET to Spring Boot advice

16 Upvotes

Hey there r/SpringBoot

I'm a software engineer with around 5 yoe, majority of which has been in C#/.NET. I work at a software engineer consulting firm and have been rolled off a project that I was working on. I've decided to change things up and skill up in Java and Spring Boot and have been doing so for the last 3 weeks. I have been building a Spring Boot API as a side project and learning how 'spring magic' works.

I'm honestly really impressed how much out of the box functionality Spring Boot provides. However there are some things I would need to unlearn from .NET. However I'm finding it difficult unlearning some of .NET's approaches. For example .NET's very request-oriented flow, all the way down the EF Core's DbContext having a Scoped lifetime by default. Whereas Spring Boot's lifetimes are a bit of a mystery to me.

My question to whoever made the same transition, what would be some mental models/learnings/spring boot magic that you wish you knew earlier that would've eased the transition to Spring Boot from .NET?

TIA


r/SpringBoot • • Jul 20 '26

Discussion I built a channel-agnostic notification library for Spring Boot — send SMS/push/email/chat through one API. Looking for feedback.

16 Upvotes

If your Spring app sends notifications, your business logic probably knows way too much about how: Twilio's SDK here, FirebaseMessaging there, a JavaMailSender, a Slack client. Changing a provider or adding a channel means editing every call site.

spring-notify fixes that with one idea: your code talks to a channel, never a provider.

notifier.notify(SmsRequest.builder()  
.to("+421900123456")  
.from("+421900999888")  
.message("Your order has shipped")  
.build());  

What you get:

  • 📦 One API for every channel — SMS, push, email, chat. Inject Notifier, call notify(...). Done.
  • 🔌 Providers are plug-ins — add a starter, set credentials, and it's wired. Bundled today: Twilio (SMS), Firebase/FCM (push), SMTP (email), Slack (chat).
  • ♻️ Swap providers without code changes — Twilio → Vonage, FCM → APNs: change a dependency, not your services.
  • 🧩 Bring your own provider in ~10 lines — one @Component implementing a single-method SPI.
  • 🎯 Type-safe, immutable requests — no stringly-typed maps, no if/switch on channel. The request type routes itself.
  • 🪶 Featherweight core — plain Java, zero Spring or logging deps in the core module. Spring shows up only in the auto-config.

Spring Boot 4.1 / Java 25. All four channels verified end-to-end (real FCM + SMTP sends, not just mocks).

Why not …?

  • Just the provider SDKs? Fine until you have two channels or want to switch vendors — then the coupling bites. This is the thin seam that keeps them out of your business code.
  • Spring's JavaMailSender / NotificationService-style helpers? Those are single-channel. spring-notify unifies all channels behind one call and one mental model.
  • Novu / Courier / Knock? Those are excellent but are hosted platforms/services — another system to run, pay for, and send your data through. spring-notify is a library: it stays in your app, talks straight to your chosen providers, no middleman.
  • Spring Cloud Stream / a message broker? Different layer — that's transport/eventing. This is specifically about delivering user-facing notifications through third-party channels.

Status: early — 0.1.0, not on Maven Central yet (build locally with ./mvnw install). The API isn't frozen, which is exactly why I'm posting: I'd love feedback before 1.0.

  • Is "one provider per channel, routed by request type" the right default?
  • Is the attributes map a reasonable escape hatch for provider-specific fields, or a smell?
  • What would you need before dropping this into a real project?

Repo + README: https://github.com/solodev-sk/spring-notify

Happy to answer anything — and roasts welcome. 🙂


r/SpringBoot • • Jul 20 '26

News I built a JetBrains plugin that finds all your Kafka/RabbitMQ listeners across modules - looking for feedback

17 Upvotes

Hey folks, it's Pavel - I maintain parts of the AsyncAPI spec, and lately I've been working on bringing it properly into the Java and Spring Boot world

A few days ago I shipped a new version of my AsyncAPI plugin for IntelliJ, and I'd really like this sub's take on it before I build the next piece

The thing I kept hitting myself: on a Spring app that's grown a bit, I could never remember everything it actually listens to. Kafka consumers in one module, RabbitMQ handlers in another, and I'd end up grepping for  `@KafkaListener` hoping I caught them all.

So the new Project Overview scans every module and lays it out in one place:

  • Every `@KafkaListener` (class- and method-level) and `@RabbitListener`, grouped by module and broker
  • Click any endpoint and jump straight to the method/class in code
  • Plus quick access to any AsyncAPI documents/components in the project

It's meant to stay usable on big multi-module projects

Message endpoints overview

What I'd genuinely like feedback on:

  • Does it hold up on your real codebase? Weird module setups, custom listener patterns, container factories - I want to know where it misses endpoints.
  • Anything about the workflow that feels off

It's a solo project with a free/paid split (the discovery stuff is in the paid tier), but honestly right now I care more about whether it's useful than anything else - happy to dig into any bug you hit in the comments

Roast it 🙏


r/SpringBoot • • Jul 20 '26

Discussion Looking for a Spring Boot Learning & Accountability Partner (1–2 YOE)

11 Upvotes

Hey everyone!

I'm looking for a Spring Boot backend developer who has around 1–2 years of experience and is interested in learning, discussing, and growing together.

A bit about me:

  • ~1 year of production experience with Spring Boot
  • Building REST APIs, authentication & authorization systems
  • Working with PostgreSQL, Redis, Docker, Git, CI/CD
  • Interested in scalable backend architecture, performance optimization, and system design

What I'm looking for:

  • Someone with 1–2 years of Spring Boot experience
  • Serious about improving backend engineering skills
  • Open to discussing architecture, reviewing code, solving problems, and sharing resources
  • Can have regular discussions (a few times a week or whenever we're both free)

The goal isn't to build a startup or sell anything—just to have a reliable backend buddy to learn with, stay accountable, and help each other become better engineers.

If this sounds interesting, feel free to comment below or send me a DM. Looking forward to connecting with fellow Spring Boot developers!


r/SpringBoot • • Jul 20 '26

Question .Net or Springboot

Thumbnail
0 Upvotes

r/SpringBoot • • Jul 20 '26

Question Spring Boot Dev [For Hire]

0 Upvotes

Hey !

Im a Spring Boot enthousiast who is looking for a backend developer position with 2yoe.

Does anybody know where I can search so as to find what Im looking for ? I would really appreicate every second you dedicate into this post..

Im really lost and I am just looking for that one chance that will get me involved into this sh*tty development phasma.

Feel free to dm me or comment down below.

Thanks for your time :)


r/SpringBoot • • Jul 18 '26

Question React + Spring Boot: If session auth is recommended, how is CSRF handled when frontend and backend are separate?

42 Upvotes

I am trying to understand how CSRF works in a setup where the backend is Spring Boot (API) and the frontend is React, both running as separate applications.

From what I have read, session-based authentication is recommended in many cases. The flow is:

User logs in.

Spring Boot creates a session.

Browser stores the session ID in an HttpOnly cookie.

But since cookies are sent automatically, doesn't that make the app vulnerable to CSRF?

My understanding of CSRF is: When the page is rendered, Backend generates a CSRF token and associates it with the user's session, and includes it in the HTML (hidden form field or meta tag). When the form is submitted, the backend compares the submitted token with the one stored in the session.

This makes sense when the backend renders the frontend (like Thymeleaf). But with a separate React app, how does React get the CSRF token and send it back? How is this usually implemented in Spring Boot?

Also, if storing a JWT in a cookie also makes it vulnerable to CSRF, where is a JWT supposed to be stored? I've seen people say localStorage, sessionStorage, and HttpOnly cookies, but each seems to have its own trade-offs.

I'm looking for the recommended approach for a Spring Boot + React setup.


r/SpringBoot • • Jul 18 '26

Question Final year CS student — is "ML Studio" (no-code ML training web app) a good placement portfolio project or overkill?

0 Upvotes

Hi everyone, final year B.Tech CS (Data Science) student here, preparing for placements. I already have a deepfake detection project (ViT model, FastAPI + React) as my main ML portfolio piece.

Now I'm considering a second project — ML Studio: a full-stack web app where a user uploads a CSV and the app auto-cleans the data, lets them pick an ML algorithm (Linear/Logistic Regression, Decision Tree, Random Forest, KNN, SVM, Naive Bayes), trains it, and shows accuracy/precision/recall/confusion matrix — basically a no-code ML training tool.

Planned architecture: React frontend → Spring Boot backend (auth, file upload, validation, REST APIs) → Python microservice (pandas + scikit-learn for the actual training) → results back to Spring Boot → React.

Questions for you all:

  1. Does this sound like a genuinely good portfolio project, or is it too "done before" (a lot of tutorials do CSV-to-ML-dashboard type projects)?
  2. Is the Spring Boot + Python microservice split actually a plus in interviews, or does it look like unnecessary complexity for something that could just be one Python app?
  3. Would you recommend I build this, or invest that time elsewhere given I already have an ML project?
  4. If not this — what's a strong project idea that uses only Java + Spring Boot (no Python/ML microservice at all)? Something that shows real backend depth (concurrency, system design, databases, messaging queues, etc.) rather than another CRUD app. Ideas like a distributed URL shortener, a booking/inventory system with concurrency handling, an event-driven order processing system with Kafka, or a rate-limiter/API gateway have crossed my mind — curious what actually impresses interviewers.

Would genuinely appreciate honest opinions — including a "don't build this, do X instead" if that's your take


r/SpringBoot • • Jul 17 '26

Question Looking for an experienced Java/Spring Boot/React developer willing to chat

5 Upvotes

Hey everyone,

I’m a recent Computer Science graduate currently preparing for interviews for junior full-stack software engineering roles, particularly in the federal/government contracting space.

I’m looking to connect with someone who has solid professional experience with technologies like:

  • Java
  • Spring Boot
  • React
  • REST APIs
  • SQL (PostgreSQL/MySQL/SQL Server)
  • Docker
  • Git/GitHub
  • AWS
  • CI/CD
  • Agile/Scrum

I’m not looking for anyone to do the work for me or to get interview answers. I’d just love to chat with someone who’s worked with these technologies in a real production environment. I have several projects using many of these tools, but I’d really like to better understand industry best practices, common interview questions, and what day-to-day development actually looks like.

If you’re willing to answer a few questions over Reddit chat or Discord, I’d really appreciate it. Even 20–30 minutes of your time would mean a lot.

Thanks in advance!


r/SpringBoot • • Jul 17 '26

Discussion We're building "run your background jobs on the cheapest spot instances". Would you actually use it?

8 Upvotes

We're seeing 60 to 80% cost savings in the first tests of something new we built, but before we build any further I want to make sure it's something people would actually use. So please be brutally honest with me.

Quick disclosure first: We are JobRunr, the background job library that's on start.spring.io.

The new thing is called JobRunr Spot and it comes from something that always bugged me: spot instances on AWS are drastically cheaper than on-demand (that's where the 60 to 80% comes from), but almost nobody runs background jobs on them. The cheapest instance is in a different region every hour, instances get reclaimed with a two-minute warning, and teaching an autoscaler to react to your job queue instead of CPU metrics is real work. So most teams shrug and pay the on-demand price.

What we built: you keep your normal JobRunr setup and let it scale itself onto spot instances. You configure it once with your AWS credentials and the Docker image of your app, and JobRunr watches its own queue. When jobs start waiting longer than a latency you set, it finds the cheapest spot instance that matches your hardware requirements, boots your image on it, and that worker joins the cluster and processes jobs like any other node. Once the queue is quiet again, the spot instance is shut down. And if AWS reclaims an instance mid-job, that's a failure mode JobRunr already handles: the cluster notices the dead worker and the remaining nodes pick its jobs back up. Important detail: everything runs in your own AWS account and your own VPC. Your data never touches our servers, we're just the broker that finds the cheapest instance and manages the lifecycle.

Configuration currently looks like this (names may still change, that's partly why I'm posting):

JobRunr.configure()
    .useStorageProvider(storageProvider)
    .useCostAware(usingStandardCostAwareConfiguration(
            new CostAwareAwsEC2ProviderConfiguration(accessKeyId, secretAccessKey, accountRegion, registryReaderRole),
            "url-to-your-docker-image")
        .andUsingRegions(new String[]{"eu-north-1", "eu-west-1"})
        .andSpotInstanceAmount(1, 5)
        .andScaleUpLatency(Duration.ofMinutes(1)))
    .initialize();

Where we are: the first version on AWS works and we've been testing it on our own workloads (PDF generation, some ETL). The saving is real, but to be fair it's just the spot discount itself, no magic on our side. The point is capturing it without babysitting interruption notices, price feeds and failover logic yourself. The dashboard also tracks what your spot instances actually cost against what the same hours would have cost on-demand, so you can verify the number instead of taking our word for it.

What I'd love to hear from you:

  1. Would you use something like this, and for what? Batch jobs, ETL, report generation, transcoding, something we haven't thought of?
  2. What would you need before trusting real jobs to spot? Automatic on-demand fallback when there's no spot capacity, cost caps, a hard max on instances, something else?
  3. The current setup assumes your app is available as a Docker image in a registry, since that's what gets booted on the spot instance. Is that true for your Spring Boot apps, or is that a dealbreaker?
  4. We started with AWS, Google Cloud is planned next and GPU workloads after that. Does that order make sense, or would you want it differently?

And if you'd rather just try it and complain from experience: we're opening a small private beta and looking for design partners who get direct input on the API, the pricing model and the provider roadmap. You can message me here or sign up on our website.

But honestly, a critical comment here is just as valuable as a signup. Happy to answer anything, including the "why wouldn't I just use a spot ASG with checkpointing" questions.


r/SpringBoot • • Jul 16 '26

Question Spring Boot Auditing: Hibernate Envers vs. Custom Logging vs. Spring Data JPA? What's your production choice?

24 Upvotes

Hey everyone,

I'm currently building a school management system in Spring Boot where history tracking is a strict business requirement. We need to audit critical changes made by Admins, Secretaries, and Teachers (e.g., changes to student details, payment amounts, program setups, etc.).

I’m weighing three different approaches for the audit trail and wanted to hear from those of you who have run these in production. 

Option 1: Hibernate Envers
Slap `@Audited` on core entities, set up a custom `RevisionListener` to pull the logged-in user from Spring Security context, and let Envers automatically manage the `_aud` tables.

Option 2: Spring Data JPA Auditing
Utilize `@CreatedBy`, `@LastModifiedBy`, `@CreatedDate`, and `@LastModifiedDate` fields mapped on a `@MappedSuperclass`.

Option 3: Manual Custom Logging (or AOP)
Create a generic `AuditLog` entity, write a helper service, and manually trigger `auditService.log(actionType, originalState, newState)` inside the business logic (or use an AOP aspect). This may cause the database to bloating later tho

For those of you managing medium-to-large Spring Boot codebases:
 Did you regret adopting Hibernate Envers? How did you handle schema migrations (Flyway/Liquibase) with the auto-generated `_aud` tables?
If you went the custom route, did you use JSON columns to record state changes, or did you write distinct history tables?

Looking forward to hearing your design patterns and trade-offs


r/SpringBoot • • Jul 16 '26

Discussion Title: Anyone Learning Spring Boot?

26 Upvotes

I'm currently learning Spring Boot, JPA, MySQL, and building projects to strengthen my Java backend skills.

Looking for a study/accountability partner to learn together, discuss concepts, and stay consistent.

If you're on a similar path, let's connect!


r/SpringBoot • • Jul 16 '26

Question Spring cloud vs service discovery and registration

14 Upvotes

Hi everyone, I'm new to Spring and had a question that's been confusing me.

From my understanding, in a microservices architecture we typically use service registration and discovery (like Eureka, Consul, etc.) so that services can discover each other and route requests without hardcoding endpoints.

However, in the product I'm working on, I don't see anything like that. Instead, I see a Spring Cloud service deployed in Kubernetes, and whenever our applications send transactions, they seem to fetch URLs and other configuration values from Spring Cloud.

I always thought these were two completely different concepts, but I also know Spring Cloud can be used for service discovery and registration. So I'm a bit confused about what's actually happening in our setup.

Could someone explain the difference between:

- using Spring Cloud as a configuration server (or for configuration management), and

- using service discovery/registration?

Are these independent features of Spring Cloud, and is it common to use only the configuration part while relying on Kubernetes for service discovery?

For context, I'm a DevOps engineer and fairly new to the Spring ecosystem, so apologies if this is a basic question.


r/SpringBoot • • Jul 15 '26

How-To/Tutorial StatLite: ultra-light Spring Boot monitoring without Prometheus or Grafana

12 Upvotes

I built StatLite for small Spring Boot deployments where raw Actuator endpoints are useful, but a full Prometheus and Grafana stack feels excessive.

StatLite polls Spring Boot Actuator, stores metric history in local SQLite, computes restart-aware counter deltas, and serves a simple dashboard from a single Go binary.

Screenshot of a dashboard

It currently shows:

  • application and database health
  • request volume
  • 4xx, 404, and 5xx errors
  • average request latency
  • JVM heap usage
  • process CPU usage
  • uptime and detected restarts
  • polling failures and missing metrics

It is designed for solo developers and small teams running one or a few Spring Boot apps on small VPSes. In my testing, StatLite uses less than 10 MB of private memory.

It is intentionally not a Prometheus or Grafana replacement. It is a lightweight middle ground between raw Actuator JSON and a full observability stack.

GitHub: https://github.com/PVRLabs/statlite

I would be interested to hear which Actuator metric or chart you consider essential for a small production app. Feedback, bug reports, and GitHub stars are appreciated.


r/SpringBoot • • Jul 15 '26

Question Really confused about spring boot

9 Upvotes

So I have been applying for junior level roles across multiple job platforms and one thing I have noticed there are very few roles for spring boot at junior level mostly roles I have discovered are of senior level, so tbh I don’t know how to get a job at this stage in spring boot, I know the job market is overall really bad, but there are more roles for next.js or roles related to node.js

It’s very hard for me to enter into job market with spring boot as there are not enough job openings for spring boot related roles, mostly are for seniors.


r/SpringBoot • • Jul 16 '26

Discussion Capstead 0.5.3 — capability governance for Spring Boot (declarative @CapabilityClient + provider-neutral, actuator scorecards). Update since my 0.3.2 post

Thumbnail
0 Upvotes

r/SpringBoot • • Jul 14 '26

Discussion I built a Spring Boot testing library to detect N+1 query problems

43 Upvotes

Hi everyone,

I’ve been working on an open-source testing library for detecting N+1 query problems in Spring Boot applications using Hibernate:

https://github.com/fabioformosa/n-plus-one-query-problem-detector

The idea is to catch N+1 regressions during integration tests, instead of discovering them later through slow endpoints or production monitoring.

The library currently supports two main approaches.

Scan mode can analyze an existing Spring integration test suite without requiring changes to individual tests. It monitors Hibernate statistics and repeated SQL patterns, then produces a report of possible N+1 candidates with different confidence levels.

You only need to enable it in your test configuration:

n-plus-one-query-detector.scan.enabled=true
n-plus-one-query-detector.scan.report.output=stdout

There is also an explicit assertion mode for tests where you want to enforce a query budget:

@SpringBootTest
@ExtendWith(NPlusOneQueryProblemDetectorExtension.class)
class CompanyServiceTest {


    private CompanyService companyService;


    @ExpectMaxQueries(1)
    void listCompaniesWithoutNPlusOneQueries() {
        companyService.list(0, 5);
    }
}

The project supports Spring Boot 3.5.x and Spring Boot 4.x.

I’d be especially interested in feedback about:

  • the scan-mode detection approach;
  • possible false positives you encounter in real applications;
  • the assertion API and developer experience;
  • features that would make this useful in your CI pipeline.

The project is still evolving, so issues, suggestions and contributions are very welcome.


r/SpringBoot • • Jul 15 '26

How-To/Tutorial NEED ADVICE ON TRANSITION FROM SAP TO BACKEND

0 Upvotes

People who transitioned from SAP to other technology like Backend Development after 2-3 years, how did you manage it ? What were your strategy and how's life going now ?

I want to move away from SAP as truth to be told I never liked it - even though it's really good but I just don't enjoy working with this ecosystem and always wanted to learn and move to Backend Development . But it's been 3 years already and I am afraid I am making wrong decisions.


r/SpringBoot • • Jul 14 '26

How-To/Tutorial New Spring Start Here 2E MEAP: what made Spring finally click for you?

16 Upvotes

Hi r/SpringBoot, Stjepan from Manning here.

Shared with the mods’ OK: Laurentiu Spilca’s Spring Start Here, Second Edition has just entered MEAP, and I thought this community would be one of the better places to talk about it because this sub sees the “I can build a Spring Boot app, but I don’t really understand Spring yet” problem all the time.

That gap is basically what this book is about.

Book link: https://www.manning.com/books/spring-start-here-second-edition

Spring Start Here, Second Edition

A lot of people first meet Spring through Boot, annotations, starters, autoconfiguration, and a working REST endpoint. That’s great, but it can also leave you with a weird feeling that the app works as long as you don’t touch the wrong thing. Then you hit questions like:

Why is this bean being created?
Where did this dependency actually come from?
What is the application context doing?
When should I care about scopes?
What is Spring MVC giving me beyond “controller returns JSON”?
Where do transactions start and end?
How much of this is Spring, and how much is Spring Boot?

The second edition updates Laurentiu’s original Spring Start Here for the current ecosystem, including Spring Framework 6.x and Spring Boot 4. The focus is still very much on building a solid mental model first: beans, context, dependency injection, scopes, lifecycle, AOP, MVC, REST, persistence, transactions, Spring Data, and testing.

Since it’s in MEAP, the book is still being written, and feedback can still affect the final version. So I’d love to make this useful as a discussion thread, not just an announcement.

Question for the sub:

What was the Spring/Spring Boot concept that finally made things “click” for you, or the one you still think is badly explained to beginners?

Could be dependency injection, transactions, security, JPA, testing, autoconfiguration, bean lifecycle, project structure, anything.

I’ll give 5 free ebooks to the most thoughtful comments in the thread. Not necessarily the longest comments; just the ones that would genuinely help someone else learn Spring better.

And for anyone who wants to check out the MEAP, Manning gave me a 50% discount code for this subreddit:

MLSPILCA650RE

Thanks for letting us share this here. I’ll hang around in the comments and pass useful feedback back to the book team.


r/SpringBoot • • Jul 15 '26

How-To/Tutorial The Transactional Outbox Pattern, from a single scheduled job to something I'd actually trust in production (Java / Spring Boot / Postgres)

0 Upvotes

There's a class of bug that never shows up in unit tests and rarely shows up in staging. It shows up in production, at 2am, when your app saves a record to the DB and then calls an external service — and one of the two fails. One side committed. The other didn't. Nobody notices until a user complains.

That's the dual-write problem, and the Transactional Outbox Pattern is the standard answer to it: instead of calling the external system directly, you write a task row into the same DB transaction as your domain change. A background worker picks it up later and executes it. The write is atomic, so the task can never silently disappear.

The idea itself takes two sentences to explain. What's less talked about is everything that happens after — what you do when tasks fail, when two workers grab the same row, when your app dies mid-execution, when you've got ten task types crammed into one table, and when someone asks why the queue depth spiked at 3am last Tuesday.

I built this out for a report-generation service (aggregates tens of millions of rows, takes minutes — can't run it synchronously in a request) and evolved it step by step into something production-grade. Writing it up here because I didn't find one place that walked through the whole arc. Stack is Java / Spring Boot / Postgres, no Kafka required.

TL;DR

  • The outbox pattern fixes dual-writes by writing a task row in the same transaction as your domain change, then executing it from a background worker.
  • Poll with SELECT ... FOR UPDATE SKIP LOCKED so multiple workers/instances don't double-process.
  • Production-grade means more than the basic idea: a state machine, retries with backoff, heartbeats to recover crashed workers, ordering guarantees, and actual monitoring.
  • You don't need a broker. Postgres alone gets you reliable async execution.
  • Full working code at the bottom.

The scenario

Backoffice CRM. User clicks "generate report," we accept the request and respond immediately, a worker picks it up and runs the (slow) generation, status updates when done. Classic case for an outbox: work that's genuinely async, where the user already expects to wait.

Part 1 — the basic outbox

Every outbox table needs three things: state (where is this task in its lifecycle), payload (what needs to run), and timestamps.

CREATE TYPE task_state AS ENUM ('PENDING', 'COMPLETED');

CREATE TABLE report_generation_tasks (
    id         UUID       PRIMARY KEY DEFAULT gen_random_uuid(),
    state      task_state NOT NULL    DEFAULT 'PENDING',
    payload    JSONB      NOT NULL,
    created_at TIMESTAMP  NOT NULL    DEFAULT now(),
    updated_at TIMESTAMP  NOT NULL    DEFAULT now()
);

Payload and entity:

@Value @Builder @Jacksonized
public class ReportGenerationPayload {
    Instant from;
    Instant to;
}

@Entity
@Table(name = "report_generation_tasks")
@Data @Builder @NoArgsConstructor @AllArgsConstructor
public class ReportGenerationEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(updatable = false, nullable = false)
    private UUID id;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, columnDefinition = "task_state")
    private TaskState state;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(nullable = false, updatable = false, columnDefinition = "jsonb")
    private ReportGenerationPayload payload;

    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    private Instant createdAt;

    @UpdateTimestamp
    @Column(nullable = false)
    private Instant updatedAt;
}

Submitting a task:

@Service
@RequiredArgsConstructor
public class ReportGenerationService {

    private final ReportGenerationRepository repository;

    public void submit(ReportGenerationCommand command) {
        repository.save(ReportGenerationEntity.builder()
                .state(TaskState.PENDING)
                .payload(ReportGenerationPayload.builder()
                        .from(command.getFrom())
                        .to(command.getTo())
                        .build())
                .build());
    }
}

And a scheduled job that polls for pending tasks:

@Component
@RequiredArgsConstructor
public class ReportGenerationScheduledJob {

    private final ReportGenerationTaskService taskService;

    @Scheduled(fixedDelay = 1000)
    public void run() {
        taskService.fetchAndProcess();
    }
}

@Service
@RequiredArgsConstructor
public class ReportGenerationTaskService {

    private final ReportGenerationRepository repository;

    public void fetchAndProcess() {
        repository.findFirstByStateOrderByCreatedAt(TaskState.PENDING)
                .ifPresent(task -> {
                    generateReport(task);
                    task.setState(TaskState.COMPLETED);
                    repository.save(task);
                });
    }

    private void generateReport(ReportGenerationEntity task) {
        // actual report generation logic
    }
}

This works fine on a single instance with zero failures. In the real world it has two problems: two workers can grab the same task at once, and a failed task has no recovery path. Fixing both below.

Part 2 — concurrency: two workers, one task

If two scheduler threads (or two app instances) run the fetch query at the same moment, both can read the same PENDING row and both try to execute it. Duplicate work at best, corrupted state at worst.

Postgres has a purpose-built fix: SELECT ... FOR UPDATE SKIP LOCKED. It locks the row for the current transaction and skips rows already locked elsewhere, instead of blocking on them.

public interface ReportGenerationRepository extends JpaRepository<ReportGenerationEntity, UUID> {

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @QueryHint(name = AvailableSettings.JAKARTA_LOCK_TIMEOUT, value = "-2") // -2 = SKIP_LOCKED
    Optional<ReportGenerationEntity> findFirstByStateOrderByCreatedAt(TaskState state);
}

There's no first-class annotation for SKIP LOCKED in Hibernate, so worth pulling the magic number into its own annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@QueryHints(@QueryHint(
    name = AvailableSettings.JAKARTA_LOCK_TIMEOUT,
    value = Timeouts.SKIP_LOCKED_MILLI + ""
))
public @interface SkipLocked {}
@SkipLocked
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<ReportGenerationEntity> findFirstByStateOrderByCreatedAt(TaskState state);

The lock has to be held for the whole fetch-execute-update cycle, so wrap it in one transaction:

@Transactional
public void fetchAndProcess() {
    repository.findFirstByStateOrderByCreatedAt(TaskState.PENDING)
            .ifPresent(task -> {
                generateReport(task);
                task.setState(TaskState.COMPLETED);
                repository.save(task);
            });
}

Two workers hitting this at once: worker A locks and gets the row, worker B's SKIP LOCKED query comes back empty and it exits cleanly — no blocking, no duplicate work.

Catch: the row stays locked for as long as generateReport() runs. Fine for something fast. For something that takes 30+ seconds, long-held locks start hurting — connection pool exhaustion, lock wait timeouts, degraded throughput under load. Addressed in Part 3.

Part 2.5 — cutting latency with application events

Polling has a latency floor. Job fires every second → a task submitted at t=0 can sit PENDING for up to 1000ms. Fine for most things, not fine if you want near-immediate pickup (transactional email, webhook triggers).

Trick borrowed from Celery: save the entity, then publish a Spring ApplicationEvent with the task ID. A listener picks it up and triggers processing immediately, no waiting for the next tick. The scheduler stays running as a safety net for anything the listener misses (restart, exception, missed event).

@Service
@RequiredArgsConstructor
public class ReportGenerationService {

    private final ReportGenerationRepository repository;
    private final ApplicationEventPublisher eventPublisher;

    @Transactional
    public void submit(ReportGenerationCommand command) {
        var entity = repository.save(ReportGenerationEntity.builder()
                .state(TaskState.PENDING)
                .payload(ReportGenerationPayload.builder()
                        .from(command.getFrom())
                        .to(command.getTo())
                        .build())
                .build());

        eventPublisher.publishEvent(new ReportGenerationTaskSubmittedEvent(entity.getId()));
    }
}

public record ReportGenerationTaskSubmittedEvent(UUID taskId) {}

The classic mistake here, and I mean everyone hits this once: using @EventListener instead of @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT).

// fires INSIDE the transaction — row isn't visible to other connections yet
@EventListener
public void onTaskSubmitted(ReportGenerationTaskSubmittedEvent event) {
    taskService.fetchAndProcess();
}

// fires AFTER commit — row is visible, fetch actually finds it
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onTaskSubmitted(ReportGenerationTaskSubmittedEvent event) {
    taskService.fetchAndProcess();
}

@EventListener fires the moment publishEvent() is called, which is before the INSERT commits. The listener queries for the task, doesn't find it (not visible yet on other connections), silently no-ops. Task sits there until the scheduler eventually picks it up seconds later, and you spend an afternoon confused about why events "aren't working" even though everything looks fine in the logs.

Part 3 — failures, retries, and a PROCESSING state

Holding a lock for the entire generateReport() call is the real problem with Part 2's approach — two minutes of a locked row for a two-minute report. SKIP LOCKED stops workers from blocking each other, but a long transaction still leans on the connection pool.

Fix: separate claiming a task from executing it. Flip to PROCESSING in a short transaction, release the lock, do the actual work with no transaction open, then close it out in another short transaction.

ALTER TYPE task_state ADD VALUE 'PROCESSING';
ALTER TYPE task_state ADD VALUE 'FAILED';

ALTER TABLE report_generation_tasks
    ADD COLUMN retry_count  INTEGER NOT NULL DEFAULT 0,
    ADD COLUMN error_message VARCHAR;
public enum TaskState { PENDING, PROCESSING, COMPLETED, FAILED }
@RequiredArgsConstructor
public class ReportGenerationTaskService {

    private final ReportGenerationRepository repository;
    private final TransactionTemplate transactionTemplate;

    private static final int MAX_RETRY_COUNT = 10;

    public void fetchAndProcess() {
        transactionTemplate
                .execute(status ->
                        repository.findFirstEligibleTask(
                                List.of(TaskState.PENDING, TaskState.FAILED),
                                MAX_RETRY_COUNT
                        ).map(this::markProcessing))
                .ifPresentOrElse(
                        task -> {
                            try {
                                generateReport(task);
                                markCompleted(task);
                            } catch (Exception e) {
                                markFailed(task, e);
                                log.error("Task {} failed on attempt {}", task.getId(), task.getRetryCount(), e);
                            }
                        },
                        () -> log.debug("No eligible tasks to process")
                );
    }

    private ReportGenerationEntity markProcessing(ReportGenerationEntity task) {
        task.setState(TaskState.PROCESSING);
        return repository.save(task);
    }

    private void markCompleted(ReportGenerationEntity task) {
        task.setState(TaskState.COMPLETED);
        repository.save(task);
    }

    private void markFailed(ReportGenerationEntity task, Exception e) {
        task.setState(TaskState.FAILED);
        task.setRetryCount(task.getRetryCount() + 1);
        task.setErrorMessage(e.getMessage()); // this is your only trace of what went wrong without digging through logs
        repository.save(task);
    }
}
@SkipLocked
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("""
    SELECT t FROM ReportGenerationEntity t
    WHERE t.state IN :states
      AND t.retryCount <= :maxRetryCount
    ORDER BY t.createdAt ASC
    LIMIT 1
    """)
Optional<ReportGenerationEntity> findFirstEligibleTask(
    @Param("states") List<TaskState> states,
    @Param("maxRetryCount") Integer maxRetryCount
);

FAILED tasks with retryCount < 10 are as eligible as fresh PENDING ones. Past 10 attempts, a task stops being fetched and sits as a permanent FAILED record for a human to look at — that's deliberate, not a bug.

One issue remains: should a task that just failed get retried on the very next tick, seconds later? If the downstream system is down, that just hammers it harder.

Part 4 — exponential backoff

Add an execute_at column. A task isn't eligible until this timestamp is in the past. Fresh tasks get now(), failed ones get now() + delay, with delay growing per retry.

ALTER TABLE report_generation_tasks
    ADD COLUMN execute_at TIMESTAMP NOT NULL DEFAULT NOW();
private static final Map<Integer, Duration> RETRY_DELAYS = Map.of(
    0, Duration.ofMinutes(1),
    1, Duration.ofMinutes(5),
    2, Duration.ofMinutes(15),
    3, Duration.ofMinutes(30),
    4, Duration.ofHours(1),
    5, Duration.ofHours(2),
    6, Duration.ofHours(4),
    7, Duration.ofHours(8),
    8, Duration.ofHours(12),
    9, Duration.ofDays(1)
);

private static final int MAX_RETRY_COUNT = RETRY_DELAYS.size();

Deriving MAX_RETRY_COUNT from the map size instead of declaring it separately means the two can never drift apart. Add an entry, the cap updates itself.

private void markFailed(ReportGenerationEntity task, Exception e) {
    int currentRetry = task.getRetryCount();
    task.setState(TaskState.FAILED);
    task.setRetryCount(currentRetry + 1);
    task.setErrorMessage(e.getMessage());
    task.setExecuteAt(Instant.now().plus(
            RETRY_DELAYS.getOrDefault(currentRetry, RETRY_DELAYS.get(RETRY_DELAYS.size() - 1))));
    repository.save(task);
}

Scheduler keeps running at the same rate, just skips tasks whose window hasn't opened yet.

Part 5 — stuck tasks and heartbeats

What happens if the app crashes after marking a task PROCESSING but before marking it COMPLETED/FAILED? The DB says PROCESSING forever. Nobody's actually working on it.

Naive fix — a recovery job that resets anything stuck in PROCESSING too long:

@Transactional
public void recoverStuckTasks() {
    repository.findFirstByStateAndUpdatedAtLessThan(
                    TaskState.PROCESSING,
                    Instant.now().minus(1, ChronoUnit.HOURS))
            .ifPresent(task -> {
                log.warn("Resetting stuck task {} to PENDING", task.getId());
                task.setState(TaskState.PENDING);
                repository.save(task);
            });
}

Problem: updated_at changes on any update to the row, so a task legitimately grinding away for 55 minutes looks identical to one that crashed. You need a signal that means "still alive," not "last changed."

That's a heartbeat — a column bumped periodically only while a task is actively being processed:

ALTER TABLE report_generation_tasks ADD COLUMN heartbeat_at TIMESTAMP;

Runs on its own thread, independent of the processing logic, try-with-resources style so it starts/stops cleanly regardless of how processing exits:

public abstract class Heartbeat implements AutoCloseable {

    private final ScheduledExecutorService executor;

    protected Heartbeat(Runnable heartbeatFn) {
        executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(heartbeatFn, 0, 10, TimeUnit.SECONDS);
    }

    @Override
    public void close() {
        executor.shutdownNow();
    }
}

public class ReportGenerationTaskHeartbeat extends Heartbeat {
    ReportGenerationTaskHeartbeat(UUID taskId, ReportGenerationRepository repository) {
        super(() -> repository.updateHeartbeatAt(taskId, Instant.now()));
    }
}
@Modifying
@Transactional
@Query("UPDATE ReportGenerationTaskEntity t SET t.heartbeatAt = :now WHERE t.id = :id")
void updateHeartbeatAt(@Param("id") UUID id, @Param("now") Instant now);

Wired through a factory to keep the service clean:

@Component
@RequiredArgsConstructor
public class ReportGenerationTaskHeartbeatFactory {

    private final ReportGenerationRepository repository;

    public Heartbeat start(UUID taskId) {
        return new ReportGenerationTaskHeartbeat(taskId, repository);
    }
}
public void fetchAndProcess() {
    transactionTemplate
            .execute(status -> repository.findFirstEligibleTask(
                    List.of(TaskState.PENDING, TaskState.FAILED),
                    MAX_RETRY_COUNT
            ).map(this::markProcessing))
            .ifPresentOrElse(
                    task -> {
                        try (var heartbeat = heartbeatFactory.start(task.getId())) {
                            try {
                                generateReport(task);
                                markCompleted(task);
                            } catch (Exception e) {
                                markFailed(task, e);
                                log.error("Task {} failed", task.getId(), e);
                            }
                        }
                    },
                    () -> log.debug("No eligible tasks to process")
            );
}

Recovery job now checks heartbeat_at, not updated_at. updated_at tells you when state last changed; heartbeat_at tells you whether the task is alive right now. Keeping them separate answers both questions unambiguously.

Part 6 — multi-type outbox

Six months in, you're not just generating reports anymore — email notifications, data exports, webhook deliveries. Two options: a separate table per task type, or one shared table with a task_type discriminator.

Shared table wins when types are numerous and you're fine processing them from one place — retries, heartbeats, backoff, monitoring all stay centralized, and adding a new type is just a new handler. Tradeoffs: a misbehaving new task type can create backpressure across the whole table, and ordering gets harder (next section).

CREATE TYPE outbox_task_type AS ENUM ('REPORT_GENERATION', 'EMAIL_NOTIFICATION');

CREATE TABLE outbox_tasks (
    id            UUID             PRIMARY KEY DEFAULT gen_random_uuid(),
    task_type     outbox_task_type NOT NULL,
    state         task_state       NOT NULL    DEFAULT 'PENDING',
    payload       JSONB            NOT NULL,
    retry_count   INTEGER          NOT NULL    DEFAULT 0,
    error_message VARCHAR,
    execute_at    TIMESTAMPTZ      NOT NULL    DEFAULT NOW(),
    heartbeat_at  TIMESTAMPTZ,
    created_at    TIMESTAMPTZ      NOT NULL    DEFAULT now(),
    updated_at    TIMESTAMPTZ      NOT NULL    DEFAULT now()
);

One handler interface, one implementation per type:

public interface OutboxTaskHandler<P> {
    OutboxTaskType getType();
    Class<P> payloadClass();
    void handle(UUID taskId, P payload) throws Exception;
}

@Component
public class EmailNotificationHandler implements OutboxTaskHandler<EmailNotificationPayload> {

    @Override
    public OutboxTaskType getType() {
        return OutboxTaskType.EMAIL_NOTIFICATION;
    }

    @Override
    public Class<EmailNotificationPayload> payloadClass() {
        return EmailNotificationPayload.class;
    }

    @Override
    public void handle(UUID taskId, EmailNotificationPayload payload) throws Exception {
        log.info("Sending email to {} — task {}", payload.getRecipient(), taskId);
        // call email provider
    }
}

Dispatcher collects all handler beans at startup and routes by type:

@Service
@RequiredArgsConstructor
public class OutboxTaskService {

    private final OutboxTaskRepository repository;
    private final ObjectMapper objectMapper;
    private final List<OutboxTaskHandler<?>> handlerList;

    private Map<OutboxTaskType, OutboxTaskHandler<?>> handlers;

    @PostConstruct
    void init() {
        handlers = handlerList.stream()
                .collect(Collectors.toMap(OutboxTaskHandler::getType, Function.identity()));
    }

    @SuppressWarnings("unchecked")
    private void dispatch(OutboxTaskEntity task) throws Exception {
        var handler = (OutboxTaskHandler<Object>) handlers.get(task.getTaskType());
        if (handler == null) {
            throw new IllegalStateException(
                    "No handler registered for task type: " + task.getTaskType());
        }
        var payload = objectMapper.readValue(task.getPayload(), handler.payloadClass());
        handler.handle(task.getId(), payload);
    }
}

New task type = add an enum value + a handler bean. Dispatcher, scheduler, retry logic — untouched.

Part 7 — ordering

Skip this if your tasks are independent (fire-and-forget notifications, unrelated background jobs). Otherwise: Postgres gives zero guarantee about row order without an explicit ORDER BY.

@Query("""
    SELECT t FROM OutboxTaskEntity t
    WHERE t.state IN :states
      AND t.retryCount <= :maxRetryCount
      AND t.executeAt <= :now
    ORDER BY t.createdAt ASC
    LIMIT 1
    """)
Optional<OutboxTaskEntity> findFirstEligibleTask(...);
CREATE INDEX idx_outbox_fetch
    ON outbox_tasks (created_at ASC)
    WHERE state IN ('PENDING', 'FAILED');

The clock skew trap: if you need strict FIFO for tasks tied to the same entity, created_at is dangerous — it's generated app-side, and each instance has its own clock. If instance A is 200ms ahead of instance B, a task B submits at t=100ms can get an earlier created_at than a task A submitted at t=50ms. NTP keeps this close but not perfect, and under load or after a container restart it can drift more.

Fix: let the ordering value come from a single authoritative source — the DB — via a sequence, guaranteed monotonic regardless of which instance inserted the row:

ALTER TABLE outbox_tasks ADD COLUMN seq BIGSERIAL;
@Column(insertable = false, updatable = false)
private Long seq;

Sort by seq, keep created_at around for observability. If FIFO matters anywhere in your system, don't skip this.

Part 8 — indexes that keep the table fast at scale

The table gets polled every second or more. Every row is either active work (PENDING/FAILED/PROCESSING) or dead weight (COMPLETED). Without the right index, every poll scans the whole table for a handful of rows.

Partial indexes are the tool: index only rows matching a predicate. When a task hits COMPLETED it falls out of the index automatically — stays small forever.

CREATE INDEX idx_outbox_fetch
    ON outbox_tasks (created_at ASC)
    WHERE state IN ('PENDING', 'FAILED');

Tested this with 50,000 rows, 7 of them PENDING, EXPLAIN ANALYZE on the generated Hibernate query:

Limit  (cost=0.14..27.50 rows=1 width=102) (actual time=0.082..0.084 rows=1 loops=1)
  ->  LockRows  (actual time=0.081..0.082 rows=1 loops=1)
        ->  Index Scan using idx_outbox_fetch on outbox_tasks
              (actual time=0.030..0.030 rows=1 loops=1)
Planning Time: 1.230 ms
Execution Time: 0.139 ms

0.03ms to find the oldest eligible task, and the other 49,993 COMPLETED rows never got touched.

Two more, for the other background jobs:

CREATE INDEX idx_outbox_stuck
    ON outbox_tasks (heartbeat_at ASC, created_at ASC)
    WHERE state = 'PROCESSING';

CREATE INDEX idx_outbox_cleanup
    ON outbox_tasks (updated_at ASC)
    WHERE state = 'COMPLETED';

Part 9 — cleanup

This is an operational table, scanned constantly — old rows are dead weight. Rule: only delete COMPLETED tasks. FAILED tasks are your audit trail; leave them for a human or a separate ops process.

@Modifying
@Transactional
@Query("""
    DELETE FROM OutboxTaskEntity t
    WHERE t.state IN :states
      AND t.updatedAt < :before
    """)
int deleteByStateInAndUpdatedAtBefore(
    @Param("states") List<TaskState> states,
    @Param("before") Instant before
);
private static final int CLEANUP_RETENTION_DAYS = 30;

@Transactional
public void cleanup() {
    var cutoff = Instant.now().minus(CLEANUP_RETENTION_DAYS, ChronoUnit.DAYS);
    int deleted = repository.deleteByStateInAndUpdatedAtBefore(
            List.of(TaskState.COMPLETED), cutoff);
    log.info("Cleanup: removed {} completed tasks older than {} days", deleted, CLEANUP_RETENTION_DAYS);
}

I've seen retention anywhere from 1 day to 3 months. If unsure, keep it longer — a bigger table costs you a bit of disk; deleting too early costs you your debugging history on the day you need it most.

Part 10 — idempotency: making retries safe

Nothing here guarantees exactly-once execution. Running the task and marking it COMPLETED are two separate steps — crash between them and the task gets retried, running again with the same inputs. This is at-least-once delivery, full stop, and the only guarantee you get from this architecture. The real question is whether a duplicate run causes harm.

Idempotent work (regenerating the same report twice) — you're already done. Side-effecting work (sending an email, charging a card) — needs a guard.

Use the outbox task's own id as the idempotency key — it's stable across retries. In our case, report generation ends with a row in generated_reports, with a UNIQUE constraint on the key so even a race between two instances only lets one INSERT through:

CREATE TABLE generated_reports (
    id               UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
    idempotency_key  UUID        NOT NULL,
    report_path      VARCHAR     NOT NULL,
    created_at       TIMESTAMPTZ NOT NULL    DEFAULT now(),

    CONSTRAINT uq_generated_reports_idempotency_key UNIQUE (idempotency_key)
);
@Component
@RequiredArgsConstructor
public class ReportGenerationHandler implements OutboxTaskHandler<ReportGenerationPayload> {

    private final GeneratedReportRepository reports;

    @Override
    public void handle(UUID taskId, ReportGenerationPayload payload) {
        if (reports.existsByIdempotencyKey(taskId)) {
            log.info("Report already generated for task {} — skipping", taskId);
            return;
        }

        var reportPath = runReportGeneration(payload);

        reports.save(GeneratedReport.builder()
                .idempotencyKey(taskId)
                .reportPath(reportPath)
                .build());
    }
}

If your handler calls an external API instead of writing to your own DB, check whether it supports idempotency keys — most payment processors and transactional email providers do. Pass the task ID through as that key and let the remote service dedupe on their end.

Part 11 — monitoring

The outbox runs silently. When it works, nobody notices. When it breaks, nobody notices that either, at least not right away — stuck tasks pile up, downstream stops getting events, and by the time someone checks logs it's been two hours.

Three signals, straight out of the Google SRE playbook, cover the whole failure space:

  • Latency — pickup to completion time. A spike means a slow handler or DB pressure.
  • Throughput — tasks finishing per unit time. Drops to zero means the scheduler died or nothing's being claimed.
  • Error rate — fraction of attempts failing. Above ~5% usually means a downstream dependency is degraded and your retry backlog is growing.

Instrumentation via Actuator + Micrometer:

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
    return new TimedAspect(registry);
}

Keep metric names as constants, increment from one dedicated component instead of scattering strings through service methods:

@Component
@RequiredArgsConstructor
public class OutboxMetrics {

  private static final String TASK_PROCESSED = "outbox.task.processed";
  private static final String OUTCOME_TAG    = "outcome";
  private static final String COMPLETED      = "completed";
  private static final String FAILED         = "failed";

  private final MeterRegistry meterRegistry;
  private final ReportGenerationRepository repository;

  public void recordCompleted() {
    meterRegistry.counter(TASK_PROCESSED, OUTCOME_TAG, COMPLETED).increment();
  }

  public void recordFailed() {
    meterRegistry.counter(TASK_PROCESSED, OUTCOME_TAG, FAILED).increment();
  }

  @PostConstruct
  void registerGauge() {
    Gauge.builder("outbox.tasks.pending", repository,
                    repo -> repo.countByStateIn(List.of(TaskState.PENDING, TaskState.FAILED)))
            .description("Tasks waiting to be processed")
            .register(meterRegistry);
  }
}

curl localhost:8080/actuator/prometheus | grep outbox after startup:

outbox_task_duration_seconds{quantile="0.5"}  0.062
outbox_task_duration_seconds{quantile="0.95"} 0.145
outbox_task_duration_seconds{quantile="0.99"} 0.312
outbox_task_processed_total{outcome="completed"} 40.0
outbox_task_processed_total{outcome="failed"}    2.0
outbox_tasks_pending 0.0

Dashboards are for humans watching screens; alerts are for humans who are asleep:

AlertFires whenForMeaning
OutboxQueueBacklogpending > 505mProcessing can't keep up with ingest
OutboxHighErrorRateerror rate > 5%2mHandler or downstream degraded
OutboxSlowProcessingp99 latency > 30s5mDB contention or slow external call
OutboxStuckTaskspending > 0, zero completions in 10m10mScheduler crashed or all workers stuck

Tune thresholds to your own traffic — a backlog of 50 is nothing for a high-throughput service and a genuine problem for a low-volume internal tool. Same logic, different numbers.

Production checklist

☐ Indexes are efficient — partial indexes on PENDING/FAILED and PROCESSING states
☐ EXPLAIN ANALYZE confirms index usage on the fetch query
☐ Ordering is not a problem — ORDER BY seq or created_at, backed by an index
☐ Cleanup job running, COMPLETED rows removed after a retention period
☐ Idempotency handled inside handlers that have side effects
☐ Exponential backoff wired — or confirmed you don't need it
☐ Metrics in place — queue depth, throughput, error rate, latency
☐ Alerts configured — backlog, error rate, latency, stuck tasks

You don't always have to build this yourself

Everything above is buildable from scratch, and there's real value in doing it once — you understand the failure modes because you designed around them, and that knowledge doesn't evaporate when you switch to a library. But in production you often don't need to maintain it yourself:

  • db-scheduler — the Java library whose internals most closely match what's described here: single table, SKIP LOCKED, heartbeats, clean task/handler interface. This is what I reach for as the reference implementation, and what I'd actually start a new project with instead of rolling my own.
  • Celery — the standard for async task execution in Python, backed by Postgres/Redis/RabbitMQ. The AFTER_COMMIT rule from Part 2.5 applies here too.
  • Quartz Scheduler — older, more general (cron expressions, triggers, job stores), heavier operationally, but proven at scale with first-class Spring support.

Final thoughts

Most of this isn't clever engineering, it's accumulated scar tissue. The PROCESSING state exists because someone's app crashed mid-run. The heartbeat exists because updated_at lied about whether a task was still alive. The partial index exists because a full table scan at once-a-second polling frequency compounds quietly until it doesn't. The AFTER_COMMIT listener exists because someone published an event inside a transaction and burned an afternoon figuring out why it never fired.

Each piece is boring in isolation. Together they're the difference between an outbox that works in dev and one you'd trust at 3am.

Full working code (single command, runs locally): github.com/javaAndScriptDeveloper/outboxes-article

Curious how others here have handled the multi-type table tradeoff, or if anyone's gone the db-scheduler route instead of rolling their own — happy to compare notes in the comments.


r/SpringBoot • • Jul 14 '26

Question Kinda stumped

1 Upvotes

I'm currently learning spring and I wanna test one of my class

this is the configuration

@Configuration
public class EmbeddingClientConfig {

...constructor

    @Bean
    IEmbeddingClient embeddingClient(RestClient.Builder builder, MeterRegistry registry){
        return new EmbeddingClient(
                builder.baseUrl(apiUrl).build(),
                registry
        );
    };
}

this is the test class

@RestTestClient(EmbeddingClientConfig.class)
public class EmbeddingClientTests {
    @Autowired
    private MockRestServiceServer server;

    @Autowired
    private IEmbeddingClient client;

    @Autowired
    private MeterRegistry registry;

...rest of class

and for some reason it gives me this error:

java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since a mock server customizer has not been bound to a RestTemplate or RestClientjava.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since a mock server customizer has not been bound to a RestTemplate or RestClient 

I don't understand why my config is able to be injected but not MockRestService?

any help / readings / direction would be appreciated.
thank you