Build a Rate Limiter With Couchbase Eventing
Blog post from Couchbase
Couchbase Server 8.0 introduces the OnDeploy Eventing function handler, enabling business logic execution during Eventing function deployment or resumption without external triggers. Previously, users relied on manual or scripted setups, but OnDeploy simplifies processes such as pre-flight checks, cache setups, and data management across services. This handler is demonstrated in a project that builds a rate limiter using the token-bucket algorithm, integrating with Couchbase Eventing. The setup involves a multi-dimensional scaling cluster with specific service-to-node mappings to enhance parallelism and efficiency. Keyspaces are defined for managing metadata, rate limits, and user accounts, while external REST API endpoints handle tier mappings and request counts. The OnDeploy handler initializes tier mapping from the API, resets user rate limits hourly, and refreshes tier limits daily, using timers for automation. The OnUpdate handler processes user events to enforce rate limits, updating user usage and forwarding requests within limits to a protected endpoint. This approach showcases the flexibility of Couchbase Eventing for integrated, database-driven application development.