Two weeks of CircleCI workflow delays traced to MongoDB stalls, with services re-declaring every index at startup and a JVM upgrade muddying the diagnosis
CircleCI, 2019. CircleCI's MongoDB replica sets, chiefly the jobs replica set behind the builds queue.
Organisation
CircleCI
Evidence
Incident report
System
CircleCI's MongoDB replica sets, chiefly the jobs replica set behind the builds queue
Multiple MongoDB replica sets store the majority of CircleCI's centralized data, and their health is critical to the working of the whole platform; the jobs replica set is the one the post ties to job and workflow delays. A large fleet of service instances connects to them, and many of the services run on the JVM.
Near: These replica sets store the majority of our | Near: The MongoDB jobs replica set was heavily degraded | Near: We run a large fleet of service instances
Figures, as the source states them
Figure
As stated
Note
Where
Read and write tickets each allowed by the WiredTiger setting
128 tickets
The recommended and default setting, per MongoDB instance
Near: allows for 128 read and write tickets each
Increase in the fleet's average JVM threads per service instance after pool tuning
40 percent
By the end of the first week; a fleet average
Near: we had a 40% increase in the number | Near: average number of JVM threads per service instance
Longest time a read-only replica was unresponsive on April 4
42 minutes
One of two replicas; the other approximately 10 minutes
Declaring already-existing indexes from every service instance took database locks that stalled MongoDB
circleci-2019-mongodb-workflow-delays#f1at corpus version 26a1b3a7e3dd
Trigger
Whenever a service that connects to a MongoDB database started, it ensured that all of its indexes were in place. The post names no particular deploy as the start of the first incident, on March 26, 2019.
Near: when a service that connects to a MongoDB | Near: On March 26, 2019 at 21:29 UTC
How it failed
A deploy of the API re-declared every index roughly one hundred times. Checking an existing index needs a database level lock, held briefly but causing significant contention, and this starved the WiredTiger tickets that limit concurrency (128 each for reads and writes), so commands blocked and the jobs replica set stalled. On April 10 some tickets were still free: the team concluded tickets alone did not explain it, and saw less free memory and more failed cache evictions on the primary.
Near: With the help of the MongoDB support team | Near: Our instructions to create indexes were starving | Section: What We Learned
Why defences did not hold
The team had assumed, incorrectly as it says, that ensuring an index that already exists is a cheap operation, and did it in many places mainly because it simplifies integration tests. A JVM upgrade made around the same time (f2) was believed for a while to be the bigger issue. On April 10 application-level metrics did not show that the MongoDB primary was still degraded.
Near: We incorrectly assumed that ensuring an index | Near: we believed the JVM upgrade was the bigger | Near: Our application-level metrics did not reveal
Impact
Between March 26 and April 10, 2019 jobs and workflows were at times unable to move through the system. On April 2 the degraded jobs replica set made the API unavailable. On April 3 the primary public and private API service degraded severely and its containers crashed with Out of Memory errors. On April 4 two read-only replicas stopped responding, for approximately 10 minutes in one case and 42 minutes in the other, and the backlog took nearly an hour to drain.
Section: Incident Report
Fix
The unnecessary index creation was removed. Two TCMalloc settings recommended by MongoDB support were applied, a MongoDB stat that correlates with time blocked on memory requests is now monitored through the Datadog Agent, and some queries now pull subsets of fields, not whole documents. In progress: increasing the hardware capacity of the MongoDB instances, as of the timeline, and preparing a minor version upgrade. Planned: refactoring how parts of the data set are stored and accessed.
Near: We quickly removed the unnecessary index creation | Section: What We Learned | Section: Next Steps
A minor JVM update with container awareness on by default shrank thread and connection pools across services
circleci-2019-mongodb-workflow-delays#f2at corpus version 26a1b3a7e3dd
Trigger
Around the time of the first incident, many services were moved to a new minor JVM version for its security changes. The update also included improvements for docker-awareness, enabled by default.
Near: Around the same time, we updated many services
How it failed
For most services the update inadvertently reduced the size of thread and connection pools, which constrained throughput.
Near: this inadvertently reduced the size of thread
Impact
Throughput was constrained, but with MongoDB slowing down at the same time the team was unclear how much of the impact came from the JVM upgrade. It first believed the upgrade was the bigger issue, and after the April 2 incident concluded it was probably not the primary cause of the series.
Near: we were unclear how much impact was coming | Near: the JVM version change was probably not
Fix
Starting the following day, Docker detection was disabled while services were tuned so that thread and connection pools were sized appropriately, and the number of service instances was scaled up to compensate in the meantime. By the end of that week there was a 40% increase in the number of threads per service instance.
Near: we disabled Docker detection while tuning our services
Fail over to a healthy host and restart a MongoDB process as soon as it shows signs of slowdown
circleci-2019-mongodb-workflow-delays#d1at corpus version 26a1b3a7e3dd
Chosen
When a MongoDB instance begins to show symptoms of slowdown, the team fails over to a healthy host and restarts the slow MongoDB process, where possible during low-traffic hours to minimise the interruption to customers.
Near: When an instance begins to show symptoms
What forced it
On April 10 queues for job and workflow processing rose sharply, and after an hour of investigation the team saw performance problems only on the primary, most worryingly pages evicted by application threads.
Near: April 10, 2019 at 13:25 UTC we were
Reasoning
Work with MongoDB support gave the team better visibility into the leading indicators of degradation, so that it can take corrective action before customers are much affected.
Section: Next Steps
Outcome
On April 10 the secondary that took over at once processed twice as many database operations, and eventually twice that again. The post says the practice has alleviated customer-visible symptoms while engineers and MongoDB continue working to improve performance.
Near: At approximately 15:00 UTC, we failed over | Near: This has alleviated customer-visible symptoms