September 26, 2024

Announcing On-Demand Cache Invalidation for Prisma Accelerate

Caching with Prisma Accelerate can significantly boost performance for read-heavy applications while reducing strain on your database. On-demand cache invalidation gives you precise control over refreshes, simplifying management and optimization while ensuring that up-to-date information is always served.

Prisma Accelerate's cache invalidation API in a code block

Quick recap on caching:

Caching stores frequently accessed data in a temporary layer for quicker access, minimizing the need for repeated fetching from the original source. Prisma Accelerate caches data in the location closest to your server to provide faster data retrieval.

Explore our speed test to experience firsthand how caching can dramatically improve your application's performance. Prisma Accelerate speed test result GIF

Benefits of caching:

  • Improves performance by reducing latency
  • Lowers server load and resource usage
  • Enhances user experience with faster response times
  • Reduces network bandwidth consumption
  • Increases scalability by handling more traffic

However, keeping cached data accurate is key. On-demand cache invalidation, which removes outdated data, ensures users receive real-time information. This is a tricky balance—improper invalidation can result in either serving stale data or clearing the cache unnecessarily, impacting both performance and reliability.

The Importance of cache invalidation

On-demand cache invalidation is crucial for maintaining data integrity while benefiting from the speed of having cached data. With earlier versions of Prisma Accelerate, depending on the cache strategy, you had to wait for TTL or SWR timers to expire, limiting control over data refresh timing. Now, with on-demand cache invalidation, you can refresh your cache exactly when needed, allowing for a more dynamic and responsive experience.

Use-case: Hackernews forum

Imagine a scenario with Hackernews, where new posts and upvotes are constantly being added. Caching can dramatically speed up fetching popular stories, reducing server load. However, without proper on-demand invalidation, users could be shown outdated rankings, comments, or even entirely removed posts. This delay can mislead users with outdated data, degrading the experience and lowering engagement.

For example, a post gaining significant upvotes won’t reflect in real-time without on-demand invalidation, leaving the top posts list inaccurate. By employing this technique, updates like votes, comments, or edits are consistently reflected, keeping the feed fresh and users engaged.

How to add Prisma Accelerate on-demand cache invalidation to your project

Continuing from the Hackernews example, you’re retrieving a cached list of the most recent posts. With a query like the one below, which retrieves the latest posts and caches the result with a high Time-to-Live (TTL) value, the load on the database is significantly reduced:

Now, with Prisma Accelerate, you can invalidate the cache by using tags, which group cached query results for easier management. Let’s look at an example:

  1. First, add a tag to the cacheStrategy of your query:

  2. Then, when adding a new post, use the $accelerate.invalidate to refresh the cache immediately with on-demand invalidation:

  3. Similarly, when you upvote a post, you can invalidate the cache as well:

And that’s how simple it is to achieve on-demand cache revalidation. Check out the example app to see how it works.

Start caching your queries

Leverage on-demand cache invalidation to enhance query performance, improve the overall responsiveness of your app, and reduce load on the database.


Get started with Accelerate

Read the docs on caching with Accelerate

Stay tuned for more exciting updates on X, and keep an eye on our changelog. If you need any help, feel free to reach out on our Discord.

Don’t miss the next post!

Sign up for the Prisma Newsletter