Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Broadcasting from microservices on Kubernetes

October 22, 2021
Ilan Pinto
Related topics:
KubernetesMicroservices
Related products:
Red Hat OpenShift

    In the era of cloud-based applications that divide tasks among multiple dedicated microservices, it is crucial to be able to dispatch events and messaging to multiple clients. This article presents an efficient architecture for broadcasting from a service using a Kubernetes headless service.

    Why broadcasting is difficult

    Many cloud-native patterns rely on messaging to deliver data to multiple receivers. Examples of these patterns include:

    • Queue-based load leveling: Sets up a message queue as a buffer between a task and a service it invokes. The queue smooths out intermittent heavy loads that can cause the service to fail or the task to time out. Multiple services can subscribe to the queue to process data faster by doing it in parallel.
    • Asynchronous request-reply: Executes client requests asynchronously so that the clients can proceed to other tasks while waiting for a slow server to respond. This pattern often uses queue-based load leveling to process the requests.

    Chats, DNS servers, and publication/subscription (pub/sub) applications are common examples where servers need to send messages to multiple consumers.

    When high performance and low latency are critical, one might seek a messaging solution based on the User Datagram Protocol (UDP). One advantage of UDP is that it is much faster than Transmission Control Protocol (TCP), because UDP skips all the handshakes and doesn't establish a connection before sending data. The trade-off is that transfers are not guaranteed to succeed, and packets can arrive out of order.

    A second advantage of UDP is that, while TCP by itself offers only unicast, other delivery options are built into UDP that allow delivery to a large number of clients on the same network (Figure 1):

    • Multicast: Transmission to a defined group of hosts.
    • Broadcast: Transmission to all of the hosts on a network or subnet.
    Broadcasting delivers each packet to multiple recipients.
    Figure 1. Broadcasting delivers each packet to multiple recipients.

    We could easily create a Python client service that listens to the broadcast address using the socket library:

    from socket import *
    
    PORT = 36000
    
    serverSocket = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP) # UDP
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
    
    # Enable broadcasting
    serverSocket.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
    serverSocket.bind(('', PORT))
    while True:
       data, addr = serverSocket.recvfrom(1024)
       print("received message:{}".format(data) )

    However, allowing UDP broadcasting poses major network security risks, such as UDP flooding and Smurf attacks. Therefore, administrators often block broadcast addresses.

    When UDP broadcasting is prohibited or you need to shift the responsibility to the sending application for another reason, this article offers another option.

    Headless services in Kubernetes

    Kubernetes offers a service component called headless services. When using a headless service, Kubernetes doesn’t allocate a dedicated cluster IP address or perform load balancing. Instead, all the pods that are assigned to the service are connected to the service. The pods' IP addresses are recorded in DNS as the service's A records. An nslookup on the headless service returns all the pods' IP addresses with the corresponding selector.

    Creating a headless service is pretty straightforward:

    apiVersion: v1
    kind: Service
    metadata:
       name:  headless-udp
    spec:
    selector:
       deployment: statefulset-udp-server
       clusterIP: None #headless service definition
       clusterIPs:
         - None
       type: ClusterIP
       ports:
        - protocol: UDP
          port: 12000
          targetPort: 12000

    The following example executes an nslookup on the headless service from a pod on the same cluster, returning the bounded pod's IP address:

    $ nslookup headless-udp
    Server:         172.30.0.10
    Address:        172.30.0.10#53
    
    Name:   headless-udp.ilpinto.svc.cluster.local
    Address: 10.128.5.80
    Name:   headless-udp.ilpinto.svc.cluster.local
    Address: 10.128.5.81

    Although the headless service does not explicitly implement load balancing, it effectively performs a simple round-robin load balancing. In the example just shown, every message sent to headless-udp hostname is routed to the first IP address (10.128.5.80) unless its pod is down, in which case the message is sent to the second IP address (10.128.5.81).

    Broadcasting from the application

    Going back to our initial problem, we would like to broadcast a message in our Kubernetes or Red Hat OpenShift cluster, but the broadcast address is blocked for security reasons. There is no option of turning on UDP broadcasting.

    The solution presented here is to move the broadcasting responsibility from the network component to the application component: In our case, the service that is sending the data. Broadcasting can be simulated by providing the sending service with the IP addresses of the relevant pods that use the headless service.

    The solution looks like this:

    • In the Kubernetes/OpenShift cluster, you set up a headless service with a selector pointing to all the subscribed services.
    • Before sending the message, the sending service queries the headless service to get the pods' IP addresses.
    • The service sends the message to all the IP addresses. The broadcast uses UDP protocol-based messaging, so no response or acknowledgment is returned to the server.
    • Optionally, subscribed services can send their responses to a third service that writes the messages to a database (Figure 2). This step is important for stateful services. Using the third service prevents multiple database writes for the same state.
    When sending to multiple recipients, use a single database writer to preserve the messages.
    Figure 2. Using a database writer to preserve the messages.

    When to use this solution

    The solution in this article should be used only when the following conditions are true:

    • The application can send out the same message multiple times, to improve chances of delivery in case the UDP-based messaging service drops a message. Messages are probably assigned numbers or other markers so that receivers can recognize and discard duplicates.
    • The microservices architecture is idempotent. All the pods processing the message at any given time should return the same result.

    Resources

    Try out this broadcast solution using demo apps and YAML resources for deployment that I have put in my GitHub repository.

    Last updated: September 20, 2023

    Recent Posts

    • LogAn: Large-scale log analysis with small language models

    • stalld’s BPF Backend: Breaking Free from debugfs

    • Running AI inference on Rebellions ATOM NPU with Red Hat AI

    • How we built integration testing for fast-moving AI backend

    • Testing infrastructure red teaming with abliterated models

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.