WordPress Caching: Professional Guide to Database Caching
Master WordPress database caching with Redis and object caching. Learn how to reduce query load by 70%, improve response times, and handle load shedding. Complete guide for SA hosting.
Key Takeaways
- Database caching reduces WordPress query load by 60–75%, cutting page response times from 500ms to 100ms even during load shedding.
- Redis object caching combined with query result caching outperforms file-based solutions; HostWP includes Redis standard on all plans for ZAR 399+/month.
- Proper implementation requires transients, persistent object caching, and database query optimization—not just installing a caching plugin.
Database caching is the single most effective way to accelerate WordPress sites under real-world SA hosting conditions. While most site owners focus on page caching and CDN, the database remains the bottleneck: every page load triggers 50–200 database queries, and without caching, your Johannesburg server repeats the same expensive lookups thousands of times daily. In this guide, I'll explain the professional-grade caching strategies we implement at HostWP, the tools that actually work, and exactly how to measure your improvements.
If you're running WooCommerce, a membership site, or any data-heavy WordPress installation in South Africa, database caching isn't optional—it's the difference between a site that crawls during Eskom load shedding and one that stays responsive. Let's dig into how to implement it properly.
In This Article
What Is Database Caching and Why It Matters
Database caching stores the results of expensive database queries in fast-access memory, so WordPress doesn't repeat the same lookup twice. Instead of hitting your Johannesburg-hosted MySQL server for every visitor request, cached data lives in RAM (Redis or Memcached) where retrieval takes microseconds instead of milliseconds.
The impact is enormous. At HostWP, we've audited over 500 SA WordPress sites and found that 78% had zero object caching enabled—meaning every single visitor was forcing the database to work from scratch. A typical WooCommerce store with 100 products, 50 categories, and dynamic pricing makes 120+ database queries per page load. Without caching, that's 120 round trips to MySQL. With object caching in place, repeat visitors see the same cached result, and page load times drop from 2.5 seconds to 600ms.
The three layers of database caching are: (1) query result caching (storing SELECT results), (2) object caching (storing post meta, options, user data), and (3) transient caching (storing temporary computed values like sales totals or feed data). Most site owners use only page-level caching (LiteSpeed or WP Super Cache), which doesn't help when dynamic content changes per user—that's where database caching saves the day.
Asif, Head of Infrastructure at HostWP: "I've personally migrated 50+ high-traffic SA sites to our LiteSpeed + Redis stack. The moment we enabled object caching, CPU load dropped 40–50% and we could serve 3x more concurrent visitors before hitting resource limits. On our infrastructure, Redis is included standard; most competitors charge R200–500 extra per month."
Transients vs. Object Caching: Which Comes First
WordPress transients and object caching solve related but different problems. Understanding the distinction is critical for proper implementation.
Transients are temporary data storage for values that expire after a set time—perfect for API responses, expensive calculations, or data that changes hourly. A transient might store "current Rand/Dollar rate" or "WooCommerce product count" with a 1-hour expiry. When the transient expires, WordPress automatically deletes it and recalculates on the next request. Transients are developer-controlled and require explicit code.
Object caching is automatic and persistent. WordPress caches all post meta, taxonomy data, user info, and option values in the object cache (usually Redis). This cache doesn't expire until you explicitly invalidate it—when a post is updated, WordPress purges related cached objects. Object caching is transparent: plugins and themes don't need modification to benefit.
In practice, use transients for calculated or external data (API calls, report totals, feed content) and let WordPress object caching handle core data (posts, users, options). Ninety percent of sites need both. A WooCommerce store should cache product prices and stock levels in object cache, but transients for recent order summaries or inventory sync calls to suppliers.
At HostWP, our Redis setup makes both instant. We back transients with Redis by default, so even developer-managed transients bypass MySQL entirely. File-based transient storage (the fallback) is 100x slower and will cripple your site during load shedding when every millisecond counts.
Why Redis Outperforms File-Based Caching
Redis is an in-memory data store that serves cached data from RAM at microsecond latency. File-based caching (WP Super Cache, W3 Total Cache file mode) stores data on disk, requiring file I/O—slow, especially on shared storage during high concurrency.
The speed difference is measurable: a typical WordPress site with Redis object caching serves cached data in 2–5 milliseconds. File-based caching takes 20–80ms due to filesystem overhead. Over 100 requests per second (common for busy SA e-commerce sites), that's a 5–10 second cumulative difference per user session.
Beyond speed, Redis offers:
- Atomic operations: Race conditions are impossible; concurrent requests don't corrupt cache.
- Memory efficiency: Built-in compression and data structure optimization.
- Persistence options: RDB snapshots or AOF journaling for crash recovery (HostWP enables both).
- Cluster support: Redis scales horizontally if your site outgrows single-server capacity.
The downside? You need a dedicated Redis instance (HostWP includes this in all plans from ZAR 399/month). Shared hosting without Redis forces you to file-based caching or Memcached, both inferior for WordPress. Xneelo and Afrihost, common SA hosting competitors, offer Redis only on premium-tier plans at extra cost—HostWP includes it standard, which alone justifies the comparison for any serious WordPress operation.
Configuration is simple: install a Redis cache plugin (Redis Cache Pro or free WP Redis), point it at your Redis server, and enable. WordPress automatically uses Redis for all object caching. No code changes needed.
Unsure if your site is using database caching? We offer a free WordPress performance audit that shows your current cache hit rate, database query count, and exact optimization roadmap.
Get a free WordPress audit →Optimizing Database Queries Before Caching
Caching is not a substitute for optimization. If your site runs 200 database queries per page and you cache all 200 results, you've still made progress—but caching 50 optimized queries is far superior. Before implementing caching, audit your queries.
Common culprits: (1) N+1 queries—looping through posts and fetching meta for each one instead of batch-loading, (2) unoptimized post loops—not specifying which columns you need, (3) missing indexes—queries that scan entire tables instead of using indexed columns, (4) transient abuse—storing huge datasets in cache when a single database column would suffice.
Use Query Monitor (free WordPress plugin) to see every query in real time. Sort by duration and count. If a single query takes 500ms, caching helps, but that query needs rewriting. Missing database indexes cause the most painful slow queries: SELECT * FROM wp_posts WHERE post_title LIKE '%search term%' without an index scans all posts line-by-line. Add an index and the same query finishes in 5ms.
WooCommerce sites often fail on product queries. Default product loops fetch every meta field (attributes, pricing, stock) for every product, then run additional queries to get category counts and review ratings. With proper indexing and meta-field optimization (using post tables instead of postmeta where possible), a product archive query drops from 40 queries to 8, then caching handles the rest.
At HostWP, our migration team always runs a query audit on incoming sites. In the past 100 migrations, we found the median site had 20–30 unnecessary queries per page that could be eliminated entirely. Combined with Redis caching, clients see 60–70% total response time reduction.
Measuring Caching Performance: Key Metrics to Track
Install Query Monitor and New Relic or Kinsta APM (if available in your region) to measure impact. Key metrics:
Cache Hit Ratio: Percentage of requests served from cache vs. cache misses requiring database queries. Target: 85%+ on public pages, 60%+ on user-specific content. If you're below 50%, your cache TTL (time-to-live) is too short or you're caching insufficiently.
Database Query Count: Absolute number of queries per page load. Audit with Query Monitor. Typical page: 20–40 queries. WooCommerce product page: 40–60. If above 100, you have optimization work to do before caching helps.
Average Query Duration: Slow queries (>100ms) need index optimization. Fast queries (<5ms) benefit hugely from caching. If your slowest query is 1 second, cache it aggressively with long TTL. If your slowest query is 2 seconds, rewrite it before caching.
Time to First Byte (TTFB): Server response time before browser starts rendering. With LiteSpeed (HostWP standard) + Redis object caching, TTFB should be 100–300ms even during peak load or load shedding. File-based caching typically yields 300–600ms. CDN like Cloudflare (included with HostWP) reduces user-perceived latency by serving cached pages from edge locations near Cape Town, Durban, or Jo'burg.
Use Google PageSpeed Insights and WebPageTest.org to track real-world metrics. Retest after implementing caching to quantify your win.
5 Database Caching Mistakes That Cost You Speed
1. Caching when you should optimize. If your homepage query takes 2 seconds due to a full-table scan, caching makes it 2 seconds of cache-building then 0ms on hit. But fix the query and it's 50ms consistently. Optimization first, caching second.
2. Setting transient TTL too short. Transients expiring every 5 minutes defeat the purpose. Unless data changes that fast, use 1-hour or 24-hour TTL. If you're API-integrating with Openserve or Vumatel fibre provisioning systems, a 1-hour cache is safe; you don't need real-time updates for internet connectivity status.
3. Object caching user-specific data carelessly. Caching "current logged-in user's cart total" globally means all users see the same total. Always include user ID in cache keys: cache_key = "cart_total_" . get_current_user_id(). WordPress handles this for post meta automatically, but custom code must be careful.
4. Not purging cache on content updates. If you cache a post excerpt and don't invalidate when the post updates, visitors see stale content for hours. Use cache invalidation hooks: add_action( 'save_post', 'purge_post_cache' ) to clear related caches when posts are edited.
5. Ignoring POPIA compliance in cache data. South Africa's POPIA law requires you to delete personal data on user request. If you're caching user email or phone in object cache, implement a user-delete hook that purges their cached data. Transient cleanup is automatic, but custom Redis caching needs explicit purge logic. HostWP's compliance guides cover this detail.
Frequently Asked Questions
Q: Does WordPress come with object caching built-in?
A: WordPress includes object caching infrastructure but uses a file-based fallback by default (very slow). Enabling true object caching requires a Redis or Memcached backend. HostWP includes Redis on all plans; install a cache plugin to activate.
Q: Will caching break my WooCommerce dynamic pricing or real-time inventory?
A: Not if configured properly. Cache product data with a 15–30 minute TTL, not indefinitely. Cache product metadata separately from stock counts. Use transients for data that changes hourly (inventory sync). Manually purge cache after bulk stock imports or price changes.
Q: Can I use file-based caching instead of Redis to save cost?
A: Technically yes, but not recommended for SA hosting. During Eskom load shedding, every millisecond counts. File-based caching is 10–20x slower than Redis and struggles under concurrent traffic. At HostWP's ZAR 399/month entry tier, Redis is included; file-caching isn't cheaper.
Q: How long should I set object cache TTL for WooCommerce products?
A: 24 hours for static product data (title, description, images), 1 hour for pricing and attributes (if prices update daily), 5 minutes for stock counts (if inventory is real-time). Mix short and long TTLs for different data types rather than one global TTL.
Q: Does Cloudflare caching replace database caching?
A: No. Cloudflare caches the final HTML page at edge locations globally. Database caching caches the data before page rendering. Both are essential: Cloudflare handles public pages; database caching handles user-specific or dynamic content (logged-in dashboard, shopping carts, personalized recommendations).