Medusa provides commerce building blocks for developers to build rich commerce applications. With the latest evolution of our toolkit towards a more modular architecture, we are taking steps to make Medusa more agnostic to infrastructure. As a big step in this direction, we are excited to announce Medusa’s new cache modules.
In this article, we will look under the hood to see how cache modules work, which cache module you can use in your project today, and, finally, we will demonstrate how easily a Memcached module can be built.
How it works
Cache modules in Medusa implement the
interface and are used internally to cache data like product prices and tax rates.Copy to clipboardICacheService
Medusa’s default starter project comes with an in-memory cache module (
) pre-installed, which uses a plain JavaScript Map object to store data. This is great for testing and development purposes, but not recommended for production environments. You can have one cache module installed at a time and for production environments, we have built a cache module that uses Redis (Copy to clipboard@medusajs/cache-inmemory
). To enable the Redis cache module, simply install the package:Copy to clipboard@medusajs/cache-redis
And configure it in your
file:Copy to clipboardmedusa-config.js
And that’s it. With just a couple of changed config lines, you use a different data store for our caching layer.
Building a cache module
As mentioned above, we are making in-memory and Redis cache modules available today. Building a new cache module is, however, very easy if you have a different caching tool in place already or prefer another caching technology. To demonstrate how to build a cache module, we will implement
to work with Memcached. For communication with the Memcached instance, we will be using the Memcached client for Node.js.Copy to clipboardICacheService
The first thing to do is to create our project. We need a basic TypeScript project configured for Node. You can clone a preconfigured starter here, and after running
, start building.Copy to clipboardyarn install
To implement the service, create a new TypeScript file in
:Copy to clipboardsrc/services
And that’s it. The final thing to add is export from the
file like so:Copy to clipboardindex.ts
You can find the entire implementation here.
To test the module with a Medusa instance, link your module project with a Medusa instance:
Next, we need to add this package to our Medusa project and tweak our
file:Copy to clipboardmedusa-config.js
With this example, we see how straightforward it is to build a (cache) module for Medusa. This, combined with the fact that Medusa is an open-source project, shows the advantages users of such projects have in terms of a growing ecosystem of existing modules.
What’s next
The introduction of cache modules is a step in adding infrastructure agnosticism to Medusa’s architecture. This will make it easier to integrate Medusa into existing stacks and improve the developer experience. Our introduction of EventBus modules is a similar evolution to Medusa’s architecture. We are excited to see what you build with these new tools and can’t wait to launch more modules in the near future.
Learn more about Cache Modules in our documentation.
Share this post
Try Medusa
Spin up your environment in a few minutes.

You may also like
On this page