<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kyle Carter</title>
    <description>The latest articles on DEV Community by Kyle Carter (@kylec32).</description>
    <link>https://dev.to/kylec32</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F9832%2F160703.png</url>
      <title>DEV Community: Kyle Carter</title>
      <link>https://dev.to/kylec32</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kylec32"/>
    <language>en</language>
    <item>
      <title>Supporting Cross Node Interactive Queries In Kafka Streams</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Mon, 21 Feb 2022 17:07:11 +0000</pubDate>
      <link>https://dev.to/kylec32/supporting-cross-node-interactive-queries-in-kafka-streams-10ed</link>
      <guid>https://dev.to/kylec32/supporting-cross-node-interactive-queries-in-kafka-streams-10ed</guid>
      <description>&lt;p&gt;&lt;a href="https://kafka.apache.org/documentation/streams/"&gt;Kafka Streams&lt;/a&gt; is a powerful tool that adds a high-level abstraction on top of Kafka’s rock-solid infrastructure to enable building streaming applications. It has several features mainly grouped around two concepts, &lt;code&gt;KStreams&lt;/code&gt; which represents an infinite stream of data, and &lt;code&gt;KTables&lt;/code&gt; which represent a projection of a stream’s data. Even calling these two concepts different is not completely true due to the &lt;a href="https://www.confluent.io/blog/kafka-streams-tables-part-1-event-streaming/"&gt;stream-table duality&lt;/a&gt;. While it is not required to have a perfect understanding of the stream-table duality to work with Kafka streams I do find having some level of understanding of it is useful when working with streams. Using these core building blocks of streams and tables (stored projections of stream data) many impressive things can be built. That said, working with infinite streams of data and with the limited query model of a &lt;code&gt;KTable&lt;/code&gt; can be tricky and has a steep learning curve. Streams actually provides another way to query your data that follows a more comfortable pattern for many developers and one that adds the ability to query your stream state from outside a stream. This is where &lt;a href="https://docs.confluent.io/platform/current/streams/developer-guide/interactive-queries.html"&gt;interactive queries&lt;/a&gt; can come into the picture.&lt;/p&gt;

&lt;p&gt;At a high level, interactive queries allow a developer to query the state of a &lt;code&gt;KTable&lt;/code&gt; (in reality the data store that backs the &lt;code&gt;KTable&lt;/code&gt;) from outside the stream processing code. This allows ad-hoc key-value queries that are performant and, especially for developers new to stream processing code, much easier to understand than querying via a stream. That said, there are some major pitfalls to using interactive queries that may not be apparent when first using them. The pitfall that this post is going to focus on is that the data that a particular instance of a service can query is only a subset of all the data available. To understand why this is, let us briefly remind ourselves how Kafka Streams works and how &lt;code&gt;KTables&lt;/code&gt; are populated.&lt;/p&gt;

&lt;p&gt;Each &lt;code&gt;KStream&lt;/code&gt; is backed by one or more topics each with one or more partitions. Outside of the case where you are only using one partition and/or you only have one instance of your streams application running at a time, different processes will be handling different slices of the data within the topic. One of the great capabilities of Kafka is that it handles this balancing of cooperating consumers automatically for you. As new instances come online they will be assigned one or more partitions that it will be in charge of processing. This allows you to scale out horizontally up to the number of partitions in your topic. This means each instance of the stream application will have its own view of the world. Records that exist on one node won’t exist on another node. This is great for scalability as no one instance needs to shoulder all the load. A simplified model of this can be seen here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CcJwZ7on--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2AmeHWUDgAvTmiyahzf6LdOg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CcJwZ7on--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2AmeHWUDgAvTmiyahzf6LdOg.png" alt="High level interactive queries architecture" width="632" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So how does this interact with interactive queries? The interactive query functionality allows an instance of the streams application to perform ad-hoc queries of the local state by key. If the partition that a specific key is assigned is the same partition that the current instance is assigned, all will work wonderfully as the data will be there in the local state store. However, if the key is assigned a partition that is not assigned to the querying instance then you are out of luck. The interactive streams documentation acknowledges this fact and leaves resolving it to the user with the hint that some kind of remote procedure call (RPC) method would likely need to be employed. While I understand that the library cannot solve all problems, not having an out-of-the-box solution for this leaves developers in a tough spot. What makes this particularly troublesome is developers may initially develop against a small data set with a topic that only has one partition and/or instance and thus not see this issue until they release it into a more production-like, horizontally-scaled environment. Of note, using streams to query a &lt;code&gt;KTable&lt;/code&gt; does not have this issue because the stream will be co-partitioned with the &lt;code&gt;KTable&lt;/code&gt; and thus will always be on the correct node to do the lookup.&lt;/p&gt;

&lt;p&gt;This post strives to propose an idea of how we can account for this issue of data locality and allow any instance of a streams application to query any data. As a scaffold to discuss this solution I have developed a simple example application that loads up a topic with various records and then exposes a REST endpoint that allows someone to retrieve the information by key. There are a lot of interesting things even in this simple application but I will focus mainly on the parts of it that enable the cross node interactive query functionality.&lt;/p&gt;

&lt;p&gt;The topology for our application is quite basic. Events come in on the “event-topic” are consumed into a stream then aggregated into a state store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k_mdpaPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/380/1%2ASjm6Hgh699t38OV3Bk_6CQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k_mdpaPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/380/1%2ASjm6Hgh699t38OV3Bk_6CQ.png" alt="Topology of example application" width="190" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing we need to handle is enabling each instance of our application to self-identify where it is being hosted and provide that information to the streams library so that it can provide that information to other nodes in the future. In this example application we have a simple configuration that gathers the current host information that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l4caAKYT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eenw0hvhhz50hrglszrk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l4caAKYT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eenw0hvhhz50hrglszrk.png" alt="Get Host Information" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above is extremely simplistic and likely won’t work in many environments where hostnames are not as immediately available as in the above but the same concept can be used. At this point, we can use the above configuration in our streams configuration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6aCliAp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hswnsbly1ph72wrbgq72.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6aCliAp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hswnsbly1ph72wrbgq72.png" alt="Streams Configuration" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have truncated much of the streams configuration here but left the two interesting things. The first is that, because we will be running two instances of this application locally, I changed the directory that state is stored so that multiple state stores can coexist on the same machine. This shouldn’t be required when using different servers but also shouldn’t hurt anything. The second is that we are telling Kafka Streams where our current service is hosted. The streams library will pass back this information later when we need to find out where a particular key is stored.&lt;br&gt;
The high-level concept of how this system is going to work is that a request will come into our application, we will check with streams to determine if the queried key is stored on the local node or if it is on a remote node. If it is local we will query the state store locally and return the result. If the data is remote we will retrieve the host information about where it is stored and then make a HTTP call to that service which can do the same lookup which will now be local, return the result via the HTTP response, and then the original service can return the result as if it had it locally. To support this we have a few pieces of code.&lt;br&gt;
The first piece of code that we need is a class that can hold all the pieces of information that are needed to determine if a remote call needs to be made and information needed to make the remote call.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d8RBFYfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xej59ngk3863l61t1i2i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d8RBFYfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xej59ngk3863l61t1i2i.png" alt="StoreInfo class" width="538" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A querying service then can call a method with the following signature to do the lookup:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S4qfu8Gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blnro0gk8yco20jgxd1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S4qfu8Gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blnro0gk8yco20jgxd1r.png" alt="Call signature" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;StoreInfo&lt;/code&gt; class is passed in as seen above as well as the key that will be queried and then a higher-order function to be used if the data is determined to be local to pull the data from the local store. The body of the above function is something like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qyMr77BJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2A3yCnmBhTITY2C5USo6WN_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qyMr77BJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2A3yCnmBhTITY2C5USo6WN_A.png" alt="Body of function" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed above this queries the metadata for a particular state store, retrieves its host info (the information we stored at configuration time), determines if it is local, if so it calls the higher-order function for processing passed in, if not it generates the body from the key, sets the right “Content-type” header, copies the authorization header from the current request to the outgoing request, generates the URL to query based on the metadata and information passed in, queries the remote service, and passes on an appropriate response.&lt;/p&gt;

&lt;p&gt;The final chunk of code we will look at is what it looks like to call this service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--orkIKk6U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AfG7vbLTNQTfjbLaES4B1RA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--orkIKk6U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AfG7vbLTNQTfjbLaES4B1RA.png" alt="Example Call" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In reality, there is not much more code required compared to if we were only doing the local query which is nice. The &lt;code&gt;InteractiveQueryService&lt;/code&gt; is also coded in such a way that it can handle various types of keys and responses.&lt;/p&gt;
&lt;h3&gt;
  
  
  Environment Setup
&lt;/h3&gt;

&lt;p&gt;Let’s see how it works. To save ourselves some effort of setting up Kafka locally we will use Upstash as our Kafka service. &lt;a href="https://upstash.com/?utm_source=Kyle1"&gt;Upstash&lt;/a&gt; is a Serverless Kafka offering where you pay per message you produce/consume. They have a free tier which is more than sufficient for this test and gives you a good idea of how the service works. To get ready to run the application you have two options. You can set up the cluster manually or use some additional code I added to the repo to create the needed infrastructure via the &lt;a href="https://developer.upstash.com/#kafka?utm_source=Kyle1"&gt;Upstash API&lt;/a&gt;. I’ll walk through both.&lt;/p&gt;
&lt;h4&gt;
  
  
  Manual Setup
&lt;/h4&gt;

