-
-
Notifications
You must be signed in to change notification settings - Fork 162
DynamoDB
Roman edited this page Dec 16, 2023
·
3 revisions
Note, if you want to avoid reaching limits on DynamoDB or paying for extra requests, consider inMemoryBlockOnConsumed option to be set.
const {DynamoDB} = require('@aws-sdk/client-dynamodb')
const {RateLimiterDynamo} = require('rate-limiter-flexible')
async function createDynamoDbRateLimiter() {
const dynamoClient = new DynamoDB({
region: 'eu-central-1',
credentials: {
accessKeyId: 'fake',
secretAccessKey: 'fake'
},
endpoint: 'https://dynamodb.eu-central-1.amazonaws.com'
});
return new Promise((resolve, reject) => {
const rateLimiter = new RateLimiterDynamo({
storeClient: dynamoClient,
dynamoTableOpts: {
readCapacityUnits: 30, // default is 25
writeCapacityUnits: 30, // default is 25
},
points: 4,
duration: 1,
},
(err) => {
if (err) {
reject(err)
} else {
resolve(rateLimiter)
}
})
})
}
createDynamoDbRateLimiter()
.then((rateLimiterDynamo) => {
rateLimiterDynamo.consume('user-id', 2)
.then((rateLimiterRes) => {
console.log(rateLimiterRes)
// {
// remainingPoints: 2,
// msBeforeNext: 987,
// consumedPoints: 2,
// isFirstInDuration: true
// }
})
.catch((rateLimiterResOrError) => {
})
})
.catch((err) => {
})
Get started
Middlewares and plugins
Migration from other packages
Limiters:
- Redis
- Memory
- DynamoDB
- Prisma
- MongoDB (with sharding support)
- PostgreSQL
- MySQL
- BurstyRateLimiter
- Cluster
- PM2 Cluster
- Memcached
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- RLWrapperBlackAndWhite Black and White lists
Knowledge base:
- Block Strategy in memory
- Insurance Strategy
- Comparative benchmarks
- Smooth out traffic peaks
-
Usage example
- Minimal protection against password brute-force
- Login endpoint protection
- Websocket connection prevent flooding
- Dynamic block duration
- Different limits for authorized users
- Different limits for different parts of application
- Block Strategy in memory
- Insurance Strategy
- Third-party API, crawler, bot rate limiting