Sunday, December 24, 2017

Santa Claus Pattern

In a previous post, I talked about the Water Drop Anti-Pattern, where the ratio of payload to data was leading to poor performances. santa.gifA way to alleviate this problem is to introduce the Santa Claus Pattern. Like Santa Claus, we will make packets. But instead of packing presents, we will pack several data messages into a bigger one. This way, the payload of sending this packet on the intranet will be insignificant compared to the useful data. Also, for a user interface, we will spare several repaints, leading to better performance.

There are of course several ways to implement this pattern. A possible solution is to use a timer:
 
 public void init() {
  executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(this::sendData, DELAY, DELAY,
TimeUnit.SECONDS);
} 
private void sendData() { 
if (!queue.isEmpty()) {
List<Message> packet = new ArrayList<>();
queue.drainTo(packet);
sendPacket(packet);
}
}


But be careful with this pattern, as you might wake up the Godzilla Anti-Pattern!

No comments:

Post a Comment