KV / Counters

Counters

Counters are numbers by key that only add up: sign-in attempts per IP, page views, uses of a feature. An add returns the new count, a counter can expire like any key, and a counter that may lose its last second to a crash can be kept in memory and cost no commit at all.

const attempts = store.counters('login-attempts', { ttl: '15m' })
const views = store.counters('page-views', { flushEvery: '1s' })

const n = await attempts.add(clientIp, 1) // 1, 2, 3…
await attempts.get(clientIp) // the count, 0 when there is none
await views.add('/notes', 1)
attempts = store.counters("login-attempts", ttl="15m")
views = store.counters("page-views", flush_every="1s")

n = await attempts.add(client_ip, 1)  # 1, 2, 3…
await attempts.get(client_ip)  # the count, 0 when there is none
await views.add("/notes", 1)
attempts, err := kv.OpenCounters(store, "login-attempts", kv.TTL(15*time.Minute))
views, err := kv.OpenCounters(store, "page-views", kv.FlushEvery(time.Second))

n, err := attempts.Add(ctx, clientIP, 1) // 1, 2, 3…
count, err := attempts.Get(ctx, clientIP)
_, err = views.Add(ctx, "/notes", 1)
let attempts = store.counters("login-attempts").ttl(Duration::from_mins(15)).open()?;
let views = store.counters("page-views").flush_every(Duration::from_secs(1)).open()?;

let n: i64 = attempts.add(&client_ip, 1)?; // 1, 2, 3…
views.add("/notes", 1)?;

Written each add, or kept in memory

CounterAn addA crash losesJoins a transaction
defaulta durable write, shared with othersnothingyes
flushEvery: '1s'a number in memory, written every secondat most that secondno

flushEvery is flush_every in Python and kv.FlushEvery in Go.

How counters behave

  • An absent or expired counter is 0.
  • A counter's ttl window starts with its first add and does not slide: 15 minutes after the first attempt it starts from 0 again. A lockout that should not reset on a clock's edge is a rate limit.
  • A sum past what an int64 holds is limit, and changes nothing.
  • On a node that copies the file from its home, an add of counters without flushEvery answers at once, with the copy's count and the adds still queued. The home counts it once, however often it is sent (contract REPL-12). An add with flushEvery, and every delete and clear, passes on to the home and waits for its answer. get reads the node's copy.

Async Rust

With the async feature, aio's counters add up the same way, and you await each call. See Rust with async.

rust
use std::time::Duration;
use tinystore::Options;
use tinystore::aio::Store;

let store = Store::open("data", Options::default())?;
let attempts = store.counters("login-attempts").ttl(Duration::from_mins(15)).open()?;
let views = store.counters("page-views").flush_every(Duration::from_secs(1)).open()?;

let n: i64 = attempts.add(&client_ip, 1).await?; // a durable write, shared with others
views.add("/notes", 1).await?; // a number in memory: answered in its first poll

The builder decides where a call runs, never the call:

Counteradd and deleteclear
defaultqueued at the writer of kv.db in the first pollqueued at the writer
flush_everyin memory, in the first poll, or on the store's threads when memory must flush firston the store's threads

get answers from memory, or reads kv.db in the first poll when a reader is idle. Once its future was polled, an add or a delete takes effect even if you drop the future, in memory or in kv.db.

See also

Edit this page on GitHub Updated 3 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results