Spring Boot Done Right: Lessons from a 400-Module Codebase

(medium.com)

71 points | by dknj 4 days ago ago

56 comments

  • EricRiese 2 minutes ago

    I wonder how many of these rules can be enforced with static analysis and ArchUnit.

  • e7h4nz 3 hours ago

    I worked on a core Spring Boot project for five or six years at a very large enterprise. In my opinion, the most dangerous thing about this framework is that it makes its core users feel far too self-assured.

    When looking at problems, your mind becomes consumed with how to force everything into design patterns—like architectural separation, DI, or interface / implementation split. This causes developers to lose sight of the actual essence of the problem because they are obsessed with conforming to the framework.

    Because the ecosystem and toolchain surrounding Spring Boot and Java are so mature and well-supported, it is very easy to find community tools that make you feel like you are doing things the "right way."

    I only realized these issues after I left Spring Boot and Java development behind. Now, I much prefer using TypeScript or Python to write code (for example, web servers).

    I also prefer using various SaaS solutions to handle authentication and user registration rather than rebuilding it all myself with Spring Boot Security. I honestly never want to go back to the days of writing Java again.

    • antonymoose 2 hours ago

      Funny how that works - I started with Spring both before and after Boot as well as some Scala.

      I wouldn’t trade Java / Spring for the world. It forces a coherence, perhaps not perfectly, but it provides guidelines to follow.

      I’ve been stuck in Node / TypeScript / Microservice hell for most of the last 15 years and I couldn’t hate it more. The open-source ecosystem is extremely lacking, even now, on something like a good XML library. I think we’ve got four different ones installed in our core platform each handling different needs (traversal, generation, manipulation, validation).

      On top of it all, since there isn’t “one way” we’ve got several dozen micro services. There isn’t a single bit of consistency other than “Express.” Logging, authentication, routing, validation - it’s all completely up in the air. It robs us all of efficiency from the lack of common language and pattern. We spend more time refreshing ourselves on any given project than doing feature work or bug fixes.

      Madness!

      • ekropotin an hour ago

        I’ve Been stuck with Spring for many years too and I think it’s just awful choice for micro services that need to scale, simply due to startup time.

        Not to defend Nodejs ecosystem - your points are 100% valid.

        With that in mind, I think Go is much better option.

        • ivan_gammel 23 minutes ago

          Spring may be a bad choice for microservices (context required, because it is not true in general), but it is strange to compare it (DI framework/mvc/orm abstraction etc) with Go (programming language). Java exists in many flavors, you do not have to use Spring to build a performant microservice.

          • ekropotin 8 minutes ago

            Fair enough! Indeed, there are ways to build performant microservices on Java, e.g. Qwarkus + GraalVM, but I've never seen anyone in enterprise world is doing it - 99% of services are still being build on Spring Boot, so Java is my mind Java in SB are inseparable.

      • e7h4nz 2 hours ago

        Removed microservices and RPC, keep all TypeScript codes in a single monorepo. Avoiding `any` and using ts-rest automatically keep types synchronized between the web and frontend applications.

        This has made my life much easier.

      • BariumBlue an hour ago

        I hear this a lot from a lot of my coworkers who like Java Spring - they trust Spring to do things right, more than themselves.

        On the other hand, I hate Java Spring because I feel like I don't trust it - it doesn't let me look into and understand the internals easily, making me feel like I'm afloat on a pile of abstracts I'm not allowed to look down into.

        Looking at some other projects enterprise js/ts codebases though, I see a lot of "I don't understand how this works so I'll try random things until it works". In that kind of environment, I can understand the attraction of Spring - it's not great, but it also won't be a flaming pile of unbaked abstractions.

        • ivan_gammel 26 minutes ago

          Spring framework may look complicated only at start, until you get it, but then it becomes quite easy to reason about.

          OTOH, Spring Boot is a huge pile of various loosely coupled framework connectors, web, queues, security, databases etc. Some of them are of good quality, some are not so good. It is that uneven mix giving the perception that Spring is a mess.

        • nh23423fefe 18 minutes ago

          > it doesn't let me look into and understand the internals easily

          what does this mean? you can single step into framework code

      • reeredfdfdf an hour ago

        I agree. Whenever I've tried jumping into NodeJS world, I quickly get overwhelmed by the lack of obvious "right" solutions to common problems. There's no way to know which of the dozens of ORM's, auth libraries or whatever will be still around 10 years from now. With Java & Spring the choice is always obvious.

        Of course there are "batteries included" frameworks for NodeJS too, but they all seem to be unstable compared to Spring Boot.

    • ecshafer 3 hours ago

      I like Java fine. I would probably prefer Ruby, Rust or LISP given the chance. But I can't disagree with anything you say. So many Java enterprise shops have absurd inheritance and "design pattern" abuse that makes it harder to actually work with the code, and slows things down.

    • chasd00 2 hours ago

      > I also prefer using various SaaS solutions to handle authentication and user registration rather than rebuilding it all myself with Spring Boot Security.

      i haven't used Spring in a decade but even back then Spring Security had support for all kinds of federated authentication/authorization providers and standards.

    • cogman10 2 hours ago

      Spring is such a massive framework and it definitely has parts that aren't, frankly, well done. More than once Spring has ultimately been the source of performance issues for us with the solution being to ditch spring and do things with just the JDK. A lot of those cases have been fixed, however, even in the best case spring will take something that could have been a single function call and turn it into a maze of 20 different function calls because it's handling all sorts of weird edge and corner cases that aren't applicable to your code. It also doesn't help that some things are simply bolted in which makes it even harder to understand what is going on under the covers.

      It also doesn't help that it's really easy to end up bringing in the entire framework for what you assumed were single function portions of the framework.

      That said, it's the framework you probably want to use if you are doing Java. Just not if you are doing fast java.

    • smrtinsert 2 hours ago

      Nothing about Spring forces you to use or engage in or use design patterns. If it does im pretty sure that's a plus. Juniors can have clear path towards being productive and feel quickly oriented.

      Like sibling comment mentioned you can integrate anything with Spring Boot. Its a high productivity framework to accomplish alot. Your frustrations speak more to the job you took rather than the framework they forced you to use.

  • stackskipton 3 hours ago

    Ops person here who has supported Java/SpringBoot applications. I think most of dislike of Java apps comes not from language or framework BUT from fact that most Java using workspaces are filled with mediocracy. They tend to be businesses with products that have extreme moats and thus quality of software barely matters. I imagine most people who would even read this medium article are dreaming of better than that.

    • evantbyrne an hour ago

      There are certain non-software industries that seem to have strong hiring preferences for workers who started in the industry over computer science chops, and a lot of those tend to use Java. For example, biotechs.

      • stackskipton 31 minutes ago

        Because most of time, understanding the business logic is harder than writing to the code in these industries.

        Java is default since it's what taught to many college graduates with added bonus that's taught to most college grads in Indian subcontinent so outsourcing is much easier.

    • ecshafer 3 hours ago

      I 100% agree. I have seen enterprise spring applications that throw away all of the speed through huge amounts of hot path object creation, nested loops, absurd amounts of factories, etc. After going through enough AbstractFactoryFactory calls to make object in an n^3 loop, the framework doesn't matter.

      • switchbak 3 hours ago

        There really is an unlimited potential for crappy code in the enterprise. I imagine the volume will increase drastically in the age of AI.

        • the_arun 2 hours ago

          Usually languages are not the issue. It is the code that we write. As long as languages help us to find/debug a problem caused by crappy code - we should be good. Coding is kinda creative work. There is no standard to measure creativity or pitfalls of using wrong patterns. The incidents & RCAs usually find these. But most of the times it is already too late to fix core problem.

    • dukeyukey 2 hours ago

      Alternatively, I think of Java and Spring Boots as being incredibly valuable by letting companies that don't really have software as a core competency to make reasonably performant and structured applications.

      Mediocrity will never not exist, and you unlock a lot of value by optimising for it.

      • ivan_gammel 20 minutes ago

        Exactly. It allows to build boring stuff at a fraction of what a SV startup would burn using some fancier tech. And most problems in the world are boring stuff.

  • robmccoll 4 hours ago

    Does this app take 5 minutes to start? That's so much dynamic Spring magic. Also, how do you keep track of control flow when anything at anytime could have been overridden by something else? It seems like tracing and debugging this thing would be like exploring someone else's codebase every time.

    • krzyk 43 minutes ago

      Who cares about startup times if apps is started only when new version is deployed?

      And 5 minutes is a bit long, maybe someone is doing some database operations during start?

    • switchbak 3 hours ago

      The class loading magic means you need to be exceptionally careful about things that would otherwise be very innocuous. It’s the rule, not the exception - that your average spring boot app will be doing tons of expensive stuff at startup. Most of which is unnecessary and was not even intended.

      The JVM doesn’t need this kind of thing either, and it gets a bad wrap from the J(2)EE days, and the “simple” replacement that Spring was supposed to be.

      No doubt there’s some benefits to be had, but I don’t think the trade-offs are worth it, especially at larger scales.

    • guzfip 15 minutes ago

      > It seems like tracing and debugging this thing would be like exploring someone else's codebase every time.

      Oh man, just wait till you start sprinkling some AOP in there. My debugger is damn near useless sometimes as I try to follow bizarre paths between parts of my code.

      • robmccoll 10 minutes ago

        Totally! But it doesn't have to be this way.

    • smrtinsert 2 hours ago

      The article talks about a specific solution they came up with. No way is this a representative example, they discard a typical pattern way at the top.

  • moondowner 2 hours ago

    Switched from Spring Boot to Ruby on Rails 3 years ago.

    I expected to see some new Spring 'tricks' in this post, but it's pretty much regular things that people might do in larger codebases.

  • sidcool 4 hours ago

    Hacker news likes to dunk on Spring Boot, but its usage in enterprises is very very high.

    • dzonga 2 hours ago

      it doesn't mean that usage of something being very high is a clever idea.

      lots of people take meth - so are you gonna take meth too.

      ```@EnableConfigurationProperties(CasConfigurationProperties.class) @EnableScheduling @ConditionalOnFeatureEnabled(feature = CasFeatureModule.FeatureCatalog.SimpleMFA) @AutoConfiguration @Import({ CasSimpleMultifactorAuthenticationComponentSerializationConfiguration.class, CasSimpleMultifactorAuthenticationConfiguration.class, CasSimpleMultifactorAuthenticationEventExecutionPlanConfiguration.class, CasSimpleMultifactorAuthenticationMultifactorProviderBypassConfiguration.class, CasSimpleMultifactorAuthenticationRestConfiguration.class, CasSimpleMultifactorAuthenticationTicketCatalogConfiguration.class, CasSimpleMultifactorAuthenticationWebflowConfiguration.class }) public class CasSimpleMultifactorAuthenticationAutoConfiguration { }```

      this is what we mean by "JavaCulture" - java | JVM really wonderful things but the culture is what sets these things back

      someone can't just go in and understand things - they have to understand a bunch of notations too.

      then if you work at a place and try do things differently the same people will tell you to revise your PR cz it ain't software engineering (or it won't scale)

      • theflyinghorse 17 minutes ago

        This is still relatively tame! I've seen stuff where there are layers of annotations some of which will spawn docker containers effectively bringing docker compose into java application as part of the set up. Some containers would need to come up, do a quick file change, then die and the the rest of configuration proceeded (this was for an automated test tool). Entirely stupid, could have been written with just couple classes of Java and a docker compose. But it is a culture. It is a culture that I think ultimately will destroy Java.

      • mindcrime 2 hours ago

        That's a ridiculously extreme example, IME. Having been working with Java for 25+ years, I've never seen anything like that in code written by a shop I worked at, FWIW. And as a corollary I'm about 100% certain that you could find some equivalent extreme case to use as an example to dunk on any language/platform.

        this is what we mean by "JavaCulture" - java | JVM really wonderful things but the culture is what sets these things back

        Maybe there are still some Java shops out there living like this. But to me, this is a 20 year old outdated notion. shrug

        • theflyinghorse 22 minutes ago

          You have worked at some outstanding java shops then! In my experience the article is precisely representative of Java culture even today

      • _ZeD_ 42 minutes ago

        dude, that is a configuration.

        it is completely declarative and lives in the best place, near your code, not on some other inaccessible repository.

        what have you preferred? a json file? a yaml one?

        I had them, and I find the annotations are more readable than other solutions.

    • geodel 3 hours ago

      Huh, Enterprise usage of Blackberry was very very high and then it was not. And at one point SOA, SOAP/WSDL/XML usage was very very high and now I am told in my very enterprise job I'd be fired if I dared bring those names up.

      Usage being high doesn't say anything about quality or suitability of a product specially in enterprise settings.

    • the_gipsy 2 hours ago

      Cuisine Magazine likes to dunk on McDonalds, but its the most popular choice of dining.

    • dukeyukey 2 hours ago

      Even in startups, lots of places are on Spring Boot (albeit more often using Kotlin over Java).

      • bberrry 2 hours ago

        My favorite stack, Kotlin with Spring Boot. A real delight to work with

    • tonyedgecombe 3 hours ago

      Yes but enterprise software is some of the worst software you will encounter.

      • sidcool 3 hours ago

        Not Spring Boot's fault :)

        • __alexs 3 hours ago

          Not a strong indicator that Enterprises have good taste though is it?

    • nilamo 3 hours ago

      I'm not that deep into Java, but I was under the impression that things like Quarkus were starting to replace Spring in enterprise use...

      • theflyinghorse 15 minutes ago

        It will take some time for Quarkus to become a significant enough presence in the market share. In my experience, for the majority of Java shops Spring Boot does just fine because it worked before and it will probably work in the future.

    • fiftyacorn 4 hours ago

      it saves a lot of reinventing the wheel

      • rockyj 3 hours ago

        And at the same time, gives you a dozen of footguns. This is just a list for the gotchas in the "@Transactional" annotation - https://dev.to/closeup1202/8-spring-transactional-pitfalls-t...

        Now read up on all the dozen of annotations. But yeah, we did not want to "re-invent the wheel".

        • fiftyacorn 3 hours ago

          Im comparing against node equivalent ORMs and find spring consistently better. Yeah ive got to read up on annotations - but when it comes to transactions its always worth revisiting them to check for changes

    • smrtinsert 2 hours ago

      And startups not in the SV echo chamber. They happily exist and make money without worrying about supply chain attacks and js engine of the week.

  • jillesvangurp an hour ago

    - Swap out Java for Kotlin. The Spring guys won't officially drop support for Java; but a lot of their recent releases are becoming very Kotlin centered. Seriously, it's much nicer to use from Kotlin. I've done both.

    - Go for declarative Kotlin DSLs over annotation magic for most things. Much easier to debug. It's just function calls. And DSLs are nice in IDEs with autocomplete.

    - Keep it simple. It's a huge framework. But you probably don't need most of it.

    - Don't go Spring everything, a lot of stuff in Spring is a bit experimental (Spring AI/MCP stuff) or a bit bare bones (Spring Data, it's a limited and extra level of indirection you mostly shouldn't need) or flat out misguided/over-engineered (Spring Batch, good alternatives are available for that).

    - Decide on synchronous or asynchronous IO. The former is a lot more scalable now that Java has green threads. The latter is relatively painless from Kotlin but an absolute PITA from Java. They are very different internally and both have their pros/cons. If you need async, Kotlin co-routines is the easier path to do that. Either way, it's one of the bigger decisions to take.

    - Don't copy their way of deeply nested inheritance hierarchies. Very much in fashion 20 years ago; a bit of an anti pattern now. Internal code complexity is the part I like least about Spring. And it has some really byzantine stuff in there with 5-6 levels of inheritance or worse.

  • exabrial an hour ago

    > What makes the CAS codebase impressive is the discipline of applying all of them consistently, across 400 modules, for years.

    I think this is the main point. You can write good code in any language following this concept.

  • fpauser an hour ago

    Bodyless classes only for carrying annotations... too much magic for my taste.

  • faangguyindia 4 hours ago

    I recently inherited java code base.

    Just rewrote it in Go. Now we are using a server which consumes 30% of ram what the existing one used to and the latency and throughput have all improved.

    Don't use these stupid java backend like sprinboot.

    • ludovicianul 3 hours ago

      I wouldn’t reduce it to don't use Java/Spring Boot. Rewrites often (not always) look great because they remove years of accumulated complexity, not because the original stack was inherently bad.

      Just rewrite it in X doesn't "just work" for complex systems. It ignores risk, and the fact that design usually matters more than language.

      • faangguyindia 2 hours ago

        i made 1 to 1 copy, not sliming anything.

      • iririririr 2 hours ago

        usually what you say is correct. but in case of entreprise java, just look at the article. you have no way of knowing or controlling which essential class your application will be running because some intern merged a similarly named class two hundred levels down in the codebase and now that is handling all your db queries. and it's not a bug but a highly praised feature.

        it's useless abstractions for the sake of useless abstractions.

        java was designed so that american architecs could write a few interfaces and cheap workforce overseas could implement the actual code.

        and the EE stuff evolved in a way that features could be shipped just by adding a new component that would inject itself in the right places. java "engineers" have no idea how http or cookies work, but they know where to load the spring-auth bean in a 2mb maven config.

        so, any rewrite from java is an exception to the rewrites are only good because you cleaned up old features rule.