Bun Stores

hitlimit-bun provides optimized storage backends that take advantage of Bun's native capabilities. The native SQLite store is particularly powerful, offering persistence with exceptional performance.

Memory Store

The default store, ideal for single-instance deployments:

memory.ts
import { hitlimit, memoryStore } from '@joint-ops/hitlimit-bun'

const limiter = hitlimit({
  limit: 100,
  window: '1m',
  store: memoryStore()  // Default, can be omitted
})

Pros: Zero latency, no dependencies
Cons: Lost on restart, single-instance only

Native SQLite Store

Bun includes native SQLite support via bun:sqlite. This store provides persistence with near-memory performance.

sqlite.ts
import { hitlimit, bunSqliteStore } from '@joint-ops/hitlimit-bun'

const limiter = hitlimit({
  limit: 100,
  window: '1m',
  store: bunSqliteStore({
    path: './rate-limits.db'
  })
})

SQLite Store Options

sqlite-options.ts
const store = bunSqliteStore({
  // Database file path (required)
  path: './data/rate-limits.db',

  // Table name (default: 'rate_limits')
  tableName: 'api_limits',

  // Enable WAL mode for better concurrency (default: true)
  walMode: true,

  // Cleanup expired entries interval in ms (default: 60000)
  cleanupInterval: 30000,

  // Use in-memory SQLite (not persisted)
  // path: ':memory:'
})

SQLite Performance Tips

  • WAL Mode - Enabled by default for better concurrent read/write performance
  • Prepared Statements - hitlimit uses prepared statements for optimal query performance
  • Automatic Cleanup - Expired entries are cleaned periodically to prevent bloat

In-Memory SQLite

For testing or ephemeral rate limiting with SQLite's API:

const store = bunSqliteStore({
  path: ':memory:'
})

Redis Store

For distributed deployments across multiple instances:

redis.ts
import { hitlimit } from '@joint-ops/hitlimit-bun'
import { redisStore } from '@joint-ops/hitlimit/stores/redis'

const limiter = hitlimit({
  limit: 100,
  window: '1m',
  store: redisStore({
    url: 'redis://localhost:6379',
    prefix: 'rl:'
  })
})

Custom Store

Implement the store interface for custom backends:

custom-store.ts
import type { Store } from '@joint-ops/hitlimit-bun'

const customStore: Store = {
  async increment(key: string, windowMs: number) {
    // Increment and return current count + reset time
    return { count: 1, reset: Date.now() + windowMs }
  },

  async get(key: string) {
    // Return current count and reset time
    return { count: 0, reset: 0 }
  },

  async reset(key: string) {
    // Reset the counter for a key
  }
}

Store Comparison

Store Persistence Distributed Performance
Memory No No Fastest
SQLite Yes No Very Fast
Redis Yes Yes Fast

Next Steps