Boost MySQL Performance with Redis: Local & Remote Caching Strategies
Learn how to prevent MySQL overload by adding Redis as a caching layer, covering local in‑memory caches, remote cache services, support for multiple data types, expiration policies, persistence mechanisms like RDB and AOF, and simplified TCP protocols to achieve high‑throughput, resilient data access.
Local Cache
Memory queries are faster than disk queries. By storing MySQL data in memory, we can avoid disk access and dramatically improve query performance. A simple approach is to create an in‑process dictionary (e.g., a
dictin Python or a
Mapin Java) where the key is the product ID and the value is the product data.
When a request arrives, the service first checks the in‑memory dictionary; if the key is missing, it falls back to MySQL, then stores the result in the dictionary for future hits. This pattern is known as local cache , which greatly reduces the load on MySQL.
Remote Cache
When multiple instances of the product service run, each maintaining its own local cache wastes memory. The solution is to extract the dictionary into a separate service— a remote cache service . All instances share this service via the network.
To avoid concurrency issues, the remote cache processes all read/write commands on a single thread, eliminating thread‑switch overhead and race conditions.
Multiple Data Types Support
The original cache only stored string keys and values. It has been extended to support additional structures: FIFO queues ( List ), sets for deduplication ( Set ), and sorted sets for ranking ( ZSet ), making the cache more versatile.
Memory Expiration Strategy
As more data structures are added, memory consumption grows. By assigning an expiration time to each entry, the cache can automatically delete stale data, relieving memory pressure. The client decides the appropriate TTL using the expire command.
Cache Eviction
If clients neglect to set expirations, memory can still fill up. The cache can apply eviction policies such as Least Recently Used (LRU) when memory approaches its limit, ensuring that hot data remains while freeing space.
Persistence
When the cache service restarts, all in‑memory data would be lost, causing a sudden surge to MySQL. To prevent this, an asynchronous thread periodically snapshots the entire memory to disk (RDB – Redis Database Backup). Additionally, an Append‑Only File (AOF) logs every write operation, allowing the service to replay the log on startup and recover most data.
The AOF file can become large, but periodic rewriting compresses it by keeping only the latest state of each key.
Simplify Network Protocol
The remote cache does not expose an HTTP API. Instead, it uses a lightweight TCP protocol: commands like
SET key valueand
GET keyare sent directly over TCP. No custom parsing code is needed because the official redis-cli tool already implements the protocol, and many language libraries provide compatible clients.
What Is Redis?
Redis is a high‑performance, remote dictionary server that supports multiple data types, expiration policies, eviction strategies, and persistence mechanisms. It acts as an in‑memory front‑door to databases like MySQL, dramatically accelerating read/write operations.
Beyond basic caching, Redis offers extensions such as RedisJSON (in‑memory JSON store), RediSearch (full‑text search), RedisGraph (graph database), and RedisTimeSeries (time‑series data), turning it into a versatile in‑memory data platform.
Summary
Redis functions as a remote dictionary service where all core read/write logic runs on a single thread, eliminating concurrency issues.
It supports various data structures, expiration policies, and eviction strategies, and provides a simple TCP‑based protocol.
Persistence is ensured via RDB snapshots and AOF logs, protecting data across restarts.
Rich extensions (e.g., RediSearch, RedisJSON) expand its capabilities beyond caching.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.