How to make a long polling with Quarkus
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Long polling in Quarkus is a practical way to deliver near real-time updates when full WebSocket infrastructure is unnecessary. The pattern keeps an HTTP request open until data is available or timeout occurs, then the client immediately reconnects. A robust implementation needs timeout handling, request cleanup, and concurrency-safe event delivery.
Long Polling Flow
Typical server flow:
- client sends request for updates
- server waits for event or timeout
- server responds with payload or empty result
- client immediately starts next request
This creates low-latency updates while staying compatible with standard HTTP tooling.
Quarkus Endpoint with Suspended Response
Using JAX-RS AsyncResponse is a straightforward approach.
When publish is called, all waiting clients receive data and reconnect.
Client Loop Example
A minimal JavaScript client:
This loop handles reconnect behavior naturally.
Production Hardening
For production quality, add:
- request cancellation handling
- connection limits per user
- authentication on poll endpoint
- observability for wait time and timeout rate
Also ensure waiting-response collections are cleaned on resume or disconnect to prevent memory leaks.
Event Source Integration
Long-poll responses usually originate from queue events, DB state changes, or internal message buses. Decouple poll management from business event creation so endpoint code remains small and testable.
You can inject an application service that calls publish on meaningful updates only, reducing noisy responses.
Scaling Long Polling in Real Systems
As concurrent clients increase, long polling must be designed with backpressure and resource limits in mind. Track how many suspended responses are active and reject or throttle excess load gracefully.
A lightweight manager service can encapsulate queueing and cleanup:
Expose waiting-count metrics and timeout-rate metrics to detect saturation before user-visible failures. For very high fan-out updates, long polling may eventually need to transition to server-sent events or WebSockets, but well-implemented long polling can still support substantial traffic.
Security and Client Identity
Long polling endpoints should validate client identity and authorization for each request. The server should not broadcast all updates to all clients unless the domain truly allows that model.
A common approach is storing waiting responses by tenant or user channel, then publishing only relevant updates. This reduces unnecessary traffic and prevents data leakage.
If authentication expires during long wait periods, return a clear status and require re-authentication before reconnect.
Common Pitfalls
A common pitfall is never timing out requests. Hanging connections can exhaust server resources.
Another issue is using non-thread-safe structures for waiting responses.
Developers also forget to remove completed responses from in-memory collections, causing leaks.
Finally, avoid sending large payloads on every poll cycle. Keep responses incremental and lightweight.
Summary
- Quarkus long polling keeps HTTP requests open until event or timeout.
- Use suspended async responses with explicit timeout handlers.
- Implement client reconnect loops with retry backoff.
- Manage waiting-response lifecycle carefully for stability.
- Add authentication, limits, and metrics before production rollout.

