Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you scale a Spring Boot application?

Tags:

I understand that Spring Boot has a built-in Tomcat server (or Jetty) which facilitates rapid development. But what do you do when you need to scale out your application because traffic has increased?

like image 438
progonkpa Avatar asked Aug 24 '16 17:08

progonkpa


1 Answers

As pointed out in the comments, there is no silver bullet here, it depends on your infrastructure and there are several tools out there to help you, you only need to choose what works best for you.

For load balancing you can either choose something like an Nginx or leave it to spring cloud which also has a lot of other handy features for scaling/clustering. Scaling shouldn't be very hard because spring boot runs on it's own server.

Some tools that help with scaling/clustering:

Spring boot app:

  • If you are going to scale, your app has to be near-stateless (e.g: you cannot have a scheduled task or something like that because when you scale to x instances, they are executed x times).

  • You can use the spring cloud project for extra added features like service discovery and other goodies that make scaling easier (e.g: When you spin up a new instance, it can get the config easily from a config server, 'register' to ease the loadbalancing between services, have cluster-like behaviour, etc...).

Infrastructure and containers:

  • Docker is a no-brainer here to handle easy launching of your applications and their replicas, if needed. If you can go further with resources and go with Kubernetes but it all depends on the use case.

  • Various servers (nodes), in case one of them fails and to easily distribute loads.

  • Ngnix for load balancing is pretty straightforward if you already don't have something done with spring cloud.

Database:

  • You really do NOT want to go with MySQL here because it can not scale well as your spring apps. You can choose something like Cassandra or Redis but that would mean restructuring your data model. Maybe the least-painful transition from MySQL to something NoSQL that can scale is a MongoDB (imho: Cassandra performs better).

Logging:

  • This can be a nightmare but spring also has a solution for this. Check out zipkin and spring sleuth.

Also, there are a lot resources here that talk a lot about architecture in general and how it is necessary to change the mindset when trying to run distributed services.

Hope this helps.

Update 2021-02-23

  • Today, Kubernetes is pretty much a de-facto standard when we talk about scaling and is preferred because of the rich set of features that you will be able to leverage and focus your app purely on business domain logic and can remove things like spring cloud for service discovery. If you can use some public clouds like EKS and GKE, you are better off without having to manage the clusters by yourself. It provides autoscaling and built-in healthchecks. Starting from Spring Boot 2.4, you have many added benefits for running Spring Boot on K8s like dedicated healthcheck endpoints for liveness and readiness probes, graceful shutdown, etc....

  • On the database side, aim for something that is managed and scales easily such as AWS Aurora or similar.

  • An important thing to mention when managing spring boot services at scale is probably configuration management. A very useful solution that you can use out of the box is Consul. This will enable you to hot reload the configuration which is important when you have 50 services that you need to restart only to change one boolean variable. Depending on how big is your application, the startup can be costly, in terms of time as well as CPU/memory resources

like image 199
Urosh T. Avatar answered Sep 19 '22 06:09

Urosh T.