Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

1 Commit

Repository files navigation

Why This Client?

This Java Segment analytics client is designed for server-side reliability, durability, and operational transparency. It addresses the limitations of both analytics-java (which can lose events) and analytics-kotlin (which always writes to disk).

  • Durable by Default:
    Events are only written to disk if uploads fail or queues overflow, minimizing disk I/O under normal conditions. This ensures durability without unnecessary performance costs.

  • Robust Reliability:
    Features configurable retries, exponential backoff, and a circuit breaker to handle API outages. Failed uploads are stored on disk and retried at configurable intervals. Batches are deleted once all retries are exhausted, ensuring disk usage remains bounded.

  • Bounded Resource Usage:
    Strict backpressure and queue limits prevent unbounded memory or disk growth. If queues are full, events are dropped or the producer is briefly blocked, depending on configuration.

See WHY.md for a detailed comparison with other Segment clients and the design rationale.


Back Pressure and blockTimeout Configuration

By default, all queues are non-blocking (blockTimeout = 0):

  • If the upload queue is full, the event is immediately offered to the storage queue.

    Increasing blockTimeout helps avoid disk usage under high load, at the cost of potentially blocking the producer briefly.

  • If the storage queue is also full, the event is dropped and a log message is emitted.

    Increasing blockTimeout can enforce that no messages are lost, at the cost of potentially blocking the producer.

See Defaults.java for all default values.

Retry Policy

Failed uploads are stored on disk and retried according to the configured retry schedule (segment.retry.at).
Once the last retry delay is exhausted, the pending batch is deleted.
This ensures disk usage is bounded and prevents infinite retry loops.


Minimal Usage

importcom.segment.analytics.Analytics;
importcom.segment.analytics.config.StorageConfig;
importcom.segment.analytics.dto.TrackMessage;
Analyticsanalytics = Analytics.builder("YOUR_WRITE_KEY")
.storageConfig(StorageConfig.builder()
.filePath("/tmp/segment-pending") .build())
.build();
// Enqueue a messageanalytics.enqueue(newTrackMessage("userId", "event"));
// Shutdown the client gracefullyanalytics.close();

System Properties (Configuration Overrides)

System properties can be used to override default values for configuration options. Set these as JVM arguments (e.g., -Dsegment.analytics.http.queue.size=1000).
If a value is set via the builder, the system property is ignored.

System PropertyDescriptionDefault Value
segment.queue.http.sizeSize of the HTTP upload queue1_000
segment.queue.http.flushSizeMax number of elements in an HTTP batch50
segment.queue.http.flushMsMax milliseconds without an HTTP batch flush30_000
segment.queue.http.blockTimeoutMax milliseconds to wait to put an element in the HTTP queue0
segment.http.circuitErrorsInAMinuteNumber of failures in 1 minute to open the HTTP circuit breaker10
segment.http.circuitSecondsInOpenSeconds to wait in open state before half-open30
segment.http.circuitRequestsToCloseNumber of successes to close the HTTP circuit breaker1
segment.http.executorSizeMax number of concurrent HTTP upload requests2
segment.http.executorQueueSizeMax number of HTTP upload requests waiting to be executed0
segment.http.connectionTimeoutSecondsMax seconds to wait to establish an HTTP connection15
segment.http.readTimeoutSecondsMax seconds to wait for an HTTP reply20
segment.http.gzipUse compression on the HTTP request (true/false)true
segment.queue.storage.sizeSize of the disk storage queue1_000
segment.queue.storage.flushSizeMax number of elements in a disk batch50
segment.queue.storage.flushMsMax milliseconds without a disk batch flush30_000
segment.queue.storage.blockTimeoutMax milliseconds to wait to put an element in the disk queue0
segment.storage.filePath to save pending messagessegment-pending-batches
segment.retry.delaySecondsSeconds to wait between retry executions60
segment.retry.initialDelaySecondsSeconds to wait before the first retry execution60
segment.retry.atSequence of retry delays (comma-separated, with units s/m/h/d)1s,30s,1m,5m,15m,1h,12h,1d,4d,7d,30d,60d

Architecture Overview

sequenceDiagram
box ClientCall
participant SegmentService
end
participant SegmentClient
box HTTP
participant QueueUpload participant Upload
end
box File
participant QueueStorage
participant Storage
end
box Retry
participant Retry
end
activate SegmentService
SegmentService->>+SegmentClient: enqueue SegmentClient<<->>QueueUpload: offer alt overflow in QueueUpload
SegmentClient<<->>QueueStorage: offer
end
SegmentClient->>-SegmentService: deactivate SegmentService
loop Batch thread
QueueUpload->>QueueUpload:poll
note over QueueUpload: flush size<br/>flush timeout<br/>batchSize
activate QueueUpload
end
QueueUpload->>+Upload: upload batch note over Upload: HTTP requests submited to a pool
Upload->>QueueUpload: deactivate QueueUpload
alt upload failed
note over Upload: CircuitBreaker
Upload->>+Storage: write batch
note over Storage: fileName with timestamp<br/>retryAfter<br/>retryCount
Storage->>-Upload: end deactivate Upload
loop Batch thread
QueueStorage->>QueueStorage:poll
note over QueueStorage: flush size<br/>flush timeout<br/>batchSize
activate QueueStorage end QueueStorage->>+Storage: wirte overflow batch
note over Storage: fileName with timestamp<br/>retryAfter<br/>retryCount
Storage->>-QueueStorage: deactivate QueueStorage
loop scheduled check activate Retry
Retry->>+Storage: list files
note over Retry: find files with retryAfter
Storage->>-Retry: loop for each file Retry->>+Upload: upload
alt succeed
Upload->>+Storage: delete file
Storage->>-Upload: end
alt failed Upload->>+Storage: move file to next retryAfter
Storage->>-Upload: end
Upload->>-Retry: end
deactivate Retry
end
Loading

About

The hassle-free way to integrate analytics into any java application.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages