May 24, 2024

Speed and Savings: Caching Database Queries with Prisma Accelerate

Dive into the benefits of per-query caching, showcasing how it can make queries faster, handle traffic surges, minimize infrastructure costs, and keep your users satisfied. Learn how to easily implement Prisma Accelerate and achieve improved app performance and cost savings.

The word "caching" with a starburst behind it, showing that it is important.

Picture this: You and your team just released your latest app, SuperWidget. Everyone is excited, and you’re pretty sure it’s going to be a hit… and it is! SuperWidget is all of a sudden used by every major company in the world. However, you quickly realize that the level of traffic far outweighs what you planned for and your app is starting to have degraded performance.

To solve this, your team jumps into action quickly. You dive into your application monitoring and realize that several queries in your application have a much larger impact than anticipated. After a long night, your team implements a number of infrastructure improvements, most notably a robust caching layer, which takes the load off the rest of your infrastructure. SuperWidget performance is no longer being negatively impacted, and your new customers are happy with their experience.

So, what could you and your team have done better?

While your team was capable, fire drills and all-nighters are the last thing you want. The solution still required a coordinated effort and a lot of wasted engineering hours. Instead, you could have saved time and frustration by using Prisma Accelerate and caching your queries with ease.

Why you should cache database queries

As you saw in the above example, caching is great when you need to reduce database or application load. By caching, you can remove expensive operations from the time it takes to load your application, also known as the “critical path”. Following requests can then use this cached data to avoid spending app or database time computing the result. If improved stability due to reduced app load wasn’t enough, less time spent computing also means your infrastructure can support a higher workload, or your app can run on reduced hardware, saving you money!

An image showing a large number of connections going from clients, to a server, to a database. The database is overloaded and stuck at 100% CPU. There is an X's for eyes emoji next to the database.

An image showing a large number of connections going from clients, to a server, through a cache, to a database. Thanks to the cache, the database is not overloaded. There is an emoji with sunglasses next to the database.

Another reason to cache would be to reduce egress costs. Many cloud providers charge egress costs for data leaving their services. This could be rows being served from your database, static images, even responses to or from other services.

While faster load times and reduced costs are direct benefits of caching, they lead to another benefit: improved perception of your application! Quicker load times lead to a more enjoyable experience and happier customers. Even if visitors are not paying you, an app that feels sluggish to load can lead to users spamming refresh at best and leaving for good at worst.

When considering whether or not to cache a query, you should definitely keep these three reasons in mind, among others. Luckily, Prisma Accelerate can help: Accelerate features per-query caching with minimal egress costs. When using Prisma Accelerate, you can easily and quickly cache a problematic query:

In the first case, where expensive or frequently accessed queries can overwhelm your database, a per-query cache will prevent that load for as long as the data is cached. If cost is the concern, your data will be accessed from your database once and then cached in Accelerate’s collection of globally distributed nodes, preventing additional costs from further database reads while also making your app faster!

When to cache

Now that you know why and how to cache, you may be tempted to begin caching every query. Before you do that, note what happened in our example: you and your team monitored your application before implementing caching.

Caching is great, but any addition can incur a cost or cause unintended side effects. At Prisma, we’re big fans of observability-driven development: instrument your app and make informed decisions. Caching should be a carefully considered option among many. If, for example, you need a certain query to always contain exactly up-to-date data, then caching will probably not be the right fit. On the other hand, cases where data doesn’t change often or doesn’t need to be up-to-date are great fits.

This isn’t to say that you need to monitor production traffic before implementing any sort of caching. Caching can also be a helpful tool during the development process! If you have automated tests in your dev environment and you see a slow query, use Accelerate to quickly cache that query and measure results. If you see a noticeable improvement, maybe caching is a good fit. Regardless of the environment, the advice remains the same: measure and benchmark, make a change, and then measure again.

If you’re interested in query insights and diving deeper into optimizing performance, check out Prisma Optimize!

How Prisma Accelerate can help

As you saw above, adding caching with Prisma Accelerate is as easy as adding a cacheStrategy option to a query. Since caching queries with Prisma Accelerate is on a per-query basis, you are only charged for the queries you do cache. The rest of your application continues to run as-is. As soon as you determine that caching is necessary for a specific query, you can add it with just few lines of code!

In addition to being able to quickly and easily implement caching, using Prisma Accelerate also means no need to set up additional caching infrastructure. While cloud providers have managed key-value stores, in the best case, you still need to manually insert data, manage indexes and replication, and be sure to invalidate when things change. With Prisma Accelerate, this is all handled for you! Add a cache strategy for the query you would like to cache and move on to building more great things.

An image with two emoji. On the left is an emoji that looks dizzy and overwhelmed. Surrounding it are a lot of things that you have to think of when managing caching infrastructure: Redis, memcached, indexes, invalidation, in-memory, sharding, AWS, GCP. The emoji on the right is winking, clearly happy. It has two steps above it: 1. find a query to cache. 2. Use the Prisma Accelerate extension's cacheStrategy feature.

Is Prisma Accelerate right for me?

When investigating any software, there are always a lot of options. We’ve found that Accelerate is a great fit for the following kinds of applications:

  • Static content, such as blog posts
  • Complex queries, such as usage calculation for billing related tasks
  • Read-heavy applications, such as social media platforms, news aggregators, and e-commerce sites

Accelerate is also a great fit for teams that want to onboard engineers fast. Since Accelerate provides managed infrastructure and is built into Prisma Client, even new team members unfamiliar with your codebase can cache queries easily.

Prisma Accelerate is more than just a cache! Accelerate is edge distributed and also handles connection pooling. Be sure to check out the full documentation.

Wrapping up

Caching is still one of the “hard” problems of software engineering and rightfully so! Simply caching more things won’t solve problems and a team needs to take the time to understand what can be cached and how it will impact their product.

While there are many different approaches to caching, Prisma Accelerate makes it easy to implement caching on a pre-query basis and gives you more time to focus on building great products. If you’d like to take the next step, learn more about Accelerate and get started caching your Prisma ORM queries today.

Start caching with Accelerate

Don’t miss the next post!

Sign up for the Prisma Newsletter