<?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: Martel Richard</title>
    <description>The latest articles on DEV Community by Martel Richard (@richardmr36).</description>
    <link>https://dev.to/richardmr36</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%2F1454763%2F4c8bf830-8544-49d2-a94a-7f9eaa80c979.jpeg</url>
      <title>DEV Community: Martel Richard</title>
      <link>https://dev.to/richardmr36</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/richardmr36"/>
    <language>en</language>
    <item>
      <title>Correlation in logging</title>
      <dc:creator>Martel Richard</dc:creator>
      <pubDate>Thu, 22 May 2025 13:34:07 +0000</pubDate>
      <link>https://dev.to/richardmr36/correlation-in-logging-4ef4</link>
      <guid>https://dev.to/richardmr36/correlation-in-logging-4ef4</guid>
      <description>&lt;h2&gt;
  
  
  What is Correlation in logging?
&lt;/h2&gt;

&lt;p&gt;It refers to the practice of linking together log entries that are part of the same transaction, user session, request, or workflow, even if those entries are generated by different components or services in a distributed system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does it matter?
&lt;/h2&gt;

&lt;p&gt;In modern applications—especially microservices or cloud-based systems—a single user action might trigger a cascade of events across multiple services. Without correlation, it's hard to trace the full path of that action through the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Correlation Works
&lt;/h2&gt;

&lt;p&gt;A correlation ID (or trace ID) is a unique identifier that is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generated at the start of a request or transaction.&lt;/li&gt;
&lt;li&gt;Passed along to all components or services involved.&lt;/li&gt;
&lt;li&gt;Logged with every log entry related to that request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This way, you can filter or search logs by the correlation ID to see the full picture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2025-05-19 10:15:21.370 INFO [correlation-id=3b9c39ab-ad6e-47c4-b6ee-dc0ee34ff807] [thread-id=boundedElastic-1] com.app.service.LoginService:10 - User logged in
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Log4j support for correlation
&lt;/h2&gt;

&lt;p&gt;Log4j (Logging for Java) is a standard logging library in Java. It has MDC that allows you to enrich your log messages with contextual information that can be very helpful for debugging and tracing execution, especially in multi-threaded or distributed applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is MDC?
&lt;/h2&gt;

&lt;p&gt;Mapped Diagnostic Context is essentially a thread-local map where you can store key-value pairs. These values can then be included in your log output using a pattern layout. Since it's thread-local, each thread has its own copy of the MDC data, which avoids interference between thread.&lt;/p&gt;

