{"id":17885,"library":"rate-limit-memcached","title":"Memcached Store for express-rate-limit","description":"rate-limit-memcached is a specialized storage adapter designed for the `express-rate-limit` middleware, enabling distributed rate limiting using a Memcached server. It is currently at version 1.0.1, indicating a stable and mature state after its initial 1.0.0 release. The package is actively maintained within the `express-rate-limit` ecosystem, though a specific release cadence is not formally stated. Its key differentiator is providing an efficient, externalized state management solution for rate limiting, making it suitable for applications deployed across multiple instances where local memory stores are insufficient. It integrates seamlessly with the core `express-rate-limit` package, providing configuration options for Memcached server locations and custom client instances, ensuring flexibility for various deployment scenarios.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/express-rate-limit/rate-limit-memcached","tags":["javascript","express","api","memcached","rate-limit","typescript"],"install":[{"cmd":"npm install rate-limit-memcached","lang":"bash","label":"npm"},{"cmd":"yarn add rate-limit-memcached","lang":"bash","label":"yarn"},{"cmd":"pnpm add rate-limit-memcached","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the core rate limiting middleware.","package":"express-rate-limit","optional":false},{"reason":"Used as the default Memcached client if none is provided via the `client` option.","package":"memcached","optional":true}],"imports":[{"note":"Primarily designed for ESM. While CommonJS might work via transpilation, direct `require` can lead to issues with named exports.","wrong":"const MemcachedStore = require('rate-limit-memcached')","symbol":"MemcachedStore","correct":"import { MemcachedStore } from 'rate-limit-memcached'"},{"note":"This is a peer dependency; ensure it's imported from `express-rate-limit`.","wrong":"const rateLimit = require('express-rate-limit')","symbol":"rateLimit","correct":"import { rateLimit } from 'express-rate-limit'"},{"note":"Type import for configuring the store, useful for TypeScript projects.","symbol":"MemcachedStoreOptions","correct":"import type { MemcachedStoreOptions } from 'rate-limit-memcached'"}],"quickstart":{"code":"import express from 'express';\nimport { rateLimit } from 'express-rate-limit';\nimport { MemcachedStore } from 'rate-limit-memcached';\n\nconst app = express();\n\n// Ensure a Memcached server is running at localhost:11211 or specified locations\nconst limiter = rateLimit({\n\twindowMs: 15 * 60 * 1000, // 15 minutes\n\tmax: 100, // Limit each IP to 100 requests per window\n\tstandardHeaders: 'draft-7',\n\tlegacyHeaders: false,\n\tstore: new MemcachedStore({\n\t\t// Optional: The prefix attached to all keys stored in Memcached\n\t\tprefix: 'rl:', \n\t\t// The server location(s), passed directly to Memcached client constructor\n\t\tlocations: [process.env.MEMCACHED_HOST || 'localhost:11211'], \n\t\t// Optional: Pass a custom Memcached client instance instead of default\n\t\t// client: myCustomMemcachedClient\n\t}),\n});\n\n// Apply the rate limiting middleware to all requests\napp.use(limiter);\n\napp.get('/', (req, res) => {\n  res.send('Hello, you are rate limited!');\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n  console.log('Ensure Memcached is running, e.g., `memcached -p 11211`');\n});\n","lang":"typescript","description":"Demonstrates initializing `express-rate-limit` with `rate-limit-memcached` for distributed rate limiting across an Express application, using environment variables for Memcached host."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or higher.","message":"This package requires Node.js version 16 or above. Older Node.js versions are not supported and may lead to runtime errors.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Ensure Memcached is installed and running at the specified `locations` (default `localhost:11211`). Verify network connectivity and firewall rules if Memcached is remote.","message":"This store requires a running Memcached server instance. If no server is accessible at the configured `locations`, the rate limiter will fail to store and retrieve counts, potentially leading to uncaught errors or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install or upgrade `express-rate-limit` to a version `>= 6.0.0`.","message":"The `express-rate-limit` peer dependency must be version 6 or greater. Earlier versions are not compatible and will result in installation warnings or runtime errors.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"If you encounter issues with the default client, consider installing `memcached` directly, passing your own `Memcached` client instance, or debugging the default client's connection via the `locations` and `config` options.","message":"If you don't provide a custom `client` option to `MemcachedStore`, the package internally instantiates the `memcached` npm package. Ensure `memcached` is accessible if you rely on this default behavior.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Use `import { MemcachedStore } from 'rate-limit-memcached';` for ES Modules. Ensure your project is configured for ESM if using `type: module` in `package.json`.","cause":"Attempting to import `MemcachedStore` using CommonJS `require()` syntax or incorrect destructuring in an ESM context.","error":"TypeError: MemcachedStore is not a constructor"},{"fix":"Start your Memcached server (e.g., `memcached -p 11211`). Verify the `locations` array in `MemcachedStore` options. Check firewall settings or network connectivity for remote servers.","cause":"The Memcached server is not running or is not accessible at the specified `locations`.","error":"Memcached connection error: Connection refused"},{"fix":"Install `express-rate-limit` explicitly: `npm install express-rate-limit@^6`. If conflicts persist, try `npm install --force` (use with caution) or `npm install --legacy-peer-deps`.","cause":"The `express-rate-limit` peer dependency version requirement is not met by other installed packages or is missing.","error":"npm ERR! ERESOLVE unable to resolve dependency tree"},{"fix":"Convert your project to use ES Modules by adding `\"type\": \"module\"` to your `package.json` and using `import` statements, or configure your bundler (Webpack, Rollup, etc.) to handle CommonJS modules.","cause":"Attempting to use `require()` in an ES Module context without proper setup or bundler transformation.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}