|
1 | 1 | ---
|
2 | 2 | title: Tolerant Reader
|
3 |
| -category: Integration |
| 3 | +category: Resilience |
4 | 4 | language: en
|
5 | 5 | tag:
|
6 |
| - - Decoupling |
| 6 | + - API design |
| 7 | + - Decoupling |
| 8 | + - Fault tolerance |
| 9 | + - Integration |
7 | 10 | ---
|
8 | 11 |
|
| 12 | +## Also known as |
| 13 | + |
| 14 | +* Lenient Consumer |
| 15 | + |
9 | 16 | ## Intent
|
10 | 17 |
|
11 |
| -Tolerant Reader is an integration pattern that helps creating robust communication systems. The idea |
12 |
| -is to be as tolerant as possible when reading data from another service. This way, when the |
13 |
| -communication schema changes, the readers must not break. |
| 18 | +Allows a system to be more resilient to changes in the data structures it consumes by ignoring elements that it does not recognize. |
14 | 19 |
|
15 | 20 | ## Explanation
|
16 | 21 |
|
17 | 22 | Real world example
|
18 | 23 |
|
19 |
| -> We are persisting rainbowfish objects to file and later on they need to be restored. What makes it |
20 |
| -> problematic is that rainbowfish data structure is versioned and evolves over time. New version of |
21 |
| -> rainbowfish needs to be able to restore old versions as well. |
| 24 | +> Imagine a postal system that delivers letters and packages to recipients. In this system, postal workers deliver mail regardless of additional information or stickers that might be present on the envelopes or packages. If a package has extra labels or instructions that the postal system does not recognize, the postal worker ignores these and focuses only on the essential information like the address. This approach ensures that the delivery process remains functional even when senders use different formats or include unnecessary details, similar to how the Tolerant Reader pattern works in software by ignoring unrecognized data elements to maintain functionality and compatibility. |
22 | 25 |
|
23 | 26 | In plain words
|
24 | 27 |
|
25 |
| -> Tolerant Reader pattern is used to create robust communication mechanisms between services. |
| 28 | +> Tolerant Reader pattern is used to create robust communication mechanisms between services. |
26 | 29 |
|
27 | 30 | [Robustness Principle](https://java-design-patterns.com/principles/#robustness-principle) says
|
28 | 31 |
|
29 | 32 | > Be conservative in what you do, be liberal in what you accept from others.
|
30 | 33 |
|
31 | 34 | **Programmatic Example**
|
32 | 35 |
|
| 36 | +We are persisting `RainbowFish` objects to file. Later on they need to be restored. What makes it problematic is that `RainbowFish` data structure is versioned and evolves over time. New version of `RainbowFish` needs to be able to restore old versions as well. |
| 37 | + |
33 | 38 | Here's the versioned `RainbowFish`. Notice how the second version introduces additional properties.
|
34 | 39 |
|
35 | 40 | ```java
|
| 41 | +@Getter |
| 42 | +@RequiredArgsConstructor |
36 | 43 | public class RainbowFish implements Serializable {
|
37 | 44 |
|
38 |
| - private static final long serialVersionUID = 1L; |
39 |
| - |
40 |
| - private final String name; |
41 |
| - private final int age; |
42 |
| - private final int lengthMeters; |
43 |
| - private final int weightTons; |
44 |
| - |
45 |
| - /** |
46 |
| - * Constructor. |
47 |
| - */ |
48 |
| - public RainbowFish(String name, int age, int lengthMeters, int weightTons) { |
49 |
| - this.name = name; |
50 |
| - this.age = age; |
51 |
| - this.lengthMeters = lengthMeters; |
52 |
| - this.weightTons = weightTons; |
53 |
| - } |
54 |
| - |
55 |
| - public String getName() { |
56 |
| - return name; |
57 |
| - } |
58 |
| - |
59 |
| - public int getAge() { |
60 |
| - return age; |
61 |
| - } |
62 |
| - |
63 |
| - public int getLengthMeters() { |
64 |
| - return lengthMeters; |
65 |
| - } |
66 |
| - |
67 |
| - public int getWeightTons() { |
68 |
| - return weightTons; |
69 |
| - } |
| 45 | + private static final long serialVersionUID = 1L; |
| 46 | + |
| 47 | + private final String name; |
| 48 | + private final int age; |
| 49 | + private final int lengthMeters; |
| 50 | + private final int weightTons; |
70 | 51 | }
|
71 | 52 |
|
| 53 | +@Getter |
72 | 54 | public class RainbowFishV2 extends RainbowFish {
|
73 | 55 |
|
74 |
| - private static final long serialVersionUID = 1L; |
75 |
| - |
76 |
| - private boolean sleeping; |
77 |
| - private boolean hungry; |
78 |
| - private boolean angry; |
79 |
| - |
80 |
| - public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) { |
81 |
| - super(name, age, lengthMeters, weightTons); |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Constructor. |
86 |
| - */ |
87 |
| - public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, |
88 |
| - boolean hungry, boolean angry) { |
89 |
| - this(name, age, lengthMeters, weightTons); |
90 |
| - this.sleeping = sleeping; |
91 |
| - this.hungry = hungry; |
92 |
| - this.angry = angry; |
93 |
| - } |
94 |
| - |
95 |
| - public boolean getSleeping() { |
96 |
| - return sleeping; |
97 |
| - } |
98 |
| - |
99 |
| - public boolean getHungry() { |
100 |
| - return hungry; |
101 |
| - } |
102 |
| - |
103 |
| - public boolean getAngry() { |
104 |
| - return angry; |
105 |
| - } |
| 56 | + @Serial |
| 57 | + private static final long serialVersionUID = 1L; |
| 58 | + |
| 59 | + private boolean sleeping; |
| 60 | + private boolean hungry; |
| 61 | + private boolean angry; |
| 62 | + |
| 63 | + public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) { |
| 64 | + super(name, age, lengthMeters, weightTons); |
| 65 | + } |
| 66 | + |
| 67 | + public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, |
| 68 | + boolean hungry, boolean angry) { |
| 69 | + this(name, age, lengthMeters, weightTons); |
| 70 | + this.sleeping = sleeping; |
| 71 | + this.hungry = hungry; |
| 72 | + this.angry = angry; |
| 73 | + } |
106 | 74 | }
|
107 | 75 | ```
|
108 | 76 |
|
109 |
| -Next we introduce the `RainbowFishSerializer`. This is the class that implements the Tolerant Reader |
110 |
| -pattern. |
| 77 | +Next we introduce the `RainbowFishSerializer`. This is the class that implements the Tolerant Reader pattern. |
111 | 78 |
|
112 | 79 | ```java
|
113 |
| -public final class RainbowFishSerializer { |
114 |
| - |
115 |
| - private RainbowFishSerializer() { |
116 |
| - } |
117 | 80 |
|
118 |
| - public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException { |
119 |
| - var map = Map.of( |
120 |
| - "name", rainbowFish.getName(), |
121 |
| - "age", String.format("%d", rainbowFish.getAge()), |
122 |
| - "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()), |
123 |
| - "weightTons", String.format("%d", rainbowFish.getWeightTons()) |
124 |
| - ); |
| 81 | +@NoArgsConstructor |
| 82 | +public final class RainbowFishSerializer { |
125 | 83 |
|
126 |
| - try (var fileOut = new FileOutputStream(filename); |
127 |
| - var objOut = new ObjectOutputStream(fileOut)) { |
128 |
| - objOut.writeObject(map); |
| 84 | + public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException { |
| 85 | + var map = Map.of( |
| 86 | + "name", rainbowFish.getName(), |
| 87 | + "age", String.format("%d", rainbowFish.getAge()), |
| 88 | + "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()), |
| 89 | + "weightTons", String.format("%d", rainbowFish.getWeightTons()) |
| 90 | + ); |
| 91 | + |
| 92 | + try (var fileOut = new FileOutputStream(filename); |
| 93 | + var objOut = new ObjectOutputStream(fileOut)) { |
| 94 | + objOut.writeObject(map); |
| 95 | + } |
129 | 96 | }
|
130 |
| - } |
131 |
| - |
132 |
| - public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException { |
133 |
| - var map = Map.of( |
134 |
| - "name", rainbowFish.getName(), |
135 |
| - "age", String.format("%d", rainbowFish.getAge()), |
136 |
| - "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()), |
137 |
| - "weightTons", String.format("%d", rainbowFish.getWeightTons()), |
138 |
| - "angry", Boolean.toString(rainbowFish.getAngry()), |
139 |
| - "hungry", Boolean.toString(rainbowFish.getHungry()), |
140 |
| - "sleeping", Boolean.toString(rainbowFish.getSleeping()) |
141 |
| - ); |
142 |
| - |
143 |
| - try (var fileOut = new FileOutputStream(filename); |
144 |
| - var objOut = new ObjectOutputStream(fileOut)) { |
145 |
| - objOut.writeObject(map); |
| 97 | + |
| 98 | + public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException { |
| 99 | + var map = Map.of( |
| 100 | + "name", rainbowFish.getName(), |
| 101 | + "age", String.format("%d", rainbowFish.getAge()), |
| 102 | + "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()), |
| 103 | + "weightTons", String.format("%d", rainbowFish.getWeightTons()), |
| 104 | + "angry", Boolean.toString(rainbowFish.getAngry()), |
| 105 | + "hungry", Boolean.toString(rainbowFish.getHungry()), |
| 106 | + "sleeping", Boolean.toString(rainbowFish.getSleeping()) |
| 107 | + ); |
| 108 | + |
| 109 | + try (var fileOut = new FileOutputStream(filename); |
| 110 | + var objOut = new ObjectOutputStream(fileOut)) { |
| 111 | + objOut.writeObject(map); |
| 112 | + } |
146 | 113 | }
|
147 |
| - } |
148 | 114 |
|
149 |
| - public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException { |
150 |
| - Map<String, String> map; |
| 115 | + public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException { |
| 116 | + Map<String, String> map; |
151 | 117 |
|
152 |
| - try (var fileIn = new FileInputStream(filename); |
153 |
| - var objIn = new ObjectInputStream(fileIn)) { |
154 |
| - map = (Map<String, String>) objIn.readObject(); |
155 |
| - } |
| 118 | + try (var fileIn = new FileInputStream(filename); |
| 119 | + var objIn = new ObjectInputStream(fileIn)) { |
| 120 | + map = (Map<String, String>) objIn.readObject(); |
| 121 | + } |
156 | 122 |
|
157 |
| - return new RainbowFish( |
158 |
| - map.get("name"), |
159 |
| - Integer.parseInt(map.get("age")), |
160 |
| - Integer.parseInt(map.get("lengthMeters")), |
161 |
| - Integer.parseInt(map.get("weightTons")) |
162 |
| - ); |
163 |
| - } |
| 123 | + return new RainbowFish( |
| 124 | + map.get("name"), |
| 125 | + Integer.parseInt(map.get("age")), |
| 126 | + Integer.parseInt(map.get("lengthMeters")), |
| 127 | + Integer.parseInt(map.get("weightTons")) |
| 128 | + ); |
| 129 | + } |
164 | 130 | }
|
165 | 131 | ```
|
166 | 132 |
|
167 |
| -And finally here's the full example in action. |
| 133 | +And finally, here's the full example in action. |
168 | 134 |
|
169 | 135 | ```java
|
170 |
| - var fishV1 = new RainbowFish("Zed", 10, 11, 12); |
171 |
| - LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(), |
172 |
| - fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons()); |
173 |
| - RainbowFishSerializer.writeV1(fishV1, "fish1.out"); |
174 |
| - |
175 |
| - var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out"); |
176 |
| - LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}", |
177 |
| - deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(), |
178 |
| - deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons()); |
179 |
| - |
180 |
| - var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true); |
181 |
| - LOGGER.info( |
182 |
| - "fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}", |
183 |
| - fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(), |
184 |
| - fishV2.getHungry(), fishV2.getAngry(), fishV2.getSleeping()); |
185 |
| - RainbowFishSerializer.writeV2(fishV2, "fish2.out"); |
186 |
| - |
187 |
| - var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out"); |
188 |
| - LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}", |
189 |
| - deserializedFishV2.getName(), deserializedFishV2.getAge(), |
190 |
| - deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()); |
| 136 | +// Write V1 |
| 137 | +var fishV1 = new RainbowFish("Zed", 10, 11, 12); |
| 138 | +LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(), fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons()); |
| 139 | +RainbowFishSerializer.writeV1(fishV1, "fish1.out"); |
| 140 | + |
| 141 | +// Read V1 |
| 142 | +var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out"); |
| 143 | +LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}", deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(), deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons()); |
| 144 | + |
| 145 | +// Write V2 |
| 146 | +var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true); |
| 147 | +LOGGER.info("fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}", fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(), fishV2.isHungry(), fishV2.isAngry(), fishV2.isSleeping()); |
| 148 | +RainbowFishSerializer.writeV2(fishV2, "fish2.out"); |
| 149 | + |
| 150 | +// Read V2 with V1 method |
| 151 | +var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out"); |
| 152 | +LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}", deserializedFishV2.getName(), deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()); |
191 | 153 | ```
|
192 | 154 |
|
193 | 155 | Program output:
|
194 | 156 |
|
195 | 157 | ```
|
196 |
| -fishV1 name=Zed age=10 length=11 weight=12 |
197 |
| -deserializedFishV1 name=Zed age=10 length=11 weight=12 |
198 |
| -fishV2 name=Scar age=5 length=12 weight=15 sleeping=true hungry=true angry=true |
199 |
| -deserializedFishV2 name=Scar age=5 length=12 weight=15 |
| 158 | +15:38:00.602 [main] INFO com.iluwatar.tolerantreader.App -- fishV1 name=Zed age=10 length=11 weight=12 |
| 159 | +15:38:00.618 [main] INFO com.iluwatar.tolerantreader.App -- deserializedFishV1 name=Zed age=10 length=11 weight=12 |
| 160 | +15:38:00.618 [main] INFO com.iluwatar.tolerantreader.App -- fishV2 name=Scar age=5 length=12 weight=15 sleeping=true hungry=true angry=true |
| 161 | +15:38:00.619 [main] INFO com.iluwatar.tolerantreader.App -- deserializedFishV2 name=Scar age=5 length=12 weight=15 |
200 | 162 | ```
|
201 | 163 |
|
202 | 164 | ## Class diagram
|
203 | 165 |
|
204 |
| - |
| 166 | + |
205 | 167 |
|
206 | 168 | ## Applicability
|
207 | 169 |
|
208 |
| -Use the Tolerant Reader pattern when |
| 170 | +* Use when a system needs to consume data from external sources that may change over time. |
| 171 | +* Applicable when backward compatibility is required in API design. |
| 172 | +* Suitable for integration scenarios where different systems exchange data and evolve independently. |
| 173 | + |
| 174 | +## Known Uses |
| 175 | + |
| 176 | +* JSON or XML parsers that skip unknown elements. |
| 177 | +* API clients in microservices architectures that interact with multiple versions of a service. |
| 178 | + |
| 179 | +## Consequences |
| 180 | + |
| 181 | +Benefits: |
| 182 | + |
| 183 | +* Increases the robustness and flexibility of the system. |
| 184 | +* Allows independent evolution of producers and consumers in a distributed system. |
| 185 | +* Simplifies versioning by enabling backward compatibility. |
| 186 | + |
| 187 | +Trade-offs: |
| 188 | + |
| 189 | +* May result in silent failures if important data is ignored. |
| 190 | +* Can complicate debugging and tracing of issues due to missing or unrecognized data. |
| 191 | + |
| 192 | +## Related Patterns |
209 | 193 |
|
210 |
| -* The communication schema can evolve and change and yet the receiving side should not break |
| 194 | +* [Adapter](https://java-design-patterns.com/patterns/adapter/): Both patterns deal with data transformation and integration, but the Adapter Pattern focuses on converting interfaces, while Tolerant Reader focuses on ignoring unrecognized data. |
| 195 | +* [Facade](https://java-design-patterns.com/patterns/facade/): Simplifies interactions with complex systems, similar to how Tolerant Reader simplifies data consumption by ignoring irrelevant data. |
| 196 | +* [Strategy](https://java-design-patterns.com/patterns/strategy/): Can be used in conjunction with Tolerant Reader to dynamically switch between different data handling strategies. |
211 | 197 |
|
212 | 198 | ## Credits
|
213 | 199 |
|
214 |
| -* [Martin Fowler - Tolerant Reader](http://martinfowler.com/bliki/TolerantReader.html) |
215 |
| -* [Service Design Patterns: Fundamental Design Solutions for SOAP/WSDL and RESTful Web Services](https://www.amazon.com/gp/product/032154420X/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=032154420X&linkId=94f9516e747ac2b449a959d5b096c73c) |
| 200 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI) |
| 201 | +* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR) |
| 202 | +* [Service Design Patterns: Fundamental Design Solutions for SOAP/WSDL and RESTful Web Services](https://amzn.to/4dNIfOx) |
| 203 | +* [Tolerant Reader - Martin Fowler](http://martinfowler.com/bliki/TolerantReader.html) |
0 commit comments