&lt;p&gt;After verifying your email address and logging into the console go to the Kafka section:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MH2SOb3a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2ATLAhy7ywWkgrOtsiOObudg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MH2SOb3a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2ATLAhy7ywWkgrOtsiOObudg.png" alt="Kafka Screen" width="800" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Press create cluster and fill out the information. There is no required setup as far as the cluster is concerned for this POC so give the cluster any name and choose the region closest to you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bRxDqTVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1058/1%2AD2J4u9ewy2dCvkyBlPn3uQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bRxDqTVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1058/1%2AD2J4u9ewy2dCvkyBlPn3uQ.png" alt="Cluster Creation Step 1" width="529" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you can create your first topic. Unfortunately (at least with the free tier) the credentials you get from Upstash don’t allow the application to create its own topics. This is safer for production but just requires a little more work for this POC. Create a topic with the following information, the rest of the defaults are fine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wfWF02iq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1046/1%2AQgOaz_MKAa76vUP704h7KQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wfWF02iq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1046/1%2AQgOaz_MKAa76vUP704h7KQ.png" alt="Cluster Creation Step 2" width="523" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to create another topic that will be used to back the state store inside of Kafka Streams.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yTsqjbh0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/726/1%2AzczzS7PtdU8hEQDStf-Xhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yTsqjbh0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/726/1%2AzczzS7PtdU8hEQDStf-Xhg.png" alt="Topics" width="363" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rScUP3jl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1064/1%2AsZTUk8_Ttd7n6qwKxOUM3Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rScUP3jl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1064/1%2AsZTUk8_Ttd7n6qwKxOUM3Q.png" alt="Topic 2 Creation" width="532" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can go back to the “Details” tab and grab our configuration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DdveSzMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AdufvFkDbDnfwJfgWwnIocg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DdveSzMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AdufvFkDbDnfwJfgWwnIocg.png" alt="Connection Details" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy those values and paste them over the existing values in the &lt;code&gt;application.properties&lt;/code&gt; file in our repository. Make sure to prefix each of the keys with &lt;code&gt;"kafka."&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gTXeKXgG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1292/1%2AA53ZZisQFgqvZqhmKQ6rIg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gTXeKXgG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1292/1%2AA53ZZisQFgqvZqhmKQ6rIg.png" alt="application.properties" width="646" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are ready to go.&lt;/p&gt;
&lt;h4&gt;
  
  
  Automated Setup
&lt;/h4&gt;

&lt;p&gt;For the automated setup, we first need to create a “Management API Key” by going up to “Account” -&amp;gt; “Management API” -&amp;gt; “Create API Key”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T5r2nrna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2Abi_dnNORnNZLKjXQy1iYhA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T5r2nrna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2Abi_dnNORnNZLKjXQy1iYhA.png" alt="API Key" width="800" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Grab that value and the email address you signed up with and put them into the variables at the top of the &lt;code&gt;ConfgureEnvironment.java&lt;/code&gt; class&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ryLoUy3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AhnG_ySxZe9wuv-4aaIBkpA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ryLoUy3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AhnG_ySxZe9wuv-4aaIBkpA.png" alt="Email Address &amp;amp; API Key" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run that class and grab the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JHE3hmb---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AP3p9yVDO-iVyUs15Ei498Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JHE3hmb---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AP3p9yVDO-iVyUs15Ei498Q.png" alt="Run Output" width="800" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, paste those values over the existing values in the application.properties file.&lt;/p&gt;
&lt;h3&gt;
  
  
  Seeing the Code In Action
&lt;/h3&gt;

&lt;p&gt;With our environment setup complete we can now test our application. Run the &lt;code&gt;bootJar&lt;/code&gt; gradle task to build the jar and then, in two different terminal windows, navigate to the folder with the jar. We will then run the application with the following commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;java -jar cross-node-iq-0.0.1-SNAPSHOT.jar&lt;/code&gt;&lt;br&gt;
&lt;code&gt;java -jar cross-node-iq-0.0.1-SNAPSHOT.jar --server.port=8081&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After both nodes have come up and the logs have quieted down (indicating that rebalancing is complete) we are ready to query the application. You can choose to query either the node on &lt;code&gt;8080&lt;/code&gt; or the one on &lt;code&gt;8081&lt;/code&gt; since the whole point of this is it doesn’t matter which node is in charge or which key, it should work with either. For my test I will make the following request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -i -H “Authorization:Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwic3ViIjoidGVzdFVzZXIifQ.ZRq6TnZiBlkY1CDkkQP2RnTOMV58OxgC30W0u7AjTCg” localhost:8081/example/1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lot of the above command is to manage the authentication. If you haven’t changed the &lt;code&gt;authentication.jwt.secret&lt;/code&gt; from the &lt;code&gt;application.properties&lt;/code&gt; file the above value should work for you too.&lt;/p&gt;

&lt;p&gt;This gave me the following result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P-RfQptw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1350/1%2AQkEeBqatKQigQ4ETIJg8_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P-RfQptw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1350/1%2AQkEeBqatKQigQ4ETIJg8_A.png" alt="Curl Output" width="675" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checking the logs of the node I queried we see it did not have the answer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5-h4n7jF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AM8aScEQwUmq8gkNuj8buJg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5-h4n7jF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AM8aScEQwUmq8gkNuj8buJg.png" alt="Node 1 Logs" width="800" height="22"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checking the logs of the other node we see that it had it locally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sq72CUP2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2A0vxC_aVuDOdRN4QKhUUo7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sq72CUP2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2A0vxC_aVuDOdRN4QKhUUo7g.png" alt="Node 2 Logs" width="800" height="21"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So it does indeed work. We can change the port number and query the one that has it locally and we see that it responds exactly the same. Looking at how long it takes to respond you can see some differences when it has to jump nodes and that will always have some effect but could be mitigated somewhat by keeping an open connection between nodes if that was desired.&lt;br&gt;
As I have said above, there is a lot more to this application and some interesting concepts I think. Using Avro schemas without a schema registry, serializing/deserializing Avro for HTTP requests and response, simple authentication, etc. So do go ahead and jump in the code and see what there is to learn.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/kylec32/cross-node-iq-poc"&gt;Repository Link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So where does this put us? With our new found ability to query across nodes are interactive queries ready to roll? Not so much. There are still other issues with interactive queries that must be accounted for, a major one being that during rebalances and hydration of state they are unusable. The above solution is also far from an end solution, it is merely the beginning of a much more feature-rich RPC framework that would be required if this was rolled out into a production environment. It does however show what is possible with a few good abstractions. This is also not the only way to solve this issue either, others have chosen to merely respond with &lt;a href="https://www.imperva.com/blog/not-just-for-processing-how-kafka-streams-as-a-distributed-database-boosted-our-reliability-and-reduced-maintenance/"&gt;basically a redirect&lt;/a&gt; when a client requests data that the current node does not have. All implementations have their tradeoffs and we need to know what we are optimizing for as we dive into these implementations. Even with its current limitations, interactive queries may be perfect for an application you are building, you just need to accept the failure modes that it has. If those failure modes are acceptable then interactive queries could be a great solution for interacting with the state within Kafka Streams in your application.&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>streams</category>
      <category>upstash</category>
    </item>
    <item>
      <title>Effective Java: Consider Serialization Proxies Instead of Serialized Instances</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Mon, 21 Feb 2022 16:28:01 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-consider-serialization-proxies-instead-of-serialized-instances-1gl9</link>
      <guid>https://dev.to/kylec32/effective-java-consider-serialization-proxies-instead-of-serialized-instances-1gl9</guid>
      <description>&lt;p&gt;Throughout all the recent items as we have discussed Java serialization, we have been discussing many of the challenges that come along with it. While on the surface it looks simple to implement, in reality, it is far from it. Due to the effectively hidden constructor provided by the serialization framework &lt;code&gt;Serializable&lt;/code&gt; code is open to many potential issues that need to be protected against. Thankfully there is a pattern, called the &lt;em&gt;serialization proxy pattern&lt;/em&gt; that can help us sidestep many of these issues.&lt;/p&gt;

&lt;p&gt;One of the best parts of the serialization proxy pattern is that it is rather straightforward, especially compared to some of the alternatives.  The first step is creating a private static nested class that holds all the necessary information to create your target object, this is your serialization proxy. This class will have a single constructor of the type of the enclosing class. This constructor simply copies the data from the parameter into its internal state, with no need for consistency checks or defensive copies. Now the serialization proxy and enclosing class need to add &lt;code&gt;implements Serializable&lt;/code&gt; to their class signature. Let's look at an example of the serialization proxy that we would write for the &lt;code&gt;Period&lt;/code&gt; class we have been discussing in recent items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SerializationProxy&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;SerializationProxy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Period&lt;/span&gt; &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Any number will do here.&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;serialVersionUID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1234567890L&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our next step is to add a &lt;code&gt;writeReplace&lt;/code&gt; method to the enclosing class. This method will look exactly like this in every implementation (assuming you call your private static serialization proxy &lt;code&gt;SerializationProxy&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;writeReplace&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SerializationProxy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this method is on a class and it is serialized it causes the serialization system to return a &lt;code&gt;SerializationProxy&lt;/code&gt; instance instead of an instance of the enclosing class. With this code in place that means that the serialization system will never create an instance of the enclosing class. To make sure no one tries to craft one maliciously we can add the following. (Again this code could be copied verbatim in a class implementing this pattern)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;readObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectInputStream&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InvalidObjectException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidObjectException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Proxy required"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final step in the pattern is to provide a &lt;code&gt;readResolve&lt;/code&gt; method in the &lt;code&gt;SerializationProxy&lt;/code&gt; class that returns a logically equivalent instance of the enclosing class. The way it does this is by using only the enclosing class's public API. The benefit this provides is that it doesn't need to do anything special to protect the creation, all the protections can live within the enclosing class which it would already have to protect itself from "regular" API consumers. This is the benefit of this pattern, it doesn't use any "magic" constructors or capabilities, it simply forces the serialization framework to use the regular language primitives. In our example the &lt;code&gt;readResolve&lt;/code&gt; function would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;readResolve&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Period&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additional benefits this pattern provides to us above and beyond what the previously discussed patterns do is that the member variables in &lt;code&gt;Period&lt;/code&gt; can once again be &lt;code&gt;final&lt;/code&gt; which they always wanted to be to enforce immutability. The other huge benefit is the pattern is simple. A good proportion of it is simply copying and pasting the same code. That is to say, the effort you put in versus the benefit you get out with this pattern is great. The final additional benefit would be that the &lt;code&gt;readObject&lt;/code&gt; method can return a different type of object than the originally serialized instance was. This benefit is taken advantage of by the &lt;code&gt;EnumSet&lt;/code&gt; class in the core of the language. &lt;code&gt;EnumSet&lt;/code&gt; uses the serialization proxy pattern to make for safer serialization and to allow for it to use the most efficient implementation (&lt;code&gt;RegularEnumSet&lt;/code&gt; or &lt;code&gt;JumboEnumSet&lt;/code&gt;) depending on how many types are in a particular enum.&lt;/p&gt;

