Skip to content

perf: index account.username used by every authenticated request - #380

Open
devin-ai-integration[bot] wants to merge 1 commit into
DevOpsfrom
devin/task-39-username-index
Open

devin-ai-integration[bot] wants to merge 1 commit into
DevOpsfrom
devin/task-39-username-index

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

AccountRepository.findByUsername runs on every authenticated request (login via loadUserByUsername, GET /dashboard, GET /transactions, POST /deposit|/withdraw|/transfer, and the duplicate check in /register), but account.username had no index, so each lookup was a full table scan (EXPLAIN: type=ALL, 198,600 rows at 200k accounts).

@Entity
+@Table(indexes = @Index(name = "idx_account_username", columnList = "username"))
public class Account implements UserDetails {

With spring.jpa.hibernate.ddl-auto=update, Hibernate creates it at startup (create index idx_account_username on account (username)); EXPLAIN becomes type=ref, key=idx_account_username, rows=1. Non-unique on purpose, so startup can't fail on a database that already holds duplicate usernames.

Before / after (local, no Datadog service for this app)

MySQL 8.0 (repo blueprint container), 200,001 accounts, 400,050 transactions (50 for the bench user). bench.py with 20 warm-up requests, then n=500, c=10, logged-in JSESSIONID. Two runs each; second runs swapped the index on the same running app.

Path Before p50 / p95 / p99 (ms) After p50 / p95 / p99 (ms)
GET /dashboard (run 1) 47.6 / 86.6 / 105.2 7.3 / 13.5 / 16.5
GET /dashboard (run 2) 43.8 / 68.4 / 91.4 2.2 / 4.3 / 5.4
GET /transactions (run 1) 53.0 / 81.9 / 107.5 12.8 / 27.4 / 71.3
GET /transactions (run 2) 41.1 / 64.7 / 71.2 4.7 / 8.3 / 11.0

Repro

  1. Start MySQL per the blueprint: docker run -d --name bankapp-mysql -e MYSQL_ROOT_PASSWORD='Test@123' -e MYSQL_DATABASE=bankappdb -p 3306:3306 mysql:8.0 --default-authentication-plugin=mysql_native_password
  2. ./mvnw -DskipTests package && java -jar target/bankapp-0.0.1-SNAPSHOT.jar
  3. curl -X POST localhost:8080/register -d 'username=bench&password=benchpw'
  4. Seed: 200k accounts via a recursive CTE INSERT INTO account ... SELECT ... CONCAT('user', n) plus 50 transactions for bench and 400k for the others.
  5. Log in with curl, then BENCH_PORT=8080 BENCH_HEADERS="Cookie: JSESSIONID=<sid>" python3 bench.py /dashboard 500 10 (same for /transactions).

Cost

  • Index size 5.6 MB at 200k accounts (innodb_index_stats). It builds in 0.27 s at first startup on that table.
  • One extra B-tree insert per registration. That is negligible next to the BCrypt hash the same request computes.
  • No query, API or schema shape change beyond the secondary index.

Response equivalence

The rendered /dashboard and /transactions HTML for the bench user is byte-identical before and after, apart from per-request UUIDs. That includes all 50 transaction rows.

Tests

./mvnw test against the MySQL container: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, BUILD SUCCESS.

The touched entity comes from the original author's initial commit (goldencat, e6208f7).


Latency Optimizer opened this PR for Taylor
Implemented in session Find and fix next slow path in new repo
Review with Devin · Open in Devin Desktop



Note

Devin errored when opening this Pull Request as taylorcurranpython.
As a fallback, Devin opened this PR as itself.

Co-Authored-By: taylor.curran <taylorcurranagtc@gmail.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Custom reviewers

  • 🧪 Test 2 reviewer: no findings · View
  • 🚩 TD Enterprise Architecture: no findings · View
  • 👁️ TD Information Security: no findings · View
  • 🧠 TD French Language (Bill 96): no findings · View
  • ⏱️ TD API Contracts: no findings · View
  • 🧹 Henry's Style Guide: no findings · View
  • ✨ iOS Smoketester: no findings · View
  • ✨ Blast Radius Reviewer: no findings · View
  • 🛡️ API Security Reviewer: no findings · View

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration

Copy link
Copy Markdown
Author

Browser verification: indexed branch, seeded MySQL (200k accounts)

Tested in Chrome against the branch build:

  • bench signs in at $0.00, and history shows all 50 seeded rows.
  • Whole-dollar deposit and withdrawal persist.
  • A transfer updates both sides.
  • Insufficient funds and an unknown recipient are rejected.
  • A new user can register and sign in, and a wrong password is rejected.

MySQL picks idx_account_username for the username lookup (type=ref, rows=1).

The run also found three UI quirks. They are already on the base branch, and this PR does not touch those templates:

  • Duplicate registration shows the hard-coded "User already present." from register.html.
  • The type="number" amount inputs don't accept cents.
  • The history header is clipped at the top of the viewport.
Dashboard Transactions
Dashboard Transactions

Recording

Written by Devin

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants