Ports
A port is a logical endpoint for network communication. Every network service listens on a specific port.
How Ports Work
IP Address: 192.168.1.1
Port: 8080
Combined: 192.168.1.1:8080
Think of it as:
IP Address = Building address
Port = Apartment number
Port Number Ranges
| Range | Name | Usage |
|---|---|---|
| 0-1023 | Well-Known | System services (HTTP=80, HTTPS=443, SSH=22) |
| 1024-49151 | Registered | Applications (MySQL=3306, PostgreSQL=5432) |
| 49152-65535 | Dynamic/Ephemeral | Client-side temporary ports |
Common Backend Ports
| Port | Service |
|---|---|
| 80 | HTTP |
| 443 | HTTPS |
| 22 | SSH |
| 3306 | MySQL |
| 5432 | PostgreSQL |
| 6379 | Redis |
| 8080 | HTTP Alt (common for dev servers) |
| 8443 | HTTPS Alt |
| 27017 | MongoDB |
Port Binding
When a backend application starts, it binds to a port:
# application.yml
server:
port: 8080
The application listens for incoming connections on that port:
Client --> 192.168.1.1:8080 --> Your Application
Port Conflicts
Only one process can bind to a port at a time:
$ java -jar app.jar
Error: Address already in use: bind
# Another process is already using port 8080
Why Ports Matter for Backend
- Each service needs its own port
- Firewalls control port access
- Load balancers route to specific ports
- Docker containers expose specific ports
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding Ports is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Ports in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Ports implementation
// Key aspects: validation, error handling, logging, testing
public class Ports {
// Production-ready implementation
}Identify and handle edge cases for Ports. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Ports. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. Which port is commonly used for HTTPS?
2. Can two processes listen on the same port?
3. What is the primary purpose of Ports?
4. What is a common mistake when implementing Ports?
Flashcards
Question
What is a port?
Click to reveal answer
Answer
A logical endpoint for network communication
Question
Common backend ports?
Click to reveal answer
Answer
80 (HTTP), 443 (HTTPS), 8080 (dev), 3306 (MySQL), 5432 (PostgreSQL)
Question
What is Ports?
Click to reveal answer
Answer
Ports is a key concept in backend development.
Question
When to use Ports?
Click to reveal answer
Answer
Use Ports when building production systems that require reliability, scalability, and maintainability.
Question
Ports best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Port = logical network endpoint
- 2.Well-known ports: 0-1023 (HTTP=80, HTTPS=443)
- 3.Only one process can bind to a port
- 4.Each backend service needs its own port
Interview Tips
- •Know common port assignments
- •Understand port binding and conflicts
Cheat Sheet
Ports
- HTTP: 80, HTTPS: 443
- Dev: 8080, MySQL: 3306, PostgreSQL: 5432, Redis: 6379
- Rule: One process per port