&lt;p&gt;There is always a cost to whatever we do though so what are the costs here? First, it is not usable with classes that are built to be extended. It is also not compatible with classes that can have circular references in their object graphs. Finally, it comes at a computational cost with measurements in the 14% slower range for using this pattern. All this being said, this pattern can be extremely useful to know. By taking some simple actions and accepting some potentially minimal drawbacks we can have far safer code. &lt;/p&gt;

&lt;p&gt;With the end of this item, we have reached the end of the book &lt;em&gt;Effective Java&lt;/em&gt;. There is a lot of insight that can be gleaned from this book and practicing its guidance. I know that I have learned a lot from the book about some lesser-known features of the language and some of the sharp edges I can watch out for in my day-to-day work. I hope you have gleaned some benefits too. I would like to do some more reviews like this in the future so be sure to subscribe for updates.  &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serializable</category>
      <category>serialization</category>
    </item>
    <item>
      <title>Effective Java: For Instance Control, Prefer Enum types to readResolve</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Wed, 16 Feb 2022 14:06:48 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-for-instance-control-prefer-enum-types-to-readresolve-36m3</link>
      <guid>https://dev.to/kylec32/effective-java-for-instance-control-prefer-enum-types-to-readresolve-36m3</guid>
      <description>&lt;p&gt;In a previous section, we discussed different ways to make singleton objects in Java. One of the methods we discussed followed the following pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Elvis&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Elvis&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Elvis&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Elvis&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;leaveTheBuilding&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By making the constructor &lt;code&gt;private&lt;/code&gt; we prevent unexpected creations of the &lt;code&gt;Elvis&lt;/code&gt; object. The problem with this pattern is that if you add &lt;code&gt;implements Serializable&lt;/code&gt; to the class we open ourselves up for bypassing the private constructor. As has been mentioned in previous chapters, serialization effectively introduces a new, system-provided, constructor. &lt;/p&gt;

&lt;p&gt;The built-in functionality to handle this issue and to take back some control of the instances produced by a class is the &lt;code&gt;readResolve&lt;/code&gt; function. This function allows the substitution of another instance in place of the one created by &lt;code&gt;readObject&lt;/code&gt;. If your class defines a &lt;code&gt;readResolve&lt;/code&gt; function with the proper signature it will be invoked after the &lt;code&gt;readObject&lt;/code&gt; function. The reference returned by this method will then be returned in place of the newly created object. In common usage, no reference to the newly created object is retained and thus it can be garbage collected. &lt;/p&gt;

&lt;p&gt;Using this functionality to sure up our above &lt;code&gt;Elvis&lt;/code&gt; class could end up looking something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;readResolve&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ends up being pretty straightforward in this case, rather than do anything with the newly created object we simply return our one true &lt;code&gt;Elvis&lt;/code&gt; instance. Since no data from the serialization is used we can, and should declare all instance fields as &lt;code&gt;transient&lt;/code&gt;. If you have instance fields that are object reference types then you must declare them &lt;code&gt;transient&lt;/code&gt; to avoid a possible attack where an attacker could get a hold of the deserialized object before it is garbage collected and thus can keep it around resulting in your singleton no longer being a singleton. &lt;/p&gt;

&lt;p&gt;The particular steps of this attack aren't strictly necessary to understand. Interested readers are encouraged to read the source material. Suffice to say it is possible by creating a "stealer" class that causes a circular dependency with the deserialized object and thus avoiding garbage collection can be made. While not a likely attack, it is better to be safe than sorry. &lt;/p&gt;

&lt;p&gt;While declaring all fields as &lt;code&gt;transient&lt;/code&gt; is one method of avoiding this issue, there are other ways to accomplish it as well. Another pattern from our previous singleton chapter used a single-element enum type to facilitate the singleton. This puts much of the singleton safety semantics on the JVM to perform and releases you from that burden. Our example as an enum type would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;Elvis&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="no"&gt;INSTANCE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;favoriteSongs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Hound Dog"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Heartbreak Hotel"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printFavorites&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;favoriteSongs&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;readResolve&lt;/code&gt; still may be necessary even with the above pattern when the instances of a class are not known at compile time.&lt;/p&gt;

&lt;p&gt;Another thing to note when using the &lt;code&gt;readResolve&lt;/code&gt; function is the visibility of the method. If your class is &lt;code&gt;final&lt;/code&gt; then &lt;code&gt;readResolve&lt;/code&gt; should be &lt;code&gt;private&lt;/code&gt;. If your class is non-final you have more options. If you make it &lt;code&gt;private&lt;/code&gt; it will not do any instance control for subclasses. If it is package-private it will only apply to subclasses that live in the same package. Finally, if you make it &lt;code&gt;protected&lt;/code&gt; or &lt;code&gt;public&lt;/code&gt; and a subclass doesn't override it any deserialization of the class will create an instance of the super class, not the subclass, which will likely cause a &lt;code&gt;ClassCastException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In summary, the use of enum type singletons should be preferred whenever possible when trying to enforce instance control on a serializable class. If it is not possible to use the enum pattern then careful consideration needs to be taken when writing the class's &lt;code&gt;readResolve&lt;/code&gt; method. You should make sure that all the class's instance fields are either primitive or marked transient to protect against potential attacks against your instance control mechanism.  &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serializable</category>
      <category>serialization</category>
    </item>
    <item>
      <title>Effective Java: Write readObject Methods Defensively</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Tue, 01 Feb 2022 20:40:31 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-write-readobject-methods-defensively-1fke</link>
      <guid>https://dev.to/kylec32/effective-java-write-readobject-methods-defensively-1fke</guid>
      <description>&lt;p&gt;In a previous item, a date range class was discussed. It includes &lt;code&gt;Date&lt;/code&gt; fields and is careful to avoid breaking its invariants of its start date needing to come before its end date. The way that it accomplishes that is via careful coding of its constructor as well as its accessors. Let's refresh our familiarity with this class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Period&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Period&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Start is after end"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's consider if the need arose to make this class &lt;code&gt;Serializable&lt;/code&gt;. Thinking back to our previous item of discussion about the physical and logical models of our classes, it is reasonable to come to the conclusion that we can use this same form as the serialized form. Because of this, we may be tempted to simply throw &lt;code&gt;implements Serializable&lt;/code&gt; on the class and call it good. Unfortunately, this would open our class up to not keeping its invariants. &lt;/p&gt;

&lt;p&gt;As we have discussed before, Java's default serialization system effectively creates a new hidden constructor for our class. In our existing implementation of the &lt;code&gt;Period&lt;/code&gt; class the constructor is very critical to facilitating the safety of our class invariants. The effectively new constructor that exists with serialization does not have these same checks that protect our invariants so we must provide additional code to keep our internal data safe. &lt;/p&gt;

&lt;p&gt;The way that Java facilitates our taking ownership of the construction of our object during deserialization is via the &lt;code&gt;readObject&lt;/code&gt; method. This method takes a byte stream as its sole parameter and populates the state of an object. Usually, the byte stream that is consumed by this method will have been generated by serializing a normally constructed (and thus invariant keeping) instance of the object. However, since it is simply a stream of bytes, we can never be sure where those bytes came from and whether we can trust the source. We thus could be presented with an artificially created byte stream that does not meet our invariants and thus we can end up with an object that shouldn't be possible to create.  &lt;/p&gt;

&lt;p&gt;With this new consideration in mind we may attempt to resolve the issue by adding a method such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;readObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectInputStream&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                  &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ClassNotFoundException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultReadObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidObjectException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" after "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the spirit of the above is reasonable it is not enough. For the same reason that the original blog post was written (making defensive copies) this implementation opens itself up for being passed a byte stream that preserves the invariants of the class initially but then, since the reference could be modified by code outside of the class, the invariants could be broken by simply changing the value outside the class. &lt;/p&gt;

&lt;p&gt;To solve this problem we must remember to always defensively copy any field that contains an object reference when using serialization. We thus can extend our above solution as follows to make it safe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;readObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectInputStream&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                  &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ClassNotFoundException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultReadObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

  &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTime&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
  &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTime&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidObjectException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" after "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this version, we do our defensive copy &lt;em&gt;and then&lt;/em&gt; do our validity check. This allows us to have full control of the variables when we do the check. We, unfortunately, do need to remove the &lt;code&gt;final&lt;/code&gt; modifier on the member variables for this to work but that is the price we pay for safe deserialization. &lt;/p&gt;

