HareMq.DedupCache (hare_mq v1.4.0)

Copy Markdown

ETS-backed deduplication cache for RabbitMQ message processing.

Storage

Each cache instance owns a named ETS table (:named_table, :public, :set). The table name is derived from the registered server name so lookups can go directly to ETS without passing through the GenServer message queue.

Writes vs reads

  • add/3 (and add/4) — synchronous GenServer.call. This guarantees that a subsequent is_dup? call on any process will see the entry.
  • is_dup?/2 (and is_dup?/3) — reads ETS directly, completely bypassing the GenServer. Concurrent read throughput is not limited by a single process.

Expiry

A 1-second timer runs handle_info(:clear_cache) which calls :ets.select_delete/2 to remove all rows whose expired_at timestamp is in the past. This is a C-level operation with no Elixir-side allocation, keeping the GenServer responsive regardless of cache size.

Named instances

Pass name: to start_link/1 to run isolated caches side-by-side:

{HareMq.DedupCache, name: {:global, :dedup_cache_tenant_a}}

Then pass the same name as the third/fourth argument to is_dup?/3 and add/4. Publishers accept a dedup_cache_name: option.

Summary

Functions

add(message, deduplication_ttl, deduplication_keys \\ [])

add(message, deduplication_ttl, deduplication_keys, cache_name)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(table_name)

Callback implementation for GenServer.init/1.

is_dup?(message, deduplication_keys \\ [])

is_dup?(message, deduplication_keys, cache_name)

start_link(opts \\ [])