Enable Action Caching in Moleculer Services to Reduce Response Time
Learn how to turn on Moleculer’s built‑in action caching, configure a storage backend, verify cache hits, and roll back the change if needed.
25 Jun 2026, 21:19 UTC

Desired outcome
After completing this guide, your Moleculer service will return cached results for repeated identical action calls, reducing response time and CPU load while preserving the ability to invalidate or disable the cache when data changes.
Prerequisites
- A Node.js project that uses Moleculer (v0.14 or later).
- Access to the broker configuration file (usually
moleculer.config.jsor similar). - If you plan to use Redis as the cache backend, a running Redis instance reachable from the service.
- Basic familiarity with editing JavaScript/TypeScript files and restarting the Node process.
Procedure
-
Choose a cache storage backend
Moleculer supports in‑memory (default), Redis, or MongoDB. For a single‑node dev setup, in‑memory is sufficient. For production or clustered deployments, Redis is commonly used because it shares cache across nodes.
-
Enable caching in the broker options
Open your broker configuration file and add a
cachesection. Below are two examples; replace placeholders with your actual values.// moleculer.config.js module.exports = { cache: { enabled: true, // master switch // In‑memory backend (default) // redis: { host: 'localhost', port: 6379 } // uncomment for Redis }, // other broker options … };If you use Redis, ensure the service can reach the host and port; otherwise the broker will throw an error when a cached action is invoked.
-
Configure caching for a specific action
You can enable caching globally (all actions) or per‑action. To cache only certain actions, add a
cacheproperty to the service schema.// services/user.service.js module.exports = { name: 'users', actions: { // This action will be cached async getProfile(ctx) { const { userId } = ctx.params; // Simulate expensive work await new Promise(r => setTimeout(r, 200)); return { id: userId, name: 'Alice', role: 'admin' }; } }, // Cache settings for all actions in this service cache: { ttl: 5000, // time‑to‑live in milliseconds // Use specific params to build the cache key keys: ['params.userId'] } };The
keysarray tells Moleculer which resolved parameters to include in the generated cache key. You can also provide a customkeyResolverfunction if your parameters are complex objects. -
Start (or restart) the service
After saving the changes, restart your Node process so the broker loads the new configuration.
-
Verify that caching is working
- Call the action twice with identical parameters (e.g.,
userId: 42) using a client, the Moleculer CLI, or a test script. - Check the broker logs. You should see a line similar to
cache misson the first call andcache hiton the second. - Optionally, measure response times; the second call should be noticeably faster.
- If you chose Redis, run
KEYS moleculer:cache:*inredis-cliafter the first call to confirm a key appears, and verify it disappears after the TTL expires.
- Call the action twice with identical parameters (e.g.,
Expected checks
- Log entries show
cache missfor the first invocation andcache hitfor subsequent identical calls. - Response time of the second call is significantly lower than the first (e.g., >50 % reduction).
- When using Redis, the
KEYScommand lists cache keys that match the patternmoleculer:cache:*. - Disabling caching (
enabled: false) removes thecache hit/misslogs and restores original response times.
Recovery options (rollback)
Enabling caching only changes broker configuration and does not alter persistent data. To revert:
- Set
cache.enabled: falsein the broker options or remove thecacheproperty from the service schema. - Restart the service.
- Confirm that log entries no longer contain
cache hitorcache missand that response times return to baseline.
If you experience stale data because the TTL is too high, lower the ttl value or manually invalidate with this.broker.cache.clear('users.getProfile') (replace with your service/action name) after data updates.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.