&lt;p&gt;For example, imagine you're handling multiple user requests in a web application. Each request is processed in a separate thread. You might want to log the user ID, session ID, or transaction ID with every log message related to that request. MDC makes this easy.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use MDC?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;com.myapp.controller.MyController.java&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;try {
  MDC.put("correlation-id", UUID.randomUUID().toString());
  log.info("Testing contextual logging");
}
finally {
  MDC.remove("correlation-id");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;log4j2.xml&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;&amp;lt;PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [correlation-id=%X{correlation-id}] [thread-id=%t] %logger{36} - %m%n%xwEx"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;NOTE: When you use %X followed by a key in curly braces, like %X{key}, it will output the value associated with that key from the MDC. If you use %X without a key, it will output all entries in the MDC, in the format key1=value1, key2=value2, ...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Log Output&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;2025-05-22 12:56:14.216  INFO [correlation-id=1b4a4f98-54b1-4951-b3e5-7c7ca742564b] [thread-id=http-nio-9052-exec-1] com.myapp.controller.MyController - Testing contextual logging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Caveats with MDC
&lt;/h2&gt;

&lt;p&gt;MDC uses thread-local storage, meaning the context is tied to the current thread. So it has the caveats same as the ThreadLocal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Thread pools&lt;/code&gt; If a thread is reused and MDC is not cleared, old context data may leak into unrelated requests and lead to inconsistent logging. So always clear MDC after use.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Async processing&lt;/code&gt; MDC data may not propagate to new threads unless explicitly passed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context Propogation
&lt;/h2&gt;

&lt;p&gt;In asynchronous or reactive programming like Project Reactor, context (like ThreadLocal values used for logging or tracing) can be lost when execution hops between threads. This breaks features like MDC logging or distributed tracing. In such cases, the context has to be propogated across threads. &lt;a href="https://spring.io/blog/2023/03/28/context-propagation-with-project-reactor-1-the-basics" rel="noopener noreferrer"&gt;Take a look at this spring blog to understand more on this!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, context propogation bridges ThreadLocal and Reactor Context and allows MDC to work to work seamlessly in reactive flows. This also helps tools like Micrometer and other tracing libraries and enables distributed tracing in the reactive world.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/javarevisited/demystifying-context-propagation-in-spring-web-flux-with-threadlocalaccessor-contextregistry-mdc-c3740e0b990e" rel="noopener noreferrer"&gt;Check out this Medium article.&lt;/a&gt; The author explains context propagation clearly with practical examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://micrometer.io/docs/contextPropagation" rel="noopener noreferrer"&gt;https://micrometer.io/docs/contextPropagation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/mdc-in-log4j-2-logback" rel="noopener noreferrer"&gt;https://www.baeldung.com/mdc-in-log4j-2-logback&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/asyncparadigm/logging-in-a-multithreaded-environment-and-with-completablefuture-construct-using-mdc-1c34c691cef0" rel="noopener noreferrer"&gt;https://medium.com/asyncparadigm/logging-in-a-multithreaded-environment-and-with-completablefuture-construct-using-mdc-1c34c691cef0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>logging</category>
      <category>tracing</category>
      <category>mdc</category>
      <category>contextpropogation</category>
    </item>
    <item>
      <title>Collation in Database - What you need to know</title>
      <dc:creator>Martel Richard</dc:creator>
      <pubDate>Thu, 27 Mar 2025 11:29:14 +0000</pubDate>
      <link>https://dev.to/richardmr36/collation-in-database-what-you-need-to-know-4aa2</link>
      <guid>https://dev.to/richardmr36/collation-in-database-what-you-need-to-know-4aa2</guid>
      <description>&lt;h2&gt;
  
  
  What is Collation?
&lt;/h2&gt;

&lt;p&gt;Collation refers to a set of rules that determine how data is sorted and compared. Collation settings specify the correct character sequence, case sensitivity, accent marks, character width and the characters used in comparison operations, such as equality and inequality.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does this affect the data?
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Character data sort order
&lt;/h4&gt;

&lt;p&gt;This specifies how string data is sequenced. For example, whether 'a' comes before 'b'.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Case sensitivity
&lt;/h4&gt;

&lt;p&gt;This determines whether 'A' and 'a' are considered the same.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Accent sensitivity
&lt;/h4&gt;

&lt;p&gt;This determines whether 'a' and 'á' are considered the same.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Kana Sensitivity
&lt;/h4&gt;

&lt;p&gt;This determines whether two characters that look the same but are represented differently in Kana (a Japanese script) are treated as equal.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Width sensitivity
&lt;/h4&gt;

&lt;p&gt;This determines whether a single-byte character (half-width) and the same character represented as a double-byte character (full-width) are treated as equal.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where do you specify the collation?
&lt;/h2&gt;

&lt;p&gt;Collation can be specified at different levels depending on the specific database system. For example, in SQL Server, it can be specified at:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Server level&lt;/li&gt;
&lt;li&gt;Database level&lt;/li&gt;
&lt;li&gt;Column level&lt;/li&gt;
&lt;li&gt;Expression (SQL Query) level&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The above levels are mentioned in the descending order of hierarchy i.e. when you create a database, you specify the default collation for the database. Every column in the database that uses &lt;em&gt;character data types&lt;/em&gt; has a collation. If not specified, the column uses the default collation of the database.&lt;/p&gt;




&lt;h2&gt;
  
  
  How collation affects the query results?
&lt;/h2&gt;

&lt;p&gt;For example, the default collation in SQL Server is &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;SQL_Latin1_General_CP1_CI_AS&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The elements of this collation name represent the following.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt; - This prefix means that the collation is a SQL Server collation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Latin1_General&lt;/strong&gt; - This is the base collation, which defines the rules that are used for sorting and comparing characters within the Latin1 character set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CP1&lt;/strong&gt; - This stands for &lt;a href="https://en.wikipedia.org/wiki/Windows-1252" rel="noopener noreferrer"&gt;Code Page 1252&lt;/a&gt;, which is a character encoding of the Latin alphabet, used by default in the legacy components of Microsoft Windows in English and some other Western languages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CI&lt;/strong&gt; - This stands for Case Insensitive. This means that the SQL Server does not consider case during comparison. For example, 'A' and 'a' are considered to be the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AS&lt;/strong&gt; - This stands for Accent Sensitive. This means that accented characters and unaccented characters are considered to be different. For example, 'a' and 'á' are considered to be different.&lt;/p&gt;

&lt;p&gt;Overall, SQL_Latin1_General_CP1_CI_AS is a SQL Server collation that is case insensitive and accent sensitive, using the Latin1 General rules and the 1252 Code Page. This is a commonly used collation in the English language for SQL Server.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to check your collation settings in SQL Server
&lt;/h2&gt;

&lt;p&gt;You can determine the server's character set in SQL Server by using the following SQL query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SERVERPROPERTY('Collation')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need more information about the collation, you can use &lt;code&gt;fn_helpcollations&lt;/code&gt; function which returns all the collations supported by your server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM fn_helpcollations()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  When characters cannot be represented in a given collation
&lt;/h2&gt;

&lt;p&gt;In the context of the SQL_Latin1_General_CP1_CI_AS collation in SQL Server, which is based on the Windows-1252 code page, certain characters from other scripts or languages that are not covered by Windows-1252 may not be saved correctly.&lt;/p&gt;

&lt;p&gt;For example, characters from scripts such as Arabic, Hebrew, Cyrillic, Chinese, Japanese, Korean, and many others, cannot be properly represented in this collation. So, if you try to save a character like the Arabic letter "ج" or the Chinese character "汉" using this collation, such characters will be replaced with "?".&lt;/p&gt;

&lt;p&gt;Refer &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-ver16#collation" rel="noopener noreferrer"&gt;Collation in SQL Server&lt;/a&gt; to know more on various collation rules and levels.&lt;/p&gt;

</description>
      <category>database</category>
      <category>sqlserver</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
