<?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 Srujana A on Medium]]></title>
        <description><![CDATA[Stories by Srujana A on Medium]]></description>
        <link>https://medium.com/@srujanreddya76?source=rss-a68f759e520f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_R6XE7AY2wlWpjkn</url>
            <title>Stories by Srujana A on Medium</title>
            <link>https://medium.com/@srujanreddya76?source=rss-a68f759e520f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:41:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srujanreddya76/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[Why and How We Use Rust Inside Go (and Why It Works So Well)]]></title>
            <link>https://medium.com/@srujanreddya76/why-and-how-we-use-rust-inside-go-and-why-it-works-so-well-ffd00d39d79d?source=rss-a68f759e520f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffd00d39d79d</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Srujana A]]></dc:creator>
            <pubDate>Wed, 07 Jan 2026 18:17:56 GMT</pubDate>
            <atom:updated>2026-01-07T18:17:56.729Z</atom:updated>
            <content:encoded><![CDATA[<p>Go and Rust are often positioned as competitors. In practice, many high-performance teams quietly use <strong>both</strong> — and for good reason.</p><p>Go is exceptional at building services quickly: networking, concurrency, deployment, and developer experience are hard to beat. Rust, on the other hand, excels where performance, memory safety, and low-level control matter most.</p><p>This article explains <strong>how Rust can be used inside Go projects</strong>, <strong>why teams do it</strong>, and <strong>real-world cases where this hybrid approach shines</strong>.</p><h3>The Philosophy: Go for Control Flow, Rust for the Engine</h3><p>Think of Go as the <strong>control plane</strong> and Rust as the <strong>execution engine</strong>.</p><ul><li>Go handles APIs, orchestration, concurrency, and service wiring</li><li>Rust handles performance-critical, memory-sensitive, or unsafe logic</li></ul><p>This separation keeps systems clean and maintainable while pushing critical paths to native speed.</p><h3>How Rust and Go Actually Talk to Each Other</h3><p>Go cannot directly import Rust code. Instead, Rust exposes a <strong>C-compatible interface</strong>, and Go calls it using <strong>cgo</strong>.</p><p>The integration looks like this:</p><pre>Rust → C ABI → Go (via cgo)</pre><p>This approach is stable, explicit, and widely used in production systems.</p><h3>Step 1: Writing Rust Code Go Can Call</h3><p>Rust functions must use the C ABI and avoid name mangling.</p><pre>#[no_mangle]<br>pub extern &quot;C&quot; fn add(a: i32, b: i32) -&gt; i32 {<br>    a + b<br>}</pre><p>Key rules:</p><ul><li>extern &quot;C&quot; ensures ABI compatibility</li><li>#[no_mangle] preserves function names</li><li>Only C-compatible types cross the boundary</li></ul><p>The Rust library is compiled as a shared object (.so, .dylib, or .dll).</p><h3>Step 2: Importing Rust into Go with cgo</h3><p>Go uses cgo to link against the compiled Rust library.</p><pre>/*<br>#cgo LDFLAGS: -L. -lrustlib<br>#include &quot;rustlib.h&quot;<br>*/<br>import &quot;C&quot;</pre><pre>func main() {<br>    result := C.add(2, 3)<br>    println(result)<br>}</pre><p>At runtime, Go calls Rust as if it were a native library.</p><h3>Handling Strings and Memory Correctly</h3><p>This is where discipline matters.</p><p>Rust and Go have <strong>different memory models</strong>. A safe rule:</p><ul><li>The language that allocates memory must also free it</li></ul><p>Rust exposes explicit free functions, and Go must call them when done. This avoids leaks and undefined behavior.</p><h3>Real-World Case Studies</h3><h3>Case Study 1: Cloudflare — Cryptography and Networking</h3><p>Cloudflare uses Rust extensively for:</p><ul><li>TLS implementations</li><li>Packet parsing</li><li>Performance-sensitive networking logic</li></ul><p>Go services call into Rust components to:</p><ul><li>Reduce memory bugs</li><li>Achieve consistent low latency</li><li>Replace unsafe C code with safer Rust alternatives</li></ul><p>Rust provides C-compatible interfaces, making it easy to embed in existing Go and C infrastructure.</p><p><strong>Why Rust here?</strong><br> Cryptography and protocol parsing are error-prone and security-sensitive — Rust’s guarantees matter.</p><h3>Case Study 2: Figma — Performance-Critical Engines</h3><p>Figma’s backend uses multiple languages, including Rust, to power:</p><ul><li>File processing</li><li>Real-time collaboration engines</li><li>Serialization and binary formats</li></ul><p>While APIs and orchestration are handled in higher-level languages, Rust handles the core logic that must be fast, deterministic, and memory-safe.</p><p><strong>Lesson:</strong> Keep business logic expressive, push core engines to Rust.</p><h3>Case Study 3: Game Backends and Simulation Engines</h3><p>Many multiplayer game backends use:</p><ul><li>Go for matchmaking, networking, and services</li><li>Rust for physics, simulation, or authoritative game logic</li></ul><p>Rust ensures:</p><ul><li>No data races in simulations</li><li>Predictable performance under load</li><li>Safe multithreading without GC pauses</li></ul><p>Go remains ideal for horizontally scaling services around the engine.</p><h3>Case Study 4: Blockchain and Distributed Systems</h3><p>Blockchain clients and validators often:</p><ul><li>Use Rust for consensus, cryptography, and state machines</li><li>Use Go for RPC servers, tooling, and orchestration</li></ul><p>This split allows teams to:</p><ul><li>Audit critical logic more easily</li><li>Prevent memory vulnerabilities</li><li>Maintain high throughput under load</li></ul><h3>Why Teams Choose Rust Inside Go</h3><h3>1. Performance Without Sacrificing Safety</h3><p>Rust delivers C/C++-level performance with far fewer memory bugs.</p><h3>2. No Garbage Collector on Hot Paths</h3><p>Rust avoids GC pauses — critical for:</p><ul><li>Low-latency systems</li><li>Real-time workloads</li><li>Tight memory environments</li></ul><h3>3. Better Than cgo + C/C++</h3><p>Many Go projects already use cgo. Rust is simply a <strong>safer upgrade</strong>.</p><h3>4. Clean Architectural Boundaries</h3><p>The FFI boundary forces discipline:</p><ul><li>Well-defined APIs</li><li>Clear ownership rules</li><li>Reduced coupling</li></ul><p>This often improves system design rather than harming it.</p><h3>When You Should Not Do This</h3><p>Rust-Go integration is not free.</p><p>Avoid it if:</p><ul><li>Performance is not a bottleneck</li><li>The logic is simple and stable</li><li>Your team lacks Rust expertise</li><li>A pure Go library already solves the problem</li></ul><p>Sometimes simplicity wins — and that’s okay.</p><h3>Final Thoughts</h3><p>Using Rust inside Go is not about language wars. It’s about <strong>using the right tool for the right job</strong>.</p><p>Go keeps teams productive.<br> Rust keeps critical code fast and safe.</p><p>When combined thoughtfully, they form a powerful, production-proven stack used by some of the most demanding systems in the world.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffd00d39d79d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go without Context: Rethinking Cancellation and Timeout Propagation in Modern Go Systems]]></title>
            <link>https://medium.com/@srujanreddya76/go-without-context-rethinking-cancellation-and-timeout-propagation-in-modern-go-systems-ecb87c89234d?source=rss-a68f759e520f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecb87c89234d</guid>
            <dc:creator><![CDATA[Srujana A]]></dc:creator>
            <pubDate>Thu, 16 Oct 2025 16:24:53 GMT</pubDate>
            <atom:updated>2025-10-16T16:24:53.950Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>If you’ve been writing Go for a while, you’ve probably seen this line so many times it feels like a mantra:</p><pre>ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)<br>defer cancel()</pre><p>We sprinkle context.Context across our Go codebases — in HTTP handlers, database calls, and RPC clients — often without thinking twice.</p><p>But here’s the problem:<br>In modern distributed systems, <strong>context misuse has become one of the biggest sources of silent failure</strong>. Timeouts cascade across microservices, goroutines die prematurely, and debugging “context deadline exceeded” has become a rite of passage for Go engineers.</p><p>It’s time to ask a bold question:</p><blockquote><strong><em>What if we rethought Go’s context propagation model from the ground up?</em></strong></blockquote><h3>The Original Purpose of context.Context</h3><p>context.Context was designed to carry <strong>cancellation signals and request-scoped data</strong> down call chains.</p><p>The intention was simple:</p><ul><li>Avoid global variables for request-scoped data.</li><li>Allow cancellations/timeouts to propagate gracefully.</li><li>Enable observability tools to track requests end-to-end.</li></ul><p>In practice, though, this elegant mechanism has turned into a <strong>catch-all plumbing nightmare</strong>.</p><h3>The Dark Side of Context</h3><p>Let’s look at a typical Go service pattern:</p><pre>func (s *Service) HandleRequest(ctx context.Context, req *Request) (*Response, error) {<br>    ctx, cancel := context.WithTimeout(ctx, 3*time.Second)<br>    defer cancel()<br>   data, err := s.repo.Fetch(ctx, req.ID)<br>    if err != nil {<br>        return nil, err<br>    }<br>    return s.processor.Process(ctx, data)<br>}</pre><p>Looks harmless, right?<br>But this pattern is <strong>silently dangerous</strong> in distributed systems.</p><ol><li><strong>Nested timeouts</strong> — Each layer wraps a new timeout, sometimes shorter than the parent, leading to premature cancellations.</li><li><strong>Cascading failures</strong> — When one microservice cancels its context, dependent services get “context deadline exceeded,” even if they were healthy.</li><li><strong>Lack of visibility</strong> — There’s no built-in way to visualize or trace context trees across service boundaries.</li><li><strong>Context leaks</strong> — If you create a new context with context.WithCancel or context.WithTimeout and forget to call cancel(), you may leak goroutines.<br>Call it too early? You cancel important work.</li></ol><p>The result:<br>A production graph full of “random” timeout errors and sleepless debugging nights.</p><h3>Observing Context Chaos</h3><p>In large Go systems, it’s nearly impossible to answer simple questions like:</p><ul><li>Which service initiated a cancellation?</li><li>Was it a timeout or a manual cancel?</li><li>Which goroutine chain did it affect?</li></ul><p><strong>Traditional logs won’t tell you that.</strong></p><p>That’s where we need to <strong>rethink context — not just how we use it, but how we <em>observe</em> it.</strong></p><h3>Rethinking Context Propagation</h3><p>Let’s imagine a better approach:<br>A <strong>Context Manager</strong> that tracks and visualizes cancellation trees in real time.</p><p>We can wrap Go’s native context with metadata and tracing hooks.</p><pre>type ManagedContext struct {<br>    context.Context<br>    ID        string<br>    ParentID  string<br>    StartTime time.Time<br>}</pre><pre>func NewManagedContext(parent context.Context, name string) (*ManagedContext, context.CancelFunc) {<br>    ctx, cancel := context.WithCancel(parent)<br>    id := uuid.NewString()</pre><pre>    mc := &amp;ManagedContext{<br>        Context:   ctx,<br>        ID:        id,<br>        ParentID:  GetContextID(parent),<br>        StartTime: time.Now(),<br>    }</pre><pre>    go trackContextLifecycle(mc)<br>    return mc, cancel<br>}</pre><blockquote><em>⚠️ </em><strong><em>Note:</em></strong><em> This pattern is </em><strong><em>experimental</em></strong><em> and not part of Go’s standard library.<br>It’s meant to illustrate how you could add observability and metadata layers on top of native </em><em>context.Context for research or tooling purposes.</em></blockquote><p>With a small OpenTelemetry integration, we can <strong>trace context cancellations across services</strong> — something Go’s standard library doesn’t yet provide.</p><p>Now you can <strong>see</strong> when a context was created, how long it lived, and what triggered its cancellation.</p><h3>Intelligent Timeout Propagation</h3><p>Instead of blindly setting per-layer timeouts, we can build a <strong>hierarchical timeout model</strong>:</p><pre>func WithSmartTimeout(ctx context.Context, fraction float64) (context.Context, context.CancelFunc) {<br>    deadline, ok := ctx.Deadline()<br>    if !ok {<br>        return context.WithTimeout(ctx, 5*time.Second)<br>    }<br>remaining := time.Until(deadline)<br>    childTimeout := time.Duration(float64(remaining) * fraction)<br>    return context.WithTimeout(ctx, childTimeout)<br>}</pre><p>This ensures that deeper layers <strong>inherit proportional deadlines</strong>, preventing over-aggressive timeouts.</p><blockquote><strong><em>Tip:</em></strong><em> This approach demonstrates proportional time budgeting.<br>In production, make sure your timeout fractions don’t over-constrain nested operations.</em></blockquote><h3>Context Visualization</h3><p>With OTEL integration, you can visualize context propagation like this:</p><pre>Request Root Context (ID: a1b2)<br> ├── DB Fetch (ID: c3d4) — 120ms<br> ├── Cache Check (ID: e5f6) — 15ms<br> └── Downstream gRPC (ID: g7h8) — Cancelled after 300ms</pre><p>Suddenly, context becomes <strong>observable</strong> — no longer a silent destroyer of goroutines.</p><h3>Go Without Context — But With Control</h3><p>The title “Go Without Context” doesn’t mean removing it.<br>It means <em>removing blind dependence</em> on it.</p><p>A modern Go system should:</p><ol><li>Use <strong>managed contexts</strong> with metadata and tracing.</li><li>Avoid <strong>nested timeouts</strong>; propagate them intelligently.</li><li>Leverage <strong>OpenTelemetry</strong> for visualization.</li><li>Treat cancellation as a <strong>first-class event</strong>, not just an error.</li></ol><h3>A Glimpse of the Future</h3><p>There’s an open opportunity here for Go’s ecosystem:</p><ul><li>A <strong>context visualizer dashboard</strong> for live tracing.</li><li>An <strong>extended Context API</strong> for deadline budgeting.</li><li>IDE integrations to <strong>detect context misuse</strong> (like linter hints for premature cancellation).</li></ul><p>Rethinking context isn’t about syntax — it’s about <strong>making cancellation intentional, visible, and safe</strong>.</p><h3>Conclusion</h3><p>Go’s context was revolutionary in simplifying request-scoped lifecycle management.<br>But as systems have grown more complex and distributed, its simplicity has become a double-edged sword.</p><p>It’s time for a new era — <strong>context with insight</strong>.<br>Because the next time your logs say <em>“context deadline exceeded”</em>, you shouldn’t have to guess <em>why</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecb87c89234d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>