<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Debashis Das on Medium]]></title>
        <description><![CDATA[Stories by Debashis Das on Medium]]></description>
        <link>https://medium.com/@debashd1991?source=rss-22ecf69c6b3d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hi_pGhHfA3kDV5wJ</url>
            <title>Stories by Debashis Das on Medium</title>
            <link>https://medium.com/@debashd1991?source=rss-22ecf69c6b3d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:35:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@debashd1991/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Video Streaming using WebRTC]]></title>
            <link>https://medium.com/@debashd1991/video-streaming-using-webrtc-4eb457d84a4?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4eb457d84a4</guid>
            <category><![CDATA[webrtc]]></category>
            <category><![CDATA[udp]]></category>
            <category><![CDATA[meeting-rooms]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Mon, 27 Mar 2023 12:05:38 GMT</pubDate>
            <atom:updated>2023-03-27T12:21:41.819Z</atom:updated>
            <content:encoded><![CDATA[<p>WebRTC helps us setup peer to peer connection between peers. Wrote a Signalling Server which acts as a handshake initialiser between them.</p><h3>Basic mechanism</h3><p>To understand how UDP works I have written an article <a href="https://medium.com/@debashd1991/user-datagram-protocol-udp-7f36f6a260c4">here</a>. So to implement this for browsers we are provided with javascript frameworks like WebRTC. This basically have 3 components</p><ol><li>Candidate</li><li>Offer</li><li>Answer</li></ol><p>Here when an WebRTC client is created, it need to inform this to the other peers if present. Which is done by creating a web-socket connection between the signalling service and the web client.</p><p>In my case when the web-socket connection is established I send an ‘READY’ event to the Signalling server. Which basically passes this information to the other peers if any peer exists.</p><p>When a peer exists it need to fetch the list of <strong>ICE Candidates</strong> and pass those values to the other peer(If you don’t know about ICE please read my other article on UDP). This list is passed using the web-socket to the other peer.</p><p>These are basically the candidates which gives information about the peer’s STUN server values, public IP etc and pass it to the other peer so that a remote communication can be setup. This is done asynchronously.</p><blockquote>remote.addIceCandidate(newRTCIceCandidate(message.data.iceCandidate));<br>remote : remote webrtc connection</blockquote><p>Next local peer when identifies that a peer exists it sends the offer to the other peer using the same web-socket connection. Offer is created using</p><blockquote>const offer = await pc.createOffer({iceRestart:true , offerToReceiveAudio: true, offerToReceiveVideo: true});</blockquote><blockquote>await pc.setLocalDescription(offer);</blockquote><p>When the offer is received on the other end, the remote connection on the other end creates an answer and sends it back using the web-socket connection.</p><blockquote>const answer = await remote.createAnswer();</blockquote><blockquote>await remote.setLocalDescription(answer);</blockquote><p>This can be compared with the SYN-ACK mechanism present in TCP. Meanwhile the listeners for candidates are already sending candidate asynchronously to the remote peer as well as listeners for the ‘track’ are in place to embed it into the srcObject of the &lt;video/&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pryBdxlAvQrgb8XKP8bMCw.png" /></figure><h3>LOGS</h3><p>I wanted to attach the logs for better understanding</p><pre>READY : {&quot;eventType&quot;:&quot;READY&quot;,&quot;peerId&quot;:61648}<br>READY : {&quot;eventType&quot;:&quot;READY&quot;,&quot;peerId&quot;:40047}<br>OFFER : {&quot;eventType&quot;:&quot;OFFER&quot;,&quot;offer&quot;:{&quot;type&quot;:&quot;offer&quot;,&quot;sdp&quot;:&quot;v=0\r\no=- 2793359868846476396 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:/9T9\r\na=ice-pwd:7U5USkY5Fv+j+jKRXYC7LxHx\r\na=ice-options:trickle\r\na=fingerprint:sha-256 7A:C1:C5:D6:C6:ED:FF:74:87:53:51:A1:D1:17:75:9C:EC:81:CD:9A:A9:D4:D0:BC:3F:2D:4C:AC:2D:D0:BB:CC\r\na=setup:actpass\r\na=mid:0\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=recvonly\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:63 red/48000/2\r\na=fmtp:63 111/111\r\na=rtpmap:9 G722/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:110 telephone-event/48000\r\na=rtpmap:126 telephone-event/8000\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 35 36 37 38 102 103 104 105 106 107 108 109 127 125 39 40 41 42 43 44 45 46 47 48 112 113 114 115 116 49\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:/9T9\r\na=ice-pwd:7U5USkY5Fv+j+jKRXYC7LxHx\r\na=ice-options:trickle\r\na=fingerprint:sha-256 7A:C1:C5:D6:C6:ED:FF:74:87:53:51:A1:D1:17:75:9C:EC:81:CD:9A:A9:D4:D0:BC:3F:2D:4C:AC:2D:D0:BB:CC\r\na=setup:actpass\r\na=mid:1\r\na=extmap:14 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:13 urn:3gpp:video-orientation\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\r\na=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type\r\na=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing\r\na=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\r\na=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\r\na=recvonly\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8/90000\r\na=rtcp-fb:96 goog-remb\r\na=rtcp-fb:96 transport-cc\r\na=rtcp-fb:96 ccm fir\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=rtpmap:97 rtx/90000\r\na=fmtp:97 apt=96\r\na=rtpmap:98 VP9/90000\r\na=rtcp-fb:98 goog-remb\r\na=rtcp-fb:98 transport-cc\r\na=rtcp-fb:98 ccm fir\r\na=rtcp-fb:98 nack\r\na=rtcp-fb:98 nack pli\r\na=fmtp:98 profile-id=0\r\na=rtpmap:99 rtx/90000\r\na=fmtp:99 apt=98\r\na=rtpmap:100 VP9/90000\r\na=rtcp-fb:100 goog-remb\r\na=rtcp-fb:100 transport-cc\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=fmtp:100 profile-id=2\r\na=rtpmap:101 rtx/90000\r\na=fmtp:101 apt=100\r\na=rtpmap:35 VP9/90000\r\na=rtcp-fb:35 goog-remb\r\na=rtcp-fb:35 transport-cc\r\na=rtcp-fb:35 ccm fir\r\na=rtcp-fb:35 nack\r\na=rtcp-fb:35 nack pli\r\na=fmtp:35 profile-id=1\r\na=rtpmap:36 rtx/90000\r\na=fmtp:36 apt=35\r\na=rtpmap:37 VP9/90000\r\na=rtcp-fb:37 goog-remb\r\na=rtcp-fb:37 transport-cc\r\na=rtcp-fb:37 ccm fir\r\na=rtcp-fb:37 nack\r\na=rtcp-fb:37 nack pli\r\na=fmtp:37 profile-id=3\r\na=rtpmap:38 rtx/90000\r\na=fmtp:38 apt=37\r\na=rtpmap:102 H264/90000\r\na=rtcp-fb:102 goog-remb\r\na=rtcp-fb:102 transport-cc\r\na=rtcp-fb:102 ccm fir\r\na=rtcp-fb:102 nack\r\na=rtcp-fb:102 nack pli\r\na=fmtp:102 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f\r\na=rtpmap:103 rtx/90000\r\na=fmtp:103 apt=102\r\na=rtpmap:104 H264/90000\r\na=rtcp-fb:104 goog-remb\r\na=rtcp-fb:104 transport-cc\r\na=rtcp-fb:104 ccm fir\r\na=rtcp-fb:104 nack\r\na=rtcp-fb:104 nack pli\r\na=fmtp:104 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f\r\na=rtpmap:105 rtx/90000\r\na=fmtp:105 apt=104\r\na=rtpmap:106 H264/90000\r\na=rtcp-fb:106 goog-remb\r\na=rtcp-fb:106 transport-cc\r\na=rtcp-fb:106 ccm fir\r\na=rtcp-fb:106 nack\r\na=rtcp-fb:106 nack pli\r\na=fmtp:106 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\na=rtpmap:107 rtx/90000\r\na=fmtp:107 apt=106\r\na=rtpmap:108 H264/90000\r\na=rtcp-fb:108 goog-remb\r\na=rtcp-fb:108 transport-cc\r\na=rtcp-fb:108 ccm fir\r\na=rtcp-fb:108 nack\r\na=rtcp-fb:108 nack pli\r\na=fmtp:108 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f\r\na=rtpmap:109 rtx/90000\r\na=fmtp:109 apt=108\r\na=rtpmap:127 H264/90000\r\na=rtcp-fb:127 goog-remb\r\na=rtcp-fb:127 transport-cc\r\na=rtcp-fb:127 ccm fir\r\na=rtcp-fb:127 nack\r\na=rtcp-fb:127 nack pli\r\na=fmtp:127 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f\r\na=rtpmap:125 rtx/90000\r\na=fmtp:125 apt=127\r\na=rtpmap:39 H264/90000\r\na=rtcp-fb:39 goog-remb\r\na=rtcp-fb:39 transport-cc\r\na=rtcp-fb:39 ccm fir\r\na=rtcp-fb:39 nack\r\na=rtcp-fb:39 nack pli\r\na=fmtp:39 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d001f\r\na=rtpmap:40 rtx/90000\r\na=fmtp:40 apt=39\r\na=rtpmap:41 H264/90000\r\na=rtcp-fb:41 goog-remb\r\na=rtcp-fb:41 transport-cc\r\na=rtcp-fb:41 ccm fir\r\na=rtcp-fb:41 nack\r\na=rtcp-fb:41 nack pli\r\na=fmtp:41 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=f4001f\r\na=rtpmap:42 rtx/90000\r\na=fmtp:42 apt=41\r\na=rtpmap:43 H264/90000\r\na=rtcp-fb:43 goog-remb\r\na=rtcp-fb:43 transport-cc\r\na=rtcp-fb:43 ccm fir\r\na=rtcp-fb:43 nack\r\na=rtcp-fb:43 nack pli\r\na=fmtp:43 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=f4001f\r\na=rtpmap:44 rtx/90000\r\na=fmtp:44 apt=43\r\na=rtpmap:45 AV1/90000\r\na=rtcp-fb:45 goog-remb\r\na=rtcp-fb:45 transport-cc\r\na=rtcp-fb:45 ccm fir\r\na=rtcp-fb:45 nack\r\na=rtcp-fb:45 nack pli\r\na=rtpmap:46 rtx/90000\r\na=fmtp:46 apt=45\r\na=rtpmap:47 AV1/90000\r\na=rtcp-fb:47 goog-remb\r\na=rtcp-fb:47 transport-cc\r\na=rtcp-fb:47 ccm fir\r\na=rtcp-fb:47 nack\r\na=rtcp-fb:47 nack pli\r\na=fmtp:47 profile=1\r\na=rtpmap:48 rtx/90000\r\na=fmtp:48 apt=47\r\na=rtpmap:112 H264/90000\r\na=rtcp-fb:112 goog-remb\r\na=rtcp-fb:112 transport-cc\r\na=rtcp-fb:112 ccm fir\r\na=rtcp-fb:112 nack\r\na=rtcp-fb:112 nack pli\r\na=fmtp:112 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=64001f\r\na=rtpmap:113 rtx/90000\r\na=fmtp:113 apt=112\r\na=rtpmap:114 red/90000\r\na=rtpmap:115 rtx/90000\r\na=fmtp:115 apt=114\r\na=rtpmap:116 ulpfec/90000\r\na=rtpmap:49 flexfec-03/90000\r\na=rtcp-fb:49 goog-remb\r\na=rtcp-fb:49 transport-cc\r\na=fmtp:49 repair-window=10000000\r\n&quot;},&quot;peerId&quot;:40047}<br>OFFER : {&quot;eventType&quot;:&quot;OFFER&quot;,&quot;offer&quot;:{&quot;type&quot;:&quot;offer&quot;,&quot;sdp&quot;:&quot;v=0\r\no=- 4979058268518238231 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:AwBT\r\na=ice-pwd:IU4xtIN3X4MtLHruBniaTpAS\r\na=ice-options:trickle\r\na=fingerprint:sha-256 F7:1A:EE:E7:AD:86:EB:78:28:11:9F:B7:DF:C3:49:4F:A2:FE:11:10:6C:DA:87:C4:2B:09:30:D7:4F:36:6C:24\r\na=setup:actpass\r\na=mid:0\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=recvonly\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:63 red/48000/2\r\na=fmtp:63 111/111\r\na=rtpmap:9 G722/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:110 telephone-event/48000\r\na=rtpmap:126 telephone-event/8000\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 35 36 37 38 102 103 104 105 106 107 108 109 127 125 39 40 41 42 43 44 45 46 47 48 112 113 114 115 116 117 118 49\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:AwBT\r\na=ice-pwd:IU4xtIN3X4MtLHruBniaTpAS\r\na=ice-options:trickle\r\na=fingerprint:sha-256 F7:1A:EE:E7:AD:86:EB:78:28:11:9F:B7:DF:C3:49:4F:A2:FE:11:10:6C:DA:87:C4:2B:09:30:D7:4F:36:6C:24\r\na=setup:actpass\r\na=mid:1\r\na=extmap:14 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:13 urn:3gpp:video-orientation\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\r\na=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type\r\na=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing\r\na=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\r\na=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\r\na=recvonly\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8/90000\r\na=rtcp-fb:96 goog-remb\r\na=rtcp-fb:96 transport-cc\r\na=rtcp-fb:96 ccm fir\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=rtpmap:97 rtx/90000\r\na=fmtp:97 apt=96\r\na=rtpmap:98 VP9/90000\r\na=rtcp-fb:98 goog-remb\r\na=rtcp-fb:98 transport-cc\r\na=rtcp-fb:98 ccm fir\r\na=rtcp-fb:98 nack\r\na=rtcp-fb:98 nack pli\r\na=fmtp:98 profile-id=0\r\na=rtpmap:99 rtx/90000\r\na=fmtp:99 apt=98\r\na=rtpmap:100 VP9/90000\r\na=rtcp-fb:100 goog-remb\r\na=rtcp-fb:100 transport-cc\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=fmtp:100 profile-id=2\r\na=rtpmap:101 rtx/90000\r\na=fmtp:101 apt=100\r\na=rtpmap:35 VP9/90000\r\na=rtcp-fb:35 goog-remb\r\na=rtcp-fb:35 transport-cc\r\na=rtcp-fb:35 ccm fir\r\na=rtcp-fb:35 nack\r\na=rtcp-fb:35 nack pli\r\na=fmtp:35 profile-id=1\r\na=rtpmap:36 rtx/90000\r\na=fmtp:36 apt=35\r\na=rtpmap:37 VP9/90000\r\na=rtcp-fb:37 goog-remb\r\na=rtcp-fb:37 transport-cc\r\na=rtcp-fb:37 ccm fir\r\na=rtcp-fb:37 nack\r\na=rtcp-fb:37 nack pli\r\na=fmtp:37 profile-id=3\r\na=rtpmap:38 rtx/90000\r\na=fmtp:38 apt=37\r\na=rtpmap:102 H264/90000\r\na=rtcp-fb:102 goog-remb\r\na=rtcp-fb:102 transport-cc\r\na=rtcp-fb:102 ccm fir\r\na=rtcp-fb:102 nack\r\na=rtcp-fb:102 nack pli\r\na=fmtp:102 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f\r\na=rtpmap:103 rtx/90000\r\na=fmtp:103 apt=102\r\na=rtpmap:104 H264/90000\r\na=rtcp-fb:104 goog-remb\r\na=rtcp-fb:104 transport-cc\r\na=rtcp-fb:104 ccm fir\r\na=rtcp-fb:104 nack\r\na=rtcp-fb:104 nack pli\r\na=fmtp:104 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f\r\na=rtpmap:105 rtx/90000\r\na=fmtp:105 apt=104\r\na=rtpmap:106 H264/90000\r\na=rtcp-fb:106 goog-remb\r\na=rtcp-fb:106 transport-cc\r\na=rtcp-fb:106 ccm fir\r\na=rtcp-fb:106 nack\r\na=rtcp-fb:106 nack pli\r\na=fmtp:106 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\na=rtpmap:107 rtx/90000\r\na=fmtp:107 apt=106\r\na=rtpmap:108 H264/90000\r\na=rtcp-fb:108 goog-remb\r\na=rtcp-fb:108 transport-cc\r\na=rtcp-fb:108 ccm fir\r\na=rtcp-fb:108 nack\r\na=rtcp-fb:108 nack pli\r\na=fmtp:108 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f\r\na=rtpmap:109 rtx/90000\r\na=fmtp:109 apt=108\r\na=rtpmap:127 H264/90000\r\na=rtcp-fb:127 goog-remb\r\na=rtcp-fb:127 transport-cc\r\na=rtcp-fb:127 ccm fir\r\na=rtcp-fb:127 nack\r\na=rtcp-fb:127 nack pli\r\na=fmtp:127 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f\r\na=rtpmap:125 rtx/90000\r\na=fmtp:125 apt=127\r\na=rtpmap:39 H264/90000\r\na=rtcp-fb:39 goog-remb\r\na=rtcp-fb:39 transport-cc\r\na=rtcp-fb:39 ccm fir\r\na=rtcp-fb:39 nack\r\na=rtcp-fb:39 nack pli\r\na=fmtp:39 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d001f\r\na=rtpmap:40 rtx/90000\r\na=fmtp:40 apt=39\r\na=rtpmap:41 H264/90000\r\na=rtcp-fb:41 goog-remb\r\na=rtcp-fb:41 transport-cc\r\na=rtcp-fb:41 ccm fir\r\na=rtcp-fb:41 nack\r\na=rtcp-fb:41 nack pli\r\na=fmtp:41 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=f4001f\r\na=rtpmap:42 rtx/90000\r\na=fmtp:42 apt=41\r\na=rtpmap:43 H264/90000\r\na=rtcp-fb:43 goog-remb\r\na=rtcp-fb:43 transport-cc\r\na=rtcp-fb:43 ccm fir\r\na=rtcp-fb:43 nack\r\na=rtcp-fb:43 nack pli\r\na=fmtp:43 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=f4001f\r\na=rtpmap:44 rtx/90000\r\na=fmtp:44 apt=43\r\na=rtpmap:45 AV1/90000\r\na=rtcp-fb:45 goog-remb\r\na=rtcp-fb:45 transport-cc\r\na=rtcp-fb:45 ccm fir\r\na=rtcp-fb:45 nack\r\na=rtcp-fb:45 nack pli\r\na=rtpmap:46 rtx/90000\r\na=fmtp:46 apt=45\r\na=rtpmap:47 AV1/90000\r\na=rtcp-fb:47 goog-remb\r\na=rtcp-fb:47 transport-cc\r\na=rtcp-fb:47 ccm fir\r\na=rtcp-fb:47 nack\r\na=rtcp-fb:47 nack pli\r\na=fmtp:47 profile=1\r\na=rtpmap:48 rtx/90000\r\na=fmtp:48 apt=47\r\na=rtpmap:112 H264/90000\r\na=rtcp-fb:112 goog-remb\r\na=rtcp-fb:112 transport-cc\r\na=rtcp-fb:112 ccm fir\r\na=rtcp-fb:112 nack\r\na=rtcp-fb:112 nack pli\r\na=fmtp:112 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=64001f\r\na=rtpmap:113 rtx/90000\r\na=fmtp:113 apt=112\r\na=rtpmap:114 H264/90000\r\na=rtcp-fb:114 goog-remb\r\na=rtcp-fb:114 transport-cc\r\na=rtcp-fb:114 ccm fir\r\na=rtcp-fb:114 nack\r\na=rtcp-fb:114 nack pli\r\na=fmtp:114 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=64001f\r\na=rtpmap:115 rtx/90000\r\na=fmtp:115 apt=114\r\na=rtpmap:116 red/90000\r\na=rtpmap:117 rtx/90000\r\na=fmtp:117 apt=116\r\na=rtpmap:118 ulpfec/90000\r\na=rtpmap:49 flexfec-03/90000\r\na=rtcp-fb:49 goog-remb\r\na=rtcp-fb:49 transport-cc\r\na=fmtp:49 repair-window=10000000\r\n&quot;},&quot;peerId&quot;:61648}<br>ANSWER : {&quot;eventType&quot;:&quot;ANSWER&quot;,&quot;answer&quot;:{&quot;type&quot;:&quot;answer&quot;,&quot;sdp&quot;:&quot;v=0\r\no=- 461311989409366199 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:lRfc\r\na=ice-pwd:AlwwoLC+Y7aIuAtNtAiML2rx\r\na=ice-options:trickle\r\na=fingerprint:sha-256 3F:BE:3A:BD:F8:31:D9:F6:29:13:1B:68:0C:CE:B7:56:4D:BC:D3:04:59:E7:52:E0:58:3D:FC:6C:0F:35:68:77\r\na=setup:active\r\na=mid:0\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=inactive\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:63 red/48000/2\r\na=fmtp:63 111/111\r\na=rtpmap:9 G722/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:110 telephone-event/48000\r\na=rtpmap:126 telephone-event/8000\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 102 103 104 105 106 107 108 109 127 125 39 40 112 113 116 117 118\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:lRfc\r\na=ice-pwd:AlwwoLC+Y7aIuAtNtAiML2rx\r\na=ice-options:trickle\r\na=fingerprint:sha-256 3F:BE:3A:BD:F8:31:D9:F6:29:13:1B:68:0C:CE:B7:56:4D:BC:D3:04:59:E7:52:E0:58:3D:FC:6C:0F:35:68:77\r\na=setup:active\r\na=mid:1\r\na=extmap:14 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:13 urn:3gpp:video-orientation\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\r\na=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type\r\na=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing\r\na=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\r\na=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\r\na=inactive\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8/90000\r\na=rtcp-fb:96 goog-remb\r\na=rtcp-fb:96 transport-cc\r\na=rtcp-fb:96 ccm fir\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=rtpmap:97 rtx/90000\r\na=fmtp:97 apt=96\r\na=rtpmap:98 VP9/90000\r\na=rtcp-fb:98 goog-remb\r\na=rtcp-fb:98 transport-cc\r\na=rtcp-fb:98 ccm fir\r\na=rtcp-fb:98 nack\r\na=rtcp-fb:98 nack pli\r\na=fmtp:98 profile-id=0\r\na=rtpmap:99 rtx/90000\r\na=fmtp:99 apt=98\r\na=rtpmap:100 VP9/90000\r\na=rtcp-fb:100 goog-remb\r\na=rtcp-fb:100 transport-cc\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=fmtp:100 profile-id=2\r\na=rtpmap:101 rtx/90000\r\na=fmtp:101 apt=100\r\na=rtpmap:102 H264/90000\r\na=rtcp-fb:102 goog-remb\r\na=rtcp-fb:102 transport-cc\r\na=rtcp-fb:102 ccm fir\r\na=rtcp-fb:102 nack\r\na=rtcp-fb:102 nack pli\r\na=fmtp:102 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f\r\na=rtpmap:103 rtx/90000\r\na=fmtp:103 apt=102\r\na=rtpmap:104 H264/90000\r\na=rtcp-fb:104 goog-remb\r\na=rtcp-fb:104 transport-cc\r\na=rtcp-fb:104 ccm fir\r\na=rtcp-fb:104 nack\r\na=rtcp-fb:104 nack pli\r\na=fmtp:104 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f\r\na=rtpmap:105 rtx/90000\r\na=fmtp:105 apt=104\r\na=rtpmap:106 H264/90000\r\na=rtcp-fb:106 goog-remb\r\na=rtcp-fb:106 transport-cc\r\na=rtcp-fb:106 ccm fir\r\na=rtcp-fb:106 nack\r\na=rtcp-fb:106 nack pli\r\na=fmtp:106 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\na=rtpmap:107 rtx/90000\r\na=fmtp:107 apt=106\r\na=rtpmap:108 H264/90000\r\na=rtcp-fb:108 goog-remb\r\na=rtcp-fb:108 transport-cc\r\na=rtcp-fb:108 ccm fir\r\na=rtcp-fb:108 nack\r\na=rtcp-fb:108 nack pli\r\na=fmtp:108 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f\r\na=rtpmap:109 rtx/90000\r\na=fmtp:109 apt=108\r\na=rtpmap:127 H264/90000\r\na=rtcp-fb:127 goog-remb\r\na=rtcp-fb:127 transport-cc\r\na=rtcp-fb:127 ccm fir\r\na=rtcp-fb:127 nack\r\na=rtcp-fb:127 nack pli\r\na=fmtp:127 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f\r\na=rtpmap:125 rtx/90000\r\na=fmtp:125 apt=127\r\na=rtpmap:39 H264/90000\r\na=rtcp-fb:39 goog-remb\r\na=rtcp-fb:39 transport-cc\r\na=rtcp-fb:39 ccm fir\r\na=rtcp-fb:39 nack\r\na=rtcp-fb:39 nack pli\r\na=fmtp:39 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d001f\r\na=rtpmap:40 rtx/90000\r\na=fmtp:40 apt=39\r\na=rtpmap:112 H264/90000\r\na=rtcp-fb:112 goog-remb\r\na=rtcp-fb:112 transport-cc\r\na=rtcp-fb:112 ccm fir\r\na=rtcp-fb:112 nack\r\na=rtcp-fb:112 nack pli\r\na=fmtp:112 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640034\r\na=rtpmap:113 rtx/90000\r\na=fmtp:113 apt=112\r\na=rtpmap:116 red/90000\r\na=rtpmap:117 rtx/90000\r\na=fmtp:117 apt=116\r\na=rtpmap:118 ulpfec/90000\r\n&quot;},&quot;peerId&quot;:40047}<br>ANSWER : {&quot;eventType&quot;:&quot;ANSWER&quot;,&quot;answer&quot;:{&quot;type&quot;:&quot;answer&quot;,&quot;sdp&quot;:&quot;v=0\r\no=- 6236592981191235858 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:NB53\r\na=ice-pwd:m9a0GgACeAxOzKfNPnYVq2f1\r\na=ice-options:trickle\r\na=fingerprint:sha-256 DC:03:AC:BC:06:CE:ED:9D:BB:E1:2B:91:FC:41:72:DB:B6:C8:59:6B:C9:2E:3A:58:6A:EC:01:F3:46:F2:91:79\r\na=setup:active\r\na=mid:0\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=inactive\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:63 red/48000/2\r\na=fmtp:63 111/111\r\na=rtpmap:9 G722/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:110 telephone-event/48000\r\na=rtpmap:126 telephone-event/8000\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 102 103 104 105 106 107 108 109 127 125 39 40 45 46 112 113 114 115 116\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:NB53\r\na=ice-pwd:m9a0GgACeAxOzKfNPnYVq2f1\r\na=ice-options:trickle\r\na=fingerprint:sha-256 DC:03:AC:BC:06:CE:ED:9D:BB:E1:2B:91:FC:41:72:DB:B6:C8:59:6B:C9:2E:3A:58:6A:EC:01:F3:46:F2:91:79\r\na=setup:active\r\na=mid:1\r\na=extmap:14 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:13 urn:3gpp:video-orientation\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\r\na=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type\r\na=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing\r\na=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\r\na=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\r\na=inactive\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8/90000\r\na=rtcp-fb:96 goog-remb\r\na=rtcp-fb:96 transport-cc\r\na=rtcp-fb:96 ccm fir\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=rtpmap:97 rtx/90000\r\na=fmtp:97 apt=96\r\na=rtpmap:98 VP9/90000\r\na=rtcp-fb:98 goog-remb\r\na=rtcp-fb:98 transport-cc\r\na=rtcp-fb:98 ccm fir\r\na=rtcp-fb:98 nack\r\na=rtcp-fb:98 nack pli\r\na=fmtp:98 profile-id=0\r\na=rtpmap:99 rtx/90000\r\na=fmtp:99 apt=98\r\na=rtpmap:100 VP9/90000\r\na=rtcp-fb:100 goog-remb\r\na=rtcp-fb:100 transport-cc\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=fmtp:100 profile-id=2\r\na=rtpmap:101 rtx/90000\r\na=fmtp:101 apt=100\r\na=rtpmap:102 H264/90000\r\na=rtcp-fb:102 goog-remb\r\na=rtcp-fb:102 transport-cc\r\na=rtcp-fb:102 ccm fir\r\na=rtcp-fb:102 nack\r\na=rtcp-fb:102 nack pli\r\na=fmtp:102 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f\r\na=rtpmap:103 rtx/90000\r\na=fmtp:103 apt=102\r\na=rtpmap:104 H264/90000\r\na=rtcp-fb:104 goog-remb\r\na=rtcp-fb:104 transport-cc\r\na=rtcp-fb:104 ccm fir\r\na=rtcp-fb:104 nack\r\na=rtcp-fb:104 nack pli\r\na=fmtp:104 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f\r\na=rtpmap:105 rtx/90000\r\na=fmtp:105 apt=104\r\na=rtpmap:106 H264/90000\r\na=rtcp-fb:106 goog-remb\r\na=rtcp-fb:106 transport-cc\r\na=rtcp-fb:106 ccm fir\r\na=rtcp-fb:106 nack\r\na=rtcp-fb:106 nack pli\r\na=fmtp:106 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\na=rtpmap:107 rtx/90000\r\na=fmtp:107 apt=106\r\na=rtpmap:108 H264/90000\r\na=rtcp-fb:108 goog-remb\r\na=rtcp-fb:108 transport-cc\r\na=rtcp-fb:108 ccm fir\r\na=rtcp-fb:108 nack\r\na=rtcp-fb:108 nack pli\r\na=fmtp:108 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f\r\na=rtpmap:109 rtx/90000\r\na=fmtp:109 apt=108\r\na=rtpmap:127 H264/90000\r\na=rtcp-fb:127 goog-remb\r\na=rtcp-fb:127 transport-cc\r\na=rtcp-fb:127 ccm fir\r\na=rtcp-fb:127 nack\r\na=rtcp-fb:127 nack pli\r\na=fmtp:127 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f\r\na=rtpmap:125 rtx/90000\r\na=fmtp:125 apt=127\r\na=rtpmap:39 H264/90000\r\na=rtcp-fb:39 goog-remb\r\na=rtcp-fb:39 transport-cc\r\na=rtcp-fb:39 ccm fir\r\na=rtcp-fb:39 nack\r\na=rtcp-fb:39 nack pli\r\na=fmtp:39 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d001f\r\na=rtpmap:40 rtx/90000\r\na=fmtp:40 apt=39\r\na=rtpmap:45 AV1/90000\r\na=rtcp-fb:45 goog-remb\r\na=rtcp-fb:45 transport-cc\r\na=rtcp-fb:45 ccm fir\r\na=rtcp-fb:45 nack\r\na=rtcp-fb:45 nack pli\r\na=rtpmap:46 rtx/90000\r\na=fmtp:46 apt=45\r\na=rtpmap:112 H264/90000\r\na=rtcp-fb:112 goog-remb\r\na=rtcp-fb:112 transport-cc\r\na=rtcp-fb:112 ccm fir\r\na=rtcp-fb:112 nack\r\na=rtcp-fb:112 nack pli\r\na=fmtp:112 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640034\r\na=rtpmap:113 rtx/90000\r\na=fmtp:113 apt=112\r\na=rtpmap:114 red/90000\r\na=rtpmap:115 rtx/90000\r\na=fmtp:115 apt=114\r\na=rtpmap:116 ulpfec/90000\r\n&quot;},&quot;peerId&quot;:61648}</pre><p>I am not attaching candidate event as it contains information about my Public IP.</p><p>Github links :</p><p><a href="https://github.com/debashis-das/VideoStream">https://github.com/debashis-das/VideoStream</a></p><p><a href="https://github.com/debashis-das/SignalingServer">https://github.com/debashis-das/SignalingServer</a></p><h3>Conclusion</h3><p>This can be extended to support multiple remote peers and setup a meeting room. This can also be integrated with face recognition algorithms easily by receiving videos and identify the other person, maybe understand how many people are interested in listening to that meeting(mood check of people in realtime). As well as maybe modify his face to something else like in SPY movies. This research area is awesome/cool isn’t it ?</p><p>Keep Exploring !!</p><h3>References :</h3><p><a href="https://webrtc.org/">WebRTC</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4eb457d84a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tried to solve same question of Leetcode using Bard which I solved using ChatGPT]]></title>
            <link>https://medium.com/@debashd1991/tried-to-solve-same-question-of-leetcode-using-bard-which-i-solved-using-chatgpt-525d4fb0a423?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/525d4fb0a423</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[ds-algo]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[bard-ai]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Mon, 27 Mar 2023 10:39:48 GMT</pubDate>
            <atom:updated>2023-03-27T10:47:22.457Z</atom:updated>
            <content:encoded><![CDATA[<p>I have solved this question using ChatGPT and did a detailed analysis on it in this <a href="https://medium.com/@debashd1991/tried-to-solve-todays-daily-challenge-problem-of-leetcode-using-chatgpt-analysis-and-comparision-6ce1d41457f9">article</a>. This is just an analysis on the above question. It has nothing to do with supporting any AI technologies. Just for public awareness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OPpIZspeJDH3w1dfI2mgxQ.png" /></figure><pre>public class BagsWithFullCapacity {<br>    public static int maxBagsWithFullCapacity(int[] capacity, int[] rocks, int additionalRocks) {<br>        int[] remainingCapacity = new int[capacity.length];<br>        for (int i = 0; i &lt; capacity.length; i++) {<br>            remainingCapacity[i] = capacity[i] - rocks[i];<br>        }<br>        // Sort the remaining capacity in descending order.<br>        Arrays.sort(remainingCapacity, Collections.reverseOrder());<br>        // Initialize the number of bags to 0.<br>        int numBags = 0;<br>        // Iterate through the remaining capacity and add rocks to the bags until the additional rocks are used up.<br>        for (int remainingRocks = additionalRocks; remainingRocks &gt; 0; remainingRocks--) {<br>            // Check if there is a bag with remaining capacity.<br>            if (remainingCapacity[0] &gt; 0) {<br>                // Add rocks to the bag with the smallest remaining capacity.<br>                remainingCapacity[0] -= 1;<br>                // Increment the number of bags.<br>                numBags++;<br>            } else {<br>                // No bag has remaining capacity.<br>                break;<br>            }<br>        }<br>        // Return the number of bags.<br>        return numBags;<br>    }<br>}</pre><h3><strong>Language Selection</strong></h3><p>In this case it selected language as Java. Not sure why it selected Java but that was the default response. The solution it gave had <strong>syntax error </strong>which was unexpected when it gave a solution. However the error was not a direct one but an indirect one.</p><p>By this I mean when it tried to initialise an array it was of <strong>int </strong>type but when it tried to sort the array it assumed it to be of an <strong>Integer</strong> type and used <strong>Collections.reversedOrder()</strong> on it. Which can only be applied on a <strong>non-primitive</strong> type.</p><h3>Logic Selection</h3><p>It didn’t give the right answer. Why do it need to <strong>sort the array in reverse order</strong>. Maybe it is trying to optimise it to get the <em>minimum no. of bags</em> which itself is an assumption as we need to get the <em>maximum number of bags</em>. I also tried to dry run this with inputs but still it couldn’t identify the problem. Being an AI, it should be able to identify mistakes and give valid responses if it mimics to be a person. As humans make mistake, so AI can also make mistakes but it should have capabilities to correct those mistakes during a dry run and change the code.</p><h4>Pre-filled Bags</h4><p>At this point it had 1 extra <strong>for loop</strong> added</p><pre>for (int i = 0; i &lt; capacity.length; i++) {<br>    remainingCapacity[i] = Math.max(remainingCapacity[i], 0);<br>}</pre><p>This loop assume checks if the values are negative or not and sets it to zero. Which itself is a constraint.</p><blockquote>0 &lt;= rocks[i] &lt;= capacity[i]</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JnVs48kqs6Gg_Qx0ywhJOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zp1QtDLkOTEe5jMA_TtXUA.png" /></figure><h4>Fully filled bags</h4><p>It also didn’t consider a situation where the bags are already full, it should add those bags as well in the maximum count. So I asked to fix that edge-case as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9wmt7MitZ92pV8ivLYZghg.png" /></figure><p>I got this answer :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pVvbxTNxLliX2fmeCwGm7w.png" /></figure><p>The result it gave was written in python.</p><h3>Conclusion</h3><p>This can be compared with an interview where the interviewer asked an above question and AI gives the answer based the assumptions it made on it own. Assumption can be correct/wrong. If an interviewer sees the assumptions taken are wrong it asks why did you do this ? based on it, the candidate modifies his assumptions and gives you a correct response. This is how learning works. Make mistakes to move to the next level.</p><p>Keep Learning !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=525d4fb0a423" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transport Layer Security(TLS)]]></title>
            <link>https://medium.com/@debashd1991/transport-layer-security-tls-774f4819438b?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/774f4819438b</guid>
            <category><![CDATA[certificate-authority]]></category>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[diffie-hellman]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[tls]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Tue, 21 Feb 2023 15:12:06 GMT</pubDate>
            <atom:updated>2023-02-22T09:28:12.973Z</atom:updated>
            <content:encoded><![CDATA[<p>This blog assumes that you guys know about <strong>symmetric</strong> and <strong>asymmetric</strong> cryptography algorithms and how they work. In this blog I explain the concepts of TLS as well as setup an https server written in python</p><p>TLS works on top of TCP layer and provide end to end encryption of data during transport. Current version of TLS 1.3 is used worldwide but minimum requirement is TLS 1.2 for HTTP/2.</p><p>A secure web application requires 3 important features <strong>Confidentiality, Authentication and Data Integrity.</strong></p><p><strong>Confidentiality</strong> is achieved through <strong>symmetric encryption</strong> in which same key is used for encryption and decryption of the data.</p><p><strong>Data Integrity</strong><em> </em>is achieved using<strong> MAC(message authentication code) hashing technique</strong> in which a secret key is combined with data to generate a hash called HMAC(Hash based message authentication code). Since anybody can do hashing we add a flavour of secret key to it. Server/Clients with only secret key can undo the hash. This is providing Data Integrity with authentication.</p><p><strong>Authentication</strong> is achieved globally by PKI (Public key infrastructure) which maintains digital certificates. So to use HTTPS you need to digitally identify yourself in the internet by registering your key in CA(Certificate Authority). So that they provide you with digital signed certificates also called X.509(format of public key certificates) and use it to identify your domain.<em> Lets talk about this in section(Certificate Authorities).</em></p><p>The main goal of TLS communication is to derive 4 keys from session key.</p><ol><li>Client Encryption Key</li><li>Client HMAC Key</li><li>Server Encryption Key</li><li>Server HMAC key</li></ol><p>These are used to create 2 uni-directional tunnels of data. One from server to client and other from client to server. So even if anybody procure one set of keys, they are able to view data flowing only on one side. Which itself is a challenge.</p><h3><strong>Key-Exchange (RSA/</strong>Diffie-Hellman<strong> </strong>algorithm<strong>)</strong></h3><p>As Confidential and Data Integrity requires a same secret key on both client and server as mentioned above. We need to provide this, either by transferring (RSA) or by generating same keys on client and server (DH) which is done by setting up a key-exchange. Key exchange is setup using <strong>Asymmetric encryption</strong> like RSA, Diffie-Hellman(DH) algorithms. RSA has been used for a very long time by browsers. It basically has public-private key pair. This is used for both authentication of the server as well as to generate session symmetric key. Therefore, when a session begins, the client creates a symmetric key, encrypts it with the server’s public key, and sends it. Since server has the private key, it can decrypt it.</p><p>Now let’s talk about a hacker who gets hold of the private key. Now he is able to decrypt the entire session. <strong>As symmetric key used is sent over the session</strong>. He can even record the session and later when he has the private key, he can decrypt the session. Which makes HTTPS insecure. Due to these reasons DH algorithm has replaced RSA for a key exchange. DH is a little different in how it works. In case of DH the symmetric key used never leaves the server and the client. But is generated same in both places.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*Ar55jRowmJF0iZ3xURoFKA.png" /><figcaption>DH ALGO</figcaption></figure><p>As you can see in the above diagram a common secret is being generated, which never leaves server and client. Now even if he gets hold of the private key he is not able to generate the symmetric key for decryption of data. Additionally, we can generate <strong>new short-time/ephemeral symmetric keys and discard the old keys in every key-exchange call.</strong> Since the keys never leave server and client, a hacker can never decrypt data. Worst case even if he compromises client or server and get hold of the private key and the session keys for past and future. He cannot decrypt the current session data. Even if he records the session, he cannot decrypt the past session data as new ephemeral keys are used and <strong>there is no relation between current ephemeral keys and past ephemeral keys</strong>.</p><h3><strong>How it works ?</strong></h3><p>Server before the handshake starts has a <strong>CA certificate issued, public key and a private key</strong>. Client provide list of cipher suites supported by the browser. Based on this server chooses 1 cipher suite looking like this <strong>TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384</strong>. It includes the following information</p><ol><li>Protocol : TLS</li><li>Key Exchange : Elliptic Curve Diffie-Hellman Ephemeral (ECDHE)</li><li>Authentication : Rivest Shamir Adleman algorithm (RSA)</li><li>Encryption : Advanced Encryption Standard with 256bit key in Galois/Counter mode (AES 256 GCM)</li><li>Hashs : Secure Hash Algorithm 384 (SHA384)</li></ol><p>After cipher is selected symmetric session key is generated (based on RSA/DH) and then this key is used for carrying out the information in an secure way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*58HIk1-PdZjDnjPmGdhGTg.png" /></figure><h4><strong>Client Request</strong></h4><p>Client sends <strong>Client Hello </strong>which starts the TLS session. Inside <strong>Client Hello</strong> we have the following</p><ol><li>Version : Highest TLS version supported but the client</li><li>Random Number : Sequence generated 32 bytes, some versions have hidden timestamp encoded in 1st four bytes.</li><li>Session ID : Value used to identify particular set of session keys. Initially 0000… in Client Hello</li><li>Ciper Suits : List of Cipher Suits supported by client for Confidentiality, Authentication and Data Integrity</li><li>Extensions : optional additional features added to TLS</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*9PJYIXZWJtQe0BqnLqesNQ.png" /><figcaption>Client Hello</figcaption></figure><p>In the above diagram session id is not empty and is reusing the old connection. You will read about it more in <em>Session Resumption</em> below</p><h3><strong>Server Response</strong></h3><h4><strong>Server Hello</strong></h4><p>Server sends the response to this with all the <em>above mentioned data(client-hello)</em> saying <strong>Server Hello. </strong><em>Version</em> contains the Highest Version of TLS supported by server, <em>Random number</em> same as above but generated at server, <em>Session ID</em> is a value generated by server to identify the session and a Cipher Suits selected from the list provided in Client Hello.</p><h4><strong>Certificate</strong></h4><p>It send a <strong>certificate chain</strong> to the client as well as <strong>CRL</strong>(certificate revocation list) list. To verify the server is a trusted one, not any dummy server setup by a hacker.</p><h4><strong>Server Hello Done</strong></h4><p>Server send this to acknowledge client that no more messages are coming from server. As different version have different no. of requests before this.</p><h3><strong>Client Request</strong></h3><h4><strong>ClientKeyExchange</strong></h4><p>This is the time when asymmetric key-exchange is setup. This validates that the server is actually the guy authenticated. If old session is used this doesn’t happen. As it remembers the keys exchanged. Both RSA and DH have different mechanism to exchange keys as described above.</p><blockquote><em>Incase of RSA Client generates the symmetric key and encrypts with public key and sends it. While incase of DH a key is generated from its own secret and common data(this can be a common text) and then encrypted and send.</em></blockquote><h3>Client/Server Request</h3><h4><strong>ChangeCipherSpec</strong></h4><p>The server gets the key-exchange parameters send by the client and verifies the message integrity.</p><h4><strong>Finished</strong></h4><p>This message is send in an encrypted format to the client. Client decrypts the message, verifies the MAC and the tunnel is established. This means encryption verification is finished and it can go ahead with the calls.</p><h3><strong>TLS Extensions</strong></h3><h4><strong>Application layer Protocol Negotiation (ALPN)</strong></h4><p>When two peer within a network want to communicate using a custom protocol it is very hard to make servers and clients understand it. The way taken by HTTP and TLS is they have fixed ports 80 and 443 over which the communication happens and all the intermediate servers understand it. So when a custom protocol is defined we can go the same route but this is an impractical approach as port assignment need to be approved across servers, firewalls and intermediate servers as they only allow traffic from 80 and 443. So to enable any new protocol we can reuse the same ports 80 and 443. For this purpose <strong>HTTP Specification <em>Upgrade</em> </strong>was designed. However it adds 1 more round trip and is unreliable in presence of firewalls, intermediate servers. In practice it is mostly used in case of web socket connection.</p><p><strong>So why not use TLS. It will hide all the information from the underlying firewalls and intermediate servers as well as you don’t have worry about hops around the internet.</strong></p><p>ALPN is a TLS extension which handle protocol negotiation between them without additional roundtrips. You can add <strong>ProtocolNameList</strong> field in the list of supported protocol in the <em>Client Hello </em>and Server can select and return <strong>ProtocolName</strong> in the <em>Server Hello </em>message. This sets up the secure tunnels and what protocol is used within the tunnel is also agreed on. However in case of HTTP you don’t need this.</p><h4><strong>Server Name Indication (SNI)</strong></h4><p>So now we know that client only need to know IP address to setup a TLS connection(as 443 is fixed for TLS). But what happens if Server wants to host multiple independent sites(hostnames) with each having its own TLS certificate on the same IP address. This is not possible right , so one server per certificate ? Actually, this can be achieved using SNI hostname send in the client hello. Server can check hostname and return appropriate certificate in the <em>Certificate</em> response</p><h3><strong>TLS Session Resumption</strong></h3><p>As you see from above mechanisms like Certificate exchange, Selection of appropriate Cipher Suite of TLS etc. It creates huge latency. So why not reuse the same TLS connection. Few features were introduced to achieve the same.</p><h4><strong>Session Identifiers</strong></h4><p>If you look into the Client Hello call it contains a Session ID which is sent as 0000.. initially. This is used to reactivate an old TLS session. When a next TLS connection goes with a client hello, we add the session id of the previous TLS connection. <strong>This uses the same set of Cipher Suite and Certificate</strong>. This saves 1 RTT of the connection. So above calls don’t happen for this new TLS connection. But if session id is not cached in the server. It need to fetch it again(Default mechanism). So if you see it from Server perspective, it has to keep this information per client somewhere cached and it should be available across all the instances when load balancer is envolved. We need to carefully write proper mechanism for scaling out our systems with TLS. <em>Due to this above reason API Gateways have inbuilt TLS support, and you don’t have to write TLS related logic of distributed caching in your application. </em>When the call enters the subnet it doesn’t need SSL so simple protocol based communication can happen within.</p><h4><strong>Session Tickets</strong></h4><p>It is also a mechanism which helps us maintain the session level information in the client and we don’t have to keep this information in our server. It checks if client supports Session Tickets. If yes, then it sends all the session related data encrypted using its private key to the client as a <strong>New Session Ticke</strong>t record. This is included in <em>Client Hello</em> of next TLS connection as <em>SessionTicket </em>extension header. So this way all the session data is stored in the client and the ticket is safe as it is encrypted using the private key present in the server and server can only decrypt it.</p><p>The above 2 mechanisms are commonly referred as session caching and stateless resumption. However these values have expiration, which helps in invalidating sessions.</p><h3><strong>Certificate Authorities(CA)</strong></h3><p>Now let’s discuss on how do you know that the server is the correct one responding to you rather than a dummy server put in place by a hacker to respond to all the calls coming to it. This is done globally by the Certificate Authorities, who register the servers by providing a certificate. Which is shared to us as a Certificate response of <em>Client Hello. </em>This also contains the public key within its signature. This certificate can be chain of certificates formed by Chain of Trust. Let’s say A and B both generate public and private keys and hide their private keys as well as share their public keys with each other.<em> A and B made sure that the public keys shared is not exposed to an imposter or maybe shared it physicall</em>y. When data is encrypted using that private key by <strong>B </strong>and send to <strong>A</strong>. <strong>A</strong> can decrypt it using the public key of <strong>B</strong>. Then <strong>A</strong> verifies and understand that it is <strong>B</strong> who has send the data. Now Suppose a new user C comes who is trusted by <strong>A</strong> and <strong>B</strong> doesn’t know about <strong>C</strong>. So to have a mutual trust <strong>C</strong> asks <strong>A</strong> to sign his key. <strong>A</strong> encrypts <strong>C</strong>’s key with his own signature using his private key and sends it to <strong>B</strong>. As <strong>B</strong> has <strong>A</strong>’s public key. He can decrypt and see the key provided by <strong>C</strong> and verify that <strong>A</strong> actually has signed it. Then <strong>B</strong> also begins to trust <strong>C </strong>and a chain of trust is formed. This is used to grow the list of trusted parties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*U4tVhGyHb59JZ4VMIkK1qQ.png" /><figcaption>chain of trust</figcaption></figure><p>Every browser and operating system follows the same process and it has its own set of certificates within it. You can also import certificates on your own to build a chain of trust. In the above screenshot you can see DigiCert Global Root CA is the root certificate then DigiCert SHA2 Secure Server CA is the intermediate Server signed by root(DIgiCert Global Root CA) and then <a href="http://www.linked.com/">www.linked.com</a> is signed by intermediate(DigiCert SHA2 Secure Server CA). It is also responsibility of CA to revoke a compromised certificate. So to verify a certificate client has to check the whole chain of trust.</p><p>CA’s keeps track of the revoked certificates called <strong>Certificate Revocation List(CRL)</strong> and periodically updates it with a serial number of every certificate. This is cached by the client and it checks against every certificate you download from the server. This has limitations : <em>There is no push mechanism here for instant notification.</em> <em>If a cached certificate is revoked, you don’t get an instant notification until the cache expires.</em> <em>Sometimes when browser need to fetch the latest certificates from CA then the TLS handshake is blocked and extra latency is added in the TLS system.</em></p><p>To verify this <strong>OCSP (Online Certificate Status Protocol)</strong> was introduced which provides a mechanism to perform real-time check over the serial number in question. However it has its own limitations. CA should be able to handle the whole load of the world internet and should be available always. Realtime invocation can track all the clients in the internet, which takes away clients privacy. It also blocks the validation of certificate chain.</p><p>This lead to <strong>OCSP stapling</strong>. According to this instead of Client making the OCSP call. It is done by the server periodically, fetching signed and timestamped responses/staples from the CA. Which is then send to the client as part of the TLS Handshake. Allowing the client to validate the certificate and the attached revocation record signed by the CA. This way the call is also secure, client’s history is also not exposed as well as client fails properly if verification fails(OSCP “Must Staple” flag)</p><h3><strong>TLS Record Protocol</strong></h3><p>TLS session data is framed using a protocol called TLS Record Protocol. It is responsible for all the aspects of TLS. Workflow looks something like this. Record protocol gets application data and is divided into 16KB blocks. Then it uses MAC to generate HMAC of the data. Then data is encrypted using the symmetric key. When this is done data is passed to TCP for transport and this way it goes forward. On the receiver the data is decrypted using the symmetric key, verify the MAC, extract and pass the data along. This is all done by the TLS layer with record size of 16KB with 5 bytes of header, a MAC and a padding in case <strong>Block Cipher </strong>from the<strong> Cipher suite</strong> is selected. This varies with version to version. 20 bytes in case of SSLv3, TLS 1.0 and TLS 1.1 and upto 32 bytes for TLS 1.2</p><p>To make the data available for decryption the whole record must be present. In case of TLS over UDP it is a Datagram which is the smallest unit. So there is no problem but when we talk about <em>TCP the record is broken down into packets if the record size is large, so for decryption all the packets must reach the receiver to decrypt, which adds latency. Now if the record size is small then it adds extra CPU and byte overhead in record framing</em>. So for best optimisation if we know the size of data travelling and based on data type we can dynamically set it. If not, <strong>having a constant size will incur problems.</strong></p><h3><strong>Optimisations</strong></h3><p>If you have reached till this point. Congratulations !!</p><p>As seen there is a lot going on in a TLS based system. We need a lot of optimisations to have a proper performant connections.</p><ol><li><strong>Enable TLS Session Resumption</strong> : As we have seen it uses the old session id to establish the connection as well as removes 1 RTT. Which reduces CPU based overheads on creation of symmetric keys every time.</li><li><strong>Enable TLS False start</strong>: We can start sending data after we send ChangeCipherSpec and Finished from the client. We prevents 1 RTT for us in a new connection. Since symmetric key has reached/generated in client. However it can have issues with poorly implemented servers. Every browser have handled TLS False start in a different way. Some requires ALPN protocol advertisement. <em>Please read about it before you enable</em></li><li><strong>Dynamic record size</strong> : TLS Record needs dynamic record size based on type of data we are sending so that it doesn’t create any overheads mentioned above. So we can talk about in terms of TCP. As we have slow-start in a TCP. The congestion window is small initially. TCP packet should carry exactly one TLS record. When congestion window is large we can have a reasonable size of TLS record and break large files into stream of packets. If you have seen websites like Netflix take a lot of time if you open a movie using the url, you watched a day before, as the congestion window size need to increase since you are visiting that server for the 1st time during the day.</li><li><strong>Optimisation on reuse of old connections</strong> : When a connection is made, we need ur servers to allow <strong>keep alive connections</strong> check all the connection timeout settings. Servers sometimes have very short timeouts which might remove session related information of the old connections and new connection is created unnecessarily every time. Analytics and Logs can be used to find the optimised values of timeouts for your application.</li><li><strong>Horizontal Scaling(early termination)</strong> : This is one the best ways to keep the RTT as small as possible. Keep you server scaled in regions near to the client.Apply logics like partitioning, replications, Use CDNs(geo distributed) as much as possible.</li><li><strong>Proper configuration of Certificate Chain</strong> : Verifying the chain of trust is one of the critical steps in the first RTT of a TLS connection. We should provide the whole certificate chain to the client including the intermediate certificates. If not it tries to download it from the url provided of the child certificate. If this is also not present, the verification fails. So this additional DNS lookup with TCP handshake and http call adds an overhead to the process. On the other hand keep the certificate chain clean with only the list of certificates required, do not include unnecessary certificates. This helps us keep the size of the tcp connection as small as possible with not exceeding the initial congestion window. If that happens it adds more latency.(slow-start of TCP)</li><li><strong>OCSP Stapling</strong> : This is one of the important feature to be enabled in the server side. It gives the best security and performance in the certificate verification process. However it has 2 issues , it doesn’t provide information to include status information of the intermediate certificates and the format of the extension doesn’t offer multiple(certificate) status methods. So a new extension was introduced(RFC 6961) indicate by ‘status_request_v2’. Which if we included in extension of the client hello. This gives us response for all the chain of certificates. Please read<strong> RFC 6961</strong> for more details. <em>This makes CA servers are not flooded with calls of client coming for verification of intermediate certificates but calls coming in from server for all certificates in the chain</em>. However we need to make sure that the size doesn’t overflow the no. of packets of the initial congestion window.</li><li><strong>Enable HTTP Strict Transport Security (HSTS)</strong> : Using HTTP header ’Strict-Transport-Security: max-age=16070400 includeSubDomains’ enforces the client with few rules</li></ol><blockquote>All request must be send over HTTPS including sub resource requests as well as navigations. So if the sub-resource is configured in non-https, the user agent converts it to https</blockquote><blockquote>Max-age tell about the HSTS rules in seconds in the above example it is set to 186 days</blockquote><blockquote>‘includeSubDomains’ will enforce same rules for subdomains</blockquote><blockquote>If HTTPS tunnel is not established, it fails the request rather than falling down to http version of request</blockquote><blockquote>It also eliminate the need of redirection for HTTP to HTTPS</blockquote><p>The above rules are should be tested before deployment. As failure to connect using HTTPS is treated as a browser error. It is a just a design choice to prevent network hackers into tricking people to use non-https based connections. One more important thing to note is the 1st connection leaves client’s browser unprotected. Which can be intercepted by a hacker and stop the call from enforcing HSTS so most browsers have “<strong>HSTS preload list</strong>”. Where it maintains the list of websites to enforce HSTS. However this list gets deleted in case of fresh install. Or wipes the local state.</p><p>9. Enable HTTP Public Key Pinning (HPKP) : This help us pin the required HTTP certificate(“pin”). The most secure way is to pin the leaf certificate of the chain as you are pinning certificates which should be accepted by the browser. Pinning help use segregate the certificates. If a root has an intermediate certificate pinned means root trusts only this intermediate certificate.</p><p>10. Audit your content and update you resources to use <strong>Content Security Policy(CSP)</strong></p><p><em>Content-Security-Policy: upgrade-insecure-requests : Tells browser to update all calls to HTTPS</em></p><p><em>Content-Security-Policy-Report-Only: default-src https:; report-uri </em><a href="https://abc/report/endpoint"><em>https://abc/report/endpoint</em></a><em> : Tells the browser to report any non-https violations</em></p><p>11. Upgrade you servers with latest TLS libraries</p><p>12. Enable HTTP/2</p><h3><strong>Testing a TLS connection</strong></h3><p>Here openssl to connect to google.com</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*iU15JU1zXpzkPUtHb8vqWg.png" /><figcaption>openssl 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*p8tJppb9_sjqKxlT86c4Hg.png" /><figcaption>openssl 2</figcaption></figure><h3>Conclusion</h3><p>TLS requires a lot of things to be configured properly. I created a HTTPS server in python to do some research on it. Sharing it with everyone <br><a href="https://github.com/debashis-das/HTTPSServer/blob/main/simple-https-server.py">https://github.com/debashis-das/HTTPSServer/blob/main/simple-https-server.py</a></p><p>Use Wireshark to debug the calls in <a href="https://wiki.wireshark.org/CaptureSetup/Loopback">loopback mode</a></p><p>References :</p><ul><li><a href="https://hpbn.co/">High Performance Browser Networking (O&#39;Reilly)</a></li><li><a href="https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange">Diffie-Hellman key exchange - Wikipedia</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=774f4819438b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gateway to Internet]]></title>
            <link>https://medium.com/@debashd1991/gateway-to-internet-ad4843475f81?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad4843475f81</guid>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[latency]]></category>
            <category><![CDATA[protocol]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Tue, 21 Feb 2023 09:06:50 GMT</pubDate>
            <atom:updated>2023-02-21T09:06:50.072Z</atom:updated>
            <content:encoded><![CDATA[<p>When data travels from client to server and vice versa speed becomes an important factor. <strong>It depends on latency and bandwidth of the systems in place.</strong> Let’s say we transmit 10 mb file over a 100 Mbps path. It takes this 0.1 sec to transmit. If you carefully look into the units. It is 10 <strong>Mega-Bytes</strong> and 100 <strong>mega-bits</strong> per second which is actually 80 <strong>mega-bits</strong> and 0.8 sec to transmit it. Interesting isn’t it.</p><p>In this process it incurs multiple delays listed below</p><blockquote><strong><em>Propagation delay</em></strong></blockquote><blockquote>Amount of time required for a message to travel from the sender to receiver, which is a function of distance over speed with which the signal propagates.</blockquote><blockquote><strong><em>Transmission delay</em></strong></blockquote><blockquote>Amount of time required to push all the packet’s bits into the link, which is a function of the packet’s length and data rate of the link.</blockquote><blockquote><strong><em>Processing delay</em></strong></blockquote><blockquote>Amount of time required to process the packet header, check for bit-level errors, and determine the packet’s destination.</blockquote><blockquote><strong><em>Queuing delay</em></strong></blockquote><blockquote>Amount of time the packet is waiting in the queue until it can be processed.</blockquote><p>These delays contribute in latency of the data. Now let’s talk about the fastest way data can travel. It is using fiber optic cable. We know speed of light is 2.99 x 10⁸ m/s in vacuum. So if we consider fibre-optic cable, it has its own refractive index around 1.44 to 1.68 which decreases it by a certain value. Lets rounds it to 2 x 10⁸ m/s. However each fiber-optic cable have multiple fibers and each fiber can carry multiple wavelengths i.e. WDM. So bandwidth can never be an issue I guess.</p><p>If data has to travel huge distances for you application to load, it might have a few ms of delay. Basically as per google there can be on an average 40 hops to reach data across the entire world. <strong>Latencies can depend on local ISP, its software, topology of the network and time of day</strong></p><blockquote>traceroute &lt;domain&gt; : shows you the total number of hops</blockquote><p>So here we can safely say that bandwidth can never be an issue for us. When that happens we layout more cables or develop WDM to carry more data. But on the other-hand latency has a peak value of speed of light, which cannot be improved upon unless we are able to bend the laws of physics. So to improve this CDNs, local caching, replication, partitioning and so many ways other ways are developed so that the data can be served from as near as possible with either eventual consistency or strong consistency depending on what kind of data we are talking about.</p><h3><strong>Transmission Control Protocol (TCP)</strong></h3><p>To understand the basic of data transfer we need to understand how our systems handle network connections. Which leads to TCP, our very old friend which we take help from without knowing its contribution. T<strong>CP or Transmission Control Protocol is a protocol used for data transfers for HTTP</strong>. It takes care of lot of things which we didn’t even think of. TCP guarantees that the data being transferred reaches the other end without any issue. It handles <strong>retransmission of lost data, order of data, congestion control and avoidance</strong> etc. TCP is optimised for transferring correct data, it doesn’t care about how fast it is transferring it. Which is actually awesome since the time this was designed, people didn’t even think of how fast the data is transferred but to transfer correct data. Let’s talk about TCP in details and then algorithms used to optimise it.</p><p>TCP works on on a three-way handshake with</p><p>(client)SYNC -&gt; (server)SYNC-ACK -&gt; (client)ACK.</p><p>It starts by sending a SYNC to server with a random number. Server increments it and adds 1 more random number with the SYNC-ACK message and then ACK is send to the server with both numbers incremented. <strong>After this client can start sending data as server has acknowledged and server can send data after it has received ACK</strong>. This adds 1 RTT before any transfer of data.</p><h4><strong>Congestion Collapse</strong></h4><p>This system has a problem. If data(all packets) doesn’t reach within the required time(before timeout) a retransmission happens of the error packets. Which might lead to multiple duplicate packets within the network however the client/server can identify the duplicate packets and discard it but this might affect the bandwidth of the system. This problem is called <strong>congestion collapse.</strong></p><h4>Flow Control</h4><p>This lead to an algorithm called <strong>flow-control</strong>. Which adds 1 more RTT. After ACK is received by the server. It send ACK back to client with <strong>rwnd(own receive window) field which is the size of the space buffer it hold of the incoming data.</strong> It keeps on publishing there values to each other with ACKs as there window decreases until it becomes 0 for one of them. <em>Which is when it is not able to process any more messages</em>. It can happen for both server and client. As in case of client it always ends up exhausting its window size but in case of servers this happens when client is sending a stream of data like a video from the client.</p><p>However flow control is limiting the sender from sending data within its window, it does not take into account the bandwidth between the sender and the receiver which can overwhelm the network. So <strong>slow-start </strong>was introduced to handle this. As neither of them knows how much bandwidth is present we start with a small window size of data. Here <strong>Congestion window size (cwnd) is used which is the value per TCP connection for the no of packets in flight before an ACK is received.</strong> This is a value kept locally. <em>So the no of packets inflight is the minimum of (</em><strong><em>cwnd</em></strong><em> and </em><strong><em>rwnd</em></strong><em>).</em> So on every ACK server keeps on doubling the size of the <strong>cwnd</strong> which leads to exponential increase in <strong>cwnd</strong> until it optimises based on the bandwidth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VgnBl8GPpdRFP1FcAFtFQA.png" /><figcaption>change in window size</figcaption></figure><p>The above mechanism is good to no effect when we have large stream downloads as the connection gets optimised within its life-span but what it greatly effects are small-quick requests. But one important think here is that this happens only for a new connection and then it is kept alive throughout the process. This process of increase happens till it reaches a point when there is a packet loss there is when <strong>Congestion Avoidance Algorithm</strong> kicks in and it decreases the window size. You can see in the figure above Window size once decreases and once increases from 130880 -&gt; 131008 -&gt; 130944 -&gt; 130816 -&gt; 130752 -&gt; 130688….</p><p>There are multiple algorithms used here like PRR(Proportional Rate Reduction) Cubic TCP etc. Currently linux uses CUBIC from 2006 (Linux kernel 2.6.13) whereas MacOS(OS X Yosemite) uses it from 2014 and Windows from 2017 (Windows 10.1709)</p><blockquote><strong>Out of Context :</strong></blockquote><blockquote>If you see this in terms of micro-services. It can happen that a server get overwhelmed with requests it is not able to process. This is when we add a <strong>rate-limiter</strong> before the service with a configuration of its rwnd(own receive window) which keeps on decreasing until it is not able to process any more calls and bypass the call in different ways. So rate-limiter acts as our own flow-controller.</blockquote><blockquote>Now imagine a feedback mechanism as well from the service itself, which looks in the heap of the service or the average sleep time of threads, or <strong>no. of requests without ACK(cwnd)</strong> and gives this info to rate-limiter which can be used to increase the size of the queue which is handling the no. of requests. This way based on no. of requests hitting the server can be optimised using a rate-limiter. It is like we solved this problem in terms of packets and now we are implementing it in terms of requests.</blockquote><p><strong>NOTE :</strong> As we are decreasing the size to the window and it becomes zero when it takes time for the receiver to process the current set of requests. If this happens frequently it would create data gaps, when there is no inflight messages, so to address this bandwidth should be big enough to avoid these data gaps. This is addressed using BDP. Read about this for more details</p><h3><strong>Head-of-line blocking</strong></h3><p>This is one of the issues which we have been dealing from <strong>decades</strong> with an ’s’. Finally with HTTP 3 this issue is no more. Since TCP was build long time ago people didn’t think about speed. They optimised for data integrity. Lets take an example, suppose 5 packets provide complete data for an http call and one of them is lost(jitter). All these 4 packets are kept in TCP buffer until retransmission happens and packet is received in the tcp buffer. This mechanism helps in-order/reliable transfer of messages. <strong>As 1 packet loss can introduce latency in a tcp based system</strong></p><h3>Conclusion</h3><p>TCP mechanisms where introduced when there was nothing else. But now we have matured to a point where latency is what we are thinking of. Because of this a lot of improvements where done on top of TCP which lead to a release of HTTP 2. But then came HTTP 3 which works on top of UDP. <strong><em>So is this an end of TCP era ?</em></strong> Time will tell more about this.</p><p>Keep Learning !!!</p><p>References :</p><p><a href="https://hpbn.co/">High Performance Browser Networking (O&#39;Reilly)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad4843475f81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[User Datagram Protocol (UDP)]]></title>
            <link>https://medium.com/@debashd1991/user-datagram-protocol-udp-7f36f6a260c4?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f36f6a260c4</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[broadcasting]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[udp]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Tue, 21 Feb 2023 08:10:55 GMT</pubDate>
            <atom:updated>2023-02-21T09:07:40.511Z</atom:updated>
            <content:encoded><![CDATA[<p>To understand UDP, we need to understand what a <strong>Datagram</strong> is. It is a self independent data carrying entity which has sufficient information of source and destination nodes and can travel to destination <strong>without any information of the previous nodes it travelled from</strong>. <strong>Packets</strong> are used in terms of reliable transfer of data whereas <strong>datagram</strong> is used for unreliable form of data, which has no guarantees of delivery. Which lead to the name UDP “unreliable datagram protocol”.</p><p>As TCP and UDP are layers above IP. Let’s talk a little about IP. IP layer’s main purpose is to encapsulate message within its source ip, destination ip and other parameters. <strong>IP doesn’t take care about reliable delivery, failure handling, retransmission and other mechanism build for data integrity.</strong> TCP adds all these layers above it from data integrity(for more information read my other <a href="https://medium.com/@debashd1991/gateway-to-internet-ad4843475f81"><strong><em>blog</em></strong></a>). But in case of UDP, It just adds 4 more fields source port, destination port , length of the packet and checksum. In which <strong>2 parameters source port and checksum are optional</strong>. As checksum is part of IP encapsulation, TCP checksum is optional.</p><p>So UDP has no ACK, no retransmission, no in-order stuffs, no head of line blocking, no connection establishment, no congestion flow/control. Which makes UDP stateless. Whatever you need above this is implemented by the application. UDP datagram are read as full messages at a time. <strong>It is the smallest independent unit of data transfer.</strong></p><blockquote><strong>Out of Context</strong></blockquote><blockquote>Because of the above aspects of UDP, QUIC is a protocol build over it. Which does all the things that TCP does but using UDP and in a different way so that it does not have constraints like <strong>head of line blocking</strong> etc and <strong>multiplexing is easier</strong>. This is used in HTTP 3, which is gaining popularity in recent time. We will talk about this in my later blogs.</blockquote><p>This is it for UDP but then NAT came into picture. Which changed everything. <em>NAT (Network Address Translator) is a way to basically save public IPs from exhausting and create a local network which is provided with a NAT instance</em>. So when a call goes out of the local network it uses NAT public IP to address the call over Internet. Now as datagrams are stateless it doesn’t have any mechanism for connection establishment. Because of this, a call going from a machine in a LAN to the internet can easily send data but what about receiving data. It cannot receive it as the public IP used is of the NAT server. It cannot know from which local machine the call came from. Even if we maintain state(IP and port of the call) in our NAT instance for UDP, we don’t have any connection termination or connection keep alive mechanism for UDP so NAT doesn’t know when to delete the entry.</p><h3>STUN</h3><p>Due to above problem <strong>STUN (Session Traversal Utilities for NAT)</strong> protocol came into existence and <strong>STUN</strong> servers are required to keep the public IP and PORT of the receiver(NAT) in the public network/Internet. You need to configure <strong>STUN</strong> servers in your application which uses DNS discovery to find the server and then get the public IP and port of the destination peer. Now when the call travels it makes an entry in the NAT entries table and reaches the destination. If the destination is also inside a NAT, it would have already gone through the STUN mechanism and have an entry in its own NAT. <strong>So before the data transfer starts using their public IP and PORT, the entires in their NAT instance is already established using STUN</strong>. <em>STUN protocol also has a simple keep alive mechanism to refresh timeouts for the entries in NAT.</em></p><h3>TURN</h3><p>STUN is sometimes not enough for data transfer to start. In those cases TURN(Traversal using relays around NAT) protocol which uses relay servers as a fallback and can even switch to TCP if everything fails. TURN depends on a reliable public relay server. So when the connection starts it basically calls the same TURN server which handles negotiation and when it is completes data transfer can start. <strong>But this is not peer to peer as a relay server is involved.</strong> And it might get under a lot of load when multiple data flows start. So, this is a last resort for data communication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*6CevufoRgB4l8YxZOsDrxw.png" /></figure><p>So to manage all the above aspects of UDP ICE(Interactive Connectivity Establishment) protocol was established which does all the above out of box.</p><p>However you need a proper mechanism like TCP to setup a custom way using UDP in your application. Which lead to the invention of WebRTC. We are skipping WebRTC for later blogs.</p><h3>Conclusion</h3><p>UDP is meant for audio/video communication across peers. If 3–4 video/audio frames are lost it doesn’t effect too much which is called <strong>Jitters. </strong>Sometimes jitters are too much which means either STUN servers are not working properly or it has gone to TURN mechanisms and relay servers are hit by lot of peers. Scaling this system out is another challenge itself. So please don’t shout on your OTT provider when you miss a wicket or a goal watching a live broadcast. Ask them to scale there systems based on region/viewers, maybe add more STUN servers. If this doesn’t serve us them go for http based video delivery after the video/audio frame reaches your region. <strong>Scaling STUN servers is more important as they might introduce jitters into the data stream. Which will anyway be streamed by the http servers, as once added cannot be reframed(maybe use AI based restoration of frames).</strong></p><p>Keep Learning!!</p><p>Reference :</p><p><a href="https://hpbn.co/">High Performance Browser Networking (O&#39;Reilly)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f36f6a260c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Processing for schema-less messages]]></title>
            <link>https://medium.com/@debashd1991/data-processing-for-schema-less-messages-3fe3fc4e5fa?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fe3fc4e5fa</guid>
            <category><![CDATA[data-warehouse]]></category>
            <category><![CDATA[data-modeling]]></category>
            <category><![CDATA[data-management]]></category>
            <category><![CDATA[schemaless]]></category>
            <category><![CDATA[airflow]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Fri, 10 Feb 2023 12:36:07 GMT</pubDate>
            <atom:updated>2023-02-10T12:37:32.660Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous blog I wrote about how to manage data using <em>Data Warehouses</em> <a href="https://medium.com/@debashd1991/data-management-in-data-warehouses-535dbcd6021b">&lt;Link&gt;</a> for message which have schemas. But sometimes you don’t know how the incoming message will look like, or the message fields will vary on the type of product.</p><p>Let me give you an example for more details. Let’s say customer wants to apply for a loan and sponsors want the loan to have different set of requirements/eligibility criteria to validate against, So customer is provided with options to choose from. If products/sponsors change then there fields change and because of which all products cannot have a common design. Now say per product the values change with time. Sponsor comes tomorrow and says I need to add 1 more eligibility criteria to my product, which breaks the whole schema based design. <strong>One of the main problems faced in loan creations</strong>(<em>Note: We are not building a decision service here, we are just managing data of what decision was taken on a loan</em>). This makes it harder to have a common serialise mechanism like Apache Avro.</p><p>So what do you do ? You can make the the key value list as a string and sent to the event-bus. But how do you store it.</p><p>Can we store it in raw format and let it stay ? <strong>No</strong>, you cannot do this because what if downstream system consuming from this, need values for a specific product and it is not present. So you need to build a parsing logic which not only validates fields but also validates business logic. After validation they should be able to extract whatever fields they need per product.</p><p>Let’s talk only in terms of processing data as <strong>batches</strong> rather than stream to simplify things.</p><p>Why batches ? There are 2 types of DBs</p><ol><li>OLTP(Online Transaction Processing) : These kind of DBs are used at micro-service level where data can be handled with small and fast transactions.</li><li>OLAP(Online Analytical Processing) : These kind of DBs are used as warehouses because you can insert huge bulk of data using <strong>only one bulk query and it is very fast</strong>. So if you have one write with 100 entires this will take less time. It has multiple other functionality for processing the data in a fast way as well. This kind of DBs are not good for small and fast transactions</li></ol><p><em>So if you are storing data in a warehouse, batch based storage is the way to go. You can do stream processing and send it to an event-bus again for a bulk consumer to consume it. Which is acceptable.</em></p><h3><strong>Airflow Implementation</strong></h3><p>Let’s take a little detour to understand Airflow then we can talk about our requirement. Airflow is a workflow management platform which uses concept of DAGs(Directed Acyclic Graphs) to break-down data processing and create a data pipeline. Here each node in DAGs is a task running in a <strong>stateless</strong> way to process some data. Here stateless is very important because <em>you cannot pass a huge JSON from one task to other</em>. However you can have small state variables across DAGs.</p><p>Now for our requirement, you need to define a generic DAGs-<strong>Task</strong> which can based on the product apply validations and its business logic. So you need a configurable design.</p><p>This is achieved by having configurations stored in a common place in whatever format you like and fetch it based on productId. So based on the example above, when a new sponsor comes in. We just need to write a configuration to onboard him.</p><p>You store the product to configuration map in a table and fetch it from a blob-store like S3. Configuration looks like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*CwCFKrixwT7UJANtcuQ46Q.png" /><figcaption>per product configuration</figcaption></figure><p>Now what does this file(like product_abc.json) contains ? It contains the input field position, output field name, the type of value and a query id for business validation which you can use for <strong>conditional insert</strong>. So lets go through the flow. You store the data extracted into a <strong>temporary table</strong> and do a actual insert based on conditions like the one provided below then after the insertion happens delete the temporary table as it is no longer needed. Maybe put the data which failed validation to some error table, choice is your. Giving a dummy example for conditional insert below.</p><pre>insert all<br>    when c &gt; 10 then<br>      into actual_table (col1, col2) values (a, b)<br>  select a, b, c from temp_table;</pre><p>Here I skipped one part how input is converted into output(raw message to actual table entries). Lets consider example raw message and mapping json(like product_abc.json) below:</p><pre>{<br> &quot;a&quot;: {<br>  &quot;b&quot;: {<br>   &quot;c&quot;: [123, 890]<br>  },<br>  &quot;d&quot;: 567<br> },<br> &quot;e&quot;: 345,<br> &quot;f&quot;: {<br>  &quot;g&quot;: {<br>   &quot;h&quot;: [323, 578]<br>  }<br> }<br>}</pre><pre>{<br>  &quot;mappings&quot; : [<br> { &quot;input&quot;: &quot;a.b.c&quot;, &quot;output&quot;: &quot;c&quot;, &quot;type&quot;: &quot;Array&quot; },<br> { &quot;input&quot;: &quot;a.d&quot;, &quot;output&quot;: &quot;d&quot;, &quot;type&quot;: &quot;Integer&quot;},<br> { &quot;input&quot;: &quot;e&quot;, &quot;output&quot;: &quot;e&quot;, &quot;type&quot;: &quot;Integer&quot;},<br> { &quot;input&quot;: &quot;f.g.h&quot;, &quot;output&quot;: &quot;h&quot;, &quot;type&quot;: &quot;Array&quot;}<br>  ]<br>}</pre><p>Here important thing is we need to demoralise the values if you want to store it in <strong>proper relational format(creation of foreign keys is a mess)</strong> or you can choose a <strong>semi-relational format, </strong>both formats shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*lVcN5yJJZ3RzwUWfaXxxKw.png" /><figcaption>denormalised relation data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*4lnig8oVP7zRmEUoUGAx5w.png" /><figcaption>semi-structured data</figcaption></figure><h3><strong>Table Design</strong></h3><p>But there is one more issue here you need to have 1 table per product, <strong>so 10 products is equivalent to 10 tables.</strong> A very bad design right. So why can’t we store the values in a columnar way rather than traditional row way and have product Id and application Id attached to it, let me show using the above values for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5eIMsvXepERPKpuLKwU35Q.png" /><figcaption>columnar-way to store data</figcaption></figure><p>This way we can accommodate all products in a single table. So if there are 50 fields for one product and 20 fields for another product your table remains the same.</p><p><strong>Now you have a generic Task for a DAGs which can do extraction and validation with just a few configurations. You can scale this with a few configurations.</strong></p><h3><strong>Conclusion</strong></h3><p>When you cannot serialise a message using builtin frameworks. You get a space to get creative. I implemented it in this way. Let me know what you would have done. You can implement the above design with streams as well but then it requires a lot of synchronous operations. Not sure how much more efficient it is when done using Streams rather than Airflow. A good design need to be shared. Even if it is very small.</p><p>Keep modelling !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fe3fc4e5fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Management in Data Warehouses]]></title>
            <link>https://medium.com/@debashd1991/data-management-in-data-warehouses-535dbcd6021b?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/535dbcd6021b</guid>
            <category><![CDATA[flink]]></category>
            <category><![CDATA[data-warehouse]]></category>
            <category><![CDATA[data-management]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[airflow]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Thu, 09 Feb 2023 07:24:07 GMT</pubDate>
            <atom:updated>2023-02-09T07:24:07.836Z</atom:updated>
            <content:encoded><![CDATA[<p>Data warehouses is a place where we keep all data coming from different sources. Consider this as a central repository for all data points. Which can be used for analytics, machine-learning, archiving, data enrichment as well as data logging. In this blog I talk about how to handle different types of data sources as well as how we should store data and the basic architectures for batch and real-time processing</p><h3><strong>Data Sourcing</strong></h3><p>There are 3 types of data sources, I can think of.</p><ol><li>Event-based</li><li>File-based</li><li>API-based</li></ol><p><strong>Event-based Sources</strong></p><p>Events can be generated by multiple systems asynchronously with writes into there local DB. Commonly people use event-bus to keep track of events and whoever needs it can pick it up from the event-bus. Event-bus tech like Kafka sends warehouse different events on the data with different data points. <em>To visualise it let’s talk about a loan creation process</em>. When a customer applies for a loan, based on the customers past records like banking informations, his current assets he is provided with a score by multiple agencies. This is called <strong>credit score</strong>. So based on his credit score he is provided an eligible loan amount like if a customer applies for loan of 10,000$, if his credit score is low, he might be eligible for only 5000$. Similarly like this a lot of middle events are generated until his loan is approved.</p><p>This generated data is coming from multiple micro services and other than <strong>applicationId</strong> all the data points are different. So its manageable by an event-bus. Lets talk about ways we can publish this data into event-bus</p><ol><li>Having a <strong>single schema</strong> for all the data points and have an event-type which tells the current state of the application</li><li>Other approach would be to have multiple types of data with status but <strong>schema less</strong>.</li><li>We can also have <strong>different topics</strong> for all of them which adds more infra-based dependencies on adding a new micro service. As well as having so many individual topics is Crazy!! <strong>So I rule this option out.</strong></li></ol><p>Having a <strong>schema is very critical</strong> when it comes to data-management as data-level validations can be done at schema level so you never get a Null pointer Exception, a field provided cannot be String if it is defined Integer in schema, this is one of its top benefits. This led to the invention of data-serialisers like Apache Avro. As in it validates that the data provided by parsing it against the schema provided. If we get a parse exception this means we have data issues. So we can trust our data to be clean if it has a schema based parsing.</p><p><strong>File-based Sources</strong></p><p>You can also have data dropped as files by 3rd party services into Blob stores like AWS S3 periodically, which is consumed and stored in data warehouse. Here we need triggers when the data become available where frameworks like Apache Airflow comes in handy. We can track these upload events and then download the files and dump these values in our warehouse. Apache Airflow is a tool which basically works on the principle of DAGs(Directed Acyclic Graphs). Where each node can do some tasks and we can link these tasks to one another. We will talk about it more when we discuss about Data processing.</p><p><strong>But can we expect the data to be clean ?</strong> No this data will not be clean, you might need a data validations layer before storing this data. One other thing we can do is talk to the 3rd party teams and define a schema and then parse it using Apache Avro. If parsing fails means data coming is not in correct format. <em>We can even have a common place like git repo where we maintain these schemas and version them. So if a contract change happens, all the users of the repo come to know about it. So this way contracts never break and are even versioned.</em></p><p><strong>API-based Sources</strong></p><p>API sources don’t come into picture until we need them. These may be source of truth for some data points, can be even be used for data enrichment.</p><h3><strong>Data Processing</strong></h3><p>After having a clear idea about sources, let’s talk about processing mechanism. Data warehouses generally support <strong>both</strong> table based Relational as well as raw formats like JSON. They generally don’t enforce Foreign keys and you can query inside unstructured data as well. So do we need to normalise/denormalise the data coming in a JSON formats to multiple tables. This question is a mystery for me. But I bend towards taking out only the values we need from these JSONs and leaving it in raw formats. Only issue in this approach is you need to backfill all the data-points when 1 more data-point is needed in the future.</p><p>Now let’s talk about the <strong>DB updates</strong>. Since we are a managing data in a data warehouse should be change values directly on the columns with new values. Suppose event A saying approved loan amount is 5000$ but later an event B comes which says approved loan amount is 7000$. Here should we change the value from 5000$ to 7000$ directly. No, never as a Data warehouse you need to know what happened when and why it happened. By updating this data you are deleting a critical information. Which can be later on used by data scientists/analysts to derive so information. So if you don’t update this what will you do, here is when <strong>Data Milestoning</strong> comes into picture.<em> Milestoning is a process of keeping track on data in a timeline format</em>. You can milestone data in 2 ways either by the time the event was generated or by the time when the event was processed.</p><p>I would like to do both as that is also a critical information. Suppose an event is generated at time t1 and processed at time t2, this t2-t1 gives us information on the lag aspect of processing which happened due to Kafka, our DAGs failed etc. So this information is also critical. We are building a data warehouse were any data is an information for us. It may not be now but having a timeline based structure helps us to identify critical points in a distributed systems.</p><p><strong>Bitemporal Milestoning</strong></p><p>This is a milestoning technique which helps us milestone data based on when the event happened and when the event processed. Let’s talk about the fields required for this. We can have <strong>event_start and event_end</strong> times for when the event happened, <strong>system_start and system_end</strong> times for when the values were processes. Let’s take an example to understand how these values will be stored in our warehouse.</p><p>Insert :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jaGAQj_-TC01xJJtxpmpzA.png" /><figcaption>insert bitemporal</figcaption></figure><p>Update:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZfLhZoBigdvYcqI2H493WA.png" /><figcaption>update bitemporal</figcaption></figure><p>Here date ‘<strong>9999–12–31T23:59:59 +0000</strong>’ is used to define an <strong>infinity date</strong>. In the top you can see what happens when an update comes. Here when a new insert comes we set <em>event_start</em> as the date which we get from Kafka as when this event happened. <em>event_end</em> is set to infinity date which means it is current active record. <em>system_start</em> is the date the record was processed for warehouse insertion and <em>system_end</em> says it is the current active record as per our system.</p><p>Now if you want to view the timeline of the data you can check is against which records are active in our DB currently using <strong>‘application_id’ = ’123’ and system_end = ‘9999–12–31T23:59:59 +0000’ and do ORDER BY event_start/event_end</strong>. Gives you the whole set of changes it went through from start to end. Little complex to understand. If you need help contact me we can discuss on this.</p><p><strong>Data Enrichment</strong></p><p>Now you have at least the raw data cleaned and milestoned. Next comes business level validation. Business level validations are the validations you do on top of the data so that there is no issue in <strong>Data Integrity</strong>. Let me explain you with an example. You approve a loan of 1000$ but when a transaction happens you get withdrawn amount 2000$ which is not possible if the approved loan is 1000$. This simple example how important business level validations are important when it comes to data. Next comes if you want to check it against some value which doesn’t exist in the warehouse and is managed by different micro-service. We do a API-sourcing here to fetch those values validate the current set of values as well as if we need few fields to be extracted from it we can enrich our data based on it.</p><h3><strong>Architecture</strong></h3><p>I wanted to give 2 designs here, First for batch based processing and Second for real-time processing as user might need it based on their needs.</p><p><strong>Design 1 : Batch processing using Airflow</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xn8MuAjBEr9JMV-5-665nQ.png" /><figcaption>batch processing</figcaption></figure><p>Here Tasks are individual nodes of a DAGs structure of Airflow. We can link them however you want and process them according to it</p><p><strong>Design 2: Real-time stream processing using Flink</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*27BDFQ8DSo2mmD8QcCS8rg.png" /><figcaption>stream-processing</figcaption></figure><p>Here we take raw-streams and use sink to store raw messages and in parallel process them however you want based on requirements. Flink also has a lot of extra features of sliding windows which also adds cherry on top of it. However doing an <strong>API call during a Flink job makes it synchronou</strong>s, to avoid this we back these values before hand into our warehouse(which makes db call synchronous but less latency) and the most efficient would be storing it in event-bus by creating a parallel stream and join streams while processing it.</p><h3>Conclusion</h3><p>Data management is not as simple as it seems, it takes a lot of components in place to store clean data without losing any information. You maybe unknowing deleting metadata which can be in very critical when scale increases. <strong><em>So think before you implement components</em></strong>. In my next blog I will take about how to store data if you have schema-less data.</p><p>Keep Designing !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=535dbcd6021b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tried to solve todays Daily Challenge problem of Leetcode using ChatGPT (Analysis and comparision)]]></title>
            <link>https://medium.com/@debashd1991/tried-to-solve-todays-daily-challenge-problem-of-leetcode-using-chatgpt-analysis-and-comparision-6ce1d41457f9?source=rss-22ecf69c6b3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ce1d41457f9</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Debashis Das]]></dc:creator>
            <pubDate>Tue, 27 Dec 2022 13:23:34 GMT</pubDate>
            <atom:updated>2022-12-27T14:19:23.391Z</atom:updated>
            <content:encoded><![CDATA[<p>I had solved the question before trying it on ChatGPT. I just copy-pasted the question. <a href="https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/">https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/</a> . This is just an analysis on the above question. I am writing this for the purpose of awareness.</p><h3><strong>Language Selection</strong></h3><p>I tried to see how good is it in terms of optimisation aspect of the code it provides. I was surprised by the solution it provided. It gave a <strong>python</strong> solution without mentioning any language to code. Which means It took decision to give solution in python without asking the user. Not sure on what parameters it selected python. If any ChatGPT developers see this maybe able to explain.</p><h3>Language Conversion</h3><p>Now I wanted to try that if I ask the solution in other language like Java, will it convert the given solution or give a completely new solution. Astonishingly it converted the same solution into Java, with same variable names same structure of coding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*w7i8knN8jsXlsKWCwQNA0g.png" /><figcaption>Python code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*0EkBQQywQF43MCI5pKgzEw.png" /><figcaption>Java code</figcaption></figure><p>You can see the comments are even same. Everything exact except language. It even gave me what was different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*89IyLmCBgY1NTneOX2rGoQ.png" /></figure><h3><strong>Code</strong></h3><p>This question can also be solved using min heap. Priority Queues(PQ) in Java helps us implement min heaps. So I tried to ask for a solution using PQ. It gave me this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*ES_1tO8dk4v98pbkIUXvrQ.png" /><figcaption>pq solution</figcaption></figure><p>Same thing happened, same variable were used, same comments where used.</p><h4>Analysis</h4><p>When you solve a question in leetcode you don’t know if your solution is better or the solution provided by the people in solutions tab. As in even if both solutions have same time complexity and space in the above case which will you say is the better solution as a developer. So I asked ChatGPT. This is what I got as a reply.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BMxwKcyUtqw6LNLlAJM3Gg.png" /><figcaption>analysis</figcaption></figure><p>So it said with PQ it is a better solution as creation takes O(n) for n elements and it takes O(logn) per element for insertion and deletion. As we are targeting to remove only few elements from PQ, until we remove all additional rocks. <strong>Which makes PQ approach a better solution </strong>even if it takes O(nlogn) time.</p><p>But here comes a question, why did it not provide the PQ solution on the first place and provided it after being asked. <em>It took a decision to give solution in Python but couldn’t give the best practical solution.</em></p><h4>Analysis (without sorting)</h4><p>If we ask for a solution for this without sorting, it forgets about the <strong>maximum</strong> aspect of the question and gives me a greedy solution, which will be correct if difference(capacity[i] — rocks[i]) is already sorted. <em>This is again a wrong assumption made.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*kDWwF2jSnD577uWQqKHCSw.png" /><figcaption>greedy solution</figcaption></figure><p>This made me sad, as in you can compromise with time/space complexity but you cannot assume a wrong parameter. It didn’t even tell me about the assumption made until I told ChatGPT about it and then it gave me the old solution again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*TiV96SGwHONkhY3ir9FjhQ.png" /></figure><h3>Conclusion</h3><p>ChatGPT is a good interactive tool but its solutions are not as practical as you think nor it talks about the assumptions it made while providing the solution to you. So if you are a beginner in software industry you might get quick and easy solutions using ChatGPT but would not know the edge cases you might miss if you use this code in production. They are not bug free.</p><p>You need to be an experienced developer to understand the bottlenecks of the solution and assumptions the code is making. This is good for R&amp;D purpose but think before you copy-paste this code in production.</p><p>Still there is time to beat humans !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ce1d41457f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>