Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Broadcasting from microservices on Kubernetes

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

Share:

    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

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    • How to integrate vLLM inference into your macOS and iOS apps

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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

    Red Hat legal and privacy links

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

    Report a website issue