iTranslated by AI
A Crushing Defeat at ISUCON12
Here is the repository. I participated in a team of two, one for application and one for infrastructure, and I was in charge of the application. Our team name was "Kaettekita Nabebu" (The Returned Hot Pot Club). We chose this name because we previously participated in the ISUCON10 finals parallel team as "Nabebu (2 servings)" with the same members, and also because it's the year of Ultraman.
There was a moment when we exceeded 10,000 points, but in the end, the benchmark stopped passing, and we finished with 0 points. It was a crushing defeat, so this is a post-mortem blog post.
Reflection 1: Spent 2 Hours Obsessing Over the Local Development Environment
In ISUCON, how you develop is crucial. A tough part of this problem was that a mysterious binary was running on the server, handling authentication. To perform authentication, APIs had to be served on the same domain, so the implementation used nginx to change the proxy_pass destination. There were two major issues here:
- nginx handled static file delivery and
proxy_pass, so it wasn't possible to develop using Go alone. - The source code for the mysterious binary was private, so there was no way to run it locally.
So, I did the following:
- Added implementations for static file delivery and
proxy_passon the Go side. - Port-forwarded so the authentication server could be accessed locally.
However, this alone wasn't enough to bypass the domain name check for the JWT returned by the authentication server. The .dev domain mentioned in the manual has HSTS settings, so it cannot be accessed via HTTP. Therefore, I used a .test domain and specified both ISUCON_ADMIN_HOSTNAME and ISUCON_BASE_HOSTNAME to finally get the local development environment running.
I had to read and modify the code to achieve all this, which cost me two hours.
In hindsight, the correct answer was probably to develop directly on the production server. However, in ISUCON, instances sometimes lack memory, and adding strange settings might lead to regulation violations. I was reluctant to develop on the production server and strongly wanted to build a local environment. Believing that I was just one step away from completing it, I failed to make a timely decision and ended up wasting time.
While being able to develop locally was effective during the middle phase of development, the local environment couldn't keep up with the drastic changes in the latter half, and it eventually became useless. Making such judgments is difficult, but my lack of decisiveness was a cause of defeat.
Reflection 2: Performance Checks and Code Changes Should Have Been Done at the SQLite Stage
This problem was very characteristic in that it used both MySQL and SQLite. While the bottlenecks on the MySQL side were relatively easy to remove (as described later), I didn't know how to analyze SQLite bottlenecks, so I decided on a policy of migrating to MySQL first before investigating.
I proceeded with this decision without much doubt because a migration script was already included in the code, so I assumed that was the intended path.
However, looking at the code, there were clearly parts where indexes were missing and N+1 queries were present. Since these could have been improved on the application side even while staying with SQLite, I should have investigated and proceeded with SQLite first. This is because the migration forced me to fight against the 30-second time limit of the initialize process.
- The
initializeprocess was quite aggressive, deleting the SQLite files and then copying the original files, so the MySQL side also needed to delete all records and bring in the original data.- Since there was no time to import data, I implemented a strategy to keep the original data in tables with an
o_prefix (likeo_competition) and copy the tables during initialization. - I also wanted to copy the indexes, so I used
CREATE TABLE competition SELECT * FROM o_competition LIMIT 0to create the table structure, followed byINSERT INTO competition SELECT * FROM o_competition. Since creating indexes from the start wouldn't finish in time, I had to use various tricks like runningALTER TABLEafter theINSERTto batch index creation at the end. I ended up wasting time on these stopgap techniques just to barely pass the limit.
- Since there was no time to import data, I implemented a strategy to keep the original data in tables with an
- Since the SQLite files were split per tenant, I wanted to consolidate the tables but judged that modifying them to include tenant IDs would be too difficult, so I decided to split the databases instead.
- Actually, I hadn't noticed that every table already included a
tenant_idfor some reason, so this concern was unnecessary. - To split the databases, I wrote the worst possible code: executing
mysqlcommands directly from the Go application via the shell. - I also added code to delete all the added tenant databases during initialization.
- Actually, I hadn't noticed that every table already included a
- The process of importing initial data into the in-memory
visit_history(which I had implemented in the middle phase) was also taking too long, so I changed it to saveGROUP BYresults in a separate table and import from that table instead. - Even then, it wouldn't make it in time, so I split the MySQL servers based on the parity (even/odd) of the tenant ID to barely complete the process.
- In the first place, it was a lazy implementation that established a new connection every time.
Unless I won this battle, I couldn't release the code to improve the N+1 queries I was trying to implement, and I ended up fighting with all my might in areas that weren't the core issue.
If I had made improvements while still using the existing SQLite code, I would have realized the following, released the N+1 query improvements, and then probably migrated to MySQL smoothly:
- Migration to the same table was possible because
tenant_idwas already included. - By realizing the need for specification changes in the
player_scoretable, I could have compressed the data and reduced the initialization time.
Additionally, if initialization was taking too long, I could have stopped MySQL and copied the data_dir directly. While this might be a method you wouldn't think of unless you're familiar with MySQL internals, both members of our team were accustomed to such operations, so it was a strategy we could have easily taken. However, the idea never surfaced.
Furthermore, while I was able to achieve a score of nearly 8,000 early on by placing the SQLite files in tmpfs, once I migrated to MySQL, I couldn't get back to that score without implementing the N+1 query improvements. Migrating to MySQL without a clear strategy was a bad move. I should have thought through the tactics and acted more strategically.
Reflection 3: Failure to Implement a Multi-Server Configuration
MySQL was in a two-server configuration just to get the initialize process to pass, but it was forced, and the attempt to distribute the application via nginx failed.
I hadn't followed the code properly, but I feel like it couldn't be distributed without considering the authentication server setup as well. Failing to thoroughly trace this part of the code was fatal. As expected, understanding the application's behavior is vital in ISUCON.
Reflection 4: Kept Rerunning Benchmarks Without Checking Error Logs in the Last 10 Minutes
In the last 10 minutes, when the benchmarks stopped passing, I didn't check the cause. Finally, about a minute before the end, I noticed errors related to ulimit settings, but it was too late. Personally, this was my first time finishing with 0 points.
While the ulimit settings should have been handled from the start without hesitation, the primary cause of defeat was ignoring the basics—checking the error logs as soon as the benchmarks failed—using the lack of time as an excuse.
ISUCON reminded me of the necessity to return to basics and always act calmly and correctly regardless of the situation. I will start over and try again.
Successes
On the flip side, here are the things that went well:
- Since it was a two-person team, I could leave the infrastructure entirely to my teammate and focus on the application.
- The optimizations in the middle phase were going quite well.
The changes in the middle phase were as follows:
-
dispenseIDwas assigned appropriately usingsync/atomic. -
visit_historywas handled via an in-memory cache. - SQLite was placed in
tmpfsto temporarily boost the score to a certain level.
The middle-phase changes were implemented relatively quickly and produced results, which was good, but the delay at the beginning and the confusion at the end were problematic.
Thoughts on Application Configuration
The configuration where only the application is containerized with Docker was actually a setup I proposed when I was an organizer for the ISUCON9 qualifiers. Although it wasn't used back then, from an organizer's perspective, having to prepare multi-language environments like in ISUCON is extremely tedious, so a setup that can be solved just by providing a Dockerfile is quite rational. However, if you try to provide everything—including MySQL and nginx—via Docker, you need to create very detailed configuration files, which increases the difficulty. Therefore, I feel that the configuration of using Docker only for the application while installing everything else on the host will likely continue to be used in the future.
Actually, Docker was first used in the ISUCON6 finals when I was an organizer (I was the one who prepared the infrastructure-related settings), and at that time, there were quite a lot of complaints. Since then, it has been used several times in the finals, but this was the first time in history that Docker was used in the qualifiers. Nowadays, nobody complains about Docker, so it makes me think how much times have changed.
By the way, my team quickly removed Docker for ease of management. This wasn't done for performance reasons; it was simply because we could easily switch to running the application directly via systemd, and we wanted to make that change quickly.
I think externalizing the authentication server itself is a good experiment, but it was difficult to understand the configuration where mysterious applications were running on each server, and I spent a lot of time agonizing over it. I'm not entirely sure what the correct approach for authentication is myself, but I feel there will continue to be various experiments in this area.
This year's problem was heavyweight in both infrastructure and application aspects, with high difficulty, making it truly worthy of being called a "Mixed Martial Arts" competition. While I felt the bar for ISUCON organizers has been raised once again, I am filled with gratitude for the organizers this year who exceeded expectations. Thank you very much.
Finally
In the second half, I didn't have enough time and could only implement a part of the N+1 query improvement code. Still, since we once exceeded 10,000 points, I think we could have achieved a slightly better result if we had more time. However, there were many errors in judgment, and I felt it was difficult to win.
This year's problem featured a "crazy" implementation (in a good way) where multi-tenancy was achieved by splitting SQLite files, and it was extremely difficult to fully implement the correct approach within the time limit. However, there are many participants in recent ISUCONs who can overcome this, and I think it's a truly amazing world.
In the past, it was often said that implementing benchmarkers was the difficult part of ISUCON, but lately, know-how for benchmarker implementation has accumulated. This was a grand problem that made me feel like the organizers have entered a phase where they can take on even newer challenges. I think the ISUCON organizers, who continue to evolve, are truly wonderful despite the difficulties.
I feel like I could talk for a long time just discussing this problem's post-game analysis, so I'd love to talk with others who solved the problem if given the chance. It's a pity I can't participate in the finals, but I will continue to participate in ISUCON to grow. To the organizers, thank you very much.
Discussion
ISUCON12予選に今回初参加したものです。catatsuyさんの今までの記事が本当に役に立ちました。
この記事でもtmpfsなど新しいことを勉強させていただきました!
この場を借りてお礼させていただきます。ありがとうございました!
ありがとうございます。これからも細々と発信していくと思うのでよろしくお願いします!