&lt;p&gt;The test that you can perform to determine if the default deserialization method will work is, would you be comfortable having a constructor on your class that simply took in the member variables and saved the state without any further validation? If so, then you are likely safe using the default deserialization method as far as defensive copying goes, if not, you should take steps to protect yourself from these possible issues.&lt;/p&gt;

&lt;p&gt;The final item of consideration is that of making sure not to call overridable methods from the &lt;code&gt;readObject&lt;/code&gt; method. This is the same caution that applies to constructors (because &lt;code&gt;readObject&lt;/code&gt; is effectively a constructor) and for the same reason. If you call overridable methods from the &lt;code&gt;readObject&lt;/code&gt; method you are open to having those methods called before the whole state of the object is initialized which can lead to issues.&lt;/p&gt;

&lt;p&gt;In summary, it is best to remember that when using serialization you are effectively creating a new public constructor for your class. If you wouldn't be comfortable with having such a public constructor for your class, implement the &lt;code&gt;readObject&lt;/code&gt; method and make sure that it takes care of the state in a defensive manner. The byte streams that your &lt;code&gt;readObject&lt;/code&gt; method is passed should be handled as if they didn't come from a trusted source (because it may not have). If an entire object graph must be validated after being deserialized you can use the &lt;code&gt;ObjectInputValidation&lt;/code&gt; interface (not discussed in this item).&lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serializable</category>
      <category>serialization</category>
    </item>
    <item>
      <title>Effective Java: Consider Using a Custom Serialized Form</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Fri, 21 Jan 2022 22:01:08 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-consider-using-a-custom-serialized-form-3i70</link>
      <guid>https://dev.to/kylec32/effective-java-consider-using-a-custom-serialized-form-3i70</guid>
      <description>&lt;p&gt;As discussed in our previous post, the serialized form of an object is part of its API. This means that it is something that we should respect for some time going forward and that if we break it, we will be causing an unnecessary burden to the users of our code. This being the case, we should take great care in determining what structure the serialized version of our classes take. That is what this post focuses on. &lt;/p&gt;

&lt;p&gt;A particular object can be thought of in two different ways, that of its &lt;em&gt;physical&lt;/em&gt; representation and that of its &lt;em&gt;logical&lt;/em&gt; representation. The physical representation of an object is that of the members and pieces of its internal structure, this is what acts as the state of a particular object. The logical representation of an object serves as a representation of an object conceptually, as a human would think of it. Rather than focussing on the nuts and bolts of how it is put together, it is focussed on the meaningful components. This may not make total sense right now but with a few examples, I think it can be a beneficial model to use. &lt;/p&gt;

&lt;p&gt;The default serialized form of an object, that is the serialized version of a regular object used within the program and not the serialized version of an object specifically created for serialization, is likely an appropriate serialization to use if the physical and logical representation of an object is identical. For example, the following class's physical and logical representation are the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
  * Family Name. Must be non-null.
  * @serial 
  */&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;familyName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/**
  * Given Name. Must be non-null.
  * @serial 
  */&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;givenName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="cm"&gt;/**
  * Middle Name. May be null.
  * @serial 
  */&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// rest omitted.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logically speaking a name is made up of these components and since the object physically represents them the same way this class could be serialized as-is. You will notice that even though the above members are &lt;code&gt;private&lt;/code&gt; they still include a JavaDoc comment. This is because they are part of the serialized form of the class and therefore are part of the API. We put the &lt;code&gt;@serial&lt;/code&gt; tag to tell JavaDoc to include this on the special page of the documentation for serialization.&lt;/p&gt;

&lt;p&gt;Even when the default serialized form is appropriate for a particular class we still may need to implement the &lt;code&gt;readObject&lt;/code&gt; method to protect invariants like the non-nullability of &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; above. This will be discussed further in future posts. &lt;/p&gt;

&lt;p&gt;Now let's look at a class where the logical and the physical representations do not match. This class serves as a container for &lt;code&gt;String&lt;/code&gt; objects (let's ignore that using a collection of type &lt;code&gt;String&lt;/code&gt; would be far superior to this for a moment)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringList&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Remainder omitted.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the logical side, this class represents a collection of strings. From the physical side, this class is a doubly-linked list of entries. These two don't match. Since the default serialized form mirrors the physical form of an object it will end up representing each item individually and all connections both backward and forwards. &lt;/p&gt;

&lt;p&gt;Using the default serialized form when the physical and logical representation of an object don't match can lead to a couple of issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;It ties your class's exported API to the current implementation of the class.&lt;/em&gt; This greatly reduces the extensibility of your class. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;It consumes unnecessary space in its serialized form.&lt;/em&gt; In our above example, this would be the unnecessary links between each entry.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;It consumes unnecessary time when serializing and deserializing.&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;It can cause stack overflows.&lt;/em&gt; Because of the recursive traversal of the objects in the graph when serializing this can lead to stack overflows. Serializing our above &lt;code&gt;StringList&lt;/code&gt; having an instance with between 1,000 and 1,800 elements led to a &lt;code&gt;StackOverflowException&lt;/code&gt;. The size wasn't even consistent when the error was thrown due to internal differences in the runtime executions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's consider a reasonable serialized form for our &lt;code&gt;StringList&lt;/code&gt; example. All we need is maybe an integer detailing the size of the list and then the entries themselves. This would much closer match our logical model of what this class does. Here is what that may look like now with &lt;code&gt;readObject&lt;/code&gt; and &lt;code&gt;writeObject&lt;/code&gt; implemented to create our custom serialized form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringList&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;transient&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;transient&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// No longer serializable.&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;newString&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Omitted.&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;/**&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Serialize&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nd"&gt;@code&lt;/span&gt; &lt;span class="nc"&gt;StringList&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nd"&gt;@serailData&lt;/span&gt; &lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="nf"&gt;emitted&lt;/span&gt; &lt;span class="o"&gt;({&lt;/span&gt;&lt;span class="nd"&gt;@code&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;}),&lt;/span&gt; &lt;span class="n"&gt;followed&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;its&lt;/span&gt; &lt;span class="nf"&gt;elements&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nd"&gt;@code&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;}).&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;writeObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectOutputStream&lt;/span&gt; &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultWriteObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;readObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectInputStream&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ClassNotFoundException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultReadObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;numElements&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readObject&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Remainder omitted.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is undoubtedly more code, but it is worth the cost. The first thing that both &lt;code&gt;readObject&lt;/code&gt; and &lt;code&gt;writeObject&lt;/code&gt; do is invoke their &lt;code&gt;defaultRead/WriteObject&lt;/code&gt; method. Even though all the fields of this class are marked &lt;code&gt;transient&lt;/code&gt; we still need to invoke these. This will allow adding non-transient fields in a future release and still maintain backward compatibility. If an object is serialized in a new version with non-transient field and then deserialized in an older version where they weren't there they would simply be ignored. If we didn't take this step the serialization would fail with &lt;code&gt;StreamCorrupttedException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Again we see JavaDoc on a private method detailing how the serialization form of this object will take form. The &lt;code&gt;@serialData&lt;/code&gt; tag marks this as something that should show up on the page for serialization of the documentation. &lt;/p&gt;

&lt;p&gt;Let's consider the performance difference between this new custom serialized form and the old default form. Considering &lt;code&gt;StringList&lt;/code&gt; instances with an average &lt;code&gt;String&lt;/code&gt; length of 10 characters the new form takes half as much space when serialized. It is also twice as fast in the tests of it. Finally, there are no longer stack overflows no matter the size of the list.&lt;/p&gt;

&lt;p&gt;Even though our &lt;code&gt;StringList&lt;/code&gt; example is bad it at least is mostly usable with its default serialization. That is not always the case. Consider the case of a hash table. Its physical representation is a sequence of hash buckets containing key-value pairs. The bucket a particular item falls in is a function of its hash. This hash is not, in general, guaranteed to be the same from implementation to implementation. Therefore, not only is it less than ideal, it could be broken by simply serializing and then deserializing an object of that class.&lt;/p&gt;

&lt;p&gt;No matter what form you take for your serialized data every field of an object not marked &lt;code&gt;transient&lt;/code&gt; will be serialized when the &lt;code&gt;defaultWriteObject&lt;/code&gt; method is invoked. This means that every field that can be marked &lt;code&gt;transient&lt;/code&gt; should be. This includes derived fields, generated fields, cache value fields, or fields pointing to something specific to that one run (for example a native filehandle). Before a field should be marked as non-transient you should be able to convince yourself it is part of the logical model of the class. Do note that fields marked as &lt;code&gt;transient&lt;/code&gt; will be initialized to their default value when the class is created via deserialization. &lt;/p&gt;

&lt;p&gt;Another thing to keep in mind is that if you are creating a thread-safe class you should also synchronize the &lt;code&gt;writeObject&lt;/code&gt; method, even if you aren't using a custom serialized form. This can look something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;writeObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectOutputStream&lt;/span&gt; &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultWriteObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, no matter what form our class takes we should also explicitly set the serial version UID for serializable classes we write. This helps prevent the serial version UID from becoming an invalid source of incompatibility. It has the bonus of providing a small performance benefit as it avoids the process that must happen at runtime where the UID would need to get generated if it wasn't provided.  It doesn't matter what value we set this variable to, just pick any random &lt;code&gt;long&lt;/code&gt; value. Then when you modify your class in such a way it is no longer compatible simply increment the value. &lt;/p&gt;

