Skip to content

Redis native compressed bitmaps (redis-roaring) - #15331

Merged
sundb merged 749 commits into
redis:unstablefrom
aviggiano:unstable
Oct 14, 2026
Merged

sundb merged 749 commits into
redis:unstablefrom
aviggiano:unstable

Conversation

@aviggiano

@aviggiano aviggiano commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15296

Native Roaring bitmaps

Adds a native Roaring bitmap representation for Redis bitmaps, backed by vendored CRoaring v4.7.2 (deps/croaring, built with SIMD disabled and symbols hidden for portability). A string bitmap costs one byte per 8 bit positions up to its highest offset, regardless of how many bits are set; sparse or large bitmaps are instead stored compressed, cutting memory dramatically while preserving exact bit semantics.

Type & object model

  • New object type OBJ_BITMAP with encoding OBJ_ENCODING_BITMAP_ROARING. TYPE → bitmap, OBJECT ENCODING → bitmap-roaring.
  • Object header: encoding widened 4→5 bits to fit the new encoding value; to keep redisObject the same width, refcount drops 23→22 bits, lowering the maximum non-shared refcount from 8388605 to 4194301 (still far above any real per-object refcount).

Enabling it — bitmap-default-roaring

  • New boolean config (default no, runtime-modifiable). When off, bitmap writes keep creating plain strings. Native bitmaps remain supported and can enter the keyspace through BITOP with a Roaring source, RESTORE, replication, or loading an RDB/AOF containing them.
  • When on, bit-writes that create or extend a bitmap produce a native Roaring bitmap, an existing string bitmap converts in place on the next bit-write (TTL and module metadata preserved), and BITOP destinations are Roaring. Automatic creation/conversion is disabled on replicas and when a write's propagation excludes either AOF or replicas — notably RM_Call() without !, selective RM_Call() flags, and scripts using redis.set_repl(); such writes keep plain strings on every node.

Command surface & string boundary

  • Bit commands operate on both representations transparently: SETBIT, GETBIT, BITCOUNT, BITPOS, BITOP, BITFIELD, BITFIELD_RO.
  • A native Roaring bitmap is a distinct type: plain string commands (GET, GETRANGE, APPEND, SETRANGE, STRLEN, …) return WRONGTYPE. DEBUG BITMAP-RAW exposes the materialized bytes.
  • Bit offsets follow the historical string rule on both representations: capped at proto-max-bulk-len × 8. Internal representability is bounded at 2^60−1 logical bytes (BITROAR_MAX_BYTES). BITOP NOT over a Roaring source bounds missing-chunk allocation with a budget derived from proto-max-bulk-len (minimum 65,536 chunks of 8 KiB). Replication and AOF replay bypass this budget to apply accepted operations; dense string sources are exempt.

Persistence

  • Adds RDB_TYPE_BITMAP (33) under the existing RDB version 16 introduced for 8.12 (this PR does not bump it). This relies on shipping in 8.12: 8.12 milestone builds without this PR reject type 33 cleanly, and if the PR slips past 8.12, RDB_VERSION must be bumped. The RDB/DUMP payload is the logical byte length followed by the CRoaring64 portable serialization — compact, not a dense byte expansion. Loading enforces the representability bound and validates the blob.

Replication & AOF — deterministic by construction

  • Representation decisions are never re-derived from a replica's or AOF-loader's own config. The transition travels in the stream as the internal BITCONVERT <key> and BITROAROP commands (hidden from COMMAND, internal-client gated, since 8.12.0; ACL CAT and ACL DRYRUN are not yet filtered and still list them):
    • SETBIT/BITFIELD creating or converting a bitmap replay as BITCONVERT followed by the original command, grouped in MULTI/EXEC;
    • a BITOP whose destination is Roaring only by configuration replays as internal BITROAROP, which stores the native result directly and preserves notification and callback ordering;
    • operations on already-Roaring operands propagate verbatim.
  • BITCONVERT applies the same in-place transition the primary performed: TTL and module metadata retained, no value payload in the stream.

