DEV Community

Vishwa
Vishwa

Posted on

Cache-Aside with Memorystore: Serve the Same Product Page Without Asking the Database Every Time

Read Redis first, fall back to Cloud SQL, and have a plan for stale data

Say a phone goes on offer on a Flipkart-style store, and its product page is opened 10,000 times in a few minutes. The details and price are the same for everyone. Without a cache, the database answers the same question 10,000 times.

The problem

Cloud SQL is the source of truth for product details and price. It is good at that, but every read costs CPU, connections and time. When a few popular products get most of the traffic, the database spends its effort repeating itself, and real work like placing orders waits in the same queue.

The CDN does not solve this one. It is great for files that are the same for everyone, such as product images. Price and stock are data that the app builds and changes, so they belong in a cache next to the app, not at the edge.

The solution: cache-aside with Memorystore

Memorystore is Google Cloud's managed Redis. It is not behind the load balancer: it has a private IP in your VPC, and the app talks to it directly.

Cache-aside means the app owns the cache logic:

def get_product(product_id):
    key = f"product:{product_id}"
    cached = redis.get(key)
    if cached:
        return cached                      # hit: about 1 ms
    product = cloud_sql.fetch_product(product_id)   # miss: go to the source of truth
    redis.set(key, product, ex=300 + random_jitter())  # 5 min TTL + jitter
    return product
Enter fullscreen mode Exit fullscreen mode

On the first request, Redis misses, the app reads Cloud SQL and stores the result with a TTL. Later requests are served from memory until the TTL ends. For the phone on offer, the database sees one read per TTL window instead of one per visitor.

Handling stale data

A cache is a copy, so it can be wrong. I use three rules.

1. Write to the database, then delete the key. When a seller changes the price, the app updates Cloud SQL first and then deletes product:123. The next read misses and fetches the new price. Deleting is simpler and safer than trying to update the cached value in place.

2. TTL is the safety net. If the delete is missed, for example because the app crashed between the two steps, the key still expires in a few minutes. A random jitter on the TTL stops many keys from expiring at the same moment and sending a burst of misses to the database.

3. Some data is never cached. The payment amount and live stock at checkout come from Cloud SQL every time.

Different data gets different TTLs:

Key TTL Why
product:123 5 min + jitter Fresh enough, jitter avoids mass expiry
cart:user42 30 min Abandoned carts clean themselves up
"not found" result 30 to 60 s Stops bots hammering missing IDs

When Redis is down

The app uses a short timeout on Redis. If Redis is slow or unreachable, it treats the request as a miss and reads Cloud SQL. The site gets slower, but it stays up. Memorystore replication is asynchronous, so if a primary fails, a replica is promoted and a very recent write might be lost. For a cache that is fine: the lost entry becomes a miss.

Where I have used this pattern

In VJourney, my personal travel-companion project, Redis (Upstash) is used as a cache-aside layer, with Postgres and Firestore holding the data. Every part of that stack maps to a GCP service, and in a GCP deployment this role would be played by Memorystore.

Trade-offs and limits

  • Short staleness is a product decision. For a few minutes a user might see an old price on the product page. That is acceptable only because checkout reads the real price from Cloud SQL.
  • Hot keys still exist. One very popular key sits on one Redis shard. For extreme cases, a few seconds of in-process caching on each app instance spreads the load.
  • Memory is sized, not elastic. Memorystore capacity is chosen up front. Before a big sale I would resize or add shards from an alert, not during the spike.
  • Keep related keys together on purpose. In cluster mode, keys are hashed to slots. A hash tag like {user42}:cart keeps one user's keys on the same shard when that matters.
  • Monitor the hit ratio. A falling hit ratio means more load on Cloud SQL, often from a wrong TTL or evictions.

Takeaway

Read Redis first; on a miss, read the database and store with a TTL.

On a write, update the database and delete the key; TTL catches what you miss.

Redis is a cache, and the database is always the source of truth.

Top comments (0)