&lt;p&gt;In summary, when using Java's built-in serialization consider whether the default serialized form of a class is appropriate. You can determine if it is appropriate by determining if its logical and physical representations are the same. Serialized forms of your class are every bit as much a part of your class's public API as its methods and thus should be given just as much consideration and planning. &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serializable</category>
      <category>serialization</category>
    </item>
    <item>
      <title>Effective Java: Implement Serializable With Great Caution</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Tue, 18 Jan 2022 22:49:38 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-implement-serializable-with-great-caution-n8j</link>
      <guid>https://dev.to/kylec32/effective-java-implement-serializable-with-great-caution-n8j</guid>
      <description>&lt;p&gt;In the last topic, we covered why we should avoid using the built-in serialization framework in Java. A big part of that serialization system is the _Serializable _ interface. This interface indicates some of the magic promised by Java's serialization. Simply add this interface (which requires no methods to be implemented) and all of a sudden you have serialization. Unfortunately, this is not the case, while that enables serialization there are many concerns related to serialization that the developer of the program must keep in mind. This post covers some of those concerns. &lt;/p&gt;

&lt;p&gt;A major cost of implementing the &lt;em&gt;Serializable&lt;/em&gt; interface is the loss of encapsulation of internal data structure and thus a decrease in flexibility. Once you implement the &lt;em&gt;Serializable&lt;/em&gt; interface the output of the serialization is part of your code's API and thus must not be changed without care. A potential way to minimize this risk would be to create a &lt;em&gt;custom serialized form&lt;/em&gt; of your data (an idea that will be discussed in a future item). If you don't take this mitigating action though, by default, all of your &lt;em&gt;private&lt;/em&gt; and &lt;em&gt;package-private&lt;/em&gt; fields become part of the API.&lt;/p&gt;

&lt;p&gt;If you do change the internal structure of your class and then someone tries to use the new code to read an old object byte stream they will be presented with failures. There are specific ways to try to account for internal changes by using &lt;code&gt;ObjectOutputStream.putFields&lt;/code&gt; and &lt;code&gt;ObjectOutputStream.readField&lt;/code&gt; but these are far from a clean solution. Thus, if you are going to use Java serialization then you need to carefully design a serialized form of the class that you are ready to support for the long term.&lt;/p&gt;

&lt;p&gt;An example of the limitations on the evolution of a class imposed by serialization is &lt;em&gt;stream unique identifiers&lt;/em&gt;, also known as &lt;em&gt;serial version UIDs&lt;/em&gt;. Each serializable class has a unique identifier to specify the serializable version that it is. This can be manually set by declaring a &lt;code&gt;static final long&lt;/code&gt; field of the name &lt;code&gt;serialVersionUID&lt;/code&gt;. If you don't specify one one will be generated for you at runtime by applying a cryptographic hash (SHA-1) to the structure of your class. This will mean that it will have a consistent value &lt;em&gt;as long as you don't change anything about the structure of the class&lt;/em&gt;. This means names of the class, interfaces it implements, most member variables, and even synthetic members generated by the compiler all affect this unique identifier. So even if you made a change that shouldn't affect the serialization of the class you still could be presented with an &lt;code&gt;InvalidClassException&lt;/code&gt; at runtime when trying to use it.&lt;/p&gt;

&lt;p&gt;Another major cost of implementing &lt;em&gt;Serializable&lt;/em&gt; is that it increases the likelihood of bugs and security holes. This is covered fairly extensively in the last topic from this book. A lot of this concern comes down to a backdoor being generated for your classes to be created from. Because there is this &lt;em&gt;hidden constructor&lt;/em&gt; it is easy to forget that you must validate the invariants of your class even in this case.&lt;/p&gt;

&lt;p&gt;Yet another burden of serializability is the increased testing burden when making changes to the class. If you want a robust program you don't only need to verify that the business logic is sound, that previous bugs haven't regressed, and that your code is performant, but you also need to verify that the serializability is still sound. You can again mitigate some of this burden if you use a custom serializable form and if you minimize the number of versions of your class that can exist in the wild but the burden is still there. &lt;/p&gt;

&lt;p&gt;Sometimes implementing &lt;em&gt;Serializable&lt;/em&gt; can not be avoided. This should not be taken lightly though. Whether it is because a class is participating in a framework that needs object transmission or persistence or if the class is participating as a component of another &lt;em&gt;Serializable&lt;/em&gt; class it can have its uses. When the decision to implement the interface is undertaken it is then our responsibility to do it safely. Within the core language, it has historically been that value classes such as &lt;em&gt;BigInteger&lt;/em&gt; and &lt;em&gt;Instant&lt;/em&gt; implement as well as collections implement serializable. However, classes that represent active executing items such as &lt;code&gt;Thread&lt;/code&gt; and &lt;code&gt;Thread Pools&lt;/code&gt; have not. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Serializable&lt;/code&gt; should rarely be implemented by classes designed for inheritance as well as new interfaces should rarely extend &lt;code&gt;Serializable&lt;/code&gt;. If you do one of the above you will be putting a heavy burden on the future users of your classes and interfaces. You may need to violate this rule if your class or interface's sole purpose is to participate in a framework that requires serializability. Some examples from the core library are &lt;code&gt;Throwable&lt;/code&gt; and &lt;code&gt;Component&lt;/code&gt;. &lt;code&gt;Throwable&lt;/code&gt; requires serializability because it is enabling exceptions to be passed via RMI. &lt;code&gt;Component&lt;/code&gt; implements it so that GUIs can be sent, saved, and restored (even though this ability was rarely used). &lt;/p&gt;

&lt;p&gt;If you choose to implement a class that is built for extensibility as well as is serializable there are a few items to be aware of. If ther are any invariants that must be kept for your fields then you must ensure that no subclass overrides the &lt;code&gt;finalize&lt;/code&gt; method. You can do this by overriding it yourself and marking it as &lt;code&gt;final&lt;/code&gt;. If you don't you leave your class open to a &lt;em&gt;finalizer attack&lt;/em&gt;. Also, if you have invariants of fields that would be violated if they were reset to their default values then you must add a &lt;code&gt;readObjectNoData&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;readObjectNoDAta&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InvalidObjectException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidObjectException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Stream data required"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method was added in Java 4 to account for the edge case where a serializable superclass was added to an existing class. &lt;/p&gt;

&lt;p&gt;When deciding to &lt;em&gt;not&lt;/em&gt; implement &lt;code&gt;Serializable&lt;/code&gt; on a class built for extensibility you need to also consider if a subclass would reasonably need to implement &lt;code&gt;Serializable&lt;/code&gt;. This is because deserializing requires the superclass to have an accessible parameterless constructor. If there is no such constructor, subclasses must follow other patterns to succeed. &lt;/p&gt;

&lt;p&gt;Finally, inner (non-static) classes should not implement &lt;code&gt;Serializable&lt;/code&gt;. The way these are implemented is using &lt;em&gt;synthetic fields&lt;/em&gt; that store references to its enclosing instance and to store values of the local variables from the enclosing scope. The way that these are defined is ill-defined and thus should be avoided. Static member classes, however, don't have this issue. &lt;/p&gt;

&lt;p&gt;In summary, correctly implementing the &lt;code&gt;Serializable&lt;/code&gt; interface is full of pitfalls. Unless you have a high level of control of your environment where versioning and data inputs are constrained you will be in an uphill battle. This only gets more challenging when also introducing inheritance. &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serializable</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Prefer Alternatives To Java Serialization</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Tue, 04 Jan 2022 15:29:59 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-prefer-alternatives-to-java-serialization-p7k</link>
      <guid>https://dev.to/kylec32/effective-java-prefer-alternatives-to-java-serialization-p7k</guid>
      <description>&lt;p&gt;Java's built-in serialization has been part of the language since 1997, just two years after its inception. Even from the beginning of its life as part of the language it has been known to be risky. While the goal was well-intended, that of distributing objects with little effort, in hindsight it is largely agreed that it was not worth the costs in correctness, performance, security, and maintenance.&lt;/p&gt;

&lt;p&gt;There are countless examples throughout the history of the Java language where Java's built-in serialization has caused issues. One such example was a ransomware attack on the San Francisco Metropolitan Transit Agency Municipal Railway that shut down the entire fare collection system for two days in 2016. &lt;/p&gt;

&lt;p&gt;A core problem with Java serialization is that it aims to be so broad that it makes the attack surface extremely large. Object graphs are deserialized by the &lt;code&gt;readObject&lt;/code&gt; method on the &lt;code&gt;ObjectInputStream&lt;/code&gt; class. This effectively serves as a magic constructor that can instantiate an object of basically any type as long as it implements the &lt;code&gt;Serializable&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;There are basically no classes that aren't part of the serialization attack surface. JVM classes, third-party classes, and the classes from the application itself are all possible targets. Even if your code doesn't use Java serialization explicitly it may still be using serialization under the hood. This is because there are major parts of the Java platform such as RMI (Remote Method Invocation), JMX (Java Management Extension), and JMS (Java Messaging System) that are built on top of the serialization that Java offers. Deserialization of untrusted sources via these systems can lead to remote code execution, denial-of-service attacks, and other issues. &lt;/p&gt;

&lt;p&gt;Attackers and security researchers alike are always in search of new classes that they can exploit via serialization. Many times it is via the chaining of these exploits that the actual exploit is performed. That is exactly what happened with the railway system mentioned above. &lt;/p&gt;

