modulo
The operation that gives the remainder after division, written % in many programming languages, so 17 % 5 is 2.
In more detail
Hash tables use modulo to fold a large hash into a slot number between zero and the table size minus one. Ring buffers use it to wrap an index back to zero at the end of the array. Languages differ for negative numbers: in Python the result has the same sign as the divisor, while in C, Java and JavaScript it has the sign of the number being divided.
Examples
(7 + 1) % 8 == 0 # index 7 wraps to 0 in an 8-slot buffer7 % 3 == 1Kafka partitioners use hash % partition_count, so adding partitions moves keys.Related terms
- hash bucket — One position in a hash table's underlying array, where the keys whose hash maps to that position are stored.
- wraparound — Moving an index past the last slot of an array back to slot zero, usually computed as (index + 1) % capacity.
- ring buffer — A fixed-size array used as a loop: when writes reach the end they wrap to the start, overwriting the oldest data.
- partitioner — The producer component that chooses which partition each record is sent to.
- murmur2 — A fast, non-cryptographic hash function that Kafka clients use to turn a record key into a partition number.
Taught in
- What A Hash Function Promises — Algorithms & Data Structures for Working Engineers
- Hash Tables: Chaining, Open Addressing And Load Factor — Algorithms & Data Structures for Working Engineers
- Collisions In Practice And Hash Flooding — Algorithms & Data Structures for Working Engineers
- Deques And Ring Buffers — Algorithms & Data Structures for Working Engineers
- Choosing The Right Sequence — Algorithms & Data Structures for Working Engineers
- Where A Record Lands — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Error Storm That Fixes Itself — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Keys And Ordering Per Partition — Apache Kafka & Event Streaming
- Watching Lag And Sizing Partitions — Apache Kafka & Event Streaming