Skip to content

Commit 12b70cb

Browse files
author
Chandana Amarnath
committed
Issue iluwatar#450. First commit. Queue Based Load Leveling
1 parent 68ec24c commit 12b70cb

15 files changed

+765
-6
lines changed

pom.xml

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
THE SOFTWARE.
20-
-->
21-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2321
<modelVersion>4.0.0</modelVersion>
2422
<groupId>com.iluwatar</groupId>
2523
<artifactId>java-design-patterns</artifactId>
@@ -133,7 +131,8 @@
133131
<module>promise</module>
134132
<module>page-object</module>
135133
<module>event-asynchronous</module>
136-
</modules>
134+
<module>queue-load-leveling</module>
135+
</modules>
137136

138137
<dependencyManagement>
139138
<dependencies>
@@ -311,7 +310,7 @@
311310
</goals>
312311
</pluginExecutionFilter>
313312
<action>
314-
<ignore></ignore>
313+
<ignore/>
315314
</action>
316315
</pluginExecution>
317316
</pluginExecutions>
@@ -465,4 +464,4 @@
465464
</plugins>
466465
</reporting>
467466

468-
</project>
467+
</project>

queue-load-leveling/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: pattern
3+
title: Queue based load leveling
4+
folder: queue-load-leveling
5+
permalink: /patterns/queue-load-leveling/
6+
pumlid:
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Intermediate
11+
- Performance
12+
---
13+
14+
## Intent
15+
Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth
16+
intermittent heavy loads that may otherwise cause the service to fail or the task to time out.
17+
This pattern can help to minimize the impact of peaks in demand on availability and responsiveness
18+
for both the task and the service.
19+
20+
![alt text](./etc/queue-load-leveling.gif "queue-load-leveling")
21+
22+
23+
## Applicability
24+
25+
* This pattern is ideally suited to any type of application that uses services that may be subject to overloading.
26+
* This pattern might not be suitable if the application expects a response from the service with minimal latency.
27+
28+
## Real world example
29+
30+
* A Microsoft Azure web role stores data by using a separate storage service. If a large number of instances of the web role run concurrently, it is possible that the storage service could be overwhelmed and be unable to respond to requests quickly enough to prevent these requests from timing out or failing.
31+
32+
## Credits
33+
34+
* [Design Pattern: Queue-Based Load Leveling Pattern](https://msdn.microsoft.com/en-us/library/dn589783.aspx)
14.7 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.11" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="org.queue.load.leveling.TaskGenerator" project="queue-load-leveling"
5+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java" binary="false"
6+
corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="470" y="213"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<interface id="2" language="java" name="org.queue.load.leveling.Task" project="queue-load-leveling"
15+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="426" y="389"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</interface>
23+
<class id="3" language="java" name="org.queue.load.leveling.MessageQueue" project="queue-load-leveling"
24+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java" binary="false"
25+
corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="661" y="419"/>
27+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
28+
sort-features="false" accessors="true" visibility="true">
29+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
30+
<operations public="true" package="true" protected="true" private="true" static="true"/>
31+
</display>
32+
</class>
33+
<class id="4" language="java" name="org.queue.load.leveling.Message" project="queue-load-leveling"
34+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java" binary="false" corner="BOTTOM_RIGHT">
35+
<position height="-1" width="-1" x="594" y="657"/>
36+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
37+
sort-features="false" accessors="true" visibility="true">
38+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
39+
<operations public="true" package="true" protected="true" private="true" static="true"/>
40+
</display>
41+
</class>
42+
<class id="5" language="java" name="org.queue.load.leveling.ServiceExecutor" project="queue-load-leveling"
43+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java" binary="false"
44+
corner="BOTTOM_RIGHT">
45+
<position height="-1" width="-1" x="775" y="193"/>
46+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
47+
sort-features="false" accessors="true" visibility="true">
48+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
49+
<operations public="true" package="true" protected="true" private="true" static="true"/>
50+
</display>
51+
</class>
52+
<realization id="6">
53+
<end type="SOURCE" refId="1"/>
54+
<end type="TARGET" refId="2"/>
55+
</realization>
56+
<association id="7">
57+
<end type="SOURCE" refId="1" navigable="false">
58+
<attribute id="8" name="msgQueue"/>
59+
<multiplicity id="9" minimum="0" maximum="1"/>
60+
</end>
61+
<end type="TARGET" refId="3" navigable="true"/>
62+
<display labels="true" multiplicity="true"/>
63+
</association>
64+
<association id="10">
65+
<end type="SOURCE" refId="3" navigable="false">
66+
<attribute id="11" name="blkQueue"/>
67+
<multiplicity id="12" minimum="0" maximum="2147483647"/>
68+
</end>
69+
<end type="TARGET" refId="4" navigable="true"/>
70+
<display labels="true" multiplicity="true"/>
71+
</association>
72+
<association id="13">
73+
<end type="SOURCE" refId="5" navigable="false">
74+
<attribute id="14" name="msgQueue"/>
75+
<multiplicity id="15" minimum="0" maximum="1"/>
76+
</end>
77+
<end type="TARGET" refId="3" navigable="true"/>
78+
<display labels="true" multiplicity="true"/>
79+
</association>
80+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
81+
sort-features="false" accessors="true" visibility="true">
82+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
83+
<operations public="true" package="true" protected="true" private="true" static="true"/>
84+
</classifier-display>
85+
<association-display labels="true" multiplicity="true"/>
86+
</class-diagram>

queue-load-leveling/pom.xml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.14.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>builder</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package org.queue.load.leveling;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
*
31+
* Many solutions in the cloud involve running tasks that invoke services. In this environment,
32+
* if a service is subjected to intermittent heavy loads, it can cause performance or reliability issues.
33+
* <p>
34+
* A service could be a component that is part of the same solution as the tasks that utilize it, or it
35+
* could be a third-party service providing access to frequently used resources such as a cache or a storage service.
36+
* If the same service is utilized by a number of tasks running concurrently, it can be difficult to predict the
37+
* volume of requests to which the service might be subjected at any given point in time.
38+
* <p>
39+
* We will build a queue-based-load-leveling to solve above problem.
40+
* Refactor the solution and introduce a queue between the task and the service.
41+
* The task and the service run asynchronously. The task posts a message containing the data required
42+
* by the service to a queue. The queue acts as a buffer, storing the message until it is retrieved
43+
* by the service. The service retrieves the messages from the queue and processes them.
44+
* Requests from a number of tasks, which can be generated at a highly variable rate, can be passed
45+
* to the service through the same message queue.
46+
* <p>
47+
* The queue effectively decouples the tasks from the service, and the service can handle the messages
48+
* at its own pace irrespective of the volume of requests from concurrent tasks. Additionally,
49+
* there is no delay to a task if the service is not available at the time it posts a message to the queue.
50+
* <p>
51+
* In this example we have a class {@link MessageQueue} to hold the message {@link Message} objects.
52+
* All the worker threads {@link TaskGenerator} will submit the messages to the MessageQueue.
53+
* The service executor class {@link ServiceExecutor} will pick up one task at a time from the Queue and
54+
* execute them.
55+
*
56+
*/
57+
public class App {
58+
59+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
60+
61+
/**
62+
* Program entry point
63+
*
64+
* @param args command line args
65+
*/
66+
public static void main(String[] args) {
67+
try {
68+
// Create a MessageQueue object.
69+
MessageQueue msgQueue = new MessageQueue();
70+
71+
LOGGER.info("All the TaskGenerators started.");
72+
73+
// Create three TaskGenerator threads. Each of them will submit different number of jobs.
74+
Runnable taskRunnable_1 = new TaskGenerator(msgQueue, 5);
75+
Runnable taskRunnable_2 = new TaskGenerator(msgQueue, 1);
76+
Runnable taskRunnable_3 = new TaskGenerator(msgQueue, 2);
77+
78+
Thread taskGenerator_1 = new Thread(taskRunnable_1, "Task_Generator_1");
79+
Thread taskGenerator_2 = new Thread(taskRunnable_2, "Task_Generator_2");
80+
Thread taskGenerator_3 = new Thread(taskRunnable_3, "Task_Generator_3");
81+
82+
taskGenerator_1.start();
83+
taskGenerator_2.start();
84+
taskGenerator_3.start();
85+
86+
LOGGER.info("Service Executor started.");
87+
88+
// First create e service which will process all the jobs.
89+
Runnable srvRunnable = new ServiceExecutor(msgQueue);
90+
Thread srvExec = new Thread(srvRunnable, "Service_Executor_Thread");
91+
srvExec.start();
92+
} catch(Exception e) {
93+
LOGGER.error(e.getMessage());
94+
}
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package org.queue.load.leveling;
24+
25+
/**
26+
* Message class with only one parameter.
27+
*
28+
*/
29+
public class Message {
30+
private String msg;
31+
32+
// Empty constructor.
33+
public Message() {
34+
}
35+
36+
// Parameter constructor.
37+
public Message(String msg) {
38+
super();
39+
this.msg = msg;
40+
}
41+
42+
// Get Method for attribute msg.
43+
public String getMsg() {
44+
return msg;
45+
}
46+
47+
// Set Method for attribute msg.
48+
public void setMsg(String msg) {
49+
this.msg = msg;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return msg;
55+
}
56+
}

0 commit comments

Comments
 (0)