&lt;p&gt;Even without these chains of exploits we can run into serialization issues with even basic looking code. Attackers will often look for ways they can provide a small amount of code and get a disporportiate amount of computation performed in search of a denial-of-service attack. This is often refered to as a &lt;em&gt;deserialization bomb&lt;/em&gt;.  Let us look at one example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;bomb&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code creates an object graph of 201 &lt;code&gt;HashSet&lt;/code&gt; instances each with 3 or fewer object references. The whole graph only takes up 5,744 bytes but is impossible to deserialize. This is because deserializing a &lt;code&gt;HashSet&lt;/code&gt; requires computing the hash codes of all of its elements. The two elements of the root &lt;code&gt;HashSet&lt;/code&gt; themselves have two more hash sets all the way down the 100 levels. This causes the &lt;code&gt;hashCode&lt;/code&gt; function to be called 2^100 times. The extra frustrating part of this is that, other than the code not completing, there is no indication of a problem.&lt;/p&gt;

&lt;p&gt;So I think we have sufficiently demonstrated that Java's built-in serialization has many pitfalls. What are we to do?  The best way to solve this problem is to avoid it entirely. There is no good reason to use Java serialization in any new code you write today. Instead, you should use some other form of data transfer. These systems have the bonus of being cross-platform. These representations have the benefit of being much simpler and having a much smaller scope than Java serialization. This allows it to be much safer as they are usually focused solely on data. Some examples of these formats are JSON, Protocol Buffers (Protobuf), and Avro. While Protocol Buffers and Avro also can facilitate schema verification as well as have extensions for remote procedure call systems (RPC) they are still much simpler than Java serialization is when it comes to simply transfering state. &lt;/p&gt;

&lt;p&gt;If you however are working on a legacy system that is already using serialization there are still some things you can do to mitigate your risks. First would be to only deserialize trusted data. The official secure coding guidelines for Java say "Deserialization of untrusted data is inherently dangerous and should be avoided" in large, bold, red letters. This is the only guideline given such extreme focus thus we shouldn't ignore it. Another tool you can use is the &lt;code&gt;java.io.ObjectInputFilter&lt;/code&gt; class added in Java 9 (and also backported). This allows more control over what types of objects can and can't be deserialized in our system. You can choose to accept only certain types (an allowed list) or not accept certain types (a disallow list). If at all possible you should use a allow list as this gives the most control over what is accepted. A disallow list, while still useful, only can protect you from known issues, not the new ones just being discovered.&lt;/p&gt;

&lt;p&gt;There is still a lot of Java code that uses serialization in use today. That being the case we should understand the complexities and issues it can introduce. We should also use whatever tools we have available to limit our exposure to these issues and the blast radius that they have. In general, if you can avoid Java serialization, avoid it. In new systems don't introduce Java serialization at all. Use modern data interchange formats instead to transfer data across systems. &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>serialization</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Don't Depend on the Thread Scheduler</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Thu, 16 Dec 2021 17:26:49 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-dont-depend-on-the-thread-scheduler-31a2</link>
      <guid>https://dev.to/kylec32/effective-java-dont-depend-on-the-thread-scheduler-31a2</guid>
      <description>&lt;p&gt;Even on modern systems that have many CPU cores and thus can be concurrently executing multiple threads, they are likely no match for the number of threads in a runnable state on a system. For this reason, we have the &lt;em&gt;thread scheduler&lt;/em&gt; which determines which threads will run and for how long. Implementations of thread schedulers strive for equality in how they treat threads but their exact semantics vary from implementation to implementation. This being the case, relying on the particular semantics of a thread scheduler is not wise and can lead to unexpected behavior on different systems and non-portable code. &lt;/p&gt;

&lt;p&gt;A good plan for having a robust, responsive application is to aim for the number of runnable threads to be, on average, the number of cores your system has. Of note, this is aimed at &lt;em&gt;runnable&lt;/em&gt; threads and not simply threads that exist. Runnable threads are threads that are ready to do work and that are not in a waiting state. Threads in a waiting state are easier for the thread scheduler to reason about because they aren't requesting to be run therefore they won't be scheduled. That's not to say there is no cost to having waiting threads but those aren't being discussed in this topic because they aren't related to the thread scheduler. &lt;/p&gt;

&lt;p&gt;The simple rule of thumb is that any runnable threads should be doing useful work and not simply running to keep themselves scheduled. Threads should also keep their work short (but not too short thus spending all of its time in dispatching overhead). At its core, this means your threads should not be busy-waiting. While busy-waiting can be an advanced technique that can be used in some specific circumstances they are rare and likely not what you are wanting. Busy waiting just wastes CPU cycles while not progressing the work of the application. Let us look at an example of an extremely poor CountdownLatch implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BadCountDownLatch&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BadCountDownLatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" &amp;lt; 0"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;await&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;countDown&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This implementation underperforms the built-in implementation by 10x when 1,000 threads are waiting on the latch. While at first glance this implementation may look like something that wouldn't be written, it comes up much more than it should.&lt;/p&gt;

&lt;p&gt;When presented with an application that has issues due to thread scheduling one might be tempted to "fix" the issue by calling &lt;code&gt;Thread.yield&lt;/code&gt; on the problematic thread. Even if this works in your case it is not guaranteed to work in other cases. There are no testable semantics to the &lt;code&gt;yield&lt;/code&gt; function. A much better reaction to this issue would be to restructure your application to correctly handle the concurrent runnable threads it has.&lt;/p&gt;

&lt;p&gt;Another technique that you may be tempted to use is Thread priorities. These again don't have testable semantics and have different implementations on different systems and JVMs and thus should not be used to attempt to fix issues.&lt;/p&gt;

&lt;p&gt;Summing this topic up, do not rely on the thread scheduling algorithm of your JVM to provide correctness to your application. This includes relying on &lt;code&gt;Thread.yield&lt;/code&gt; and thread priorities. Instead, strive to keep the number of runnable threads to around the number of executable processes your system can run concurrently.&lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>concurrency</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Use Lazy Initialization Judiciously</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Wed, 08 Dec 2021 15:02:09 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-use-lazy-initialization-judiciously-23h8</link>
      <guid>https://dev.to/kylec32/effective-java-use-lazy-initialization-judiciously-23h8</guid>
      <description>&lt;p&gt;&lt;em&gt;Lazy initialization&lt;/em&gt; is the pattern of putting off the creation of an object or process until it is needed. The idea behind this pattern is that you may never need the object and thus you saved the initialization costs. The main reason that lazy initialization is used is as an optimization. The other use that lazy initialization has is breaking tricky circular dependencies in your code.&lt;/p&gt;

&lt;p&gt;As discussed in a previous item going down the path of optimizations is often fraught with peril and we can sometimes even decrease performance in the search of performance improvements. As with all optimizations, we should test out and confirm that we will truly see the improvements we are after. With lazy initialization, this will largely rely on how often we can completely avoid initialization of the object and how expensive that initialization is.  Of note though, bringing in lazy initialization, especially in the presence of multiple threads, can introduce a new level of complexity which might not be worth its cost. For these reasons, unless presented with hard evidence to the contrary, you should use eager instantiation in almost all cases. &lt;/p&gt;

&lt;p&gt;Let's say that we have determined that lazy instantiation is required, let's look at a few patterns of how to accomplish it. The first method can be used when trying to break a circular dependency since it is the simplest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="nf"&gt;getField&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computeFieldValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ends up being fairly simple. Within a synchronized method, determine if the field is initialized, if it isn't, initialize it, if it is, just return it. The &lt;code&gt;synchronized&lt;/code&gt; keyword allows us to use this method with concurrent threads at the cost of some performance. This same pattern can be used with a static field by simply marking the field as static and the function as static.&lt;/p&gt;

&lt;p&gt;The next pattern we can use is useful when we need to lazily initialize a static field with a pattern called &lt;em&gt;lazy initialization holder class idiom&lt;/em&gt;. This idiom uses the guarantee that a class will not be initialized until it is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FieldHolder&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computeFieldValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="nf"&gt;getField&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;FieldHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is quite a beautiful pattern. On the first call to the &lt;code&gt;getField&lt;/code&gt; method, the object reads &lt;code&gt;FieldHolder.field&lt;/code&gt; at which point the class will be initialized. This idiom doesn't require any explicit synchronization as that will be provided only on the initialization of the class and only does a field access which means it's going to be extremely performant as well. &lt;/p&gt;

&lt;p&gt;The next use case we may find ourselves in is needing to do lazy initialization for performance reasons on an instance field. In this case, we should look at the &lt;em&gt;double-check idiom&lt;/em&gt;. This pattern avoids the cost of synchronization once the field is initialized with the trade-off that we need to check the field twice. It first checks the field to determine if it should look into initializing the field. If it appears it needs initialization then it obtains the lock and then double checks that initialization is still required.  Because there is no locking once initialized the field must be marked as volatile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="nf"&gt;getField&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computeFieldValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is a bit convoluted, to be honest, but via that convolution there are benefits. The need for the local &lt;code&gt;result&lt;/code&gt; variable may seem unclear. The purpose of this local variable is to ensure that in the common case (the case where the field is initialized) it is only read once. While not necessary it can improve performance. In the case where a field can tolerate repeated initialization, we can instead use the &lt;em&gt;single-check idiom&lt;/em&gt;. As the name suggests, this pattern drops one of the checks to simplify the code at the cost of possible multiple initializations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="nf"&gt;getField&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;FieldType&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computeFieldValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final variant that can be considered is if we are OK with up to a reinitialization per thread and the field is of a primitive type (except &lt;code&gt;long&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt;) we can forgo the &lt;code&gt;volatile&lt;/code&gt; keyword. On some architectures that can lead to greater performance. &lt;/p&gt;

