Optimizing Apollo Configuration Reading in PHP: Reducing CPU and Memory Overhead
This article analyzes the performance bottleneck caused by large Apollo configuration reads in a PHP application and presents multiple optimization strategies—including static variables, Yac/APCu caching, require‑file with opcache, and MessagePack serialization—to significantly lower CPU and memory consumption while maintaining configuration freshness.
Background: As the number of cities supported by the Beike app grows, each city’s configuration is stored in Apollo (a distributed configuration center). Reading the entire configuration for every request, especially with around 1,000 cities and dozens of items per city, leads to high CPU and memory usage, causing service degradation under heavy concurrency.
Analysis: The configuration read‑process includes fetching the JSON‑encoded data from Apollo, decoding it, and using it throughout the request. Repeated reads and full JSON decoding create a performance bottleneck.
Optimization approaches:
Avoid repeated reads and perform minimal processing.
Improve read speed.
Solutions implemented:
3.3.1 Use static variables to cache configuration
By decoding the configuration once per request and storing the result in a static variable, subsequent calls reuse the already‑decoded data, eliminating extra Apollo reads and JSON decoding.
Advantages: Single read and decode per request; reduced CPU usage. Disadvantages: Configuration updates are not visible until the request ends.
3.3.2 Minimal json_decode
Only decode the second‑level array for the needed city_id, avoiding unnecessary decoding of unrelated entries.
Advantages: Faster when only a subset of data is needed. Disadvantages: No benefit when the target key size approaches the total size.
3.4.1 Use Yac or APCu as local cache
Yac (lock‑free shared memory) and APCu (user data cache) store the configuration locally, providing read speeds comparable to memcached with lower overhead.
Advantages: Memory usage lower than Apollo + JSON; execution time about one‑quarter of the original method. Disadvantages: Requires additional extensions and a daemon to keep the cache synchronized with Apollo.
3.4.2 Reverse‑engineer configuration into PHP files
Export configuration to PHP files using var_export , then load them with require and benefit from OPcache for fast reads.
Advantages: Significant read‑time improvement. Disadvantages: Adds file‑maintenance overhead; real‑time updates require redeployment or submodule updates.
3.5.1 Use MessagePack format
Store configuration in MessagePack, a binary serialization format that is smaller and faster to parse than JSON.
Advantages: About 60% of JSON parsing time with reduced memory footprint. Disadvantages: Introduces a new PHP extension dependency.
Conclusion: Combining Apollo with static variables, Yac/APCu caching, and selective decoding dramatically reduces CPU and memory consumption, preventing service outages. Depending on the scenario, these techniques can be mixed to achieve the best performance while balancing complexity and freshness requirements.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.