DEV Community

Cover image for The JVM doesn't care about your Business Model (How I fixed my crashing startup)
Aswin A
Aswin A

Posted on

The JVM doesn't care about your Business Model (How I fixed my crashing startup)

Ash was staring at the AWS console. His startup's dashboard was red. Again.

"I don't get it," he muttered, rubbing his eyes. It was 3:00 AM. "I have customers signing up. The marketing is working. Why does the server crash every time more than 10 people use it?"

The Architect (a shadowy figure representing his experience/conscience) leaned against the standing desk.

"Because you built it to work for you, Aswin. Not for them."

The Bottle-Neck (The Connection Pool)

"It's a Spring Boot app," Aswin defended. "It's supposed to handle traffic."

"The app handles traffic fine," The Architect said, drawing a funnel on the whiteboard. "But your Database is choking."

"I increased the max connections!" Aswin argued. "I set spring.datasource.hikari.maximum-pool-size=100."

"A classic mistake," The Architect sighed. "Imagine you own a small coffee shop. You are the only barista."

The Metaphor

"If 100 people rush in," The Architect continued, "and you try to take 100 orders at once, what happens?"

"I'd freeze. Overwhelmed."

"Exactly. That is your Database. But instead of saying 'Shop is Full', you bought 100 coffee machines (Connections) but you still only have one barista (CPU/Disk I/O). Now the barista spends all their time switching between machines, not making coffee. The CPU is thrashing."

The Leak

The Architect pointed to a line of code on Aswin's screen.

public UserDto getUserStats(Long userId) {
    Connection conn = dataSource.getConnection();
    // complex logic...
    return stats;
}
Enter fullscreen mode Exit fullscreen mode

"Where do you close the connection?" The Architect asked.

"Garbage collection handles it, right?" Aswin asked, hopefully.

"Wrong," The Architect said sharply. "Database connections are expensive physical resources. TCP sockets. Objects. Handles. If you don't return them to the pool explicitly, they stay 'Checked Out'. "

"So... if I have a pool of 10," Aswin realized, "and I run this function 10 times..."

"The 11th customer waits," The Architect finished. "Forever. Until the request times out. That is why your dashboard is red. You aren't running out of code. You are running out of 'seats' at the database table."

The Fix

"I need to rewrite the whole backend?" Aswin panicked. "I don't have time! I have investor meetings!"

"No. You need discipline," The Architect said.

She erased the bad code and wrote:

// The "try-with-resources" pattern
public UserDto getUserStats(Long userId) {
    try (Connection conn = dataSource.getConnection()) {
        // use the connection
        return logic(conn);
    } // <--- AUTOMATICALLY CLOSES HERE
}
Enter fullscreen mode Exit fullscreen mode

"Or better yet," she added, "Stop managing connections manually. Use JPA or JdbcTemplate. Let the framework handle the plumbing so you can focus on the business."

The Conclusion

Aswin deployed the fix. The graph on the dashboard turned green. The CPU usage dropped from 99% to 5%.

"The code isn't just logic," The Architect said, fading away as the sun came up. "It's resource management. Just like your startup. You have limited time, limited energy, and limited connections. Don't waste them."

Lesson Learned:

  1. Always close your resources. (Use try-with-resources).
  2. More connections != Better performance. (Configure HikariCP correctly).
  3. Don't reinvent the wheel. (Use Framework abstractions when you are a solo dev).

Top comments (0)