java
39 lines · 8 steps
Rate-limiting log spam with per-message windows
A thread-safe wrapper that emits one log per time window and reports how many duplicates it swallowed.
Explained by
highlit
1public final class ThrottledLogger {
2
3 private final Logger delegate;
4 private final long windowMillis;
5 private final ConcurrentMap<String, State> states = new ConcurrentHashMap<>();
6
7 public ThrottledLogger(Logger delegate, Duration window) {
8 this.delegate = delegate;
9 this.windowMillis = window.toMillis();
10 }
11
12 public void warn(String message) {
13 State state = states.computeIfAbsent(message, k -> new State());
14 long now = System.currentTimeMillis();
15
16 synchronized (state) {
17 if (now - state.windowStart >= windowMillis) {
18 flush(message, state, now);
19 state.windowStart = now;
20 state.suppressed = 0;
21 delegate.warn(message);
22 } else {
23 state.suppressed++;
24 }
25 }
26 }
27
28 private void flush(String message, State state, long now) {
29 if (state.suppressed > 0) {
30 delegate.warn("[suppressed {} identical messages in the last {} ms] {}",
31 state.suppressed, now - state.windowStart, message);
32 }
33 }
34
35 private static final class State {
36 long windowStart = 0L;
37 int suppressed = 0;
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Keying throttle state by message lets you rate-limit each distinct log line independently.
- 2Locking on the per-key state object confines contention instead of serializing every call.
- 3Counting and later reporting suppressed events preserves signal while cutting volume.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/rate-limiting-log-spam-with-per-message-windows-explained-java-1933/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.