Skip to content

Index trade_executed_view.order_book_id for GET /query/executed-trades/{orderBookId} - #23

Open
devin-ai-integration[bot] wants to merge 2 commits into
masterfrom
devin/task-129-executed-trades-index
Open

devin-ai-integration[bot] wants to merge 2 commits into
masterfrom
devin/task-129-executed-trades-index

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown

Summary

GET /query/executed-trades/{orderBookId} (trader-app, the trades list on the order book screen) runs TradeExecutedQueryRepository.findByOrderBookId, i.e. select ... from trade_executed_view where order_book_id=?. trade_executed_view only had its primary key (V1__baseline_schema.sql), so every call did a full scan of a table that gets one row per executed trade and never shrinks (EXPLAIN: type=ALL, rows=1000000).

This adds the index idx_trade_executed_view_order_book_id (order_book_id):

  • V2__index_trade_executed_view_order_book_id.sql: Flyway migration for the kubernetes profile (V1 is untouched, so its checksum is unchanged).
  • V2 only creates the index when information_schema.statistics does not already list it, because a database Hibernate updated under local and then baselined at V1 under kubernetes already has it (MySQL 5.7 has no CREATE INDEX IF NOT EXISTS).
  • @Table(indexes = @Index(...)) on TradeExecutedView: the same index for the local profile, which builds the schema with ddl-auto=update.

No query, entity field or response changes.

Before / after (local, scratch MySQL 5.7, one trader-app at a time, alternating A/B)

1,000,000 rows in trade_executed_view over 2,000 order books (about 500 trades each). Sequential requests to a random order book after 10 warm-ups, 100 requests per run, ms:

Pair Before p50 Before p95 Before p99 After p50 After p95 After p99
1 268.5 417.6 496.2 9.2 19.7 27.4
2 234.1 347.7 428.8 11.0 21.1 29.9
3 226.9 368.1 401.4 9.3 19.4 22.1

The exact Hibernate SQL on its own (pymysql, 3 alternating pairs × 60 queries): p95 465–585 ms → 6–14 ms, p99 505–683 ms → 15–30 ms.

Method / repro

  1. Scratch DB: docker run -d --name esarch-bench-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=trader -e MYSQL_PASSWORD=trader -p 127.0.0.1:3316:3306 -v $PWD/docker/mysql/initdb:/docker-entrypoint-initdb.d:ro mysql:5.7, then load trader-app/src/main/resources/db/migration/V1__baseline_schema.sql into appdb.
  2. Seed: 1,000,000 trade_executed_view rows (random order_book_id from 2,000 UUIDs, seed 129) with LOAD DATA LOCAL INFILE.
  3. docker run -d -p 127.0.0.1:5672:5672 -e RABBITMQ_DEFAULT_USER=trader -e RABBITMQ_DEFAULT_PASS=trader rabbitmq:3.7-management, java -jar discovery-server/target/discovery-server-0.0.1-SNAPSHOT.jar.
  4. Build (Java 8) mvn -B -DskipTests package; run SPRING_PROFILES_ACTIVE=local ESARCH_MYSQL_PORT=3316 java -jar trader-app/target/trader-app.jar --logging.level.io.pivotal.refarch=INFO --spring.jpa.hibernate.ddl-auto=validate.
  5. Time GET /query/executed-trades/{id} sequentially; each pair runs "before" (no index) then "after" (CREATE INDEX from V2) against the same running server and data.

Equivalence

  • Response bodies of all 100 requests per run (parsed, sorted by generatedId, SHA-256) are identical before/after in every pair: fe4447e63e0d3da5, c6b530eaebff2728, dc61c2fc9f46d8b6. The SQL-only runs also hash identical row sets.
  • Fresh empty databases with the new jar: the kubernetes path (Flyway + ddl-auto=validate) applies V1 and V2 successfully and validates; the local path (ddl-auto=update) creates the same index from the annotation. Both reach /actuator/health UP.
  • mvn -B verify (all modules) passes.

Cost

  • Index size about 58 MB per million trades (table PK 101 MB); building it on 1,000,000 rows took 1.9 s on MySQL 5.7 (online DDL, reads and writes continue).
  • Each TradeExecutedEvent insert also updates the index: one extra B-tree insert per trade. No caching, no stale data.

Original code

TradeExecutedView and TradeExecutedQueryRepository.findByOrderBookId come from the upstream Axon Trader: Allard Buijze allard.buijze@axoniq.io (bfd9855, 2018) and Steven van Beelen smcvbeelen@gmail.com (a083fe1, 2018). The Flyway baseline without the index came from #11 (devin-ai-integration).

Runtime verification (testing agent)

Final build (db9a553) returned the same 466 trades as the pre-fix build for a seeded order book (sorted SHA-256 match); unknown order book still 404. Three more alternating A/B pairs (100 requests per state): p95 505.7/451.3/340.7 → 43.4/61.8/18.9 ms, p99 582.5/463.4/431.0 → 244.6/123.3/36.0 ms, hashes identical. Fresh local and kubernetes schemas and a local→kubernetes switch on the same schema all reach UP with one index and V2 success=1. The order book UI does not call this endpoint, so the recording shows the live API.

Live executed-trades API Index off/on timings
Live executed-trades API Index off/on latency

Note

Opened as Devin instead of Taylor Curran: the GitHub account taylorcurranpython has no write access to COG-GTM.

Devin-Org: engineering


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.

…s/{orderBookId}

Co-Authored-By: taylor.curran <taylorcurranagtc@gmail.com>
@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[bot]

This comment was marked as resolved.

Co-Authored-By: taylor.curran <taylorcurranagtc@gmail.com>
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