Kafka Stream
StateStore
Distributed Systems
Software Architecture
Data Management

Is Kafka Stream StateStore global over all instances or just local?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka Streams is a client library for building applications and microservices, where the input and output data are stored in Kafka clusters. It offers a functional programming style API and is part of the broader Kafka ecosystem. A fundamental component of Kafka Streams is its StateStore, which is pivotal for stateful operations. Understanding whether a StateStore within Kafka Streams is global or local is crucial for properly designing and scaling your streaming applications.

Understanding StateStores in Kafka Streams

A StateStore in Kafka Streams allows you to store and query data relevant to the application's processing tasks. The primary use of StateStores is to provide stateful processing capabilities. This includes operations such as aggregations, joins, or any other processing that requires the application to maintain state across multiple messages.

There are two types of StateStores in Kafka Streams:

  1. Persistent StateStores: Data is persisted to the local disk. This ensures that the state survives application restarts.
  2. In-memory StateStores: Faster access times but does not survive restarts, as state is maintained only in memory.

Local versus Global StateStores

Local StateStores: Local StateStores are tied to specific Kafka Stream tasks and are therefore not accessible across different instances of an application. Each task in Kafka Streams manages its own StateStore, and these tasks are distributed across the application instances based on the partitions of the input topic(s). This setup implies that each StateStore instance only contains data related to the specific partitions of data it is processing.

Example: Consider a Kafka Streams application running on two instances. If instance A processes partitions 0 and 1, the StateStore on instance A only contains state for these partitions. Similarly, if instance B processes partitions 2 and 3, its StateStore only holds data for partitions 2 and 3.

Global StateStores: Global StateStores, on the other hand, maintain a read-only view of a specific Kafka topic across all application instances. Global StateStores are populated by consuming a Kafka topic, which usually uses log-compaction to ensure that only the latest values for each key are retained.

When dealing with global StateStores, all instances of a Kafka Streams application see the same data, which allows for consistent read-only access to shared state across all running instances.

When to Use Local vs. Global StateStores

  • Local StateStores are ideal when state is partition-specific, such as handling sessions or aggregating values within each stream partition.
  • Global StateStores are useful for storing data required consistently across all stream processing tasks, like reference or metadata that supports decision making in stream processors.

Table Summary: Key Differences between Local and Global StateStores

FeatureLocal StateStoreGlobal StateStore
Data ScopePartition-specific dataWhole topic data accessible from all instances
Write CapabilityRead and writeRead-only
Use CaseAggregations, joins, and windowingReference data, cross-partition lookups
Data PersistenceCan be persistent or in-memoryTypically persistent to ensure data consistency
Scalability ImpactRelated to the number of partitionsRelated to topic compaction and replication

Conclusion

Deciding between local and global StateStores in Kafka Streams depends heavily on the specific requirements of your application, including factors like the required data scope, read/write needs, and your overall system architecture. Properly leveraging these StateStores can greatly enhance the robustness, efficiency, and scalability of your streaming applications. Understanding these concepts ensures that developers can design more effective Kafka Streams applications that handle state with clarity and precision.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design