Notifications, modules, cluster

  • NOTIFY_BITMAP (class b) keyspace events; conversion is an observable in-place type change: type_changed (class c) then the command's own event when it changes the logical value — never overwritten. A conversion that leaves the logical value unchanged still emits type_changed, counts as a write, and is propagated. A newly created native key emits new (class n) rather than type_changed.
  • Modules observe the transition via the type-changed callback; REDISMODULE_KEYTYPE_BITMAP added.
  • Works under Cluster, including atomic slot migration (tests/unit/cluster/bitmap.tcl, atomic-slot-migration.tcl coverage).

Memory accounting & active defrag

  • Full memory accounting; all CRoaring allocations route through zmalloc. Active defrag walks the Roaring64 ART and its ARRAY, BITSET, and RUN containers (via a small vendor patch exposing roaring64 internals), and fork-child memory is dismissed correctly. INFO keysizes gains a distrib_bitmaps_sizes histogram of logical byte length, not memory: a sparse Roaring bitmap can be orders of magnitude smaller than its bucket, unlike distrib_<collection>_sizes, which reports allocation size. Bitmap allocation sizes are tracked internally but, as for strings, not reported by INFO.

Testing

  • tests/unit/bitroar.tcl (encoding, conversion, RDB/AOF/replication determinism incl. mode-mismatch masters/replicas, defrag, module boundary), string↔Roaring parity oracle cases in the same suite, plus additions to bitops.tcl, memefficiency.tcl, corrupt-dump-fuzzer.tcl, info-keysizes.tcl, integration/replication-3.tcl, integration/dismiss-mem.tcl, cluster suites, moduleapi/keyspace_events transition-callback replay coverage, and moduleapi/propagate selective-propagation coverage.

Documentation


Note

Medium Risk
Adds a large vendored dependency with local patches to internal 64-bit layout; incorrect integration could affect build portability and future memory/defrag paths that walk CRoaring allocations.

Overview
Vendors CRoaring v4.7.2 under deps/croaring as the C Roaring bitmap engine for Redis’s native compressed bitmap encodings, with upstream sources plus Redis-maintained build/docs (not upstream CMake).

Build integration: deps/Makefile gains a croaring target that produces libroaring.a with hidden symbol visibility, x64 SIMD and NEON disabled (ROARING_DISABLE_X64, DISABLENEON), and LTO not inherited from Redis so CI/cross-builds stay linkable. distclean and .gitignore cover the new archive; AR is AR?= so cross-compile toolchains can override the archiver.

Redis-specific maintenance: deps/README.md documents upgrade steps, local patches (portability/endian/atomics in portability.h, and 64-bit internal layout exposure for bitroar memory accounting, fork-child dismissal, and active defrag), and a small refcount-none regression test in the croaring Makefile.

Reviewed by Cursor Bugbot for commit 8963e1c. Bugbot is set up for automated code reviews on this repo. Configure here.

@jit-ci

This comment was marked as resolved.

Comment thread src/defrag.c Resolved
Comment thread src/rdb.h Resolved
Comment thread src/object.h Resolved
Comment thread src/lazyfree.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
@sundb

This comment was marked as resolved.

Comment thread src/bitmap_roaring.c Resolved
Comment thread src/rdb.h Resolved
Comment thread redis.conf Resolved
Comment thread src/rdb.c Resolved
Comment thread src/bitmap_roaring.c Resolved
Comment thread src/bitmap_roaring.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitmap_roaring.c Resolved
Comment thread src/bitmap_roaring.c Resolved
Comment thread src/debug.c Resolved
Comment thread src/bitmap_roaring.c Resolved
Comment thread src/debug.c Resolved
Comment thread src/debug.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/rdb.c Resolved
Comment thread src/server.h Resolved
Comment thread src/rdb.c Resolved
Comment thread src/rdb.c Resolved
Comment thread src/bitops.c Resolved
@sundb
sundb self-requested a review June 22, 2026 05:03
@aviggiano

ghost commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

@sundb Could you please re-review the latest changes? The remaining October 12 feedback is addressed, the upstream conflict is resolved, and the CLA check now passes. The feedback changes remain in separate commits.

@sundb

ghost commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes indication that this issue needs to be mentioned in the release notes state:needs-doc-pr requires a PR to redis-doc repository

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[NEW] Compressed bitmaps on Redis

8 participants