Urgent.News

What's breaking now, across thousands of outlets.

Tech

Preventing Cache Penetration in Spring Boot Using Redis and Bloom Filters

Preventing Cache Penetration in Spring Boot Using Redis and Bloom Filters Cache penetration occurs when high-frequency requests query non-existent keys, bypassing the Redis cache completely and hitting the relational database directly. Here is how we set up a Bloom Filter guard layer in front of Redis and PostgreSQL. 1. The Bloom Filter Guard Concept A Bloom Filter is a space-efficient…

Cache penetration is a problem where frequent requests for non-existent keys bypass the Redis cache and directly hit the relational database. To prevent this, a Bloom Filter guard layer can be implemented in front of Redis and PostgreSQL. The Bloom Filter is a compact probabilistic data structure that can efficiently check if an element is definitely not in a set or might be in a set.

In the code, a `CachePenetrationGuard` class is defined with a `BloomFilter` instance. The filter is configured to handle 500,000 expected insertions with a 1% false positive rate. The `registerKey` method adds an account ID to the filter, while `mightContain` checks if an account ID is likely to be present.

In the `AccountService`, before querying Redis or PostgreSQL, the Bloom Filter is consulted. If the account ID is not likely to be in the filter, the request is immediately rejected, saving the database from unnecessary lookups. If the filter indicates the account ID might be present, Redis is checked. If the data is found in Redis, it is returned. If not, the data is fetched from the database, cached in Redis, and then returned.

Combining Bloom Filters with Redis' TTL jitter can effectively shield backend databases from cache penetration and traffic spikes in production.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Waiting Is Not a Tool Call: Making an MCP Server's Shell Event-Driven

One of our agents ran a test suite. The suite takes four minutes. The MCP client's idle timeout is sixty seconds. You can see where this is going. At second sixty the client cancelled the call.

  • MCP client's idle timeout set to 60 seconds, causing indefinite process running
  • octofs, open-source MCP filesystem server, developed to address issue
  • Background jobs introduced to allow concurrent command execution without blocking

More from Wednesday 2 September →