Load testing: what one server handles, and what it took
escodes runs on a single 6-core server: Spring Boot, PostgreSQL, Redis, Elixir, the media worker, and Caddy, all together. I load-tested it with k6 to find out how many simultaneous users it serves, then used profilers and query statistics to address bottlenecks. Each change was made and verified one at a time using the same test. This page covers the method, the results, the fixes, and what limits it now.
In short
1,000 simultaneous users: 95% of pages within 60 ms, a typical page in 13 ms, at ~60% CPU usage.
1,500 simultaneous users: CPU saturates; the slowest 5% of pages rise to 500–900 ms, while the typical page stays near 50 ms.
Zero failed requests across all runs. Before the fixes, just 1,000 users saturated the server, with the slowest pages taking nearly 2,000 ms.
Methodology
Traffic ramped up steadily over five minutes from an independent machine in the same data center to capture performance degradation curves rather than just a single peak. Virtual users simulated realistic behavior: 90% were readers browsing timelines and threads (split between anonymous visitors and pre-seeded demo accounts), and 10% were writers publishing posts and replies.
The benchmark measured dynamic pages only; static media is served directly by Caddy from disk and was benchmarked separately.
Every comparison run started from a freshly reset database with updated table statistics. PostgreSQL’s query statistics (pg_stat_statements), recorded query plans (auto_explain), and Java Flight Recorder profiles showed where the time went in each run.
Results
95th-Percentile Response Time at 1,000 Users
Progression at 1,000 users: progressive fixes brought p95 latency down from ~1.9 seconds to under 60 ms, leaving the server well below saturation in the final run.
Test Run / Configuration
Timeline (p95)
Thread (p95)
Post (p95)
Errors
1,000 users, before fixes
1,930 ms
1,800 ms
1,730 ms
0
1,000 users, expression compiler
1,030 ms
1,050 ms
950 ms
0
1,000 users, final
58 ms
40 ms
44 ms
0
600 users, final
29 ms
19 ms
18 ms
0
1,500 users, final (saturation)
520 ms
859 ms
575 ms
0
Values represent the 95th percentile for dynamic pages only, measured from an independent load generator.
Bottlenecks and Fixes
1. The JVM was using the single-threaded Serial GC
A Java Flight Recording revealed 1,448 garbage-collection pauses in 5 minutes (totaling 15.4 seconds of stop-the-world time, with the longest pause at 393 ms). The JVM had defaulted to the single-threaded Serial collector on a 256 MB heap—behavior triggered automatically in containers allocated under ~1.8 GB of RAM.
The fix: Switching to the G1 collector with a 2 GB heap dropped total pause time to under 2 seconds, cut the longest pause to ~50 ms, and parallelized GC across all 6 cores.
2. A single query accounted for most database time
pg_stat_statements revealed that one query variant was averaging 74 ms, while all other variants took only 2–3 ms: the Popular timeline.
Its ranking algorithm scanned all posts from the past 30 days, joining tags, attachments, and mentions for every candidate before sorting and keeping just 20.
The fix: Switched to two-phase loading: sorting and paginating post IDs first, then hydrating full records only for the final 20. Caching the calculated ranking for 60 seconds for anonymous visitors removed the query from the top of the slow list and cut total database time by roughly two-thirds.
3. A third of template rendering went to evaluating expressions
Under a 1,000-user load, Java Flight Recorder profiles showed Spring Expression Language (SpEL)—which parses ${...} expressions in Thymeleaf templates—in roughly a third of all execution samples. By default, SpEL evaluates expressions via reflection on every render.
The fix: I enabled the SpEL compiler with a single configuration line, compiling frequent template expressions into direct bytecode.
A follow-up test confirmed the impact: the slowest 5% of pages became ~40% faster, overall throughput increased by 8%, and reflection calls completely disappeared from CPU profiles.
4. Two thirds of Spring’s CPU went to the template engine
Even after enabling the SpEL compiler, Thymeleaf still accounted for 64% of Spring’s CPU samples, spending most of its time walking DOM nodes and matching attributes for every post card (20 per page).
To solve this, I migrated the hot components (timelines, post cards, replies, and media embeds) to JTE, which compiles templates into plain Java bytecode at build time, while keeping outer page shells in Thymeleaf.
In the next test run, Thymeleaf’s share of CPU samples fell to 11%, Spring used roughly one-third the CPU for the same workload, and 95th-percentile response times at 1,000 users dropped from roughly 1 second to under 60 ms.
5. Postgres was burning CPU on arbitrary-precision decimal arithmetic
Query execution plans recorded with auto_explain revealed that the Popular ranking query spent 13 of its 17 ms evaluating a single expression.
In PostgreSQL 14+, EXTRACT(EPOCH FROM ...) returns a numeric rather than a floating-point number. As a result, POWER(..., 1.5) was executing as slow, software-based arbitrary-precision arithmetic for every candidate post in the 30-day window.
The fix: Adding a single cast to double precision (::float8) made the calculation 13× faster (dropping evaluation time from 9.2 ms down to 0.7 ms for 417 posts) while yielding the exact same sort order. Crucially, this prevents query execution costs from ballooning as post volume grows over time.
6. Targeted short-lived caching and asynchronous refresh
Several hot spots repeatedly recomputed identical data under load. First, Trending tags (identical for all visitors) are now computed once per minute rather than on every page load, eliminating roughly 10,000 duplicate queries during a five-minute test. Second, a reader's "For You" timeline is cached for 60 seconds instead of being re-calculated on every tab visit, reducing candidate selection queries by 60%. Finally, shared caches now refresh asynchronously in the background, eliminating the >100 ms latency spikes that previously occurred during synchronous cache misses.
GC pause time (per 5-min run)
Before15.4 s
→
After2.0 s
−87% pause time
Popular ranking math (417 posts)
Before9.19 ms
→
After0.70 ms
13× faster
Thymeleaf share of Spring CPU
Before64%
→
After11%
−83% CPU share
Timeline latency (p95 @ 1,000 users)
Before1,930 ms
→
After58 ms
33× faster
What limits it now
No single bottleneck remains. At 1,500 simultaneous users, CPU usage is distributed naturally across the stack on a 6-core machine: Spring consumes ~2.8 cores, PostgreSQL ~1.8, and Caddy ~0.5.
In the database, hydrating posts with their authors, media attachments, tags, and link preview cards represents ~85% of remaining execution time. There are no slow queries left—just the legitimate work of rendering content.
If additional headroom is ever needed, the options (ordered from cheapest to heaviest) are:
Page-level caching for anonymous traffic: Caching rendered Popular and Latest pages for 30 seconds would eliminate all database queries and template rendering for logged-out visitors.
Fragment caching for post components: Cache the shared public payload (text, author, media, tags, cards) for one minute, loading only the viewer’s personal interactions (likes, bookmarks) dynamically.
Infrastructure scaling: Running a second Spring instance or upgrading to a larger server.
None of these have been implemented yet, by design: at 1,000 simultaneous users, the server cruises comfortably at ~60% CPU with pages served under 60 ms—far more capacity than a site of this size currently needs.
Media uploads
In a dedicated media benchmark, 30 out of 300 concurrent users continuously uploaded images, audio, and video files.
Throughout the test, browsing response times stayed within 121 ms at the 95th percentile. Because media transcoding and processing run asynchronously in an isolated background worker, heavy upload traffic never degrades the reader experience.
Caveats
These benchmarks were run on a single host using synthetic seeded data rather than organic production traffic. Two factors make these results slightly conservative:
Excessive password hashing: Virtual users authenticate during the ramp-up phase, burning significantly more CPU on password hashing (BCrypt/Argon2) than real users, who rely on long-lived JWTs.
Profiling overhead: Java Flight Recorder ran continuously during profiled test runs, consuming a minor percentage of CPU (~1–3%).
In real-world production, the system should experience slightly less background overhead than recorded here.