Highlight MFA > Fix OOM issue when user count is > 100k #87
Merged
kat3samsin merged 11 commits intoproductionfrom Aug 18, 2025
Merged
Highlight MFA > Fix OOM issue when user count is > 100k #87kat3samsin merged 11 commits intoproductionfrom
kat3samsin merged 11 commits intoproductionfrom
Conversation
…ty-boost into PLTFRM-1521/refactor-highlight-mfa-count
…thub.com/Automattic/vip-security-boost into PLTFRM-1521/refactor-highlight-mfa-count
27a68f9 to
019c481
Compare
d15b134 to
7a11994
Compare
…ty-boost into PLTFRM-1521/refactor-highlight-mfa-count
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR addresses a critical performance issue where counting users without 2FA for sites with 100k+ users was causing OOM errors and timeouts. The solution replaces the original O(n) approach that looped through all users with a single optimized SQL query.
Key changes:
- Replaces individual user queries with a single JOIN-based SQL query for counting users without 2FA
- Adds 1-hour caching with cache invalidation for the count results
- Updates test expectations to account for the new SQL-based counting behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| modules/highlight-mfa-users/class-highlight-mfa-users.php | Implements optimized SQL query to count users without 2FA, replacing the loop-based approach |
| tests/phpunit/test-highlight-mfa-users.php | Updates test expectations and adds direct meta setting to work with the new SQL implementation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
andrea-sdl
approved these changes
Aug 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixed critical performance issue for sites with 100k+ users experiencing OOM when loading Users page and counting users without 2FA.
Problem: This was raised by @pandah3 here. The original implementation looped through all users calling
Two_Factor_Core::is_user_using_two_factor()for each, causing O(n) database queries. With 201k users, this caused >768MB memory exhaustion and >60s timeouts.Solution: Single optimized SQL query that JOINs users with usermeta, filters by roles/capabilities directly in SQL, and counts users without 2FA in one operation.
Results:
Code:
/modules/highlight-mfa-users/class-highlight-mfa-users.phpSupported:
Limitations:
two_factor_primary_provider_for_user, etc.)user_has_capfilter_two_factor_enabled_providersmeta structurePre-review checklist
Please make sure the items below have been covered before requesting a review:
Pre-deploy checklist
Steps to Test
vip dev-env create && vip dev-env startbulk-create-users.php(runvip dev-env info --slug=vip-security-boostto find the location)bulk-create-users.php
vip dev-env exec -- wp eval-file bulk-create-users.phpto create 100k test users