Skip to content

Commit 1fa8a60

Browse files
Azureyjtiluwatar
authored andcommitted
Sharding Pattern (iluwatar#1056)
* Create sharding module * Add Unit Tests * Fix readme hyperlink * Fix check-style issue
1 parent 50986fa commit 1fa8a60

16 files changed

Lines changed: 1010 additions & 0 deletions

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<module>subclass-sandbox</module>
185185
<module>circuit-breaker</module>
186186
<module>double-buffer</module>
187+
<module>sharding</module>
187188
</modules>
188189

189190
<repositories>

sharding/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
---
3+
layout: pattern
4+
title: Sharding
5+
folder: sharding
6+
permalink: /patterns/sharding/
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Intent
14+
Sharding pattern means divide the data store into horizontal partitions or shards. Each shard has the same schema, but holds its own distinct subset of the data.
15+
A shard is a data store in its own right (it can contain the data for many entities of different types), running on a server acting as a storage node.
16+
17+
## Applicability
18+
This pattern offers the following benefits:
19+
20+
- You can scale the system out by adding further shards running on additional storage nodes.
21+
- A system can use off the shelf commodity hardware rather than specialized (and expensive) computers for each storage node.
22+
- You can reduce contention and improved performance by balancing the workload across shards.
23+
- In the cloud, shards can be located physically close to the users that will access the data.
24+
25+
## Credits
26+
27+
* [Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications - Sharding Pattern](https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589797(v=pandp.10)?redirectedfrom=MSDN)

sharding/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 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 xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.22.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>sharding</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 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 com.iluwatar.sharding;
25+
26+
/**
27+
* Sharding pattern means dividing a data store into a set of horizontal partitions
28+
* or shards. This pattern can improve scalability when storing and accessing large
29+
* volumes of data.
30+
*/
31+
public class App {
32+
33+
/**
34+
* Program main entry point.
35+
* @param args program runtime arguments
36+
*/
37+
public static void main(String[] args) {
38+
39+
var data1 = new Data(1, "data1", Data.DataType.type1);
40+
var data2 = new Data(2, "data2", Data.DataType.type2);
41+
var data3 = new Data(3, "data3", Data.DataType.type3);
42+
var data4 = new Data(4, "data4", Data.DataType.type1);
43+
44+
var shard1 = new Shard(1);
45+
var shard2 = new Shard(2);
46+
var shard3 = new Shard(3);
47+
48+
ShardManager manager = new LookupShardManager();
49+
manager.addNewShard(shard1);
50+
manager.addNewShard(shard2);
51+
manager.addNewShard(shard3);
52+
manager.storeData(data1);
53+
manager.storeData(data2);
54+
manager.storeData(data3);
55+
manager.storeData(data4);
56+
57+
shard1.clearData();
58+
shard2.clearData();
59+
shard3.clearData();
60+
61+
manager = new RangeShardManager();
62+
manager.addNewShard(shard1);
63+
manager.addNewShard(shard2);
64+
manager.addNewShard(shard3);
65+
manager.storeData(data1);
66+
manager.storeData(data2);
67+
manager.storeData(data3);
68+
manager.storeData(data4);
69+
70+
shard1.clearData();
71+
shard2.clearData();
72+
shard3.clearData();
73+
74+
manager = new HashShardManager();
75+
manager.addNewShard(shard1);
76+
manager.addNewShard(shard2);
77+
manager.addNewShard(shard3);
78+
manager.storeData(data1);
79+
manager.storeData(data2);
80+
manager.storeData(data3);
81+
manager.storeData(data4);
82+
83+
shard1.clearData();
84+
shard2.clearData();
85+
shard3.clearData();
86+
}
87+
88+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 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 com.iluwatar.sharding;
25+
26+
/**
27+
* Basic data structure for each tuple stored in data shards.
28+
*/
29+
public class Data {
30+
31+
private int key;
32+
33+
private String value;
34+
35+
private DataType type;
36+
37+
/**
38+
* Constructor of Data class.
39+
* @param key data key
40+
* @param value data vlue
41+
* @param type data type
42+
*/
43+
public Data(final int key, final String value, final DataType type) {
44+
this.key = key;
45+
this.value = value;
46+
this.type = type;
47+
}
48+
49+
public int getKey() {
50+
return key;
51+
}
52+
53+
public void setKey(final int key) {
54+
this.key = key;
55+
}
56+
57+
public String getValue() {
58+
return value;
59+
}
60+
61+
public void setValue(final String value) {
62+
this.value = value;
63+
}
64+
65+
public DataType getType() {
66+
return type;
67+
}
68+
69+
public void setType(DataType type) {
70+
this.type = type;
71+
}
72+
73+
enum DataType {
74+
type1, type2, type3
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return "Data {" + "key="
80+
+ key + ", value='" + value
81+
+ '\'' + ", type=" + type + '}';
82+
}
83+
}
84+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 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 com.iluwatar.sharding;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* ShardManager with hash strategy. The purpose of this strategy is to reduce the
31+
* chance of hot-spots in the data. It aims to distribute the data across the shards
32+
* in a way that achieves a balance between the size of each shard and the average
33+
* load that each shard will encounter.
34+
*/
35+
public class HashShardManager extends ShardManager {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(HashShardManager.class);
38+
39+
@Override
40+
public int storeData(Data data) {
41+
var shardId = allocateShard(data);
42+
var shard = shardMap.get(shardId);
43+
shard.storeData(data);
44+
LOGGER.info(data.toString() + " is stored in Shard " + shardId);
45+
return shardId;
46+
}
47+
48+
@Override
49+
protected int allocateShard(Data data) {
50+
var shardCount = shardMap.size();
51+
var hash = data.getKey() % shardCount;
52+
return hash == 0 ? hash + shardCount : hash;
53+
}
54+
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 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 com.iluwatar.sharding;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import java.util.Random;
29+
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
/**
34+
* ShardManager with lookup strategy. In this strategy the sharding logic implements
35+
* a map that routes a request for data to the shard that contains that data by using
36+
* the shard key.
37+
*/
38+
public class LookupShardManager extends ShardManager {
39+
40+
private static final Logger LOGGER = LoggerFactory.getLogger(LookupShardManager.class);
41+
42+
private Map<Integer, Integer> lookupMap = new HashMap<>();
43+
44+
@Override
45+
public int storeData(Data data) {
46+
var shardId = allocateShard(data);
47+
lookupMap.put(data.getKey(), shardId);
48+
var shard = shardMap.get(shardId);
49+
shard.storeData(data);
50+
LOGGER.info(data.toString() + " is stored in Shard " + shardId);
51+
return shardId;
52+
}
53+
54+
@Override
55+
protected int allocateShard(Data data) {
56+
var key = data.getKey();
57+
if (lookupMap.containsKey(key)) {
58+
return lookupMap.get(key);
59+
} else {
60+
var shardCount = shardMap.size();
61+
var allocatedShardId = new Random().nextInt(shardCount - 1) + 1;
62+
return allocatedShardId;
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)