0
0
Spring Bootframework~15 mins

Embedded server concept in Spring Boot - Deep Dive

Choose your learning style9 modes available
Overview - Embedded server concept
What is it?
An embedded server is a web server that runs inside your application instead of separately. In Spring Boot, this means your app includes a server like Tomcat or Jetty inside its own package. When you run your app, the server starts automatically, so you don't need to install or manage a server separately. This makes deploying and running web apps simpler and faster.
Why it matters
Without embedded servers, developers had to install and configure web servers separately, which was complex and error-prone. Embedded servers let you package your app and server together, making it easy to run anywhere with just one command. This reduces setup time, avoids version conflicts, and helps deliver apps quickly and reliably.
Where it fits
Before learning embedded servers, you should understand basic web servers and how web applications work. After this, you can learn about containerization and cloud deployment, where embedded servers simplify running apps in containers or cloud platforms.
Mental Model
Core Idea
An embedded server is like a mini web server built right into your application, so your app can run by itself without needing extra setup.
Think of it like...
Imagine a coffee machine that comes with its own built-in grinder and water heater, so you just press start and get coffee without needing extra devices. An embedded server is like that: everything needed to serve web pages is inside your app.
┌───────────────────────────────┐
│        Your Application        │
│  ┌─────────────────────────┐  │
│  │   Embedded Web Server    │  │
│  │  (Tomcat, Jetty, etc.)  │  │
│  └─────────────────────────┘  │
└───────────────────────────────┘