&lt;p&gt;There are many complications when trying to tackle lazy initialization. If at all possible we should avoid it. That said if after testing it is confirmed it will be of benefit, lazy initialization can be useful for improving performance or breaking a circular dependency. There are proven methods we can use presented above that balance safety and performance even in a concurrent environment. &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>concurrency</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Document Thread Safety</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Thu, 02 Dec 2021 01:18:09 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-document-thread-safety-3l31</link>
      <guid>https://dev.to/kylec32/effective-java-document-thread-safety-3l31</guid>
      <description>&lt;p&gt;Users of classes you write need to know how they behave. One of the attributes of your class that a user needs to know is whether the class is thread-safe or not. Outside of this documentation a user of the class needs to guess about the class's thread-safety. This can lead either to excessive synchronization or insufficient synchronization which can lead to invariant issues. &lt;/p&gt;

&lt;p&gt;It is important to know that thread-safety is not black and white. There are levels of thread safety. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Immutable objects&lt;/em&gt; - Since there is no mutable data inside of an immutable object they are inherently thread-safe. Examples of immutable objects are: &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Long&lt;/code&gt;, and &lt;code&gt;BigInteger&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Unconditionally thread-safe objects&lt;/em&gt; - These objects are thread-safe in all usages. This can make them simple to use as all operations on the object can be considered thread-safe and we don't need to account for the differences between methods. Examples of unconditionally thread-safe objects are &lt;code&gt;AtomicLong&lt;/code&gt; and &lt;code&gt;ConcurrentHashMap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Conditionally thread-safe objects&lt;/em&gt; - These objects include both thread-safe and thread-unsafe functions depending on what part of the object you are interacting with. These objects need great documentation to help the users of the objects to know where external synchronization may be required. An example of this would be the &lt;code&gt;Collections.synchronized&lt;/code&gt; wrappers that synchronize much of interactions with the object but do require external synchronization of &lt;code&gt;iterators&lt;/code&gt; returned.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Not thread-safe objects&lt;/em&gt; - The objects contain mutable data and make no effort at synchronization. If a user of one of these objects would like to use it in a concurrent way they must bring their own synchronization. There are many, many examples of these types of objects such as &lt;code&gt;ArrayList&lt;/code&gt; and &lt;code&gt;HashMap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Thread-hostile&lt;/em&gt; - These objects are often not implemented this way on purpose and are unsafe to use even if you perform perfect external synchronization. The most common way this can occur is through the unsafe interaction with &lt;code&gt;static&lt;/code&gt; values. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As noted above, documenting a conditionally thread-safe class requires care. You must not only indicate that the class is not completely thread-safe but what locks must be obtained to make its non-thread-safe methods thread-safe. For example, let's look at the documentation for &lt;code&gt;Collections.synchronizedMap&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

  Map m = Collections.synchronizedMap(new HashMap());
      ...
  Set s = m.keySet();  // Needn't be in synchronized block
      ...
  synchronized (m) {  // Synchronizing on m, not s!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

Failure to follow this advice may result in non-deterministic behavior.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thread-safety documentation usually belongs in the class level documentation but if a specific method has special concerns the documentation can also live there. Documenting the thread-safety of static factories (like in the &lt;code&gt;Collections.synchronizedMap&lt;/code&gt; example above) is also a good idea.&lt;/p&gt;

&lt;p&gt;When a class uses a publicly accessible lock it can initially feel like it is enabling clients of the class more control. However, this flexibility can come at the cost. This is because it is incompatible with high-performance internal concurrent controls such as those used in &lt;code&gt;ConcurrentHashMap&lt;/code&gt;. It also opens yourself up for a client holding the lock for a long time either accidentally or intentionally. In either case, it can lead to serious problems. &lt;/p&gt;

&lt;p&gt;Alternatively, we can use a private lock object in our synchronized methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few things we can learn from this example. By marking the object as private we protect ourselves from someone external to our class holding the object for too long. We also mark it as &lt;code&gt;final&lt;/code&gt; protecting ourselves from accidentally reassigning it and from subclasses changing it as well. Even when using locks from the &lt;code&gt;java.util.concurrent.locks&lt;/code&gt; package we should always make them &lt;code&gt;final&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As convenient and beneficial as this private lock pattern is, we can only use this method when writing &lt;em&gt;unconditionally thread-safe objects&lt;/em&gt;. When writing a conditionally thread-safe object we are unable to do this because we have to provide the user of the class a handle to hold a lock when doing operations on the non-thread-safe parts of the object.&lt;/p&gt;

&lt;p&gt;Thread-safety can be a tricky problem within an application. It becomes much more difficult when the classes we are using haven't documented what level of thread-safety they have implemented. There is no right or wrong level of thread-safety, different types of behavior can lend themselves to different levels of thread-safety. No matter the level we can document their status. Remember to make all your lock objects &lt;code&gt;final&lt;/code&gt; and when possible also &lt;code&gt;private&lt;/code&gt;. By following these guidelines we can mitigate some of the difficulty out of working in a concurrent environment.  &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>concurrency</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Prefer Concurrency Utilities Over wait and notify</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Fri, 19 Nov 2021 16:15:31 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-prefer-concurrency-utilities-over-wait-and-notify-4b5i</link>
      <guid>https://dev.to/kylec32/effective-java-prefer-concurrency-utilities-over-wait-and-notify-4b5i</guid>
      <description>&lt;p&gt;At the core of each &lt;code&gt;Object&lt;/code&gt; in the Java language there are three methods, &lt;code&gt;wait&lt;/code&gt;, &lt;code&gt;notify&lt;/code&gt;, and &lt;code&gt;notifyAll&lt;/code&gt;. These methods allow you low-level concurrency control options. Up until Java 5, this was the go-to option for facilitating concurrency control. However, since the release of Java 5 (in 2004) there are now higher-level tools that can be used that are much easier and less error-prone. This being the case, in new code, we should be using exclusively these provided concurrency utilities. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;java.util.concurrent&lt;/code&gt; provides three different kinds of utilities. The first is the Executor framework discussed in a previous item, concurrent collections implementations, and synchronizers. &lt;/p&gt;

&lt;p&gt;The concurrent collections provided by the Java language are high-performance concurrent implementation of the common collection interfaces (&lt;code&gt;List&lt;/code&gt;, &lt;code&gt;Queue&lt;/code&gt;, and `Map). These collections bring their own concurrency controls and thus don't need (and shouldn't be) externally synchronized. At times these collections provide lock-free implementations of functions and the fact that the user of the collection doesn't need to know how the implementation works is great.&lt;/p&gt;

&lt;p&gt;Because these concurrent collections sometimes need to do multiple actions (because they are state-dependent) atomically they provide functions to facilitate this. These methods can be extremely useful so in Java 8 many of these functions were provided on the main Collection interfaces. One example of this is &lt;code&gt;putIfAbsent(key, value)&lt;/code&gt;. We could use this functionality to develop a &lt;code&gt;Strin.intern&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
private static final ConcurrentMap map = new ConcurrentHashMap&amp;lt;&amp;gt;();&lt;/p&gt;

&lt;p&gt;public static String intern(String s) {&lt;br&gt;
  String previousValue = map.putIfAbsent(s, s);&lt;br&gt;
  return previousValue == null ? s : previousValue;&lt;br&gt;
}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is pretty efficient and concise but we can make it even more performant but using the knowledge that &lt;code&gt;get&lt;/code&gt; operations are optimized in &lt;code&gt;ConcurrentHashMap&lt;/code&gt; and write the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
public static String intern(String s) {&lt;br&gt;
  String result = map.get(s);&lt;br&gt;
  if (result == null) {&lt;br&gt;
    result = map.putIfAbsent(s, s);&lt;br&gt;
    if (result == null) {&lt;br&gt;
      result = s;&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
  return result;&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This new implementation is actually faster than the built-in &lt;code&gt;intern&lt;/code&gt; function (although it doesn't take into account some of the memory management jobs that the built-in &lt;code&gt;intern&lt;/code&gt; function must perform).&lt;/p&gt;

&lt;p&gt;These built-in concurrent capable collections effectively obsolete the built-in synchronizing collection operations (&lt;code&gt;Collections.synchronizedMap&lt;/code&gt; etc). Simply replacing one of these usages with its concurrent counterpart is likely a great performance benefit. These concurrent collections are even used internally by the concurrent package to facilitate its work like the &lt;code&gt;BlockingQueue&lt;/code&gt;'s usage in &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; as discussed in the previous item. &lt;/p&gt;

&lt;p&gt;Another type of concurrent utility provided by the &lt;code&gt;java.util.concurrent&lt;/code&gt; package is &lt;em&gt;synchronizers&lt;/em&gt;. These are objects that allow one thread to wait on another thread. Although this is a fairly basic concept it can be used powerfully. The most common synchronizers are the &lt;code&gt;CountdownLatch&lt;/code&gt; and the &lt;code&gt;Semaphore&lt;/code&gt; but there are also more advanced synchronizers like the &lt;code&gt;CyclicBarrier&lt;/code&gt;, &lt;code&gt;Exchanger&lt;/code&gt;, and &lt;code&gt;Phaser&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look a little more into &lt;code&gt;CountdownLatch&lt;/code&gt;. This class serves as a single-use barrier that allows threads to wait on one another before proceeding. The class takes an &lt;code&gt;int&lt;/code&gt; in its constructor of the number of times its &lt;code&gt;countdown&lt;/code&gt; method must be invoked before it unblocks the threads waiting on it. Using this class let's build a simple timer function that has all threads initialize, wait until all are ready, start processing, and then stop and determine how long the process took. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
public static long time(Executor executor, int concurrency, Runnable action) {&lt;br&gt;
  CountDownLatch ready = new CountDownLatch(concurrency);&lt;br&gt;
  CountDownLatch start = new CountDownLatch(1);&lt;br&gt;
  CountDownLatch done = new CountDownLatch(concurrency);&lt;/p&gt;

&lt;p&gt;for(int i=0; i
    executor.execute(() -&amp;gt; {&lt;br&gt;
      ready.countDown();&lt;br&gt;
      try {&lt;br&gt;
        start.await();&lt;br&gt;
        action.run()&lt;br&gt;
      } catch (InterruptedException e) {&lt;br&gt;
        Thread.currentThread().interrupt();&lt;br&gt;
      } finally {&lt;br&gt;
        done.countdown();&lt;br&gt;
      }&lt;br&gt;
    });&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;ready.await();&lt;br&gt;
  long startNanos = System.nanoTime();&lt;br&gt;
  start.countDown();&lt;br&gt;
  done.await();&lt;br&gt;
  return System.nanoTime() - startNanos();&lt;br&gt;
}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example we use three different &lt;code&gt;CountdownLatch&lt;/code&gt;s which can get a little muddy but it does keep things pretty separated. We have the &lt;code&gt;ready&lt;/code&gt; latch that each thread checks in with and the main thread is waiting on. Once all threads check in the main thread starts the timer and triggers the &lt;code&gt;start&lt;/code&gt; latch which all the worker threads have been waiting on. All the worker threads do their work and then check-in when finished via the &lt;code&gt;done&lt;/code&gt; latch. The main thread is waiting on the &lt;code&gt;done&lt;/code&gt; latch to open and then marks the finished processing time. &lt;/p&gt;

&lt;p&gt;Let's consider a few other things about this example. What would happen if we passed in a &lt;code&gt;concurrency&lt;/code&gt; count that didn't match the number of threads? If it was too few we would end up prematurely starting our time and finishing our timer. If it was higher than our thread count then we would be deadlocked in what is known as a &lt;code&gt;thread starvation deadlock&lt;/code&gt;. You will also notice that we catch the &lt;code&gt;InterruptedException&lt;/code&gt;. By convention whenever this exception type is caught we should call &lt;code&gt;Thread.currentThread().interrupt()&lt;/code&gt; to signal to the owner of the thread that the thread has been interrupted and allow it to handle that in whatever way seems fit. Finally, you will notice the usage of &lt;code&gt;System.nanoTime()&lt;/code&gt; vs something like &lt;code&gt;System.currentTimeMillis()&lt;/code&gt;. This is because it is more accurate and because it is unaffected by the system's real-time clock. It is also of note that, unless the &lt;code&gt;Runnable&lt;/code&gt; represents a significant amount of work, this function won't return very interesting work. This is because even &lt;code&gt;System.nanonTime()&lt;/code&gt; is not accurate enough for microbenchmarking. It is for this reason that tools like JMH exist for this specific purpose. &lt;/p&gt;

&lt;p&gt;This only begins to cover the utilities provided by the concurrent utilities built into the core language. Feel free to dig deeper into these utilities.&lt;/p&gt;

&lt;p&gt;Even though there are better methods out there than using &lt;code&gt;wait&lt;/code&gt; and &lt;code&gt;notify&lt;/code&gt; directly we may need to maintain code that does use these functions. The &lt;code&gt;wait&lt;/code&gt; method is, as the name suggests, used to make a thread wait for some condition. It must be invoked in a synchronized region that locks the on the method it is invoked. The colloquial usage looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
synchronized(obj) {&lt;br&gt;
  while(&amp;lt;condition does not hold) {&lt;br&gt;
    obj.wait();&lt;br&gt;
  }&lt;br&gt;
  // perform action now that condition holds.&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Things of note, we need to always call &lt;code&gt;wait&lt;/code&gt; within a loop checking that the condition we are waiting for is true. If the condition we are waiting for is true and the &lt;code&gt;notify&lt;/code&gt; or &lt;code&gt;notifyAll&lt;/code&gt; method is called before the &lt;code&gt;wait&lt;/code&gt; method is called there is no guarantee the thread will ever wake up. By putting the check in a loop we ensure safety. If the thread moves past the &lt;code&gt;wait&lt;/code&gt; before the condition holds we lose the protection of our invariant. There are several ways that a thread can be woken up when the condition it is waiting for is not true. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another thread could have also been notified and taken the lock.&lt;/li&gt;
&lt;li&gt;Another thread could have invoked &lt;code&gt;notify&lt;/code&gt; incorrectly.&lt;/li&gt;
&lt;li&gt;The invoking thread could have triggered &lt;code&gt;notify&lt;/code&gt; too early before it was actually ready.&lt;/li&gt;
&lt;li&gt;In rare circumstances, a waiting thread can be woken up even without a &lt;code&gt;notify&lt;/code&gt; call.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One topic that comes up when discussing &lt;code&gt;wait&lt;/code&gt; and &lt;code&gt;notify&lt;/code&gt; is whether to use &lt;code&gt;notify&lt;/code&gt; or &lt;code&gt;notifyAll&lt;/code&gt;. As a reminder &lt;code&gt;notify&lt;/code&gt; wakes up one waiting thread and &lt;code&gt;notifyAll&lt;/code&gt; wakes up all waiting threads. Waking up all waiting threads is a safe, conservative choice. It will always guarantee you will wake up all threads that need to be awakened. You actually may be waking up more threads than you need to, but if you are properly checking on condition before proceeding after waiting, these additional threads that were woken up will simply go back to waiting. Using only &lt;code&gt;notify&lt;/code&gt; could lead to a bit of an optimization but in the long run, it's not likely worth it. &lt;/p&gt;

&lt;p&gt;Simply put, &lt;code&gt;wait&lt;/code&gt; and &lt;code&gt;notify&lt;/code&gt; rarely need to be used in new code. By using modern concurrent utilities we can have much simpler code, safer code, and likely more performant. If we do find ourselves maintaining code that does use &lt;code&gt;wait&lt;/code&gt; and &lt;code&gt;notify&lt;/code&gt; we should be careful we are using the functionality correctly. Always check before proceeding, loop if the condition isn't met, and prefer &lt;code&gt;notifyAll&lt;/code&gt; over &lt;code&gt;notify&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>concurrency</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Effective Java: Prefer Executors, Tasks, and Streams to Threads</title>
      <dc:creator>Kyle Carter</dc:creator>
      <pubDate>Sat, 13 Nov 2021 01:29:02 +0000</pubDate>
      <link>https://dev.to/kylec32/effective-java-prefer-executors-tasks-and-streams-to-threads-ded</link>
      <guid>https://dev.to/kylec32/effective-java-prefer-executors-tasks-and-streams-to-threads-ded</guid>
      <description>&lt;p&gt;Eventually, it seems that every developer will be presented with a problem that requires some kind of &lt;em&gt;work queue&lt;/em&gt;. These work queues can be used to store a collection of tasks to be worked in some order and move on. Although the concept of these work queues may be simple, actually developing one of these in a safe, performant manner can be tricky and error-prone. Thankfully we have a solution built right into the Java language. &lt;/p&gt;

&lt;p&gt;Within the &lt;code&gt;java.util.concurrent&lt;/code&gt; package we have the &lt;em&gt;Executor Framework&lt;/em&gt; which is a flexible framework based on separating the work to be performed, a &lt;em&gt;task&lt;/em&gt;, from the unit of execution, the &lt;em&gt;executor&lt;/em&gt;. Let's look at a very simple use of the Executor framework:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ExecutorService&lt;/span&gt; &lt;span class="n"&gt;executor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Executors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newSingleThreadExecutor&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runnable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shutdown&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The steps usually follow that model above: create the right type of executor for what you are trying to accomplish, add work to it, then shut it down (don't forget that last part as your JVM likely won't shut down in that case). &lt;/p&gt;

&lt;p&gt;This simplest case is not all you can do. You can wait for a task to finish, you can wait for any or all of a group of tasks to finish, you can wait for the executor to finish, you can retrieve the results of your asynchronous tasks, you can run particular tasks on a schedule, and more. &lt;/p&gt;

&lt;p&gt;In the above example, we used a single-threaded executor, in case you need more threads than that you can use a different factory method and configure what you need. Changing from using one thread or multiple, the rest of the code will stay the same. There are a number of options and which one is going to be best is going to be based on how busy it's going to be, what kind of work it will be doing, and what environment you are running in. In short, there isn't a one size fits all solution. Take a look at what your options are and choose which one fits your use case the best. &lt;/p&gt;

&lt;p&gt;One of the things the executor framework allows you to do is to avoid working directly with &lt;code&gt;Thread&lt;/code&gt;s. Threads are fairly error-prone to work directly with so having this abstraction layer on top of them can greatly simplify working with them and make it much safer. The executor interface takes two different types of tasks that are fairly closely related. The first is a &lt;code&gt;Runnable&lt;/code&gt;, this is a task that doesn't have a return value, and then you have a Callable which can have a return value and can throw an arbitrary exception.&lt;/p&gt;

&lt;p&gt;From Java 7 onwards the Executor framework has also extended to support fork-join tasks. These are a special kind of job where a particular piece of work is split between a number of executors that can steal work from each other in order to stay busy. One of the more recent enhancements to the Java language, parallel streams, is built on the fork-join pool that the executor framework provides. This makes it a little easier to work with but still comes with its own caveats and pitfalls. &lt;/p&gt;

&lt;p&gt;There is a lot one could dig into when considering all that the executor service can do but even this simple introduction I hope can raise awareness of it so that when you may be tempted to reach for a &lt;code&gt;Thread&lt;/code&gt; you can instead look deeper into this framework. &lt;/p&gt;

</description>
      <category>java</category>
      <category>effective</category>
      <category>concurrency</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
