Redis Caching & HikariCP Connection Pooling
Supercharge microservice performance with Redis Cache-Aside, prevent cache stampedes, and size HikariCP connection pools using the PostgreSQL concurrency formula.
Relational database queries involve disk I/O, index scans, and lock management. A well-tuned PostgreSQL database might handle 5,000 queries per second per node.
In contrast, Redis is an in-memory, single-threaded event-loop data store capable of serving over 100,000 operations per second with sub-millisecond latency.
Pairing your microservice database with a distributed Redis cache is the single most effective way to scale read throughput.
01The Cache-Aside Pattern (Lazy Loading)
In the Cache-Aside Pattern, the application code coordinates caching directly:
The Read Flow:
- Application receives request:
GET /api/v1/products/1001. - Application checks Redis:
GET product:1001. - Cache Hit: Redis returns the cached JSON string in 1ms. The database is never touched.
- Cache Miss: Redis returns
null. Application queries PostgreSQL, returns data to user, and writes data to Redis with a Time-To-Live (TTL) (e.g. 1 hour).
The Invalidation Rule: DELETE, Don't Update.
When a product's price or description is updated in PostgreSQL:
Always DELETE (evict) the Redis key instead of attempting to update it.
If Thread A updates the database, and Thread B updates the database 1 millisecond later, network latency could cause Thread A's cache update to land in Redis after Thread B's update, corrupting your cache with stale data. Evicting the key ensures the next read safely fetches fresh state from the database.
Loading the rest of the lesson…