Medusa Cache

In this guide, you'll learn about Medusa Cache.

What is Medusa Cache?#

Medusa Cache is a caching layer that improves your application's performance and reduces costs. It's available to all environments and plans out of the box at no additional cost.

Medusa Cache relies on the existing key/value Redis store already provisioned for environments. Each environment has its own isolated cache.

Diagram showcasing Redis isolation between environments


Enable Medusa Cache#

To enable Medusa Cache for your Medusa project deployed on Cloud, set the caching feature flag in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  featureFlags: {4    caching: true5  }6})

This will enable all Medusa Cache functionality in your project.


How Does Medusa Cache Work?#

Cached Data in API Routes#

By using Medusa Cache, data is cached across several business-critical APIs to boost performance and throughput. This includes all cart-related operations, where the following data is cached:

  • Regions
  • Promotion codes
  • Variant price sets
  • Variants
  • Shipping options
  • Sales channels
  • Customers

Integrated into Query#

Medusa Cache is built on the new Caching Module. The module has been integrated into Query so that query.graph calls can be cached easily.

You can enable caching for your custom query.graph calls by passing a cache option. For example:

Code
1const { data: products } = await query.graph({2  entity: "product",3  fields: ["id", "title", "handle"],4}, {5  cache: {6    enable: true7  }8})

This code caches the query result. When the same query is made again, the cached result is returned instead of querying the database.

Diagram showcasing cache miss and cache hit

Automatic Invalidation#

By default, data is cached for one hour or until invalidated. Invalidation occurs automatically when related data is created, updated, or deleted.

Learn more about automatic invalidation in the Caching Module documentation.


Performance Benchmark Comparisons#

The following benchmark comparisons demonstrate the performance improvements you can expect with Medusa Cache. These were conducted on a Cloud environment with a standard setup.

Endpoint

Without Medusa Cache (ms)

With Medusa Cache (ms)

Improvement

Product Operations

GET /store/products

354.00

83.00

~77%

GET /store/product-categories

88.00

51.00

~42%

GET /store/products/:id

161.00

123.00

~24%

Cart Operations

POST /store/carts/:id/line-items

697.00

427.00

~39%

POST /store/carts/:id/shipping-methods

1002.00

670.00

~33%

POST /store/carts/:id/promotions

302.00

203.00

~33%

GET /store/carts/:id

109.00

73.00

~33%

POST /store/carts/:id

909.00

656.00

~28%

POST /store/carts

446.00

329.00

~26%

POST /store/carts/:id/complete

1357.00

1018.00

~25%

Order Operations

GET /store/orders/:id

119.00

81.00

~32%

Other Operations

GET /store/regions

89.00

51.00

~43%

GET /store/shipping-options

272.00

173.00

~36%

POST /store/payment-collections

238.00

160.00

~33%

POST /store/payment-collections/:id/payments

152.00

102.00

~33%

Was this guide helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break