<?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 Borelli Fotso on Medium]]></title>
        <description><![CDATA[Stories by Borelli Fotso on Medium]]></description>
        <link>https://medium.com/@kaly.salas.7?source=rss-9e73ed1acb89------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tiMRTibOYCzLMf5VU78qMQ.png</url>
            <title>Stories by Borelli Fotso on Medium</title>
            <link>https://medium.com/@kaly.salas.7?source=rss-9e73ed1acb89------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 21:21:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaly.salas.7/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[5 Engineering Skills AI Can’t Replace]]></title>
            <link>https://medium.com/@kaly.salas.7/5-engineering-skills-ai-cant-replace-20b74de18218?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/20b74de18218</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Sat, 26 Sep 2026 23:01:02 GMT</pubDate>
            <atom:updated>2026-09-26T23:01:02.170Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>AI can write code. Valuable software engineering still lives in judgment, correctness, risk, and ownership.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6KiTeqKYw8i5UdZv8Vv9Ug.png" /><figcaption>5 Engineering Skills AI Can’t Replace</figcaption></figure><p>AI coding assistants and agents can now generate functions, refactor modules, write tests, explain APIs, and even operate development tools. That capability is real and useful. It is also incomplete.</p><p>Generating source code is only one slice of software engineering. Reliable systems still require reasoning about correctness, architecture, security, system behavior, trade-offs, and responsibility for production outcomes. “I can generate code faster” is not the same as “I can engineer a system that stays correct under load, fails safely, protects data, and remains maintainable six months later.”</p><p>The engineer’s role is shifting. Less pure code production. More problem-solving, system design, rigorous review, orchestration of tools and agents, and decision-making under uncertainty. The value moves toward judgment that AI can assist but cannot fully own.</p><p>Five skills become more important, not less, as code generation improves:</p><p>1. Engineering rigor <br>2. Design judgment <br>3. Security and privacy <br>4. Systems thinking <br>5. Managing AI agents</p><p>These skills reinforce one another. Rigor without design produces brittle correctness. Design without security creates elegant vulnerabilities. Systems thinking without agent management leaves you drowning in generated changes you cannot control. Together they form the durable core of engineering work that still requires a human who can be held accountable.</p><h3>1. What Makes an Engineer Valuable in the Age of AI?</h3><p>Modern coding agents can propose implementations that compile, pass basic tests, and look plausible. They can follow repository patterns when given enough context, suggest dependency updates, and iterate on feedback. That is genuine progress.</p><p>Yet production software is not a collection of plausible functions. It is a set of decisions about invariants, failure modes, resource limits, trust boundaries, and operational cost. An AI can produce a correct-looking implementation of a rate limiter. An engineer still has to decide whether the limiter belongs in the request path, how it behaves under partial failure, what happens when the shared store is unavailable, and who gets paged when the numbers are wrong.</p><p>The difference between writing code and engineering a system is the difference between producing an artifact and owning the consequences of that artifact in the real world. Review, testing, observability, and rollback are not optional extras; they are the mechanisms by which we convert generated code into something we are willing to run.</p><p>The five skills below are not a claim that AI will never improve at them. They are an engineering assessment of where judgment, context, risk management, and accountability remain hard to fully automate today, and where the cost of getting them wrong is high.</p><h3>2. Engineering Rigor</h3><p>When generation becomes cheap, the bottleneck moves to validation. Code that compiles is not necessarily code that is correct. Code that passes a few happy-path tests is not necessarily code that respects the invariants the system depends on.</p><p>Rigor means treating requirements and invariants as first-class. It means unit tests that exercise edge cases, integration tests that exercise real boundaries, property-based tests that explore the input space, negative tests that confirm rejection of invalid inputs, regression tests that lock in previously fixed bugs, and deliberate failure injection. It also means careful error handling, observability that makes failures visible, performance validation under realistic load, reproducible builds, and disciplined dependency management.</p><p>Rust helps. The type system, ownership model, `<strong>Result</strong>`/`<strong>Option</strong>`, exhaustive pattern matching, and the borrow checker eliminate entire classes of memory and concurrency errors at compile time. They do not eliminate logic errors, incorrect assumptions about external systems, or missing edge cases.</p><h4>A Subtle Bug AI Can Easily Generate</h4><p>Consider a small rate-limiting helper that an agent might produce for an HTTP service:</p><pre>use std::collections::HashMap;<br>use std::time::{Duration, Instant};<br><br>pub struct RateLimiter {<br>    limits: HashMap&lt;String, (u32, Instant)&gt;,<br>    max_requests: u32,<br>    window: Duration,<br>}<br><br>impl RateLimiter {<br>    pub fn new(max_requests: u32, window: Duration) -&gt; Self {<br>        Self {<br>            limits: HashMap::new(),<br>            max_requests,<br>            window,<br>        }<br>    }<br><br>    pub fn allow(&amp;mut self, key: &amp;str) -&gt; bool {<br>        let now = Instant::now();<br>        let entry = self.limits.entry(key.to_string()).or_insert((0, now));<br><br>        if now.duration_since(entry.1) &gt; self.window {<br>            *entry = (1, now);<br>            return true;<br>        }<br><br>        if entry.0 &lt; self.max_requests {<br>            entry.0 += 1;<br>            true<br>        } else {<br>            false<br>        }<br>    }<br>}</pre><p>The code compiles. Basic tests pass. Under concurrent access from multiple Tokio tasks, however, the `<strong>HashMap</strong>` is mutated without synchronization. Data races are undefined behavior in Rust; the program may panic, corrupt state, or silently allow extra requests.</p><p>A failing test that an experienced engineer would write early:</p><pre>#[tokio::test]<br>async fn concurrent_access_does_not_race() {<br>    use std::sync::Arc;<br>    use tokio::sync::Mutex;<br><br>    // The original implementation is not Send + Sync safely.<br>    // This test forces the issue into the open.<br>    let limiter = Arc::new(Mutex::new(RateLimiter::new(100, Duration::from_secs(1))));<br>    let mut handles = vec![];<br><br>    for i in 0..50 {<br>        let limiter = Arc::clone(&amp;limiter);<br>        handles.push(tokio::spawn(async move {<br>            let mut guard = limiter.lock().await;<br>            guard.allow(&amp;format!(&quot;key-{}&quot;, i % 5))<br>        }));<br>    }<br><br>    for h in handles {<br>        h.await.unwrap();<br>    }<br>}</pre><p>The corrected direction is either to protect the map with a `<strong>Mutex</strong>`/`<strong>RwLock</strong>`, move to a concurrent map designed for the purpose, or redesign the limiter so that each key is handled by a single task. The important point is not the specific fix; it is that the engineer owns the detection of the concurrency assumption that the generated code quietly made.</p><p>Rust’s compiler will not save you from incorrect business logic, wrong window semantics, or missing cleanup of expired keys. Those remain human responsibilities.</p><h4>How to Practice Engineering Rigor</h4><ul><li>Build a CLI tool (argument parsing, file I/O, error reporting) and drive the test suite until coverage of error paths is high.</li><li>Add a REST API with integration tests that start a real server and hit it over the network.</li><li>Introduce `<strong>proptest</strong>` for property-based testing of parsers, serializers, or rate-limiters.</li><li>Add benchmarks with `<strong>criterion</strong>` and treat regressions as failures.</li><li>Deliberately inject failures (network timeouts, disk full, poisoned locks) and verify recovery behavior.</li><li>Ask an agent to implement a feature, then spend focused time trying to break the generated code with adversarial inputs and concurrent load.</li></ul><p>Rigor is the habit of refusing to treat “<em>it compiles and the happy path works</em>” as sufficient.</p><h3>3. Design Judgment</h3><p>An agent can generate multiple plausible designs for the same problem. Choosing among them requires understanding the actual requirements, the failure modes that matter, the operational cost, and the long-term maintainability of the system.</p><p>A technically valid implementation can still be the wrong engineering decision. Synchronous code may be simpler and easier to reason about until latency or concurrency forces async. Channels may be cleaner than shared mutable state until the topology of producers and consumers changes. An in-memory store is fast until durability or multi-instance deployment becomes required. REST is familiar until the system needs event-driven fan-out. A monolith is easier to evolve until independent scaling and failure isolation become non-negotiable. A highly generic abstraction may look elegant until every call site pays for complexity that only one call site needed.</p><h4>Design Questions AI Should Not Answer Alone</h4><p>Before accepting generated architecture or code, ask:</p><ul><li>What problem are we actually solving, and for whom?</li><li>What are the realistic failure modes?</li><li>What happens under sustained load and under partial failure of dependencies?</li><li>What guarantees does this design provide (ordering, durability, consistency, latency bounds)?</li><li>What is the operational cost, CPU, memory, network, human attention?</li><li>What complexity are we introducing, and is it justified?</li><li>How difficult will this be to debug or change six months from now?</li><li>What happens when the assumptions baked into the design are violated?</li></ul><p>These questions are not philosophical. They determine whether the system will be reliable in production.</p><h4>How to Practice Design Judgment</h4><ul><li>Take a working prototype and deliberately force a design decision (sync → async, in-memory → persistent, monolith → split service). Measure the consequences.</li><li>Write a short design note before generating substantial code: problem, constraints, alternatives considered, chosen approach, and rejected alternatives with reasons.</li><li>Review generated PRs with the explicit goal of identifying design assumptions rather than style issues.</li><li>Compare two agent-generated solutions for the same feature and articulate why one is preferable in a concrete context.</li></ul><p>Judgment is the ability to say “<em>this is correct code for the wrong design.</em>”</p><h3>4. Security and Privacy</h3><p>Faster code generation increases the volume of code that must be reviewed for security properties. AI does not automatically produce secure systems. It can introduce insecure defaults, log sensitive data, grant excessive permissions, or trust inputs that should be validated.</p><p>Rust eliminates many memory-safety vulnerabilities that historically led to remote code execution. It does not eliminate broken authentication, missing authorization, injection flaws, insecure deserialization, secret leakage, or supply-chain risks.</p><h4>A Realistic Vulnerability Pattern</h4><p>An agent asked to “add a simple admin command runner” might produce something like:</p><pre>use std::process::Command;<br><br>pub fn run_admin_command(cmd: &amp;str) -&gt; Result&lt;String, std::io::Error&gt; {<br>    let output = Command::new(&quot;sh&quot;)<br>        .arg(&quot;-c&quot;)<br>        .arg(cmd)<br>        .output()?;<br>    Ok(String::from_utf8_lossy(&amp;output.stdout).to_string())<br>}</pre><p>If `<strong><em>cmd</em></strong>` comes from an authenticated but not sufficiently authorized user, or from an AI agent with broad tool access, this is command injection waiting to happen. The safer shape restricts the allowed commands to an explicit allow-list, avoids shell interpretation, and runs with the least privilege required.</p><p>Similar patterns appear with path traversal in file APIs, logging of tokens or PII, overly broad file-system permissions given to an agent, and prompt-injection paths that cause an agent to exfiltrate secrets or modify production configuration.</p><h4>A Practical Security Checklist for AI-Assisted Rust Development</h4><ul><li><strong>Dependencies</strong>: audit with `<strong><em>cargo audit</em></strong>`, prefer well-maintained crates, pin versions deliberately.</li><li><strong>Secrets</strong>: never hard-code; load from environment or a secret manager; never log them.</li><li><strong>Permissions</strong>: give agents and tools the minimum filesystem, network, and process rights required.</li><li><strong>Input validation</strong>: treat all external input as untrusted; validate early and explicitly.</li><li><strong>Filesystem</strong>: avoid user-controlled paths without canonicalization and allow-listing.</li><li><strong>Network</strong>: prefer explicit timeouts, TLS verification, and least-privilege credentials.</li><li><strong>Logging</strong>: scrub tokens, passwords, and personal data.</li><li><strong>Authentication &amp; authorization</strong>: test both positive and negative paths; prefer explicit checks over implicit trust.</li><li><strong>Testing</strong>: include security-focused tests (injection, path traversal, privilege escalation).</li><li><strong>Deployment</strong>: separate build-time and runtime secrets; review what the agent can touch in CI.</li></ul><p>Security remains a property of the whole system and the processes around it, not of the language alone.</p><h4>How to Practice Security and Privacy</h4><ul><li>Threat-model a small service before implementing it.</li><li>Give an agent deliberately limited tool permissions and observe what it can and cannot do.</li><li>Introduce a secret and verify it never appears in logs or error messages.</li><li>Run `<strong><em>cargo audit</em></strong>` and `<strong><em>cargo deny</em></strong>` as part of normal workflow.</li><li>Practice reviewing generated code specifically for trust-boundary crossings.</li></ul><h3>5. Systems Thinking</h3><p>Individual functions can be correct while the system fails. Latency compounds, retries amplify load, queues build up, connections exhaust, and partial failures cascade. Engineers increasingly need to reason about the behavior of the whole system under stress.</p><p>A common production story: an API works perfectly in development. Under load it becomes unreliable. Investigation reveals that a dependency timeout was set too high, retries were unbounded, a connection pool was sized for a single instance, and a cache stampede occurred when the cache expired. None of the individual components was “wrong”; their interactions produced the failure.</p><p>A useful mental model is the path a request actually takes:</p><p>Client → API gateway → Authentication → Service → Database → Cache → Message broker → Worker → External service</p><p>Each hop introduces latency, failure modes, and resource consumption. Backpressure, timeouts, idempotency, and circuit breaking are not optional decorations; they are how the system stays stable when parts of it degrade.</p><p>In Rust, Tokio, channels, async tasks, `<strong><em>tokio::time::timeout</em></strong>`, cancellation tokens, and careful concurrency limits become tools for expressing these system-level properties. The language does not invent the properties for you.</p><h4>How to Practice Systems Thinking</h4><ul><li>Build a small multi-component system (API + worker + queue + store) and deliberately break one component while observing the others.</li><li>Add realistic timeouts, bounded retries, and metrics; then load-test.</li><li>Trace a request end-to-end and document every place it can fail or block.</li><li>Practice reading production-like metrics and logs to reconstruct causal chains.</li></ul><p>Systems thinking is the habit of asking what happens when the happy path is no longer the common path.</p><h3>6. Managing AI Agents</h3><p>The progression is clear: autocomplete → conversational assistant → coding agent → increasingly autonomous development workflows. The skill that grows in importance is not “<strong><em>asking the AI to write code.</em></strong>” It is designing, constraining, supervising, and validating the environment in which an agent operates.</p><p>Managing an agent means:</p><ul><li>Giving clear objectives and acceptance criteria.</li><li>Breaking work into small, reviewable tasks.</li><li>Supplying precise context and repository conventions.</li><li>Defining hard constraints and tool permissions.</li><li>Sandboxing filesystem and network access.</li><li>Reviewing every generated change.</li><li>Running tests automatically and treating failures as blocking.</li><li>Validating assumptions the agent made.</li><li>Managing agent state and conversation history.</li><li>Providing clean rollback paths (branches, worktrees, CI).</li><li>Preventing destructive actions by default.</li></ul><p>There is a meaningful difference between using AI to write code and engineering a workflow in which an AI agent can safely modify a codebase.</p><h4>A Practical Rust Mini-Project: An AI Coding Harness</h4><p>A concrete way to develop all five skills at once is to build a small AI-powered coding harness in Rust. The architecture can look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3SBl81HU7NdnKoZKjSIrGg.png" /><figcaption>A Practical Rust Mini-Project: An AI Coding Harness</figcaption></figure><p>You remain responsible for:</p><ul><li>Which tools the agent may call.</li><li>Which directories it may read or write.</li><li>Whether it may run arbitrary shell commands.</li><li>How long it may iterate before human review is required.</li><li>What constitutes an acceptable change (tests pass, diff size limits, no secret patterns, etc.).</li></ul><p>Building this harness forces engineering rigor (tests around the controller), design judgment (tool interface design, state management), security (permission boundaries, secret handling), systems thinking (feedback loops, failure handling), and direct practice at managing the agent itself.</p><p>Start small: a CLI that can read a single file, propose an edit, show a diff, and apply it only after explicit approval. Then add test execution, then Git integration, then tighter sandboxing. Each increment surfaces the skills above.</p><p>Expected environment: stable Rust toolchain. Prefer well-known crates (`<strong><em>tokio</em></strong>`, `<strong><em>clap</em></strong>`, `<strong><em>reqwest</em></strong>` or an official LLM SDK, `<strong><em>git2</em></strong>` or process-based Git, `<strong><em>serde</em></strong>`) and verify current versions in their documentation rather than hard-coding them here.</p><h3>Conclusion</h3><p>AI is becoming remarkably capable at generating code. That does not diminish the importance of engineering rigor, design judgment, security and privacy, systems thinking, or the ability to manage AI agents safely. It amplifies them. Generated code still requires human validation, architectural reasoning, security review, testing, and operational ownership.</p><p>As a senior engineer who has spent years in Rust, I see the language as particularly well-suited to this environment. Its emphasis on explicitness, correctness, concurrency control, and systems-level thinking trains the same muscles that remain scarce when generation is cheap. Rust does not make you a better engineer by magic; it makes certain classes of mistakes harder and certain kinds of reasoning more natural.</p><p>If you want to stay effective in an AI-first development environment, practice the five skills deliberately. Build small systems that force you to confront failure modes. Review generated code with the same seriousness you would review a junior engineer’s work. Give agents narrow permissions and strong feedback loops. Treat production responsibility as non-transferable.</p><p>The future engineer will likely spend less time typing every line and more time defining problems, constraining solutions, validating outcomes, and owning the consequences. That is not a lesser role. It is the core of the discipline.</p><p>If you have built systems where AI-generated code met production reality, or if you have strong views on which engineering skills are becoming more critical, the comments are open. Share the experience. The conversation is more useful when it stays concrete.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20b74de18218" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Your Own Coding Harness: The Practical Path to Mastering Async Rust, LLM Agents, Tool…]]></title>
            <link>https://medium.com/@kaly.salas.7/build-your-own-coding-harness-the-practical-path-to-mastering-async-rust-llm-agents-tool-7128b7e74dc4?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/7128b7e74dc4</guid>
            <category><![CDATA[ai-engineering]]></category>
            <category><![CDATA[system-programming]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 18 Sep 2026 23:01:01 GMT</pubDate>
            <atom:updated>2026-09-18T23:01:01.892Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build Your Own Coding Harness: The Practical Path to Mastering Async Rust, LLM Agents, Tool Calling, Streaming, State Machines, and Terminal UIs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaIxG5PRGGyOwKlQB5ozrQ.png" /><figcaption>Build a Coding Harness and master 7 Keys Concepts</figcaption></figure><p>Many developers learn Rust the hard way: they grind through the Book, solve Advent of Code problems, or complete isolated exercises on ownership, lifetimes, and the borrow checker. They can write correct small programs. Then they sit down to build something real, an interactive CLI tool, a background worker, or an AI-assisted developer utility, and the ground shifts. Suddenly they need concurrent network I/O, structured error handling across layers, streaming responses, process control, and a coherent application state model. The syntax they learned no longer feels sufficient.</p><p>In my opinion, there is a better way. Instead of studying concepts in isolation, build a single, progressively evolving project that forces those concepts to appear in context. Call it a <strong>Coding Harness</strong>: a small but ambitious CLI/TUI application that starts as a simple command runner and grows into a lightweight AI agent capable of talking to an LLM, deciding when to call tools, executing controlled local processes, streaming results, managing complex interaction state, and presenting a polished terminal interface.</p><p>This project is deliberately useful. It sits at the intersection of modern systems programming and AI-powered developer tooling. As it evolves you will encounter, in order:</p><p>1. Async Rust <br>2. LLM APIs <br>3. Tool calling <br>4. Process execution + file I/O <br>5. Streaming <br>6. State machines <br>7. CLI / TUI</p><p>The architecture grows organically:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XUmlgihrHA-j2N4puqEjOw.png" /></figure><p>Each section below introduces a real problem the harness faces, explains the Rust concept that solves it, and shows concrete, idiomatic code. The examples are progressive: later sections build on earlier ones. All code targets modern Rust (edition 2021 or later) and current crate versions (tokio 1.x, reqwest 0.12/0.13 with `<strong>json</strong>` + `<strong>stream</strong>`, serde 1, clap 4, etc.). External API calls require an API key; the rest compiles and runs locally.</p><h3>1. Async Rust, The Foundation</h3><p>A coding harness must talk to network services, wait for processes, and remain responsive. Blocking the main thread on every I/O operation produces a sluggish or frozen tool. Asynchronous programming lets a single thread (or a small pool) make progress on many concurrent operations by yielding when work would otherwise block.</p><p>In Rust, `<strong>async fn</strong>` produces a `<strong>Future</strong>`. A future is a state machine that the runtime polls. `<strong>.await</strong>` suspends the current future until the awaited future is ready, without blocking the underlying OS thread. Tokio is the dominant runtime: it provides a multi-threaded work-stealing scheduler, timers, process support, and filesystem helpers. Cooperative scheduling means tasks must yield (via `<strong>.await</strong>`) so other tasks can run; a long CPU-bound loop without yielding starves the runtime. Concurrency (interleaved progress) is not the same as parallelism (simultaneous execution on multiple cores). Blocking operations (synchronous file I/O, heavy computation, or a blocking HTTP client) inside an async context are problematic because they occupy a worker thread. Prefer `<strong>tokio::fs</strong>`, `<strong>tokio::process</strong>`, and non-blocking clients, or move blocking work to `<strong>spawn_blocking</strong>`.</p><p>Error handling remains `<strong>Result</strong>`-centric; `<strong>?</strong>` works naturally across `<strong>.await</strong>` points. Cancellation is cooperative: dropping a future usually cancels it, but you must design for clean shutdown (for example, via cancellation tokens or carefully scoped tasks).</p><p>Here is a minimal async harness skeleton that demonstrates spawning concurrent work and waiting for it:</p><pre># Cargo.toml<br>[package]<br>name = &quot;coding-harness&quot;<br>version = &quot;0.1.0&quot;<br>edition = &quot;2021&quot;<br><br>[dependencies]<br>tokio = { version = &quot;1&quot;, features = [&quot;full&quot;] }<br>anyhow = &quot;1&quot;</pre><pre>// src/main.rs<br>use anyhow::Result;<br>use std::time::Duration;<br>use tokio::time::sleep;<br><br>#[tokio::main]<br>async fn main() -&gt; Result&lt;()&gt; {<br>    println!(&quot;Coding Harness — async foundation&quot;);<br><br>    // Spawn two concurrent tasks<br>    let handle1 = tokio::spawn(async {<br>        sleep(Duration::from_millis(300)).await;<br>        &quot;task-1 finished&quot;<br>    });<br><br>    let handle2 = tokio::spawn(async {<br>        sleep(Duration::from_millis(150)).await;<br>        &quot;task-2 finished&quot;<br>    });<br><br>    // Await both; the runtime interleaves them<br>    let (r1, r2) = tokio::try_join!(handle1, handle2)?;<br>    println!(&quot;{} | {}&quot;, r1, r2);<br><br>    // Demonstrate a non-blocking “heartbeat”<br>    for i in 1..=3 {<br>        println!(&quot;tick {i}&quot;);<br>        sleep(Duration::from_millis(100)).await;<br>    }<br><br>    Ok(())<br>}</pre><p>The important points: `<strong>#[tokio::main]</strong>` sets up the runtime; `<strong>tokio::spawn</strong>` creates independent tasks; `<strong>try_join!</strong>` waits for multiple futures; and every `<strong>.await</strong>` is a yield point. This is the substrate everything else builds on. Use async when you have concurrent I/O or many waiting operations; prefer ordinary synchronous code for pure CPU work or very simple scripts.</p><h3>2. LLM APIs, Talking to Models</h3><p>The harness needs to send prompts and receive completions. An LLM API is typically a JSON-over-HTTP endpoint (OpenAI-compatible chat completions, Anthropic, local servers, etc.). You need an HTTP client, serialization, authentication, timeouts, and clear separation between domain logic and provider details.</p><p>`<strong>reqwest</strong>` is the standard async HTTP client. Pair it with `<strong>serde</strong>` for typed request/response bodies. Keep secrets in environment variables. Prefer a thin client abstraction so the rest of the application never hard-codes a particular provider.</p><pre>[dependencies]<br>tokio = { version = &quot;1&quot;, features = [&quot;full&quot;] }<br>reqwest = { version = &quot;0.12&quot;, features = [&quot;json&quot;] }  # or 0.13<br>serde = { version = &quot;1&quot;, features = [&quot;derive&quot;] }<br>serde_json = &quot;1&quot;<br>anyhow = &quot;1&quot;</pre><pre>use anyhow::{Context, Result};<br>use reqwest::Client;<br>use serde::{Deserialize, Serialize};<br>use std::env;<br>use std::time::Duration;<br><br>#[derive(Serialize)]<br>struct ChatRequest {<br>    model: String,<br>    messages: Vec&lt;Message&gt;,<br>    stream: bool,<br>}<br><br>#[derive(Serialize, Deserialize, Clone)]<br>struct Message {<br>    role: String,<br>    content: String,<br>}<br><br>#[derive(Deserialize)]<br>struct ChatResponse {<br>    choices: Vec&lt;Choice&gt;,<br>}<br><br>#[derive(Deserialize)]<br>struct Choice {<br>    message: Message,<br>}<br><br>pub struct LlmClient {<br>    client: Client,<br>    api_key: String,<br>    base_url: String,<br>}<br><br>impl LlmClient {<br>    pub fn from_env() -&gt; Result&lt;Self&gt; {<br>        let api_key = env::var(&quot;OPENAI_API_KEY&quot;)<br>            .context(&quot;OPENAI_API_KEY must be set&quot;)?;<br>        let base_url = env::var(&quot;LLM_BASE_URL&quot;)<br>            .unwrap_or_else(|_| &quot;https://api.openai.com/v1&quot;.into());<br><br>        let client = Client::builder()<br>            .timeout(Duration::from_secs(60))<br>            .build()?;<br><br>        Ok(Self {<br>            client,<br>            api_key,<br>            base_url,<br>        })<br>    }<br><br>    pub async fn complete(&amp;self, messages: Vec&lt;Message&gt;) -&gt; Result&lt;String&gt; {<br>        let req = ChatRequest {<br>            model: &quot;gpt-4o-mini&quot;.into(), // or any compatible model<br>            messages,<br>            stream: false,<br>        };<br><br>        let resp = self<br>            .client<br>            .post(format!(&quot;{}/chat/completions&quot;, self.base_url))<br>            .bearer_auth(&amp;self.api_key)<br>            .json(&amp;req)<br>            .send()<br>            .await<br>            .context(&quot;LLM request failed&quot;)?<br>            .error_for_status()<br>            .context(&quot;LLM returned error status&quot;)?;<br><br>        let body: ChatResponse = resp.json().await?;<br>        let content = body<br>            .choices<br>            .into_iter()<br>            .next()<br>            .map(|c| c.message.content)<br>            .unwrap_or_default();<br><br>        Ok(content)<br>    }<br>}<br><br>#[tokio::main]<br>async fn main() -&gt; Result&lt;()&gt; {<br>    let llm = LlmClient::from_env()?;<br>    let messages = vec![Message {<br>        role: &quot;user&quot;.into(),<br>        content: &quot;Explain why Rust ownership prevents data races in one sentence.&quot;.into(),<br>    }];<br>    let answer = llm.complete(messages).await?;<br>    println!(&quot;{}&quot;, answer);<br>    Ok(())<br>}</pre><p>Domain code only sees `<strong>LlmClient</strong>` and `<strong>Message</strong>`. Provider specifics (URL, auth header shape, model name) stay inside the client. Timeouts and `<strong>error_for_status</strong>` turn transport and HTTP errors into application `<strong>Result</strong>`s. This separation is essential once tool calling and streaming appear.</p><h3>3. Tool Calling, Turning the Model into an Agent</h3><p>A pure chat interface is limited. Tool calling (function calling) lets the model decide to invoke capabilities the harness provides, listing files, reading source, running tests, then continue reasoning with the results.</p><p>The loop is:</p><pre>User prompt<br>  → LLM (with tool schemas)<br>  → tool decision (name + arguments)<br>  → dispatcher<br>  → tool execution<br>  → tool result fed back to LLM<br>  → final response (or another tool call)</pre><p>Define a `<strong>Tool</strong>` trait, validate arguments, and enforce least privilege. Never give an unrestricted shell to an LLM without heavy sandboxing and allow-lists.</p><pre>use anyhow::{bail, Result};<br>use async_trait::async_trait;<br>use serde_json::Value;<br>use std::collections::HashMap;<br>use std::path::{Path, PathBuf};<br>use std::sync::Arc;<br><br>#[async_trait]<br>pub trait Tool: Send + Sync {<br>    fn name(&amp;self) -&gt; &amp;str;<br>    fn description(&amp;self) -&gt; &amp;str;<br>    fn parameters_schema(&amp;self) -&gt; Value; // JSON Schema fragment<br>    async fn execute(&amp;self, args: Value) -&gt; Result&lt;String&gt;;<br>}<br><br>pub struct ListFilesTool {<br>    root: PathBuf,<br>}<br><br>#[async_trait]<br>impl Tool for ListFilesTool {<br>    fn name(&amp;self) -&gt; &amp;str { &quot;list_files&quot; }<br>    fn description(&amp;self) -&gt; &amp;str { &quot;List files under a controlled directory&quot; }<br>    fn parameters_schema(&amp;self) -&gt; Value {<br>        serde_json::json!({<br>            &quot;type&quot;: &quot;object&quot;,<br>            &quot;properties&quot;: {<br>                &quot;path&quot;: { &quot;type&quot;: &quot;string&quot;, &quot;description&quot;: &quot;Relative path&quot; }<br>            },<br>            &quot;required&quot;: [&quot;path&quot;]<br>        })<br>    }<br>    async fn execute(&amp;self, args: Value) -&gt; Result&lt;String&gt; {<br>        let rel = args[&quot;path&quot;].as_str().unwrap_or(&quot;.&quot;);<br>        let target = self.root.join(rel);<br>        if !target.starts_with(&amp;self.root) {<br>            bail!(&quot;path escapes allowed root&quot;);<br>        }<br>        let mut entries = Vec::new();<br>        let mut rd = tokio::fs::read_dir(&amp;target).await?;<br>        while let Some(e) = rd.next_entry().await? {<br>            entries.push(e.file_name().to_string_lossy().into_owned());<br>        }<br>        Ok(entries.join(&quot;\n&quot;))<br>    }<br>}<br><br>pub struct ToolRegistry {<br>    tools: HashMap&lt;String, Arc&lt;dyn Tool&gt;&gt;,<br>}<br><br>impl ToolRegistry {<br>    pub fn new() -&gt; Self {<br>        let mut tools = HashMap::new();<br>        let list = Arc::new(ListFilesTool {<br>            root: PathBuf::from(&quot;.&quot;), // tighten in real use<br>        });<br>        tools.insert(list.name().to_string(), list);<br>        Self { tools }<br>    }<br><br>    pub fn schemas(&amp;self) -&gt; Vec&lt;Value&gt; {<br>        self.tools.values().map(|t| {<br>            serde_json::json!({<br>                &quot;type&quot;: &quot;function&quot;,<br>                &quot;function&quot;: {<br>                    &quot;name&quot;: t.name(),<br>                    &quot;description&quot;: t.description(),<br>                    &quot;parameters&quot;: t.parameters_schema()<br>                }<br>            })<br>        }).collect()<br>    }<br><br>    pub async fn dispatch(&amp;self, name: &amp;str, args: Value) -&gt; Result&lt;String&gt; {<br>        let tool = self.tools.get(name).context(&quot;unknown tool&quot;)?;<br>        tool.execute(args).await<br>    }<br>}</pre><p>The registry turns tool selection into a simple lookup. Argument validation and path confinement happen inside each tool. In a full agent loop you would send the tool schemas with the chat request, parse any `<strong>tool_calls</strong>` from the response, execute them, append the results as tool-role messages, and continue until the model produces a final answer. Security is non-negotiable: treat every tool argument as untrusted input.</p><h3>4. Process Execution + File I/O</h3><p>A coding harness must interact with the local project: read source, write patches, run `<strong>cargo check</strong>`, `<strong>cargo test</strong>`, or `<strong>rustfmt</strong>`. Prefer `<strong>tokio::fs</strong>` for async file work and `<strong>tokio::process::Command</strong>` for external programs so you do not block the runtime.</p><pre>use anyhow::{Context, Result};<br>use std::process::Stdio;<br>use tokio::process::Command;<br><br>pub async fn run_cargo_check(workdir: &amp;std::path::Path) -&gt; Result&lt;String&gt; {<br>    let output = Command::new(&quot;cargo&quot;)<br>        .arg(&quot;check&quot;)<br>        .arg(&quot;--message-format=short&quot;)<br>        .current_dir(workdir)<br>        .stdout(Stdio::piped())<br>        .stderr(Stdio::piped())<br>        .output()<br>        .await<br>        .context(&quot;failed to spawn cargo&quot;)?;<br><br>    let stdout = String::from_utf8_lossy(&amp;output.stdout);<br>    let stderr = String::from_utf8_lossy(&amp;output.stderr);<br>    let combined = format!(&quot;{stdout}{stderr}&quot;);<br><br>    if !output.status.success() {<br>        // Surface the failure; do not swallow it<br>        anyhow::bail!(&quot;cargo check failed:\n{combined}&quot;);<br>    }<br>    Ok(combined)<br>}<br><br>pub async fn read_source(path: &amp;std::path::Path) -&gt; Result&lt;String&gt; {<br>    tokio::fs::read_to_string(path)<br>        .await<br>        .with_context(|| format!(&quot;reading {}&quot;, path.display()))<br>}</pre><p>Always capture both stdout and stderr, inspect the exit status, and propagate failures. When an LLM suggests a command, never execute it blindly, apply an allow-list (`<strong>cargo check</strong>`, `<strong>cargo test</strong>`, `<strong>rustfmt-check</strong>`, etc.), set a working directory, and impose a timeout (`<strong>tokio::time::timeout</strong>`). Process execution combined with tool calling is powerful; it is also the highest-risk surface of the harness.</p><h3>5. Streaming, Incremental Feedback</h3><p>Waiting for a complete LLM response or a long-running `<strong>cargo test</strong>` feels broken. Streaming delivers chunks as they arrive so the UI can update immediately.</p><p>HTTP streaming (Server-Sent Events or raw chunked transfer) + Rust’s `<strong>Stream</strong>` trait (from `<strong>futures</strong>` / `<strong>tokio-stream</strong>`) give you back-pressure-aware, asynchronous iteration. The mental model changes from “<strong>request → complete body → display</strong>” to “<strong>request → chunk → render → chunk → render …</strong>”.</p><pre>use futures_util::StreamExt;<br>use reqwest::Client;<br>use serde_json::Value;<br><br>pub async fn stream_completion(<br>    client: &amp;Client,<br>    api_key: &amp;str,<br>    body: Value,<br>) -&gt; Result&lt;()&gt; {<br>    let response = client<br>        .post(&quot;https://api.openai.com/v1/chat/completions&quot;)<br>        .bearer_auth(api_key)<br>        .json(&amp;body) // body must contain &quot;stream&quot;: true<br>        .send()<br>        .await?<br>        .error_for_status()?;<br><br>    let mut stream = response.bytes_stream();<br><br>    while let Some(item) = stream.next().await {<br>        let chunk = item?;<br>        let text = String::from_utf8_lossy(&amp;chunk);<br>        // OpenAI-style SSE: lines starting with &quot;data: &quot;<br>        for line in text.lines() {<br>            if let Some(data) = line.strip_prefix(&quot;data: &quot;) {<br>                if data == &quot;[DONE]&quot; {<br>                    return Ok(());<br>                }<br>                if let Ok(v) = serde_json::from_str::&lt;Value&gt;(data) {<br>                    if let Some(delta) = v[&quot;choices&quot;][0][&quot;delta&quot;][&quot;content&quot;].as_str() {<br>                        print!(&quot;{delta}&quot;);<br>                        // flush so the terminal updates immediately<br>                        use std::io::Write;<br>                        let _ = std::io::stdout().flush();<br>                    }<br>                }<br>            }<br>        }<br>    }<br>    Ok(())<br>}</pre><p>Enable the `<strong>stream</strong>` feature on reqwest. Handle partial JSON, connection drops, and the terminal `<strong>[DONE]</strong>` marker. The same pattern applies to streaming process output (`<strong>ChildStdout</strong>` as an async reader) or log lines. Streaming is what makes an interactive agent feel alive.</p><h3>6. State Machines, Explicit Control Flow</h3><p>By this point the harness has many concurrent concerns: idle waiting for input, sending a request, receiving a stream, deciding on a tool call, executing the tool, feeding the result back, handling errors, and finishing. Scattered boolean flags (`<strong>is_streaming</strong>`, `<strong>pending_tool</strong>`, …) become unmaintainable. An explicit state machine restores clarity.</p><p>Rust enums are perfect for this:</p><pre>#[derive(Debug, Clone)]<br>pub enum AppState {<br>    Idle,<br>    SendingRequest,<br>    ReceivingStream,<br>    ToolCallRequested { name: String, args: serde_json::Value },<br>    ExecutingTool { name: String },<br>    ReceivingToolResult,<br>    Completed { final_answer: String },<br>    Failed { error: String },<br>}<br><br>#[derive(Debug)]<br>pub enum Event {<br>    UserInput(String),<br>    LlmChunk(String),<br>    LlmFinished,<br>    ToolDecision { name: String, args: serde_json::Value },<br>    ToolFinished(String),<br>    Error(String),<br>    Quit,<br>}<br><br>pub fn transition(state: AppState, event: Event) -&gt; (AppState, Option&lt;Command&gt;) {<br>    match (state, event) {<br>        (AppState::Idle, Event::UserInput(prompt)) =&gt; {<br>            (AppState::SendingRequest, Some(Command::CallLlm(prompt)))<br>        }<br>        (AppState::SendingRequest, Event::LlmChunk(_)) =&gt; {<br>            (AppState::ReceivingStream, None)<br>        }<br>        (AppState::ReceivingStream, Event::ToolDecision { name, args }) =&gt; {<br>            (AppState::ToolCallRequested { name: name.clone(), args: args.clone() },<br>             Some(Command::ExecuteTool { name, args }))<br>        }<br>        (AppState::ExecutingTool { .. }, Event::ToolFinished(result)) =&gt; {<br>            (AppState::ReceivingToolResult, Some(Command::SendToolResult(result)))<br>        }<br>        (AppState::ReceivingStream, Event::LlmFinished) =&gt; {<br>            (AppState::Completed { final_answer: String::new() }, None)<br>        }<br>        (_, Event::Error(e)) =&gt; (AppState::Failed { error: e }, None),<br>        (_, Event::Quit) =&gt; (AppState::Completed { final_answer: String::new() }, None),<br>        (s, _) =&gt; (s, None), // ignore illegal transitions or log them<br>    }<br>}<br><br>pub enum Command {<br>    CallLlm(String),<br>    ExecuteTool { name: String, args: serde_json::Value },<br>    SendToolResult(String),<br>}</pre><p>Exhaustive `<strong>match</strong>` forces you to consider every combination. Side effects are expressed as `<strong>Command</strong>`s that the runtime executes; the pure transition function stays easy to unit-test. Failure states are first-class. This pattern scales cleanly as more events appear.</p><h3>7. CLI / TUI, The Human Interface</h3><p>A pure library is invisible. The harness needs a usable front-end.</p><p><strong>CLI</strong> is ideal for one-shot or scripted use:</p><pre>use clap::{Parser, Subcommand};<br><br>#[derive(Parser)]<br>#[command(name = &quot;harness&quot;, about = &quot;AI-assisted coding harness&quot;)]<br>struct Cli {<br>    #[command(subcommand)]<br>    command: Commands,<br>}<br><br>#[derive(Subcommand)]<br>enum Commands {<br>    Ask { prompt: String },<br>    Check,<br>    // ...<br>}<br><br>fn main() {<br>    let cli = Cli::parse();<br>    // dispatch into the async runtime and state machine<br>}</pre><p><strong>TUI</strong> (ratatui + crossterm) is appropriate when the interaction is ongoing: streaming tokens, live tool status, scrollable history, key bindings. A typical architecture keeps a rendering loop and an event loop (or a single `<strong>tokio::select!</strong>` that multiplexes terminal events, timers, and background task results). The UI never owns the business logic; it only reflects `<strong>AppState</strong>` and emits `<strong>Event</strong>`s.</p><p>A minimal ratatui sketch (conceptually) draws the current state, the partial stream buffer, and a status line, then reacts to key presses by sending events into the same state machine used by the headless path. The separation keeps the core testable and the UI replaceable.</p><h3>Integrating the Seven Concepts</h3><p>Putting it together yields a clean module layout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ugLA1iklTv-rDsIFiLEYsA.png" /><figcaption>Project Structure</figcaption></figure><p>Responsibilities are clear: the UI or CLI produces events; the state machine decides the next state and commands; the LLM, tool, and process layers execute those commands asynchronously and feed results back as new events; streaming keeps the UI responsive; Tokio runs everything. Separation of concerns makes each piece independently understandable and testable.</p><h3>Production Engineering Lessons</h3><p>Building this harness forces you to confront the topics that appear in real Rust systems:</p><ul><li>Idiomatic error handling with `<strong>thiserror</strong>` / `<strong>anyhow</strong>` and context</li><li>Ownership and borrowing across async boundaries (`<strong>Arc</strong>`, careful lifetimes)</li><li>Traits for abstraction (Tool, perhaps a Provider trait)</li><li>Enums for state and errors</li><li>Async boundaries and when to `<strong>spawn</strong>` vs `<strong>spawn_blocking</strong>`</li><li>Configuration via environment + cli</li><li>Observability (tracing spans around LLM calls and tool executions)</li><li>Security (path confinement, command allow-lists, secret hygiene)</li><li>Dependency discipline and feature flags</li></ul><p>You learn these not as isolated checklist items but as solutions to concrete problems the project creates. That is far more durable than memorizing the reference.</p><h3>Testing and Verification</h3><p>Unit-test pure transitions. Integration-test tools against a temporary directory. Mock the LLM client for deterministic agent-loop tests. For streaming, feed synthetic byte chunks. Never claim a snippet “<strong>was tested</strong>” unless it has been; the examples above are written to be consistent with current crate APIs and should compile once the obvious environment (API key, cargo on PATH) is present. Always re-check crate documentation before publishing production code — APIs evolve.</p><h3>Conclusion</h3><p>The seven concepts, async Rust, LLM APIs, tool calling, process + file I/O, streaming, state machines, and CLI/TUI, are not a random curriculum. They are the natural layers of a modern interactive systems tool. By growing a single Coding Harness you encounter each of them at the moment it becomes necessary, see why the Rust solution fits, and internalize the patterns that production code actually uses.</p><p>Isolated exercises teach syntax. Building something that must stay responsive under network latency, must safely execute tools suggested by a non-deterministic model, must stream results, and must present a coherent interface teaches engineering.</p><p>My professional recommendation is simple: pick a small but ambitious project that sits at the intersection of systems programming and the problems you care about, then let the project dictate the next concept you learn. A coding harness is an especially high-leverage choice because it combines concurrency, external process control, structured AI interaction, and terminal UX, skills that transfer directly to agents, developer tools, observability UIs, and many backend services.</p><p>Fork the ideas, tighten the security model, add your own tools, replace the TUI, or target a different LLM provider. Then come back and tell the rest of us what you discovered, which concepts you would insert next, and how the design changed under real use. The Rust community gets stronger when people share the concrete systems they actually built, not just the toy programs they completed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7128b7e74dc4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing a Scalable Real-Time Stock Price Alert System: Architecture Decisions Explained]]></title>
            <link>https://medium.com/@kaly.salas.7/designing-a-scalable-real-time-stock-price-alert-system-architecture-decisions-explained-d58fdf38eba9?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/d58fdf38eba9</guid>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 04 Sep 2026 23:01:01 GMT</pubDate>
            <atom:updated>2026-09-04T23:01:01.837Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>How experienced architects think about latency, scale, and reliability when millions of users want to know the moment their stock hits a target price.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*06nvr9NSEPO03BH1nBLBKQ.png" /><figcaption>Stock Price Alerts — High Level Architecture</figcaption></figure><h3>Introduction</h3><p>Imagine you own shares of Tesla. <br>You don’t want to stare at charts all day. You simply want to create an alert:</p><blockquote>“If TSLA reaches $300, send me a push notification and an email.”</blockquote><p>Seems trivial. Until you try to support it at scale.</p><p>Now multiply that by one million users. <br>Add tens of thousands of stocks and ETFs. <br>Feed the system a continuous firehose of real-time market data from exchanges. <br>Require sub-second evaluation latency when the market becomes volatile. <br>Guarantee that notifications are delivered reliably across email, SMS, push, and webhooks, without spamming users or creating duplicates.</p><p>Suddenly this “simple” feature becomes a classic distributed-systems problem involving high throughput, low latency, consistency trade-offs, and careful separation of concerns.</p><p>In this article I’ll walk you through how I would design such a system. <br>I’ll show you the architecture (the same one illustrated in the diagram above), explain every major design decision, and more importantly, teach you the <strong>thinking process</strong> behind those decisions. This is the kind of reasoning senior architects apply when they design platforms at companies like Bloomberg, Robinhood, or Uber.</p><p>We’ll cover microservices boundaries, event-driven patterns, polyglot persistence, caching strategies, backpressure, and the inevitable trade-offs. By the end you should be able to look at any real-time alerting system and understand not just <strong>what</strong> was built, but <strong>why</strong>.</p><h3>Business Requirements</h3><h4>Functional Requirements</h4><ul><li>Users can create, update, enable, disable, and delete price alerts.</li><li>Alerts support simple rules (“price &gt; $X”) and potentially more complex ones later.</li><li>Notifications must be delivered through multiple channels: email, SMS, push, and webhooks.</li><li>Market data arrives in real time from external providers (exchanges, market-data APIs, third-party feeds).</li><li>The system must evaluate every relevant alert against every incoming price tick.</li></ul><h4>Non-Functional Requirements</h4><p>These are the ones that actually drive the architecture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*XMNtmIJATnuhl73gs68E9Q.png" /></figure><p><strong>Key insight: </strong><br>If you only designed for the functional requirements, you could build a monolith that polls a database every few seconds. The non-functional requirements force you into an event-driven, polyglot, horizontally scalable architecture.</p><h3>Architecture Overview</h3><p>Look at the high-level diagram.</p><p>We have four major zones:</p><p>1. <strong>Clients: </strong>web, mobile, email/notification preferences.<br>2. <strong>Application Services (Backend): </strong>the five core microservices that own business logic.<br>3. <strong>Data &amp; Infrastructure:</strong> the storage and messaging layer (relational DB, time-series DB, Redis, Kafka, object storage).<br>4. <strong>Notification Channels</strong> + <strong>Monitoring &amp; Operations</strong>.</p><p>Responsibilities are deliberately isolated. <br>Each service owns a bounded context and communicates with others only through well-defined interfaces (synchronous HTTP for user-facing flows, asynchronous events for everything else).</p><p>This is not “microservices for the sake of microservices.” <br>It is the result of asking, for every piece of work:</p><ul><li>Who owns this data?</li><li>How often does this change?</li><li>What is the blast radius if this component fails?</li><li>Can this scale independently?</li></ul><p>Let’s examine each component.</p><h3>Walk-Through of Every Component</h3><h4>API Gateway</h4><p><strong>Responsibility:</strong><br>Single entry point for all client traffic. Handles authentication, rate limiting, request routing, and protocol translation.</p><p><strong>Why it exists:</strong><br>Without a gateway, every service would have to implement auth, rate limiting, and TLS termination. That duplicates code and creates inconsistent security posture.</p><p><strong>Why it should not do more:</strong> <br>The gateway must stay thin. Business logic belongs in domain services. Putting alert validation or price evaluation inside the gateway turns it into a god object and destroys independent deployability.</p><p><strong>Architecture tip:</strong> <br>Prefer a lightweight gateway (Kong, Envoy, AWS API Gateway, or even a simple Spring Cloud Gateway) over a heavyweight ESB-style gateway. The latter becomes a bottleneck and a single point of failure.</p><p><strong>Common mistake:</strong><br>Teams sometimes put business rules in the gateway “for convenience.” Six months later the gateway is the hardest service to change.</p><h4>User &amp; Alert Service</h4><p><strong>Responsibility:</strong> <br>User management, CRUD for alerts, alert preferences.</p><p><strong>Why it exists:</strong><br>User identity and alert configuration are tightly coupled. They change at human speed (not market speed). Keeping them together reduces the number of distributed transactions during the create/update path.</p><p><strong>Why another service should not own this:</strong><br>If the Alert Evaluation Engine owned user data, a spike in market volatility could overwhelm the service that also handles password resets and preference changes. Separation protects the critical user-facing path.</p><h4>Alert Management Service</h4><p><strong>Responsibility:</strong><br>Validate alert rules, store and manage alert definitions, enable/disable alerts.</p><p><strong>Why a separate service from User &amp; Alert Service?:</strong><br>Validation and lifecycle management of rules is more complex than simple CRUD. Rules may involve expression languages, market-hours constraints, or multi-condition logic. Isolating this logic keeps the User service focused on identity and preferences.</p><p>In practice many teams start with one service and later extract this one when the rule engine grows.</p><h4>Market Data Ingestion Service</h4><p><strong>Responsibility:</strong> <br>Consume real-time feeds from exchanges and third-party providers, normalize the data, enrich it (corporate actions, currency conversion, etc.), and publish clean price updates onto the message bus.</p><p><strong>Why it exists:</strong><br>Market data is messy. Different providers use different symbols, timestamps, and quality levels. You### Alert Evaluation Engine never want the rest of the system to deal with that mess.</p><p><strong>Why it should not evaluate alerts:</strong><br>Ingestion must stay as fast and simple as possible. Adding evaluation logic here would couple the data pipeline to business rules and make it harder to scale or swap providers.</p><h4>Alert Evaluation Engine</h4><p><strong>Responsibility:</strong><br>Consume price updates, load the relevant active alerts (from cache or DB), evaluate rules, and emit alert events when conditions are met. Also handles deduplication and throttling.</p><p><strong>This is the heart of the system:</strong><br>Everything else exists to feed this engine clean data and to deliver the results of its decisions.</p><p><strong>Why it is separate</strong><br>Evaluation is CPU- and memory-intensive and must scale independently of user-facing services. During a market crash you may need hundreds of evaluation instances while the rest of the system stays quiet.</p><h4>Notification Services (Email, SMS, Push, Webhook)</h4><p><strong>Responsibility:</strong><br>Deliver the final message to the user through the chosen channel.</p><p><strong>Why multiple services or a notification gateway?:</strong><br>Each channel has different reliability characteristics, rate limits, and cost models. Isolating them allows independent retries, circuit breakers, and provider switches (e.g., Twilio → another SMS provider).</p><h3>Architecture Patterns</h3><h4>Microservices + Bounded Contexts</h4><p>We split the system along natural business capabilities rather than technical layers. <br>Each service can be owned by a small team, deployed independently, and scaled according to its own load profile.</p><p><strong>Trade-off box:</strong><br>Microservices give you independent scaling and team autonomy at the cost of operational complexity, network latency, and distributed debugging. For a system of this size the benefits outweigh the costs. For a side project with 1 000 users they would not.</p><h4>API Gateway Pattern</h4><p>Centralizes cross-cutting concerns. <br>Clients talk to one stable endpoint; the backend can evolve freely.</p><h4>Event-Driven Architecture + Publish/Subscribe</h4><p>Price updates and alert events travel through Kafka topics. <br>Producers and consumers are completely decoupled. New consumers (analytics, auditing, ML feature stores) can be added without touching existing code.</p><p><strong>Why not synchronous REST between services?: </strong><br>A price tick arriving at 10 000 events per second would overwhelm any synchronous chain. Asynchronous messaging provides natural backpressure and buffering.</p><h4>Producer-Consumer + Stream Processing</h4><p>The Market Data Ingestion Service is a pure producer. <br>The Alert Evaluation Engine is a pure consumer. <br>Kafka’s consumer groups allow us to scale the evaluation side horizontally simply by adding more instances.</p><h4>Caching (Redis)</h4><p>Active alerts and latest prices live in Redis. <br>Evaluation becomes an in-memory operation for the vast majority of ticks.</p><p><strong>What should be cached:</strong></p><ul><li>Active alert rules (keyed by symbol)</li><li>Latest price per symbol</li><li>Rate-limit counters</li></ul><p><strong>What should not be cached:</strong></p><ul><li>Historical time-series data</li><li>User audit logs</li><li>Anything that must survive a full Redis restart without recovery logic</li></ul><h4>Database-per-Service</h4><p>Each service owns its data store. <br>User &amp; Alert Service → Relational DB <br>Market data → Time-series DB + Kafka <br>Evaluation state → Redis</p><p>This prevents the classic “shared database becomes a bottleneck and a coupling magnet.”</p><h4>Rate Limiting, Idempotency, and Throttling</h4><ul><li>API Gateway rate-limits user requests.</li><li>Evaluation Engine deduplicates and throttles alerts so a rapidly oscillating stock does not generate thousands of notifications.</li><li>Notification services are idempotent (same alert event ID produces the same delivery outcome).</li></ul><h4>Observability Pattern</h4><p>Every service emits metrics, structured logs, and distributed traces. <br>Without this, debugging a missed alert in production is nearly impossible.</p><h3>Data Layer: Why Polyglot Persistence?</h3><h4>Relational Database (Users, Alerts, Preferences, Audit Logs)</h4><p><strong>Why relational?:</strong><br>Strong consistency, rich querying, ACID transactions for user data, and mature tooling for backups and schema migrations. <br>Users and alerts change infrequently relative to market data. Relational is perfect.</p><h4>Time-Series Database</h4><p><strong>Why not PostgreSQL for prices?: </strong><br>Stock ticks are high-write, high-volume, append-only. A general-purpose RDBMS will struggle with the write throughput and the time-based queries (“give me the last 1 000 ticks for AAPL”). <br>Time-series databases (TimescaleDB, InfluxDB, QuestDB, etc.) are optimized for exactly this access pattern: compression, downsampling, and efficient range scans.</p><h4>Redis (Cache / In-Memory Store)</h4><p><strong>Why Redis?:</strong><br>Sub-millisecond reads for the evaluation hot path. <br>Active alerts must be loaded and evaluated against every relevant tick. Hitting a relational database on every tick would kill latency and throughput.</p><h4>Kafka (Message Queue / Stream)</h4><p><strong>Why Kafka instead of direct calls or a simpler queue?:</strong></p><ul><li>Durable, ordered log</li><li>Consumer groups for parallel processing</li><li>Ability to replay events for debugging or reprocessing</li><li>Natural backpressure</li></ul><p>Kafka turns the system into a pipeline of independent stages that can be scaled and evolved separately.</p><h4>Object Storage</h4><p>Historical data, backups, and large exports live in cheap, durable object storage (S3-compatible). <br>You never want expensive primary databases to hold years of tick history.</p><h3>End-to-End Request Flow</h3><p>Let’s walk through the complete lifecycle using the numbered steps from the diagram.</p><p>1. <strong>Client creates an alert</strong><br> “If stock X &gt; €40, send me an email.” <br> Request hits the API Gateway over HTTPS.</p><p>2. <strong>API Gateway</strong> authenticates the user, applies rate limiting, and routes the request to the User &amp; Alert Service / Alert Management Service.</p><p>3. <strong>Alert is validated and stored</strong> <br> The Alert Management Service validates the rule, writes it to the Relational DB, and (asynchronously) updates the active-alert cache in Redis. <br> The synchronous path returns a success response to the client quickly.</p><p>4. <strong>Market Data Ingestion Service</strong> continuously consumes real-time feeds, normalizes them, and publishes clean price-update events onto the Kafka “Market Data Stream” topic.</p><p>5. <strong>Alert Evaluation Engine</strong> consumes price updates. <br> For each symbol it loads the set of active alerts from Redis (or falls back to the DB). <br> It evaluates every rule against the new price.</p><p>6. <strong>When a condition is met</strong>, the engine produces an “Alert Event” onto the Kafka “Alert Events Stream”. <br> Deduplication and throttling logic run here so the same condition does not fire repeatedly within a short window.</p><p>7. <strong>Notification services</strong> consume the alert event and deliver it through the appropriate channel (email, SMS, push, webhook). <br> Each channel service is responsible for its own retries and provider-specific logic.</p><p>8. <strong>Everything is monitored: </strong>metrics, logs, traces, and health checks — so the operations team can detect and respond to issues before users notice.</p><p><strong>Synchronous vs Asynchronous</strong> <br>User-facing CRUD is synchronous (low latency, strong consistency). <br>Everything that happens after the price tick is asynchronous. That is the key to absorbing market spikes.</p><h3>Scalability Discussion</h3><p><strong>Horizontal scaling of stateless services</strong> <br>API Gateway, User &amp; Alert Service, Alert Management, Ingestion, and Evaluation are all designed to be stateless. <br>Add more instances behind a load balancer and you scale.</p><p><strong>Kafka partitions + consumer groups</strong> <br>Price updates are partitioned by symbol (or a hash of the symbol). <br>Each evaluation consumer instance is assigned a subset of partitions. <br>Need more throughput? Increase partitions and consumer instances.</p><p><strong>Redis</strong> <br>Active alerts can be sharded by symbol. <br>Latest prices are tiny and fit easily in memory.</p><p><strong>Handling market spikes</strong> <br>When volatility explodes, the Kafka buffer absorbs the temporary imbalance between producers and consumers. <br>The Evaluation Engine can be auto-scaled based on consumer lag.</p><p><strong>Handling millions of alerts</strong> <br>Only active alerts for the symbols that actually moved are loaded. <br>Most symbols are quiet most of the time. The system only does work for the symbols that matter right now.</p><h3>Reliability</h3><ul><li><strong>At-least-once delivery</strong> from Kafka + <strong>idempotent consumers</strong>. <br> Exactly-once is extremely hard and rarely worth the complexity for this use case.</li><li><strong>Dead-letter queues</strong> for poison messages (malformed ticks, rules that throw exceptions).</li><li><strong>Retries with exponential backoff</strong> on notification channels.</li><li><strong>Circuit breakers</strong> around external providers (SMS gateways, email providers).</li><li><strong>Health checks and readiness probes</strong> so the orchestrator (Kubernetes) can remove unhealthy instances.</li><li><strong>Distributed tracing</strong> (OpenTelemetry) so you can follow a single alert from creation through evaluation to delivery.</li></ul><h3>Performance Optimisations</h3><ul><li>Keep the evaluation path entirely in memory (Redis) for the common case.</li><li>Batch price updates when possible without violating latency SLAs.</li><li>Use Kafka’s zero-copy and sequential I/O characteristics.</li><li>Pre-compute or index alerts by symbol so evaluation never scans the entire alert table.</li><li>Apply backpressure: if the evaluation lag grows too large, temporarily slow or sample the ingestion (better to drop some non-critical ticks than to fall hours behind).</li></ul><h3>Trade-Offs, Being Honest</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*aM2ZjbrAxX97IXbqVPcWJA.png" /><figcaption>Trade offs</figcaption></figure><p>You cannot eliminate these trade-offs; you can only choose which ones you are willing to live with.</p><h3>Possible Improvements</h3><ul><li>Replace the custom evaluation loop with a stream-processing framework (Flink, Kafka Streams, or Spark Structured Streaming) for more complex windowed rules.</li><li>Move to Redis Streams or Apache Pulsar if you need simpler operational characteristics than Kafka.</li><li>Add a proper rules engine (Drools, or a custom expression language) for richer alert conditions.</li><li>Push real-time price updates to the client via WebSockets or Server-Sent Events so the UI feels live.</li><li>Multi-region active-active deployment for global users and disaster recovery.</li><li>Service mesh (Istio/Linkerd) for advanced traffic management and mTLS.</li><li>Feature flags so new alert types can be rolled out gradually.</li></ul><h3>Lessons Learned, Architectural Thinking</h3><p>1. <strong>Start with responsibilities, not technologies.</strong> <br> Ask “who owns this?” before you ask “which database?”</p><p>2. <strong>Separate the things that change at different speeds.</strong> <br> User preferences change slowly; market data changes continuously. Don’t force them into the same service or the same data store.</p><p>3. <strong>Make the hot path as simple and fast as possible.</strong> <br> Everything that is not on the critical evaluation path can be slower, more consistent, or more feature-rich.</p><p>4. <strong>Design for failure and for observability from day one.</strong> <br> You will need both.</p><p>5. <strong>Embrace eventual consistency where the business allows it.</strong> <br> Users can tolerate a few hundred milliseconds of delay on an alert. They cannot tolerate a system that collapses under load.</p><p>6. <strong>Every architectural decision answers four questions:</strong> <br> — What problem does it solve? <br> — Why is this solution appropriate here? <br> — What alternatives exist? <br> — What trade-offs does it introduce?</p><h3>Key Takeaways</h3><ul><li>Real-time alerting at scale is an event-driven problem, not a CRUD problem.</li><li>Isolate the high-throughput evaluation path from user-facing services.</li><li>Use the right storage technology for each access pattern (relational, time-series, in-memory, object).</li><li>Prefer asynchronous communication for anything that can tolerate slight delay.</li><li>Invest heavily in observability, it is the only way to operate a system this complex.</li><li>Always be explicit about the trade-offs you are accepting.</li></ul><h3>Conclusion</h3><p>Building a stock-price alert system is a wonderful exercise in distributed-systems design because the requirements are clear, the data rates are high, and the cost of getting it wrong (missing an alert) is immediate and visible to users.</p><p>The architecture we explored is not the only possible one, but it illustrates a set of principles that transfer to many other real-time platforms: clear bounded contexts, event-driven decoupling, polyglot persistence, and relentless focus on the hot path.</p><p>Next time you design a system, resist the urge to start with a list of technologies. <br>Start with the responsibilities, the scale, the failure modes, and the trade-offs. <br>The boxes on the diagram will then almost draw themselves.</p><p>If you found this useful, share it with a colleague who is about to design their first real-time system. <br>And if you have war stories from building similar platforms, I’d love to hear them in the comments.</p><p>Happy architecting.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d58fdf38eba9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing a Bank HR Management System: Lessons from a Real-World Enterprise Architecture]]></title>
            <link>https://medium.com/@kaly.salas.7/designing-a-bank-hr-management-system-lessons-from-a-real-world-enterprise-architecture-5318c0fe4178?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/5318c0fe4178</guid>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[enterprise-architecture]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[banking-tech]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Sat, 25 Jul 2026 23:01:01 GMT</pubDate>
            <atom:updated>2026-07-25T23:01:01.918Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>How layered architecture, microservices, and security-first design come together to support 16,000+ employees across 2,400 branches.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8MeEhCZ3T1VImokV2zY2TQ.png" /><figcaption>Bank HR Management System — High Level Architecture</figcaption></figure><h3>Introduction</h3><p>A few years ago, I sat in a boardroom with the CHRO and CIO of a large regional bank. They had 2,400 branches, over 16,000 employees, and a sprawling mess of HR processes. Excel spreadsheets circulated via email. Onboarding took weeks. Payroll errors triggered compliance nightmares. Performance reviews lived in disconnected systems. The legacy monolithic HR application, built in the early 2010s, could no longer handle the load, regulatory changes, or the demand for modern employee experiences.</p><p>The mandate was clear: build a secure, scalable, auditable HR platform that could serve branch employees, regional HR teams, managers, executives, and IT administrators without becoming another unmaintainable monolith.</p><p>This article walks through the architecture we designed. It is not a theoretical exercise. It is the practical outcome of balancing business needs, regulatory pressure, operational reality, and long-term maintainability. I will explain *why* we made each decision, the patterns we chose, the trade-offs we accepted, and how this system can evolve.</p><p>If you are a software engineer, architect, technical lead, or engineering manager working on enterprise systems — especially in regulated industries like banking — you will find actionable lessons here.</p><h3>Business Requirements</h3><p>Before drawing a single box, we spent weeks understanding requirements.</p><h4>Functional Requirements</h4><ul><li>Streamlined employee lifecycle: onboarding, data management, exit processes.</li><li>Performance management: goal setting, evaluations, feedback loops.</li><li>Career path planning and succession.</li><li>Comprehensive training and development programs.</li><li>Payroll processing with complex rules for salaries, bonuses, commissions, leaves, and deductions.</li><li>Role-based access for different user personas (branch staff, HR, managers, executives, IT admins).</li></ul><h4>Non-Functional Requirements</h4><p>Banks cannot compromise on these:</p><ul><li><strong>Scalability</strong>: Support 16,000+ employees and peak loads during performance review seasons or payroll runs.</li><li><strong>Security &amp; Compliance</strong>: GDPR, local banking regulations, data privacy, audit trails for every sensitive action.</li><li><strong>High Availability</strong>: 99.99% uptime. HR systems cannot go down on payday.</li><li><strong>Auditability</strong>: Every change to employee data or payroll must be traceable.</li><li><strong>Maintainability &amp; Extensibility</strong>: Business rules change frequently (tax laws, union agreements, new benefits).</li><li><strong>Performance</strong>: Sub-second response times for employee self-service.</li><li><strong>Resilience</strong>: Graceful degradation during partial outages.</li><li><strong>Cross-platform access</strong>: Works on any device, any OS, from branch desktops to executive laptops and mobile phones.</li></ul><p>Legacy approaches (monoliths or spreadsheet chaos) fail here because they create tight coupling, slow release cycles, single points of failure, and security nightmares.</p><h3>Architecture Overview</h3><p>We designed a layered, microservices-based architecture with strong emphasis on security, observability, and integration. The diagram shows the flow from users down to infrastructure.</p><h4>Users and Channels</h4><p>Different personas access the system through a responsive web application that works across devices and operating systems (Windows, macOS, Linux, Android, iOS). This Backend-for-Frontend (BFF) thinking keeps the UI responsive while backend services remain focused.</p><h4>Edge / Security Layer</h4><p>All traffic goes through DNS → Web Application Firewall (WAF) → Load Balancer → SSL/TLS Termination → DDoS Protection → API Gateway.</p><p>This is <strong>Defense in Depth</strong>. Each component filters threats at its level. The API Gateway acts as the single entry point, handling routing, rate limiting, authentication, and request transformation.</p><h4>Application Layer-Business Modules (Microservices)</h4><p>The core is five domain-oriented microservices:</p><ol><li><strong>Employee Administration</strong></li><li><strong>Performance Evaluation</strong></li><li><strong>Career Path Planning</strong></li><li><strong>Training Program</strong></li><li><strong>Payroll</strong></li></ol><p>Each service owns its bounded context. They communicate via well-defined APIs and shared services.</p><h4>Shared Services</h4><ul><li>User &amp; Role Service (IAM)</li><li>Notification Service</li><li>Document Service</li><li>Audit &amp; Logging Service</li></ul><p>These prevent duplication of critical cross-cutting logic.</p><h4>Data Layer</h4><ul><li>Operational relational database cluster (employees, payroll, etc.)</li><li>Analytics / Data Warehouse for reporting</li><li>Document storage for contracts and policies</li><li>Encrypted backup/archive</li></ul><p>Separation of operational and analytical workloads is crucial.</p><h4>Integration Layer</h4><p>Enterprise Service Bus (ESB) / Integration Services connect to:</p><ul><li>Core Banking System</li><li>Finance / Accounting</li><li>Active Directory / LDAP</li><li>Email / SMS Gateway</li><li>External Training Providers</li></ul><h4>Deployment / Infrastructure Layer</h4><p>Cloud or private cloud, Kubernetes orchestration, auto-scaling, load balancing, multi-region active-passive setup.</p><p>This is a cloud-native, containerized architecture ready for modern operations.</p><h3>Architectural Patterns Used</h3><h4>Layered Architecture</h4><p>We used classic layered architecture (presentation → application → data → infrastructure) but kept it loose. Layers provide separation of concerns and allow independent evolution. The edge layer isolates the internal system from the internet. The application layer focuses on business logic. Data and integration layers handle persistence and external dependencies.</p><p><strong>Why?</strong> It reduces cognitive load. Developers working on payroll logic don’t need to worry about WAF rules or Kubernetes pod specs.</p><h4>Microservices Architecture</h4><p>Each business domain became a microservice. This is not microservices for the sake of fashion — it was driven by domain complexity and team structure.</p><p><strong>Advantages</strong>:</p><ul><li>Independent deployment and scaling (payroll can scale during month-end without affecting training).</li><li>Technology diversity where it makes sense (though we standardized on Java/Spring Boot for most services).</li><li>Team ownership aligned with business domains.</li></ul><p><strong>Disadvantages</strong>:</p><ul><li>Distributed system complexity (network latency, eventual consistency).</li><li>Operational overhead.</li></ul><p>We mitigated these with strong shared services, observability, and CI/CD.</p><h4>API Gateway Pattern</h4><p>The API Gateway is the orchestration point. It handles authentication, authorization, routing, and sometimes aggregation for frontend needs.</p><h4>Shared Services Pattern</h4><p>Instead of every microservice implementing its own notification or audit logic, we built dedicated, reusable services. This is a pragmatic middle ground between pure microservices purity and monolith simplicity.</p><h4>Identity and Access Management (IAM)</h4><p>Centralized RBAC, SSO, MFA. Critical for a bank.</p><h4>Infrastructure as Code, Containerization, Orchestration</h4><p>Everything is declarative. Kubernetes gives us resilience and scalability.</p><h4>High Availability and Disaster Recovery Patterns</h4><p>Multi-region, active-passive, automated backups, regular DR drills.</p><h3>Why Microservices?</h3><p>Microservices are not always the right choice. For simple CRUD apps, they are overkill. But here the system was complex enough.</p><p>The HR domain naturally decomposes into bounded contexts with different business rules, release cadences, and scalability needs. Payroll has strict regulatory timing and complex calculations. Training involves external integrations. Performance management is collaborative and seasonal.</p><p><strong>Conway’s Law</strong> played a role: our teams were already organized around these domains. Aligning architecture with communication structure reduced friction.</p><p>Independent deployment meant HR could release a new performance review workflow without waiting for payroll changes — a game-changer for agility.</p><p>We used <strong>Database per Service</strong> where possible, though some shared reference data is carefully managed.</p><h3>Domain Decomposition</h3><p>We spent significant time on domain modeling with business stakeholders.</p><ul><li><strong>Employee Administration</strong>: The core entity service. Handles lifecycle events.</li><li><strong>Performance Evaluation</strong>: Focuses on sessions, goals, feedback. Highly collaborative.</li><li><strong>Career Path Planning</strong>: Long-term development, succession planning.</li><li><strong>Training Program</strong>: Needs analysis, course management, external providers.</li><li><strong>Payroll</strong>: Isolated for security and compliance. Processes salaries, bonuses, deductions. Changes here require extra scrutiny.</li></ul><p><strong>Why isolate Payroll?</strong> It touches money and sensitive personal data. Breaches here are catastrophic. Isolation limits blast radius.</p><p>These bounded contexts reduce coupling. Changes in career planning rarely break payroll processing.</p><h3>Cross-cutting Concerns</h3><p>Never duplicate security, logging, or monitoring in every service. We extracted these into platforms:</p><ul><li>Centralized IAM</li><li>ELK/Splunk for logging</li><li>Prometheus/Grafana for metrics</li><li>Distributed tracing</li><li>Automated compliance checks</li></ul><p>This approach dramatically reduces maintenance burden and security risk.</p><h3>Security</h3><p>Banks live in a hostile environment. We implemented Zero Trust principles.</p><ul><li>TLS everywhere</li><li>WAF for OWASP Top 10 protection</li><li>API Gateway for authentication and rate limiting</li><li>RBAC + Attribute-Based Access Control for fine-grained permissions</li><li>Encryption at rest and in transit</li><li>Comprehensive audit logging (who did what, when, why)</li><li>ecrets management and regular rotation</li></ul><p>Every service call is authenticated and authorized. Sensitive operations require elevated privileges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n19NYl4dEcepqvZn2AosIA.png" /></figure><h3>Data Architecture</h3><p>We separated concerns:</p><ul><li><strong>Operational DB</strong>: Normalized relational model for transactions.</li><li><strong>Analytics DB</strong>: Denormalized warehouse for fast reporting and BI.</li><li><strong>Document Store</strong>: For contracts, policies, certificates.</li><li><strong>Backup &amp; Archive</strong>: Encrypted, immutable where required.</li></ul><p>Direct queries from analytics to production databases are forbidden. ETL processes (or change data capture) keep them in sync.</p><p>Data governance policies enforce retention, privacy, and classification.</p><h3>Integration</h3><p>HR systems are never islands. We integrated with:</p><ul><li>Core Banking (for employee-linked accounts)</li><li>Finance/Accounting</li><li>LDAP for identity</li><li>Communication gateways</li><li>External providers</li></ul><p>We used a mix of synchronous REST APIs for simple queries and asynchronous messaging for complex, long-running processes (e.g., bulk payroll or training enrollments). The Integration Services / ESB layer handles protocol translation and orchestration.</p><h3>Deployment</h3><p>Kubernetes on cloud/private cloud gives us:</p><ul><li>Container orchestration</li><li>Auto-scaling</li><li>Blue-green deployments</li><li>Rolling updates</li><li>CI/CD pipelines with automated testing and security scans</li></ul><p>Infrastructure as Code (Terraform + Helm) ensures consistency across environments.</p><h3>Architecture is About Trade-offs</h3><p>No architecture is perfect. Here are the honest trade-offs we made:</p><p><strong>Benefits</strong>:</p><ul><li>Excellent scalability and resilience</li><li>Faster feature delivery per domain</li><li>Strong security posture</li><li>Clear team ownership</li></ul><p><strong>Costs</strong>:</p><ul><li>Increased operational complexity</li><li>Need for mature DevOps practices</li><li>Distributed debugging challenges</li><li>Higher initial development cost</li></ul><p>Eventual consistency appears in some flows (e.g., performance data propagating to payroll). We accepted this for availability.</p><p>Observability became non-negotiable. Without great monitoring, microservices become a distributed nightmare.</p><p>Testing strategy includes unit, integration, contract, and end-to-end tests. Chaos engineering helps validate resilience.</p><h3>Possible Improvements</h3><p>This architecture is solid but not finished. Future evolutions include:</p><ul><li><strong>Event-Driven Architecture</strong>: Introduce an event backbone (Kafka) for better decoupling.</li><li><strong>CQRS</strong>: Separate read and write models in performance and career services.</li><li><strong>Event Sourcing</strong>: For audit-heavy domains like payroll changes.</li><li><strong>Service Mesh</strong>: For advanced traffic management and security.</li><li><strong>AI/ML Integration</strong>: Recommendation engines for training, predictive attrition models.</li><li><strong>Feature Flags</strong>: For safer rollouts.</li><li><strong>Full Multi-region Active-Active</strong>: Beyond current active-passive.</li></ul><p>We also plan deeper document AI for contract processing.</p><h3>Lessons Learned</h3><ol><li><strong>Domain modeling first</strong>. Spend time understanding bounded contexts before coding.</li><li><strong>Security and observability from day one</strong>. Bolting them on later is painful and expensive.</li><li><strong>Align architecture with organizational structure</strong> (Conway’s Law in practice).</li><li><strong>Choose boring technology</strong> where it serves the purpose. Innovation should solve real problems.</li><li><strong>Trade-offs are inevitable</strong>. Document them explicitly for future teams.</li><li><strong>Invest in platforms</strong>. Shared services and infrastructure pay dividends quickly.</li></ol><p>Good architecture solves today’s problems while keeping tomorrow’s options open.</p><h3>Conclusion</h3><p>Building enterprise systems for banks is not about chasing the latest frameworks or boasting about Kubernetes clusters. It is about deeply understanding business problems, regulatory constraints, and human realities, then crafting an architecture that balances all of them.</p><p>The HR Management System we designed demonstrates that thoughtful layered architecture combined with strategic microservices, robust security, and modern deployment practices can deliver both enterprise-grade reliability and business agility.</p><p>I hope this case study gives you practical insights you can apply to your own systems — whether you are modernizing a legacy platform, designing greenfield, or mentoring the next generation of architects.</p><p>What trade-offs have you faced in your enterprise projects? Have you successfully decomposed a complex domain? When have microservices helped — and when have they hurt?</p><p><strong>Discussion Questions:</strong></p><ol><li>In your experience, at what system size or complexity does moving from a well-structured monolith to microservices become worthwhile?</li><li>How do you handle data consistency across bounded contexts in regulated industries?</li><li>What cross-cutting concerns have you seen cause the most pain when duplicated across services?</li><li>How would you evolve this architecture if the bank expanded internationally with different regulatory regimes?</li><li>For architects reading this: what’s one pattern or practice from this design you would adopt or adapt in your current project?</li></ol><p><em>Share your thoughts below. I read every comment and often continue the conversation.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5318c0fe4178" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use Your Full Power with Threads in Rust Programming]]></title>
            <link>https://medium.com/@kaly.salas.7/how-to-use-your-full-power-with-threads-in-rust-programming-d58e2336125f?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/d58e2336125f</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[programing]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 06 Feb 2026 05:06:51 GMT</pubDate>
            <atom:updated>2026-02-06T05:12:26.197Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*n1b8XyCpcN6C14cqcmu6Tg.jpeg" /><figcaption>The winner never give up</figcaption></figure><p>Mastering production-ready concurrent patterns with <strong>std::thread</strong> and friends</p><p>As Rust programmers, we’re spoiled for choice when it comes to concurrency. The async/await ecosystem is fantastic for I/O-heavy workloads, but threads — real OS threads — still reign supreme when you need raw CPU parallelism or when you’re dealing with blocking operations that don’t play nicely with async runtimes.</p><p>In this post, we’ll go deep into practical, production-ready threading patterns in Rust. You already know ownership, borrowing, and how to call <strong>std::thread::spawn</strong>. Now it’s time to level up: sharing data safely, choosing the right primitives, avoiding contention, and writing code that scales on real hardware.</p><p>Github repo to play with all the examples <a href="https://github.com/kaly7dev/rust-threads-examples">here</a></p><p>Let’s get coding.</p><h3>Thread Basics</h3><h4>Spawning and Joining</h4><p>The simplest way to start a thread is <strong>std::thread::spawn</strong>. It takes a closure and returns a <strong>JoinHandle</strong>.</p><p>Create a new project for all examples in this article:</p><pre>cargo new rust-threads-guide<br>cd rust-threads-guide</pre><p>Replace `src/main.rs` with this (example 1 — basic spawn):</p><pre>// src/main.rs<br>fn main() {<br>    let handle = std::thread::spawn(|| {<br>        for i in 1..=5 {<br>            println!(&quot;Thread: {}&quot;, i);<br>        }<br>    });<br><br>    for i in 1..=4 {<br>        println!(&quot;Main: {}&quot;, i);<br>    }<br><br>    handle.join().unwrap(); // Wait for the thread to finish<br>}</pre><p>Run it:</p><pre>cargo run</pre><p>Typical output (order may vary):</p><pre>Main: 1<br>Main: 2<br>Main: 3<br>Thread: 1<br>Thread: 2<br>Thread: 3<br>Thread: 4<br>Thread: 5<br>Main: 4</pre><p>This demonstrates true parallelism — both threads run concurrently.</p><h4>Move Closures and Ownership</h4><p>Threads often need data from the parent. Because the thread may outlive the parent scope, Rust forces you to transfer ownership with the `<strong>move</strong>` keyword.</p><pre>// src/main.rs<br>use std::thread;<br><br>fn main() {<br>    let numbers = vec![1, 2, 3, 4, 5];<br><br>    let handle = thread::spawn(move || {<br>        let sum: i32 = numbers.iter().sum();<br>        println!(&quot;Thread computed sum: {sum}&quot;);<br>    });<br><br>    // numbers is moved, so we can&#39;t use it here anymore<br>    handle.join().unwrap();<br>}</pre><p>Output:</p><pre>Thread computed sum: 15</pre><p>Without `<strong>move</strong>`, you’d get a compile error — Rust protects you from dangling references.</p><h4>Scoped Threads (Zero-Cost Borrowing Across Threads)</h4><p>Since Rust 1.63, `<strong>std::thread::scope</strong>` lets you spawn threads that borrow data from the parent without `Arc` or cloning.</p><pre>// src/main.rs<br>use std::thread;<br><br>fn main() {<br>    let data = vec![10, 20, 30];<br><br>    thread::scope(|s| {<br>        s.spawn(|| {<br>            println!(&quot;Borrowed in thread 1: {}&quot;, data[0]);<br>        });<br><br>        s.spawn(|| {<br>            let sum: i32 = data.iter().sum();<br>            println!(&quot;Borrowed in thread 2: sum = {sum}&quot;);<br>        });<br>    });<br><br>    // data is still usable here<br>    println!(&quot;Back in main: {:?}&quot;, data);<br>}</pre><p>Run `<strong>cargo run</strong>`. Output:</p><pre>Borrowed in thread 1: 10<br>Borrowed in thread 2: sum = 60<br>Back in main: [10, 20, 30]</pre><p>`<strong>scope</strong>` guarantees all spawned threads finish before exiting the block — perfect for temporary parallelism.</p><h3>Sharing Immutable Data Between Threads</h3><p>To share data across threads, the type must implement `<strong>Send</strong>` (can be moved) and/or `Sync` (can be shared immutably via reference).</p><p>`<strong>Arc&lt;T&gt;</strong>` (Atomic Reference Counted) is the standard way to share immutable data cheaply.</p><p>Add this to `<strong>Cargo.toml</strong>` if you haven’t already (we’ll use it often):</p><pre>[dependencies]</pre><p>(No external deps yet — `Arc` is in `std`.)</p><p>Example: sharing a large read-only config.</p><pre>// src/main.rs<br>use std::sync::Arc;<br>use std::thread;<br><br>fn main() {<br>    let config = Arc::new(vec![String::from(&quot;host=db.example.com&quot;); 1000]);<br><br>    let config1 = Arc::clone(&amp;config);<br>    let handle1 = thread::spawn(move || {<br>        println!(&quot;Worker 1 sees {} entries&quot;, config1.len());<br>    });<br><br>    let config2 = Arc::clone(&amp;config);<br>    let handle2 = thread::spawn(move || {<br>        println!(&quot;Worker 2 sees {} entries&quot;, config2.len());<br>    });<br><br>    handle1.join().unwrap();<br>    handle2.join().unwrap();<br><br>    // Original Arc still alive<br>    println!(&quot;Main still has config with {} entries&quot;, config.len());<br>}</pre><p>Output:</p><pre>Worker 1 sees 1000 entries<br>Worker 2 sees 1000 entries<br>Main still has config with 1000 entries</pre><p>`<strong>Arc</strong>` is cheap to clone and perfect when data is truly immutable. If you need mutation, read on.</p><h3>Sharing Mutable Data Between Threads</h3><p>Shared mutability requires synchronization.</p><h4>Mutex: Exclusive Access</h4><p>The classic pattern: `<strong>Arc&lt;Mutex&lt;T&gt;&gt;</strong>`.</p><pre>// src/main.rs<br>use std::sync::{Arc, Mutex};<br>use std::thread;<br><br>fn main() {<br>    let counter = Arc::new(Mutex::new(0));<br>    let mut handles = vec![];<br><br>    for _ in 0..10 {<br>        let counter = Arc::clone(&amp;counter);<br>        let handle = thread::spawn(move || {<br>            let mut num = counter.lock().unwrap();<br>            *num += 1;<br>        });<br>        handles.push(handle);<br>    }<br><br>    for h in handles {<br>        h.join().unwrap();<br>    }<br><br>    println!(&quot;Final counter: {}&quot;, *counter.lock().unwrap());<br>}</pre><p>Output:</p><pre>Final counter: 10</pre><h4>Poisoning and Error Handling</h4><p>A `<strong>Mutex</strong>` becomes poisoned if a thread panics while holding the lock. Always consider `<strong>PoisonError</strong>`.</p><p>Better pattern:</p><pre>// In the thread closure<br>match counter.lock() {<br>    Ok(mut num) =&gt; {<br>        *num += 1;<br>    }<br>    Err(e) =&gt; {<br>        eprintln!(&quot;Mutex poisoned: {e}&quot;);<br>        // Decide: propagate, recover, etc.<br>    }<br>}</pre><h4>RwLock: Many Readers, Few Writers</h4><p>When reads heavily outnumber writes, `<strong>RwLock</strong>` reduces contention.</p><pre>// src/main.rs<br>use std::sync::{Arc, RwLock};<br>use std::thread;<br><br>fn main() {<br>    let data = Arc::new(RwLock::new(vec![1, 2, 3]));<br><br>    let mut handles = vec![];<br><br>    // 8 readers<br>    for _ in 0..8 {<br>        let data = Arc::clone(&amp;data);<br>        let handle = thread::spawn(move || {<br>            let read_guard = data.read().unwrap();<br>            println!(&quot;Read: sum = {}&quot;, read_guard.iter().sum::&lt;i32&gt;());<br>        });<br>        handles.push(handle);<br>    }<br><br>    // 2 writers<br>    for i in 4..6 {<br>        let data = Arc::clone(&amp;data);<br>        let handle = thread::spawn(move || {<br>            let mut write_guard = data.write().unwrap();<br>            write_guard.push(i);<br>        });<br>        handles.push(handle);<br>    }<br><br>    for h in handles {<br>        h.join().unwrap();<br>    }<br><br>    println!(&quot;Final data: {:?}&quot;, *data.read().unwrap());<br>}</pre><p>Typical output:</p><pre>Read: sum = 6<br>Read: sum = 6<br>…<br>Final data: [1, 2, 3, 4, 5]</pre><p><strong>Performance note</strong>: `<strong>RwLock</strong>` has higher overhead than `<strong>Mutex</strong>` on some platforms. Benchmark with `<strong>criterion</strong>` if performance is critical.</p><h4>Avoiding Deadlocks</h4><p>- Always acquire locks in a consistent global order.<br>- Keep critical sections short.<br>- Prefer channels over shared state when possible.</p><h3>Concurrency Primitives Beyond Basic Locks</h3><h4>Channels (std::sync::mpsc)</h4><p>Channels are often the safest way to communicate.</p><pre>// src/main.rs<br>use std::sync::mpsc;<br>use std::thread;<br><br>fn main() {<br>    let (tx, rx) = mpsc::channel();<br><br>    thread::spawn(move || {<br>        for i in 1..=5 {<br>            tx.send(i).unwrap();<br>        }<br>        // Sender dropped → receiver sees end<br>    });<br><br>    for received in rx {<br>        println!(&quot;Got: {received}&quot;);<br>    }<br>}</pre><p>Output:</p><pre>Got: 1<br>Got: 2<br>Got: 3<br>Got: 4<br>Got: 5</pre><h4>Crossbeam Channels (Better Performance)</h4><p>`<strong>std::mpsc</strong>` is single-producer only for the fast path. `<strong>crossbeam</strong>` gives multi-producer, multi-consumer and unbounded channels.</p><p>Add to `<strong>Cargo.toml</strong>`:</p><pre>[dependencies]<br>crossbeam = &quot;0.8&quot;</pre><p>Example:</p><pre>// src/main.rs<br>use crossbeam::channel;<br>use std::thread;<br><br>fn main() {<br>    let (tx, rx) = channel::unbounded();<br><br>    for i in 0..4 {<br>        let tx = tx.clone();<br>        thread::spawn(move || {<br>            tx.send(format!(&quot;Hello from thread {i}&quot;)).unwrap();<br>        });<br>    }<br><br>    drop(tx); // Close all senders<br><br>    for msg in rx {<br>        println!(&quot;{msg}&quot;);<br>    }<br>}</pre><h4>Atomics</h4><p>For lock-free counters or flags.</p><pre>// src/main.rs<br>use std::sync::atomic::{AtomicUsize, Ordering};<br>use std::sync::Arc;<br>use std::thread;<br><br>fn main() {<br>    let counter = Arc::new(AtomicUsize::new(0));<br>    let mut handles = vec![];<br><br>    for _ in 0..10 {<br>        let counter = Arc::clone(&amp;counter);<br>        let handle = thread::spawn(move || {<br>            counter.fetch_add(1, Ordering::Relaxed);<br>        });<br>        handles.push(handle);<br>    }<br><br>    for h in handles {<br>        h.join().unwrap();<br>    }<br><br>    println!(&quot;Atomic count: {}&quot;, counter.load(Ordering::Relaxed));<br>}</pre><p>Use `<strong>Relaxed</strong>` only when you don’t need ordering guarantees — otherwise `<strong>SeqCst</strong>`.</p><h3>Advanced Patterns &amp; Performance Tips</h3><h4>Reducing Contention</h4><p>A single shared `<strong>Mutex`</strong> becomes a bottleneck under high contention. Shard instead: Use multiple mutexes and assign threads to them to parallelize access.</p><pre>// examples/sharding.rs<br>use std::sync::{Arc, Mutex};<br>use std::thread;<br><br>fn main() {<br>    const NUM_SHARDS: usize = 4;<br>    const NUM_THREADS: usize = 16;<br>    const INCREMENTS_PER_THREAD: usize = 100_000;<br><br>    // Create shards: Vec&lt;Arc&lt;Mutex&lt;usize&gt;&gt;&gt;<br>    let shards: Vec&lt;Arc&lt;Mutex&lt;usize&gt;&gt;&gt; = (0..NUM_SHARDS).map(|_| Arc::new(Mutex::new(0))).collect();<br><br>    let mut handles = vec![];<br><br>    for i in 0..NUM_THREADS {<br>        let shard = Arc::clone(&amp;shards[i % NUM_SHARDS]); // Assign shard round-robin<br>        let handle = thread::spawn(move || {<br>            for _ in 0..INCREMENTS_PER_THREAD {<br>                let mut count = shard.lock().unwrap();<br>                *count += 1;<br>            }<br>        });<br>        handles.push(handle);<br>    }<br><br>    for handle in handles {<br>        handle.join().unwrap();<br>    }<br><br>    // Sum all shards<br>    let total: usize = shards.iter().map(|s| *s.lock().unwrap()).sum();<br>    println!(&quot;Total: {}&quot;, total);<br>}</pre><p>This scales better because contention is spread across shards. On a multi-core machine, you’ll see near-linear speedup compared to a single mutex.</p><h4>parking_lot vs std Locks</h4><p>`<strong>parking_lot</strong>` offers faster, more feature-rich locks.</p><p>Add to `<strong>Cargo.toml</strong>`:</p><pre>[dependencies]<br>parking_lot = &quot;0.12&quot;</pre><p>It’s usually a drop-in replacement with lower overhead and no poisoning semantics (panics propagate normally).</p><h4>Thread Pools</h4><p>Spawning a thread per task is expensive. Use a pool.</p><p><strong>Rayon</strong> is the gold standard for data parallelism.</p><p>Add:</p><pre>[dependencies]<br>rayon = &quot;1.10&quot;</pre><pre>// src/main.rs<br>use rayon::prelude::*;<br><br>fn main() {<br>    let numbers: Vec&lt;_&gt; = (0..1_000_000).collect();<br><br>    let sum: u64 = numbers.par_iter().map(|&amp;x| x as u64).sum();<br><br>    println!(&quot;Parallel sum: {sum}&quot;);<br>}</pre><p>Rayon automatically manages a thread pool sized to your CPU cores.</p><h3>Common Mistakes &amp; Debugging Tips</h3><p>- Forgetting `<strong>move</strong>` → lifetime errors.<br>- Missing `<strong>Arc::clone</strong>` → “use after move” errors.<br>- Holding locks across `<strong>.await</strong>` or blocking calls → deadlocks.<br>- Assuming `<strong>Mutex</strong>` poisoning never happens — always handle `<strong>lock()</strong>` result.<br>- Overusing shared state — prefer message passing.</p><p><strong>Debugging</strong>:<br>- `<strong>RUST_BACKTRACE=1 cargo run</strong>`<br>- Use `<strong>log</strong>` + `<strong>env_logger</strong>` for thread-safe logging.<br>- `<strong>thread-sanitizer</strong>` via `<strong>cargo install cargo-san</strong>` and `<strong>RUSTFLAGS=”-Zsanitizer=thread</strong>” cargo test`</p><h3>Quick Reference: Threading Primitives Cheat Sheet</h3><ul><li>`<strong>Arc&lt;T&gt;</strong>` → Share immutable data cheaply (`<strong>T: Sync</strong>`).<br>- `<strong>Arc&lt;Mutex&lt;T&gt;&gt;</strong>` → Exclusive mutable access, simple cases.<br>- `<strong>Arc&lt;RwLock&lt;T&gt;&gt;</strong>` → Many concurrent readers.<br>- `<strong>mpsc::channel</strong>` → Communicate between threads safely.<br>- `crossbeam::channel` → Multi-producer, unbounded, select!.<br>- `<strong>Atomic*</strong>` → Lock-free counters/flags (understand ordering!).<br>- `<strong>rayon</strong>` → Data parallelism without manual thread management.<br>- `<strong>parking_lot</strong>` → Faster locks, no poisoning.</li></ul><p>Github repo to play with all the examples <a href="https://github.com/kaly7dev/rust-threads-examples">here</a></p><h3>Conclusion</h3><p>Threads in Rust give you unparalleled control and performance when used correctly. The key is minimizing shared mutable state, choosing the right synchronization primitive, and thinking about contention early.</p><p><strong>My personal advice as a senior Software engineer</strong>:</p><p><strong>Do</strong>:<br>- Default to channels for communication.<br>- Use `<strong>Arc&lt;Mutex&lt;T&gt;&gt;</strong>` for simple shared state.<br>- Reach for `<strong>rayon</strong>` whenever you have embarrassingly parallel work.</p><p><strong>Don’t</strong>:<br>- Spawn thousands of threads manually.<br>- Hold locks longer than absolutely necessary.<br>- Ignore poisoning or lock ordering.</p><p>Experiment with these patterns in your own projects. Try rewriting a sequential loop with `<strong>rayon</strong>` and measure the speedup.</p><p>What’s your biggest threading pain point in Rust? Drop a comment below — I read them all. If you found this helpful, clap, share, and let me know what topic you’d like next (async deep-dive? FFI concurrency?).</p><p>Happy Coding! 🦀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d58e2336125f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 Ways to Use Polymorphism in Rust — When to Use Generics, Trait Objects, and Enums]]></title>
            <link>https://medium.com/@kaly.salas.7/3-ways-to-use-polymorphism-in-rust-when-to-use-generics-trait-objects-and-enums-94a451765e7d?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/94a451765e7d</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[sofware-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rustlang]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 21 Nov 2025 14:10:58 GMT</pubDate>
            <atom:updated>2025-11-21T14:11:37.332Z</atom:updated>
            <content:encoded><![CDATA[<h3>3 Ways to Use Polymorphism in Rust — When to Use Generics, Trait Objects, and Enums</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YDmxPPdWPXsMEODxsO3Kfw.png" /></figure><p>As Rust developers, we often need to write code that handles different types in a uniform way — that’s polymorphism at its core. In languages like Java or C++, polymorphism leans heavily on class inheritance and virtual methods. Rust takes a different path: no inheritance, but powerful tools like traits for behavior sharing and algebraic data types (ADTs) for structure. This leads to three main approaches: generics for static dispatch, trait objects for dynamic dispatch, and enums for sum-type polymorphism.</p><p>Why does this matter? Rust’s design prioritizes performance, safety, and flexibility without the pitfalls of classical OOP. In this article, we’ll dive into each method with code-first examples, trade-offs, and practical advice. By the end, you’ll know when to pick generics for speed, trait objects for flexibility, or enums for simplicity. Let’s get coding.</p><h3>Approach 1: Static Dispatch with Generics and Trait Bounds</h3><p>Generics let you write polymorphic code that’s resolved at compile time via <strong>monomorphization</strong>. Rust’s compiler generates specialized versions of your generic function for each concrete type used. This means no runtime overhead — no virtual tables (vtables) or indirection. It’s fast, allows inlining, and catches type errors early.</p><p><strong>When to use it</strong>: For performance-critical code where types are known at compile time, like in libraries or algorithms that operate on fixed sets of types.</p><p>Here’s a runnable example. Create a new Cargo project (`cargo new poly_generics`), add this to `src/main.rs`, and run `cargo run`.</p><pre>// Define a trait for polymorphic behavior<br>trait Shape {<br> fn area(&amp;self) -&gt; f64;<br>}<br><br>// Concrete implementations<br>struct Circle {<br> radius: f64,<br>}<br><br>impl Shape for Circle {<br> fn area(&amp;self) -&gt; f64 {<br> std::f64::consts::PI * self.radius * self.radius<br> }<br>}<br><br>struct Rectangle {<br> width: f64,<br> height: f64,<br>}<br><br>impl Shape for Rectangle {<br> fn area(&amp;self) -&gt; f64 {<br> self.width * self.height<br> }<br>}<br><br>// Generic function with trait bound: static dispatch<br>fn print_area&lt;T: Shape&gt;(shape: &amp;T) {<br> println!(&quot;Area: {}&quot;, shape.area());<br>}<br><br>fn main() {<br> let circle = Circle { radius: 5.0 };<br> let rect = Rectangle { width: 4.0, height: 3.0 };<br>print_area(&amp;circle); // Compiler generates print_area::&lt;Circle&gt;<br> print_area(&amp;rect); // And print_area::&lt;Rectangle&gt;<br>}</pre><p>Output:</p><pre>Area: 78.53981633974483<br>Area: 12</pre><p><strong>Benefits</strong>: Compile-time type checking ensures safety. No vtable means calls can be inlined, reducing overhead. Binary size might grow with many monomorphized instances (code bloat), but for most apps, it’s negligible. Perf-wise, it’s as fast as non-polymorphic code.</p><p><strong>Trade-off</strong>: Can’t handle heterogeneous collections (e.g., a `Vec` of mixed shapes) because types must be uniform.</p><h3>Approach 2: Dynamic Dispatch with Trait Objects</h3><p>Trait objects enable runtime polymorphism by erasing the concrete type and using a vtable for method dispatch. Use `dyn Trait` to indicate dynamic behavior. Traits must be <strong>object-safe</strong>: no generic methods, no methods returning `Self`, and no associated types without bounds.</p><p><strong>When to use</strong>: For flexible APIs like plugins or when types aren’t known until runtime, such as heterogeneous collections.</p><p><strong>Examples</strong>: `&amp;dyn Trait` for borrowing, `Box&lt;dyn Trait&gt;` for owning (heap-allocated), and `Rc&lt;dyn Trait&gt;` for shared ownership.</p><p>Runnable example (`cargo new poly_dyn`):</p><pre>use std::rc::Rc;<br><br>// Same Shape trait and impls as above<br><br>fn main() {<br> // Heterogeneous collection with Box&lt;dyn Shape&gt;<br> let shapes: Vec&lt;Box&lt;dyn Shape&gt;&gt; = vec![<br> Box::new(Circle { radius: 5.0 }),<br> Box::new(Rectangle { width: 4.0, height: 3.0 }),<br> ];<br><br>for shape in &amp;shapes {<br> println!(&quot;Area: {}&quot;, shape.area());<br> }<br><br>// Borrowing with &amp;dyn Shape<br> let circle = Circle { radius: 5.0 };<br> let shape_ref: &amp;dyn Shape = &amp;circle;<br> println!(&quot;Ref area: {}&quot;, shape_ref.area());<br><br>// Shared ownership with Rc&lt;dyn Shape&gt;<br> let shared_shape: Rc&lt;dyn Shape&gt; = Rc::new(Rectangle { width: 4.0, height: 3.0 });<br> let clone = shared_shape.clone();<br> println!(&quot;Shared area: {}&quot;, shared_shape.area());<br> println!(&quot;Clone area: {}&quot;, clone.area());<br>}</pre><p>Output matches the generics example, but now we can mix types.</p><p>To inspect size/layout:</p><pre>use std::mem::size_of_val;<br><br>// In main, after creating shapes<br>let box_shape = Box::new(Circle { radius: 5.0 }) as Box&lt;dyn Shape&gt;;<br>println!(&quot;Size of Box&lt;dyn Shape&gt;: {}&quot;, size_of_val(&amp;box_shape)); // Typically 16 bytes (data ptr + vtable ptr)</pre><p>On 64-bit systems, it’s usually 16 bytes: one pointer to data, one to vtable.</p><p>Micro-benchmark for overhead (add to main; run multiple times for averages):</p><pre>use std::time::Instant;<br><br>// Static dispatch benchmark<br>let circle = Circle { radius: 5.0 };<br>let start = Instant::now();<br>for _ in 0..1_000_000 {<br> print_area(&amp;circle); // Inlined, fast<br>}<br>let static_duration = start.elapsed();<br><br>// Dynamic dispatch benchmark<br>let dyn_circle: Box&lt;dyn Shape&gt; = Box::new(Circle { radius: 5.0 });<br>let start = Instant::now();<br>for _ in 0..1_000_000 {<br> let _ = dyn_circle.area(); // vtable lookup<br>}<br>let dyn_duration = start.elapsed();<br><br>println!(&quot;Static: {:?}, Dynamic: {:?}&quot;, static_duration, dyn_duration);</pre><p>Results (on my machine): Static ~1ms, Dynamic ~5ms. Dynamic adds vtable lookup and indirection; heap allocation for `Box` costs extra (~10–20ns per alloc).</p><p>Use-cases: Building extensible systems like a UI framework with widgets or a plugin loader.</p><h3>Approach 3: Enums and ADTs for Polymorphism</h3><p>Enums provide polymorphism via sum types: variants represent different “subtypes,” and you handle them exhaustively with `match`. No dispatch overhead — everything’s resolved at compile time. Memory is the size of the largest variant plus a tag.</p><p><strong>When preferable</strong>: Fixed, known set of types; need exhaustive handling; avoid indirection.</p><p>Runnable example (`cargo new poly_enum`):</p><pre>enum ShapeEnum {<br> Circle { radius: f64 },<br> Rectangle { width: f64, height: f64 },<br>}<br><br>impl ShapeEnum {<br> fn area(&amp;self) -&gt; f64 {<br> match self {<br> ShapeEnum::Circle { radius } =&gt; std::f64::consts::PI * radius * radius,<br> ShapeEnum::Rectangle { width, height } =&gt; width * height,<br> }<br> }<br>}<br><br>fn main() {<br> let shapes = vec![<br> ShapeEnum::Circle { radius: 5.0 },<br> ShapeEnum::Rectangle { width: 4.0, height: 3.0 },<br> ];<br>for shape in &amp;shapes {<br> println!(&quot;Area: {}&quot;, shape.area());<br> }<br>// Memory comparison<br> println!(&quot;Size of ShapeEnum: {}&quot;, std::mem::size_of::&lt;ShapeEnum&gt;()); // ~24 bytes (largest variant + tag)<br>}</pre><p><strong>Compare</strong>: Enums use stack space efficiently (no pointers unless variants are large). Perf matches generics (no overhead). Vs. trait objects: No heap, no vtable — faster for small sets. Vs. generics: Easier for heterogeneous lists without traits.</p><h3>Understanding Box in Polymorphism</h3><p>`Box&lt;T&gt;` moves ownership to the heap, useful for large data, recursive types, or when size isn’t known at compile time (like trait objects). It derefs to `T`, so `Box&lt;dyn Trait&gt;` owns a trait object on the heap.</p><p><strong>When to use</strong>: To avoid stack overflows with big structs or enable owning trait objects (e.g., `Vec&lt;Box&lt;dyn Shape&gt;&gt;`). Ownership: Exclusive, like any owned value — use `Rc` or `Arc` for sharing.</p><p><strong>Interaction</strong>: With trait objects, `Box` handles the fat pointer (data + vtable). Without it, you can’t own unsized types like `dyn Trait`.</p><h3>Traits vs. Inheritance</h3><p>In OOP, inheritance lets subclasses inherit and override parent behavior:</p><p>Hypothetical C++:</p><pre>class Shape {<br>public:<br> virtual double area() = 0;<br>};<br><br>class Circle : public Shape {<br> double radius;<br>public:<br> Circle(double r) : radius(r) {}<br> double area() override { return 3.14 * radius * radius; }<br>};</pre><p>Rust uses traits + composition: Define behavior separately, impl on structs. No “is-a” relationship — favor “has-a.”</p><p>Rust equivalent (using traits):</p><pre>// As in Approach 1: trait Shape, impl on Circle/Rectangle<br>// Composition: Structs can embed others if needed, e.g., a ComplexShape with a vec of Shapes</pre><p><strong>Difference</strong>: Traits avoid diamond inheritance issues; promote composition for flexibility. Rust’s way is safer (no fragile base class) but requires more boilerplate.</p><h3>Static vs. Dynamic Dispatch: Side-by-Side Comparison</h3><p><strong>Static (generics): Compile-time resolution.</strong><br>- <strong>Pros</strong>: Max perf (inlining), small runtime size per call.<br>- <strong>Cons</strong>: Code bloat, can’t mix types.</p><p><strong>Dynamic (trait objects): Runtime resolution.</strong><br>- <strong>Pros</strong>: Flexible (heterogeneous data), smaller binary.<br>- <strong>Cons</strong>: Slower (vtable), object safety limits.</p><p><strong>Ergonomics</strong>: Generics feel typesafe; dynamic needs `dyn` and boxing.</p><p><strong>Scenarios</strong>: Static for hot loops; dynamic for APIs.</p><h3>The Cost of Dynamic Dispatch</h3><p><strong>Dynamic dispatch uses vtables:</strong> Each trait object points to a table of function pointers. Calls involve indirection (load vtable, jump) — no inlining across boundaries.</p><p>Heap cost: `Box` allocates (~10–50ns), plus pointer chase.</p><p><strong>From benchmark:</strong> Dynamic calls ~5x slower in tight loops due to indirection. Real apps: Negligible unless in perf hotspots. Extra indirection adds cache misses.</p><h3>Pros/Cons Table</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Og1hsunIfRBosCb3slB6Qw.png" /></figure><h3>Conclusion and Guidance</h3><p>Choose generics for performance in known-type scenarios (e.g., math libraries). Go dynamic with trait objects for runtime flexibility (e.g., web frameworks handling plugins). Use enums for simple, closed polymorphisms (e.g., error handling like `Result`).</p><p>Rules of thumb:<br>- Start with enums if variants are few and fixed.<br>- Use generics for speed without mixing types.<br>- Fall back to trait objects for open extensibility.</p><p><strong>Real-world</strong>: Generics in iterators; trait objects in async runtimes; enums in parsers.</p><p>For more, check Rust Book’s traits chapter. Keep experimenting — Rust’s polymorphism shines in practice. in my point of view of course.</p><p>What about yours ?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94a451765e7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Generics and Traits in Rust: Best Practices for Cleaner Code]]></title>
            <link>https://medium.com/@kaly.salas.7/mastering-generics-and-traits-in-rust-best-practices-for-cleaner-code-846bf9c1ba9f?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/846bf9c1ba9f</guid>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[tech-blog]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 14 Nov 2025 12:38:09 GMT</pubDate>
            <atom:updated>2025-11-14T12:46:40.418Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zgp2yKoIyAVDQb5PgIh2hA.png" /></figure><p>As a senior software engineer with years of experience in systems programming, I’ve seen how Rust’s type system can elevate code from functional to elegant. In this post, we’ll dive into best practices for using generics and traits, drawing from real-world patterns that reduce boilerplate and enhance flexibility. This isn’t beginner territory — we’ll assume you’re comfortable with Rust basics and focus on idiomatic usage for advanced designs.</p><h3>Introduction</h3><p>Generics in Rust allow you to write functions, structs, and enums that operate on abstract types, parameterizing code to work across different data types without sacrificing type safety. For instance, a generic vector `Vec&lt;T&gt;` can hold integers, strings, or custom structs interchangeably.</p><p>Traits, on the other hand, define shared behavior or interfaces that types can implement. They’re like contracts: a type implementing `Iterator` promises to provide `next()` functionality.</p><p>Together, generics and traits matter because they promote code reuse, enforce compile-time guarantees, and enable polymorphic designs. Without them, you’d duplicate code for each type, leading to maintenance nightmares. In performance-critical systems like those I build, they ensure efficiency without runtime overhead, thanks to monomorphization — Rust’s way of generating specialized code at compile time.</p><h3>Generics Improving Code Design</h3><p>Generics shine when they abstract away type-specific details, reducing duplication and improving API ergonomics. Consider a scenario where you need a function to find the larger of two values. Without generics, you’d write separate versions for `i32`, `f64`, etc.</p><p>Here’s a non-generic approach:</p><pre>fn max_i32(a: i32, b: i32) -&gt; i32 {<br> if a &gt; b { a } else { b }<br>}<br><br>fn max_f64(a: f64, b: f64) -&gt; f64 {<br> if a &gt; b { a } else { b }<br>}</pre><p>This works but scales poorly. Now, with generics:</p><pre>fn max&lt;T: Ord&gt;(a: T, b: T) -&gt; T {//PartialOrd<br> if a &gt; b { a } else { b }<br>}<br><br>fn main() {<br> println!(&quot;{}&quot;, max(5, 3)); // 5<br> println!(&quot;{}&quot;, max(3.14, 2.71)); // 3.14<br>}</pre><p>(We’ll cover the `Ord` bound shortly.) This is a best practice because it eliminates redundancy — one function handles any comparable type. It also improves API design: users aren’t locked into specific types, making your library more flexible and easier to extend. In larger codebases, this abstraction hides implementation details, fostering better modularity.</p><h3>Type Restrictions with Traits</h3><p>Unbounded generics are powerful but often too permissive. To restrict types, use trait bounds like `T: TraitName`, ensuring `T` implements the required behavior. This catches errors at compile time and documents intent clearly.</p><p>For example, our `max` function needs types that support comparison, so we bound `T` to `Ord` (which implies `PartialOrd` and `Eq`):</p><pre>use std::cmp::Ord;<br><br>fn max&lt;T: Ord&gt;(a: T, b: T) -&gt; T {<br> if a &gt; b { a } else { b }<br>}<br><br>#[derive(Ord, PartialOrd, Eq, PartialEq)]<br>struct Point {<br> x: i32,<br> y: i32,<br>}<br><br>fn main() {<br> let p1 = Point { x: 1, y: 2 };<br> let p2 = Point { x: 3, y: 0 };<br> let larger = max(p1, p2);<br> println!(&quot;Larger point: ({}, {})&quot;, larger.x, larger.y); // (3, 0)<br>}</pre><p>Here, `Point` implements `Ord` via derive, allowing custom ordering (by default, lexicographical). The benefit? Compile-time safety: trying `max` on non-comparable types fails early. This practice enhances robustness — your generic code only accepts valid inputs — and promotes trait-oriented design, where behaviors are composable.</p><h3>Using the `AddAssign` Trait</h3><p>The `AddAssign` trait defines the `+=` operator, allowing in-place addition. It’s relevant in generic code for mutable operations on accumulators or counters, ensuring types support efficient modification without unnecessary allocations.</p><p>When designing generics, bounding with `AddAssign` lets you write functions that mutate values generically. For instance, a generic summer:</p><pre>use std::ops::AddAssign;<br><br>fn accumulate&lt;T: AddAssign + Default + Copy&gt;(values: &amp;[T]) -&gt; T {<br> let mut total = T::default();<br> for &amp;value in values {<br> total += value;<br> }<br> total<br>}<br><br>fn main() {<br> let ints = [1, 2, 3];<br> println!(&quot;{}&quot;, accumulate(&amp;ints)); // 6<br>let floats = [1.5, 2.5, 3.5];<br> println!(&quot;{}&quot;, accumulate(&amp;floats)); // 7.5<br>}</pre><p>(We use `Copy` for borrowing; more on that next.) This is useful in performance-sensitive code, like data processing pipelines, where in-place ops avoid cloning. Implementing `AddAssign` for custom types extends this to domains like vector math, making your APIs more operator-friendly.</p><h3>Using the `Copy` Trait</h3><p>The `Copy` trait marks types that can be duplicated trivially via bit-wise copy, without invoking `Clone`. It’s for small, cheap-to-copy types like primitives (`i32`, `bool`) or structs/enums containing only `Copy` fields. Implement it when your type’s semantics allow implicit copying — e.g., not for resources like files or large arrays.</p><p>In generics, `Copy` interacts by allowing value semantics without ownership transfer. Without it, generics might require `Clone`, which is costlier.</p><p>Example: A generic stack that pushes copies:</p><pre>#[derive(Debug, Copy, Clone)]<br>struct Pair&lt;T&gt;(T, T);<br><br>fn push_copy&lt;T: Copy&gt;(stack: &amp;mut Vec&lt;T&gt;, value: T) {<br> stack.push(value); // Copies value<br>}<br><br>fn main() {<br> let mut stack: Vec&lt;Pair&lt;i32&gt;&gt; = Vec::new();<br> let p = Pair(1, 2);<br> push_copy(&amp;mut stack, p);<br> push_copy(&amp;mut stack, p); // p still usable<br> println!(&quot;{:?}&quot;, stack); // [Pair(1,2), Pair(1,2)]<br>}</pre><p>Here, `Copy` shapes API design by enabling pass-by-value without moving, ideal for immutable data. Best practice: Derive `Copy` only for types under ~128 bytes; otherwise, use references or `Clone` to avoid hidden performance hits.</p><h3>A Better Way to Call Generics</h3><p>Idiomatic Rust favors explicitness in generics. When defining, use `where` clauses for complex bounds to improve readability:</p><pre>fn process&lt;T&gt;(data: T)<br>where<br> T: std::fmt::Debug + Clone,<br>{<br> let cloned = data.clone();<br> println!(&quot;{:?}&quot;, cloned);<br>}</pre><p>For calling, infer types when possible, but use turbofish (`::&lt;&gt;`) for ambiguity:</p><pre>let vec: Vec&lt;i32&gt; = (1..10).collect(); // Inferred<br>let parsed = &quot;42&quot;.parse::&lt;i32&gt;().unwrap(); // Turbofish needed</pre><p>Common mistakes: Over-bounding (e.g., requiring `Clone` when `&amp;T` suffices) leads to unnecessary constraints. Avoid by preferring references. Another pitfall: Ignoring lifetimes in generics — always specify `’a` when borrowing. Test generics with multiple types early to catch edge cases.</p><h3>Conclusion</h3><p>We’ve covered key best practices: Leverage generics for abstraction and reuse, constrain them with traits like `Ord` for safety, use `AddAssign` for mutable ops, apply `Copy` judiciously for efficient copying, and define/call generics idiomatically to avoid pitfalls.</p><p>These patterns aren’t just theory — they’ve streamlined my production codebases, reducing bugs and boosting maintainability. I encourage you to refactor a generic-heavy module in your next project. Experiment with bounds and see how they tighten your designs. Share your experiences in the comments — what’s your go-to trait bound?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=846bf9c1ba9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[It’s Important to Keep the Distinction Between the Per-Thread Call Stack and the Per-Method…]]></title>
            <link>https://medium.com/@kaly.salas.7/its-important-to-keep-the-distinction-between-the-per-thread-call-stack-and-the-per-method-22321dd27581?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/22321dd27581</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Sat, 21 Jun 2025 03:56:57 GMT</pubDate>
            <atom:updated>2025-06-21T03:56:57.471Z</atom:updated>
            <content:encoded><![CDATA[<h3>It’s Important to Keep the Distinction Between the Per-Thread Call Stack and the Per-Method Evaluation Stack Clear in Java Programming</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qDeO4yScv55UD5984JKtbg.png" /></figure><h3>Introduction: Why Stack Management Matters</h3><p>Stack management in Java might sound like a dry topic, but it’s a key part of writing efficient and bug-free code. The call stack and the evaluation stack are two core pieces of the Java Virtual Machine (JVM) that handle how your program runs. Messing them up can slow down your app or crash it with errors like the infamous StackOverflowError. Plus, debugging these issues without understanding stacks is like trying to fix a car without knowing what’s under the hood. In this article, we’ll break down these concepts with examples and tips to help you avoid common traps.</p><p>Think of the call stack as a stack of plates: each time you call a method, you add a plate. If you keep stacking without taking any off — like with infinite recursion — the pile collapses. That’s your StackOverflowError. The evaluation stack, meanwhile, is more like a calculator’s memory, crunching numbers and operations within a single method. Let’s explore how they work in the JVM.</p><h3>Architecture Overview: Call Stack vs. Evaluation Stack</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2hbq0cerYaG0UvC-sTARQ.png" /></figure><p>In Java, the JVM uses two types of stacks to manage your code:</p><ul><li><strong>Call Stack (Per-Thread):</strong> Every thread in your program gets its own call stack. It tracks the methods being executed by that thread. When a method is called, a “frame” is added to the top of the stack. When the method finishes, the frame is removed.</li><li><strong>Evaluation Stack (Per-Method):</strong> Each method frame has its own evaluation stack. This is where the JVM does its math — like adding numbers or comparing values — while running the method’s instructions.</li></ul><p>Here’s a simple conceptual model:</p><pre>Thread 1 Call Stack<br>+-----------------+<br>| Method A Frame  |<br>| - Local Vars    |<br>| - Eval Stack:   |<br>|   [5, 10, ...]  |<br>+-----------------+<br>| Method B Frame  |<br>| - Local Vars    |<br>| - Eval Stack:   |<br>|   [x, y, ...]   |<br>+-----------------+</pre><p>Each thread has its own call stack, and within that, each method has its own evaluation stack for temporary calculations. Next, let’s dig into how these stacks are used at runtime.</p><h3>Deep Dive: How Stacks Work and Common Pitfalls</h3><h3>Call Stack Allocation and Use</h3><p>When you start a thread in Java, the JVM gives it a call stack with a fixed size (adjustable with the -Xss flag). Each method call pushes a frame onto this stack, holding details like local variables and the current instruction. If you call too many methods without returning—like in a recursive loop—the stack runs out of space, triggering a StackOverflowError.</p><h3>Evaluation Stack Allocation and Use</h3><p>The evaluation stack lives inside each method’s frame. It’s where the JVM handles operations in your code. For instance, in int sum = 5 + 10;, the JVM pushes 5 and 10 onto the evaluation stack, adds them, and pops off the result (15). You don’t control this stack directly—it’s managed by the JVM’s bytecode execution.</p><h3>Common Pitfalls</h3><ol><li><strong>StackOverflowError:</strong> This happens when the call stack gets too deep, often from infinite recursion. It’s not about the evaluation stack — it’s about too many method calls piling up.</li><li><strong>Local Variable Confusion:</strong> Local variables live in the method frame, not the evaluation stack. The evaluation stack is just for temporary work, like calculating expressions. Mixing these up can lead to scope-related bugs.</li></ol><p>Now, let’s see these ideas in action with some code.</p><h3>Code Examples</h3><h3>1. Recursive Method and Stack Overflow</h3><p>This snippet shows how recursion can overflow the call stack.</p><pre>public class RecursiveExample {<br>    public static void main(String[] args) {<br>        try {<br>            recursiveMethod(1);<br>        } catch (StackOverflowError e) {<br>            System.out.println(&quot;Stack overflow occurred.&quot;);<br>        }<br>    }<br><br>public static void recursiveMethod(int depth) {<br>        System.out.println(&quot;Depth: &quot; + depth);<br>        recursiveMethod(depth + 1); // Keeps adding frames to the call stack<br>    }<br>}</pre><p><strong>Expected Output:</strong> After printing some depth values, you’ll see:</p><pre>Stack overflow occurred.</pre><p>The stack trace will list recursiveMethod over and over, showing the call stack’s collapse.</p><h3>2. Local Variables and the Evaluation Stack</h3><p>Here’s a simple example of the evaluation stack at work.</p><pre>public class EvaluationStackExample {<br>    public static void main(String[] args) {<br>        int a = 5;    // Stored in the method frame<br>        int b = 10;   // Stored in the method frame<br>        int sum = a + b; // JVM uses evaluation stack to add a and b<br>        System.out.println(&quot;Sum: &quot; + sum);<br>    }<br>}</pre><p><strong>Expected Output:</strong></p><pre>Sum: 15</pre><p>The JVM pushes a and b onto the evaluation stack, adds them, and stores the result in sum. The variables themselves stay in the frame.</p><h3>3. Multithreading and Per-Thread Call Stacks</h3><p>This shows how each thread has its own call stack.</p><pre>public class MultithreadedExample {<br>    public static void main(String[] args) {<br>        Thread thread1 = new Thread(() -&gt; {<br>            methodA(); // Runs on thread1’s call stack<br>        });<br>        Thread thread2 = new Thread(() -&gt; {<br>            methodB(); // Runs on thread2’s call stack<br>        });<br>        thread1.start();<br>        thread2.start();<br>    }<br><br>public static void methodA() {<br>        System.out.println(&quot;Method A called by &quot; + Thread.currentThread().getName());<br>        try {<br>            Thread.sleep(1000); // Simulate work<br>        } catch (InterruptedException e) {<br>            e.printStackTrace();<br>        }<br>    }<br>    public static void methodB() {<br>        System.out.println(&quot;Method B called by &quot; + Thread.currentThread().getName());<br>        try {<br>            Thread.sleep(1000); // Simulate work<br>        } catch (InterruptedException e) {<br>            e.printStackTrace();<br>        }<br>    }<br>}</pre><p><strong>Expected Output (order may vary):</strong></p><pre>Method A called by Thread-0<br>Method B called by Thread-1</pre><p>Each thread runs its method on its own call stack, so they don’t interfere.</p><h3>Summary &amp; Best Practices</h3><h3>Key Takeaways</h3><ul><li>The <strong>call stack</strong> is unique to each thread and tracks method calls.</li><li>The <strong>evaluation stack</strong> is part of each method and handles calculations.</li><li>A StackOverflowError means the call stack is too deep, not the evaluation stack.</li><li>Local variables live in the method frame, not the evaluation stack.</li></ul><h3>Best Practices</h3><ol><li>Avoid deep recursion — use loops instead when you can.</li><li>Test stack limits in multithreaded apps and tweak -Xss if needed.</li><li>Watch for infinite loops in recursive code during development.</li><li>Keep local variable scope clear to avoid confusion with the evaluation stack.</li></ol><h3>Further Reading</h3><ul><li><a href="https://docs.oracle.com/javase/specs/jvms/se17/html/index.html">Java Virtual Machine Specification</a></li><li><a href="https://www.baeldung.com/java-stack">Understanding the Java Stack</a></li><li><a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/">Java Multithreading Tutorial</a></li></ul><p>Mastering the difference between the call stack and evaluation stack will level up your Java skills. It’s not just about avoiding errors — it’s about writing smarter, faster code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22321dd27581" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rust Performance Optimizations Compared to Other Programming Languages]]></title>
            <link>https://medium.com/@kaly.salas.7/rust-performance-optimizations-compared-to-other-programming-languages-c2e3685163e2?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2e3685163e2</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Fri, 20 Jun 2025 03:39:04 GMT</pubDate>
            <atom:updated>2025-06-20T03:39:04.850Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ayrWbLHz8TaJNwgp8AcL0Q.png" /></figure><p>Rust is a modern programming language celebrated for its blend of performance, safety, and concurrency. As a systems programming language, it provides fine-grained control over hardware resources while ensuring memory safety. In this post, we’ll explore Rust’s key performance optimizations — zero-cost abstractions, monomorphization, inline assembly and SIMD, minimal runtime, and allocator and memory layout control — compared to languages like C++, Java, and Go. Each section includes executable code examples to illustrate these concepts, followed by a discussion of Rust’s limitations and trade-offs.</p><h3>1. Zero-cost Abstractions</h3><p>Rust’s zero-cost abstractions allow developers to use high-level features like iterators, pattern matching, and generics without runtime overhead. These abstractions compile down to efficient machine code, matching the performance of manually written low-level code.</p><p>Consider summing a vector’s elements using an iterator versus a manual loop:</p><pre>fn sum_vec_iterator(vec: &amp;Vec&lt;i32&gt;) -&gt; i32 {<br>    vec.iter().sum()<br>}<br><br>fn sum_vec_manual(vec: &amp;Vec&lt;i32&gt;) -&gt; i32 {<br>    let mut sum = 0;<br>    for i in 0..vec.len() {<br>        sum += vec[i];<br>    }<br>    sum<br>}<br><br>fn main() {<br>    let vec = vec![1, 2, 3, 4, 5];<br>    println!(&quot;Iterator sum: {}&quot;, sum_vec_iterator(&amp;vec));<br>    println!(&quot;Manual sum: {}&quot;, sum_vec_manual(&amp;vec));<br>}</pre><p>The iterator-based `sum_vec_iterator` is more expressive, yet Rust’s compiler optimizes it into a tight assembly loop, identical to the manual version. Tools like [Godbolt](<a href="https://godbolt.org/">https://godbolt.org/</a>) reveal that both produce equivalent machine code, proving the abstraction incurs no runtime cost. Unlike Java or Go, where abstractions might introduce overhead (e.g., iterator objects or runtime checks), Rust delivers C++-level performance with cleaner syntax.</p><h3>2. Monomorphization</h3><p>Rust handles generics via monomorphization, compiling a unique function for each concrete type used, eliminating runtime indirection like C++’s virtual tables (vtables). This ensures maximum performance at the cost of increased binary size.</p><p>Here’s a generic addition function:</p><pre>fn add&lt;T: std::ops::Add&lt;Output = T&gt;&gt;(a: T, b: T) -&gt; T {<br>    a + b<br>}<br><br>fn main() {<br>    let int_sum = add(1, 2);<br>    let float_sum = add(1.0, 2.0);<br>    println!(&quot;Integer sum: {}&quot;, int_sum);<br>    println!(&quot;Float sum: {}&quot;, float_sum);<br>}</pre><p>Rust generates separate `add` functions for `i32` and `f64`, ensuring direct calls with no overhead. Compare this to C++’s virtual functions:</p><pre>#include &lt;iostream&gt;<br><br>class Base {<br>public:<br>    virtual int add(int a, int b) = 0;<br>};<br><br>class IntAdd : public Base {<br>public:<br>    int add(int a, int b) override { return a + b; }<br>};<br><br>int main() {<br>    Base* adder = new IntAdd();<br>    int result = adder-&gt;add(1, 2);<br>    std::cout &lt;&lt; &quot;Sum: &quot; &lt;&lt; result &lt;&lt; std::endl;<br>    delete adder;<br>    return 0;<br>}</pre><p>In C++, the virtual call requires a vtable lookup, adding runtime overhead. Rust’s monomorphization avoids this, offering predictable, high performance, though it contrasts with Go’s interface-based polymorphism, which sacrifices speed for flexibility.</p><h3>3. Inline Assembly and SIMD</h3><p>Rust supports inline assembly via the `asm!` macro and SIMD (Single Instruction, Multiple Data) through `std::arch`, enabling developers to optimize performance-critical code.</p><h4>Inline Assembly</h4><p>A simple example adds two integers using assembly:</p><pre>use std::arch::asm;<br><br>fn add_asm(a: i32, b: i32) -&gt; i32 {<br>    let mut result: i32;<br>    unsafe {<br>        // Use a more portable assembly syntax that works across architectures<br>        #[cfg(target_arch = &quot;x86_64&quot;)]<br>        asm!(<br>            &quot;mov {0:e}, {1:e}&quot;,<br>            &quot;add {0:e}, {2:e}&quot;,<br>            out(reg) result,<br>            in(reg) a,<br>            in(reg) b,<br>        );<br>        <br>        #[cfg(target_arch = &quot;aarch64&quot;)]<br>        asm!(<br>            &quot;add {0:w}, {1:w}, {2:w}&quot;,<br>            out(reg) result,<br>            in(reg) a,<br>            in(reg) b,<br>        );<br>        <br>        #[cfg(not(any(target_arch = &quot;x86_64&quot;, target_arch = &quot;aarch64&quot;)))]<br>        {<br>            // Fallback for unsupported architectures<br>            result = a + b;<br>        }<br>    }<br>    result<br>}<br><br>fn main() {<br>    let sum = add_asm(3, 4);<br>    println!(&quot;Assembly sum: {}&quot;, sum);<br>}</pre><p>This fine-tuned control is rare in higher-level languages like Java or Go but aligns with C++’s inline assembly capabilities.</p><h4>SIMD</h4><p>For vectorized operations, here’s an example adding two float arrays:</p><pre>use std::arch::x86_64::*;<br><br>fn add_vectors(a: &amp;[f32], b: &amp;[f32], c: &amp;mut [f32]) {<br>    assert!(a.len() == b.len() &amp;&amp; b.len() == c.len());<br>    for i in (0..a.len()).step_by(4) {<br>        unsafe {<br>            let va = _mm_loadu_ps(&amp;a[i]);<br>            let vb = _mm_loadu_ps(&amp;b[i]);<br>            let vc = _mm_add_ps(va, vb);<br>            _mm_storeu_ps(&amp;mut c[i], vc);<br>        }<br>    }<br>}<br><br>fn main() {<br>    let a = [1.0, 2.0, 3.0, 4.0];<br>    let b = [5.0, 6.0, 7.0, 8.0];<br>    let mut c = [0.0; 4];<br>    add_vectors(&amp;a, &amp;b, &amp;mut c);<br>    println!(&quot;Result: {:?}&quot;, c);<br>}</pre><p>SIMD processes multiple elements simultaneously, offering significant speedups for large datasets. While C++ also supports SIMD via intrinsics, Rust’s safety guarantees and `std::arch` abstractions make it more accessible and less error-prone.</p><h3>4. Minimal Runtime</h3><p>Rust’s lack of a garbage collector and minimal runtime make it ideal for systems-level and bare-metal programming. Unlike Java, with its JVM overhead, or Go, with its garbage collector and goroutine scheduler, Rust imposes almost no runtime cost.</p><p>A simple “Hello, World!” in Rust:</p><pre>fn main() {<br>    println!(&quot;Hello, World!&quot;);<br>}</pre><p>Compiles to a tiny binary with instant startup, contrasting with Java’s JVM initialization or Go’s runtime setup. This lean approach suits embedded systems and performance-critical applications where C++ is a traditional competitor.</p><h3>5. Allocator and Memory Layout Control</h3><p>Rust offers custom allocators (e.g., `jemalloc`) and precise memory layout control via attributes like `#[repr(C)]`, optimizing for specific use cases.</p><h4>Custom Allocator</h4><p>Set `jemalloc` as the global allocator:</p><pre>use jemallocator::Jemalloc;<br><br>#[global_allocator]<br>static GLOBAL: Jemalloc = Jemalloc;<br><br>fn main() {<br>    println!(&quot;Using jemalloc allocator&quot;);<br>}</pre><p>This enhances performance in allocation-heavy scenarios, a flexibility Java and Go lack due to their managed runtimes.</p><h4>Memory Layout</h4><p>Control a struct’s layout:</p><pre>#[repr(C)]<br>struct AlignedStruct {<br>    a: u8,<br>    b: u32,<br>    c: u16,<br>}<br><br>fn main() {<br>    let s = AlignedStruct { a: 1, b: 2, c: 3 };<br>    println!(&quot;Struct: {} {} {}&quot;, s.a, s.b, s.c);<br>}</pre><p>`#[repr(C)]` ensures C-compatible alignment, critical for interoperability or cache optimization (e.g., with SIMD). C++ offers similar control, but Rust’s explicitness reduces accidental misalignment risks.</p><h3>6. Limitations and Trade-offs</h3><p>Rust’s performance comes with trade-offs. Its steep learning curve, driven by ownership and the borrow checker, can frustrate newcomers. For example, managing lifetimes in complex codebases requires practice. Additionally, while Rust’s ecosystem grows, it may lack the maturity of C++’s in domains like game development. Compile times can also be longer due to extensive optimizations, though the resulting binaries are highly efficient.</p><p>Compared to Java or Go, Rust demands more upfront effort for safety and performance, but the payoff is unparalleled control and speed in systems programming.</p><h3>Conclusion</h3><p>Rust’s performance optimizations — zero-cost abstractions, monomorphization, inline assembly and SIMD, minimal runtime, and memory control — set it apart from C++, Java, and Go. Its ability to deliver safe, efficient code makes it a standout choice for operating systems, game engines, and more. While challenges like the learning curve exist, Rust’s benefits make it a powerful tool for developers prioritizing performance.</p><p>Explore Rust further via its [official documentation](<a href="https://www.rust-lang.org/">https://www.rust-lang.org/</a>) and see how it can transform your next project!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e3685163e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Deep Dive into the Compacting Phase of Java Garbage Collection]]></title>
            <link>https://medium.com/@kaly.salas.7/a-deep-dive-into-the-compacting-phase-of-java-garbage-collection-597658006882?source=rss-9e73ed1acb89------2</link>
            <guid isPermaLink="false">https://medium.com/p/597658006882</guid>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Borelli Fotso]]></dc:creator>
            <pubDate>Sat, 07 Jun 2025 03:39:01 GMT</pubDate>
            <atom:updated>2025-06-07T03:39:01.140Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rCLF0d4X37haiJB3JCxRRQ.png" /></figure><p>As a Java developer, you’ve likely heard of <strong>Garbage Collection (GC)</strong> — the automatic memory management feature that frees you from manually deallocating memory. But have you ever wondered what happens during the <strong>compacting phase</strong> of GC? In this post, we’ll explore what compacting is, why it’s crucial for efficient memory management, and how it works across different GC algorithms. We’ll also use clear examples to illustrate how memory fragmentation occurs and how compacting resolves it, making the concept accessible even if you’re new to the topic.</p><h3>What is Compacting in Garbage Collection?</h3><p>In Java, the <strong>heap memory</strong> is where objects are allocated. Over time, as objects are created and garbage collected, the heap can become <strong>fragmented</strong>. Fragmentation occurs when free memory is scattered in small, non-contiguous blocks, making it difficult to allocate large objects even if there’s enough total free memory.</p><p><strong>Compacting</strong> is the process of moving live objects together in the heap to consolidate free space into larger, contiguous blocks. This eliminates fragmentation, ensuring that memory can be allocated efficiently for new objects.</p><p>Think of it like tidying up a messy room: by moving all the clutter to one side, you create a large, open space where you can easily place new items.</p><h3>Why is Compacting Important?</h3><p>Without compacting, fragmentation can lead to several issues:</p><ul><li><strong>Inefficient Memory Usage</strong>: Small free blocks may go unused, wasting memory.</li><li><strong>Allocation Failures</strong>: Even if there’s enough total free memory, large objects might not find a contiguous block big enough to fit.</li><li><strong>Performance Degradation</strong>: Frequent allocation failures can trigger more frequent GC cycles, impacting application performance.</li></ul><p>Compacting ensures that the heap remains organized, allowing for smooth and efficient memory allocation.</p><h3>How Compacting Works in Different GC Algorithms</h3><p>Java offers several GC algorithms, each with its own approach to compacting. Let’s explore how compacting is handled in some of the most common collectors.</p><h3>1. Serial GC</h3><p>The <strong>Serial GC</strong> is a stop-the-world, single-threaded collector. During its compacting phase:</p><ul><li>It identifies all live objects.</li><li>It moves these objects to one end of the heap, placing them contiguously.</li><li>The remaining space becomes a large, contiguous free block.</li></ul><p>This straightforward approach is effective but can lead to longer pause times since the application is stopped during the entire GC cycle.</p><h3>2. Parallel GC</h3><p>Similar to the Serial GC, the <strong>Parallel GC</strong> also performs compaction by moving live objects together. However, it uses multiple threads to speed up the process, reducing pause times compared to the Serial GC.</p><h3>3. G1 GC (Garbage-First)</h3><p>The <strong>G1 GC</strong> divides the heap into regions and focuses on collecting regions with the most garbage first. During compaction:</p><ul><li>It selects regions with a high proportion of garbage.</li><li>It evacuates live objects from these regions to other regions.</li><li>Once a region is evacuated, it becomes entirely free, effectively compacting the heap.</li></ul><p>This region-based approach allows for more incremental compaction, reducing pause times.</p><h3>4. ZGC (Z Garbage Collector)</h3><p><strong>ZGC</strong> is designed for low-latency applications. It performs most of its work, including compaction, concurrently with the application threads. During compaction:</p><ul><li>It uses a technique called “relocation” to move objects without stopping the application.</li><li>It updates references to the moved objects in a concurrent manner.</li></ul><p>This allows ZGC to maintain very short pause times, making it suitable for applications requiring high responsiveness.</p><h3>Illustrating Memory Fragmentation and Compacting</h3><p>To better understand how fragmentation occurs and how compacting resolves it, let’s look at some visual examples.</p><h3>Before Compacting</h3><p>Imagine the heap memory as a sequence of blocks:</p><pre>[Object A] [Free] [Object B] [Free] [Object C] [Free]</pre><p>Here, the free spaces are scattered between live objects, leading to fragmentation.</p><h3>After Compacting</h3><p>After the compacting phase:</p><pre>[Object A] [Object B] [Object C] [Free] [Free] [Free]</pre><p>Now, all live objects are moved together, and the free space is consolidated into a large, contiguous block.</p><h3>Fragmentation Preventing Allocation</h3><p>Suppose we need to allocate a new object that requires a contiguous block larger than any individual free block:</p><pre>[Object A] [Free (small)] [Object B] [Free (small)] [Object C] [Free (small)]</pre><p>Even if the total free memory is sufficient, the allocation might fail because there isn’t a single free block large enough.</p><h3>Compacting Enabling Allocation</h3><p>After compacting:</p><pre>[Object A] [Object B] [Object C] [Free (large)]</pre><p>Now, the large contiguous free space can easily accommodate the new object.</p><h3>Real-World Java Code Example</h3><p>Let’s consider a simple Java program that allocates objects of varying sizes, leading to fragmentation.</p><pre>public class MemoryFragmentationExample {<br>    public static void main(String[] args) {<br>        // Allocate several small objects<br>        Object obj1 = new byte[1024];<br>        Object obj2 = new byte[1024];<br>        Object obj3 = new byte[1024];<br>        <br>        // Deallocate some objects to create fragmentation<br>        obj2 = null;<br>        <br>        // Now, try to allocate a larger object<br>        try {<br>            Object largeObj = new byte[2048];<br>            System.out.println(&quot;Large object allocated successfully.&quot;);<br>        } catch (OutOfMemoryError e) {<br>            System.out.println(&quot;Failed to allocate large object due to fragmentation.&quot;);<br>        }<br>    }<br>}</pre><p>In this example, after deallocating obj2, there’s a free block between obj1 and obj3. If the heap is fragmented, allocating a larger object might fail even if there’s enough total free memory. However, with compacting, the GC would move obj1 and obj3 together, creating a larger free block for the new allocation.</p><h3>Conclusion</h3><p>Compacting is a vital phase in Java Garbage Collection that ensures efficient memory usage by eliminating fragmentation. By moving live objects together, it creates large, contiguous free spaces, making it easier to allocate new objects. Different GC algorithms implement compacting in various ways, from stop-the-world approaches to concurrent relocation, catering to different application needs.</p><p>Understanding compacting helps you appreciate the intricacies of memory management in Java and how the JVM works behind the scenes to keep your applications running smoothly.</p><p><strong>Key Takeaways:</strong></p><ul><li>Compacting consolidates free memory by moving live objects together.</li><li>It prevents allocation failures due to fragmentation.</li><li>Different GC algorithms handle compacting differently, balancing pause times and concurrency.</li></ul><p>Next time you encounter memory-related issues in your Java application, you’ll have a better understanding of how the GC’s compacting phase might be working to resolve them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=597658006882" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>