With the recent surge of Microservices Architecture (MSA) and AI-based services, the ability to stably process large volumes of real-time events has emerged as a new point of focus. Collecting, propagating, and processing various forms of data—such as user interactions, sensor data, and log streams—without delay is a crucial factor that determines a service's responsiveness and scalability.
To meet these requirements, Redis Pub/Sub and Kafka have established themselves as representative event broker solutions. These technologies serve as the core pillars of data pipelines in large-scale distributed systems, efficiently supporting complex inter-service communication and data synchronization. This article aims to elucidate the characteristics of Redis Pub/Sub and Kafka and explore practical strategies for combining them to design and expand a high-performance real-time event broker architecture.
Architecture Analysis: The Synergy of Redis Pub/Sub and Kafka
A large-scale real-time event broker architecture typically necessitates two distinct characteristics simultaneously. The first is the capability to process short-lifecycle, delay-sensitive events with extremely low latency. The second is the ability to reliably manage large-scale data streams, ensuring high throughput and data persistence. Redis Pub/Sub excels in the former, while Kafka demonstrates strengths in the latter.
To understand this intuitively, this architecture adopts a hybrid approach, utilizing Redis Pub/Sub for rapid propagation of transient and latency-sensitive events, and Kafka for events that require persistent storage, complex stream processing, backpressure management, and high scalability. The data flow typically involves an event originating from a specific application being immediately delivered to relevant consumers via Redis Pub/Sub, and then, if necessary, also sent to Kafka for long-term storage, batch processing, or complex stream analysis.
The core components include event producers, the Redis Pub/Sub broker, the Kafka cluster, and event consumers. Event producers can publish events directly to either Redis or Kafka depending on event characteristics, or they can distribute events to both systems via an integrated gateway. Consumers for each system subscribe to and process the events they require.
Working Principle of Redis Pub/Sub
Redis Pub/Sub offers a mechanism for publishing and subscribing to messages with very low latency, leveraging Redis's in-memory characteristics. This operates around the concept of 'channels,' where message publishers send messages to a specific channel, and subscribers receive messages by subscribing to that channel.
To elaborate on the core principle, the Redis server does not store messages per channel. Messages are transmitted immediately to all clients subscribed to that channel upon publication. This prioritizes immediate propagation over message persistence. If a subscriber is not connected, the message is lost. Therefore, Redis Pub/Sub is suitable for scenarios where message loss is relatively tolerable, such as real-time chat, notifications, and temporary cache invalidation.
A simple Redis Pub/Sub example is as follows:
redis-cli PUBLISH my_channel "Hello, Redis Pub/Sub!"
구독자 (Subscriber) 터미널
redis-cli SUBSCRIBE my_channel
In the example above, the 'PUBLISH' command publishes a message to 'my_channel', and the 'SUBSCRIBE' command receives messages from that channel in real time. As demonstrated, Redis Pub/Sub is intuitive and can be implemented with minimal overhead.
Kafka's Distributed Processing Mechanism
Kafka is a platform specialized in processing event streams with high throughput and persistence in large-scale distributed environments. Unlike Redis Pub/Sub, Kafka is designed to persistently store messages and enable multiple consumer groups to consume messages independently.
Kafka's core components are as follows:
- Producer: Publishes events to Kafka topics.
- Consumer: Consumes events from Kafka topics. 'Consumer Groups' can be formed for parallel processing.
- Broker: A Kafka server that manages topic partitions and stores messages.
- Topic: A logical unit for categorizing messages.
- Partition: The smallest unit comprising a topic, ensuring the order of logs where messages are stored. Topics are divided into multiple partitions for parallel processing.
Messages are stored in partitions, and each message is assigned a unique sequence number called an 'offset'. Consumers track messages based on these offsets, and in the event of a failure, they can resume from the last processed offset without message loss, thereby recovering. This is a key reason why Kafka provides high stability and data integrity.
Examples of Kafka topic creation and message publishing are as follows:
kafka-topics --create --topic my-kafka-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
kafka-console-producer --broker-list localhost:9092 --topic my-kafka-topic
> This is a Kafka message.
kafka-console-consumer --bootstrap-server localhost:9092 --topic my-kafka-topic --from-beginning
Kafka, as demonstrated, reliably processes large-scale event streams in distributed environments and is optimized for asynchronous communication requiring data persistence and high throughput.
Integration Strategy for Hybrid Architecture
To effectively integrate Redis Pub/Sub and Kafka, a strategy that maximizes the strengths of each system is required. Redis Pub/Sub can be utilized for service notifications or user session synchronization that demand immediate responses, while Kafka can be employed for logging critical business events, data analytics pipelines, and long-running job queues.
For specific integration methods, one could consider a Dual Publishing pattern where, upon event occurrence, the event is immediately propagated to real-time consumers via Redis Pub/Sub, and simultaneously, the same event is published to a Kafka topic via a Kafka Producer. In this process, an integrated gateway or a separate application service would manage the dual publishing logic. Furthermore, utilizing tools such as Kafka Connect to synchronize specific Kafka topic events to Redis, or vice versa, integrating Redis Streams with Kafka, also offers flexibility.
Performance Comparison: Redis Pub/Sub vs. Kafka
These two technologies exhibit different performance characteristics and usage scenarios. The following table compares the main performance and functional aspects of Redis Pub/Sub and Kafka.
| Characteristic | Redis Pub/Sub | Kafka |
|---|
| Latency | Very Low (sub-millisecond) | Low (several milliseconds) |
| Throughput | Medium to High | Very High (hundreds of thousands of messages per second or more) |
| Message Persistence | None (lost if client is disconnected) | Yes (configurable, disk storage) |
| Message Order Guarantee | Publish order guaranteed within a single channel | Publish order guaranteed within a single partition |
| Scalability | Load increases with the number of clients | Easy horizontal scaling (add partitions/brokers) |
| Complexity | Low (single server) | High (distributed cluster) |
Through this comparison, it becomes evident that Redis Pub/Sub is advantageous for scenarios where a short lifecycle and rapid response are crucial, while Kafka is a robust solution when stable processing and analysis of large-scale data streams are required.
Practical Configuration: Optimization and Tuning Points
When configuring Redis Pub/Sub and Kafka in a production environment, optimizing for scalability, stability, and performance is paramount.
Redis Pub/Sub Configuration
Due to its in-memory nature, Redis Pub/Sub is sensitive to memory usage. It is essential to specify memory limits using the
maxmemory
setting and appropriately configure maxmemory-policy
to prevent OOM (Out Of Memory) errors. Furthermore, if the number of client connections increases significantly, it can impose a load on the Redis server; thus, considering a proxy layer or utilizing Redis Cluster is advisable. Managing slow subscribers with the client-output-buffer-limit pubsub
setting is recommended to prevent server resource exhaustion.
maxmemory 2gb
maxmemory-policy allkeys-lru
client-output-buffer-limit pubsub 32mb 8mb 60
The above setting configures Redis to remove keys using an LRU (Least Recently Used) policy if memory exceeds 2GB and sets a Pub/Sub client buffer limit to prevent excessive memory usage.
Kafka Cluster Configuration
Kafka operates in a distributed cluster environment, necessitating meticulous tuning of broker, topic, and partition settings. The number of partitions (
num.partitions
) during topic creation is directly related to parallel processing throughput; therefore, it should be determined considering the expected throughput and the number of consumer groups. A replication factor (replication.factor
) of at least 3 is generally recommended for data reliability and high availability.
# server.properties 예시
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.retention.hours=168 # 7일 저장
log.segment.bytes=1073741824 # 1GB 세그먼트
Additionally, disk I/O performance critically affects Kafka throughput; thus, utilizing fast storage and optimizing operating system cache settings are important.
Monitoring and Operations
Continuous monitoring is essential for ensuring system stability in a large-scale real-time event broker. Key monitoring metrics include message publish rate, message consumption rate, consumer lag, network bandwidth, CPU, and memory usage.
For Redis, the
INFO
command can be used to check the number of connected clients, memory usage, and commands processed per second. Kafka allows for the collection of various broker and topic metrics via JMX (Java Management Extensions), and it is common practice to integrate Prometheus and Grafana for visualized dashboards.
Operational considerations include managing backpressure caused by message surges, consumer processing delays, and broker failures. Given the potential for message loss in Redis Pub/Sub, it is advisable to duplicate critical data to a persistent system like Kafka. In the event of a Kafka cluster failure, the status of the ZooKeeper (or KRaft) cluster should be checked, broker logs analyzed to identify the cause, and recovery procedures such as Leader Election performed.
Conclusion
A large-scale real-time event broker architecture leveraging Redis Pub/Sub and Kafka plays a pivotal role in modern high-volume, high-performance distributed systems. Redis Pub/Sub excels at processing transient events with low latency, while Kafka is optimized for handling large-scale data streams with high throughput, persistence, and robust scalability.
This hybrid approach maximizes the advantages of each technology, effectively addressing complex real-time data processing requirements that would be challenging to satisfy with a single solution. Clearly delineating the roles of the two technologies based on system characteristics and data criticality, and establishing an appropriate integration strategy, are crucial for successful architectural implementation.
The potential for real-time event processing in large-scale distributed environments is immense. It remains to be seen how Redis Pub/Sub and Kafka will evolve and combine with new technologies. During practical implementation, focus should be placed on ensuring system stability and performance through continuous monitoring and tuning.
Tags: Kafka, Redis, Kafka, in services, challenge, high availability, architecture, in-depth