In this article, we will take an in-depth look at how to build a cache manager in Node.js to drastically improve the performance of your application, specifically for listing Minecraft servers. Without caching, the response time for listing all the servers was over 2 minutes due to multiple heavy computations and I/O operations.
With the introduction of a caching mechanism, this time was reduced to an impressive 2 milliseconds. Let’s dive into the code and understand how this was achieved.
The purpose of this system is to manage a list of Minecraft servers efficiently. The core functionality includes fetching server information, such as the server's MOTD (Message of the Day), online status, game version, and operational details (e.g., ops, whitelist, banned players).
The data is sourced from Docker containers, local file systems, and remote authentication services.
When you’re dealing with numerous Minecraft servers, querying real-time server data can become very expensive in terms of performance. Each query requires:
- Accessing container information via Docker.
- Checking server online status via network requests.
- Fetching MOTD and other server metadata.
- Interacting with the file system to fetch or generate tokens.
These operations, especially when scaled to multiple servers, can significantly slow down the response time. Without caching, the requests took over 2 minutes to process.
This system mitigates those delays by introducing an efficient in-memory cache.
The system employs a caching layer that stores various pieces of server information (MOTD, online status, etc.) in memory. This avoids repeated heavy I/O and network operations, thus drastically reducing the request time.
### Key Components of the Cache
1.**MOTD Cache:** Stores the Message of the Day for each server.
2.**Online Status Cache:** Stores whether a server is online or offline.
3.**Token Cache:** Stores authentication tokens for server owners.
4.**Container Info Cache:** Caches Docker container details for each server.
This function first checks the secret key for authentication. If the secret key is valid, it proceeds to list all servers.
The server data is retrieved from the in-memory cache, avoiding the need to re-fetch the data from slow external sources like Docker, network connections, and the file system.
By introducing an in-memory caching system, this application was able to reduce request times from over 2 minutes to just 2 milliseconds. This system efficiently caches key server data, eliminating the need to repeatedly query external services like Docker, network services, and file systems.
This caching strategy could be adapted and extended further, for instance by adding cache expiration policies or integrating Redis for distributed caching in a multi-node architecture.