Run app → Server starts inside → App serves web requests
Build-Up - 7 Steps
1
FoundationWhat is a web server?
🤔
Concept: Introduce the basic idea of a web server as software that listens for web requests and sends responses.
A web server is a program that waits for people to ask for web pages or data. When someone visits a website, their browser sends a request to the server. The server then sends back the page or data they asked for. Examples include Apache, Nginx, and Tomcat.
Result
You understand that a web server is needed to make websites work by handling requests and responses.
Knowing what a web server does helps you see why embedding one inside your app changes how you run web applications.
2
FoundationTraditional server setup explained
🤔
Concept: Explain how web servers and applications were separate before embedded servers.
Traditionally, you install a web server like Tomcat on a machine. Then you deploy your web app to that server. The server runs independently and manages your app. This means you must configure the server, start it, and deploy your app separately.
Result
You see the extra steps and complexity involved in running web apps with separate servers.
Understanding this separation shows why embedding the server inside the app simplifies deployment.
3
IntermediateEmbedded server basics in Spring Boot
🤔
Concept: Introduce how Spring Boot packages an embedded server inside the app.
Spring Boot includes a server like Tomcat or Jetty inside your application package (a JAR file). When you run the JAR, the embedded server starts automatically. This means you don't need to install or start a server separately. Your app is self-contained and ready to serve web requests.
Result
You can run your Spring Boot app with a simple command and it will serve web pages immediately.
Knowing that the server is inside the app helps you understand why deployment is easier and more portable.
4
IntermediateHow Spring Boot chooses the embedded server
🤔Before reading on: do you think Spring Boot uses only one server type or can it switch between servers? Commit to your answer.
Concept: Explain that Spring Boot supports multiple embedded servers and picks one by default, but you can change it.
By default, Spring Boot uses Tomcat as the embedded server. However, you can switch to Jetty or Undertow by changing dependencies in your project. This flexibility lets you choose the server that fits your needs best without changing your code.
Result
You understand that embedded servers are configurable and not fixed, giving you control over your app's behavior.
Knowing this flexibility helps you customize your app's performance and features by selecting different embedded servers.
5
IntermediateRunning and packaging embedded server apps
🤔
Concept: Show how to build and run a Spring Boot app with embedded server.
When you build your Spring Boot app, it creates a single executable JAR file containing your code and the embedded server. You run it with 'java -jar yourapp.jar'. The embedded server starts inside the app, listens on a port (default 8080), and serves requests.
Result
You can deploy your app anywhere with Java installed, no extra server setup needed.
Understanding this packaging and running process shows how embedded servers simplify deployment pipelines.
6
AdvancedCustomizing embedded server settings
🤔Before reading on: do you think you can change the embedded server's port and settings easily? Commit to your answer.
Concept: Explain how to configure embedded server properties in Spring Boot.
You can customize the embedded server by setting properties in application.properties or application.yml files. For example, you can change the port with 'server.port=9090' or configure SSL, session timeout, and more. This lets you tailor the server behavior without extra code.
Result
Your app runs with the embedded server configured exactly as you want.
Knowing how to customize embedded servers lets you adapt your app to different environments and requirements.
7
ExpertEmbedded server lifecycle and internals
🤔Before reading on: do you think the embedded server runs as a separate process or inside the app's process? Commit to your answer.
Concept: Reveal how the embedded server runs inside the same process as the app and how Spring Boot manages its lifecycle.
The embedded server runs inside the same Java process as your application. Spring Boot starts the server during app startup by creating and configuring server objects. It manages server threads to handle requests concurrently. When the app stops, Spring Boot shuts down the server gracefully. This tight integration improves performance and simplifies resource management.
Result
You understand the deep integration between your app and the embedded server, not just a loose connection.
Understanding the shared process and lifecycle management explains why embedded servers are efficient and easy to control.
Under the Hood
Spring Boot uses a special starter dependency that includes an embedded server library (like Tomcat). When the app starts, Spring Boot creates a server instance programmatically, configures it with your app's settings, and starts listening on a network port. The server runs inside the same Java Virtual Machine (JVM) process as your app, sharing memory and threads. Requests come in, the server dispatches them to your app's controllers, and responses go back through the server to clients.
Why designed this way?
Embedded servers were designed to simplify deployment and reduce configuration errors. Before, managing separate servers was complex and slowed development. By embedding the server, Spring Boot makes apps self-contained and portable. This design trades off some flexibility of separate servers for ease of use and faster startup, which suits modern microservices and cloud environments.
┌───────────────────────────────┐
│       Spring Boot App          │
│  ┌─────────────────────────┐  │
│  │ Embedded Server (Tomcat) │  │
│  │  - Runs inside JVM       │  │
│  │  - Listens on port       │  │
│  │  - Manages threads       │  │
│  └──────────┬──────────────┘  │
│             │ Requests         │
│             ▼                  │
│  ┌─────────────────────────┐  │
│  │   Your Controllers       │  │
│  │   Handle requests        │  │
│  └─────────────────────────┘  │
└───────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does an embedded server run as a separate program from your app? Commit to yes or no.
Common Belief:An embedded server runs as a separate program or process alongside your app.
Tap to reveal reality
Reality:The embedded server runs inside the same process as your application, sharing memory and threads.
Why it matters:Thinking it runs separately can lead to confusion about resource usage and debugging, causing inefficient troubleshooting.
Quick: Do you think embedded servers require manual installation like traditional servers? Commit to yes or no.
Common Belief:You must install and configure embedded servers separately before running your app.
Tap to reveal reality
Reality:Embedded servers come packaged inside your app, so no separate installation or configuration is needed.
Why it matters:Believing otherwise wastes time on unnecessary setup and complicates deployment.
Quick: Is it true that you cannot change the embedded server type in Spring Boot? Commit to yes or no.
Common Belief:Spring Boot only supports one embedded server type and cannot switch between them.
Tap to reveal reality
Reality:Spring Boot supports multiple embedded servers like Tomcat, Jetty, and Undertow, and you can switch by changing dependencies.
Why it matters:Not knowing this limits your ability to optimize app performance or features by choosing the best server.
Quick: Does embedding the server mean you lose all server configuration options? Commit to yes or no.
Common Belief:Embedded servers have fixed settings and cannot be customized.
Tap to reveal reality
Reality:You can customize embedded server settings extensively via configuration files and code.
Why it matters:Assuming no customization leads to missed opportunities for tuning app behavior and security.
Expert Zone
1
Embedded servers share the JVM process with your app, so memory leaks or crashes in your app can bring down the server too.
2
Spring Boot's auto-configuration detects the presence of embedded server libraries and configures them automatically, reducing boilerplate code.
3
Switching embedded servers may require adjusting server-specific configurations or dependencies, even though your app code stays the same.
When NOT to use
Embedded servers are not ideal when you need a dedicated, standalone server environment with advanced management features or when integrating with legacy infrastructure. In such cases, deploying your app as a WAR file to an external server like a full Tomcat installation is better.
Production Patterns
In production, embedded servers are often used in microservices and containerized apps for fast startup and easy scaling. Teams customize server settings for performance and security, and use health checks and graceful shutdown hooks to manage server lifecycle.
Connections
Containerization (Docker)
Embedded servers simplify containerization by packaging app and server together.
Knowing embedded servers helps understand why container images can be smaller and easier to run, as they need only one executable.
Microservices architecture
Embedded servers enable self-contained microservices that run independently.
Understanding embedded servers clarifies how microservices can be deployed and scaled individually without external server dependencies.
Operating system processes
Embedded servers run inside the app's process, unlike traditional servers running as separate OS processes.
Knowing process boundaries helps in debugging, resource management, and understanding app-server interactions.
Common Pitfalls
#1Trying to run the app without the embedded server dependency included.
Wrong approach:java -jar myapp.jar # but the build excludes embedded server dependencies
Correct approach:Ensure your build includes spring-boot-starter-web or the embedded server dependency, then run: java -jar myapp.jar
Root cause:Not including the embedded server dependency means no server is inside the app, so it cannot start to serve requests.
#2Changing the server port in code but forgetting to update configuration files.
Wrong approach:In code: server.setPort(9090); but application.properties still has server.port=8080
Correct approach:Set the port consistently in application.properties with server.port=9090 or configure programmatically without conflicting settings.
Root cause:Conflicting configurations cause confusion about which port the server listens on, leading to connection errors.
#3Assuming embedded server logs appear in a separate place like external servers.
Wrong approach:Looking for server logs in external server folders when using embedded server.
Correct approach:Check your application's console output or configured log files, as embedded server logs are part of your app's logs.
Root cause:Misunderstanding embedded server integration leads to wasted time searching for logs in wrong locations.
Key Takeaways
Embedded servers bundle a web server inside your application, making it self-contained and easy to run.
Spring Boot uses embedded servers like Tomcat by default but lets you switch to others like Jetty or Undertow.
Running your app starts the embedded server automatically, removing the need for separate server setup.
You can customize embedded server settings through configuration files to fit your needs.
Understanding embedded servers helps you deploy modern web apps faster and manage them more efficiently.