Basic Concurrency Patterns
Implement simple concurrent applications using process spawning, message reception, and explore basic patterns like client-server interactions.
Basic Concurrency Patterns is a free Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Concurrency Patterns in Erlang
Concurrency patterns are proven ways to structure how processes interact, so you build robust, predictable concurrent apps.
The Request-Response Pattern
The Request-Response pattern is a conversation: a client sends a request, the server does the work, then replies. The backbone of interactive services.
Building a Simple Server
A server process loops on receive, handling each message and replying with SenderPid ! Reply; self() gives it its own PID.
Code: Echo Server Example
This echo server receives any message, grabs the sender's PID, and sends the message right back tagged as echoed.
-module(echo_server).
-export([start/0, loop/0]).
start() ->
spawn(echo_server, loop, []).
loop() ->
receive
{FromPid, Message} ->
FromPid ! {self(), echoed, Message},
loop();
_ ->
io:format("Server received unknown message.~n"),
loop()
end.Code: Echo Client Interaction
The matching echo client spawns the server, sends {self(), msg} so the server knows where to reply, then waits for the response.
-module(echo_client).
-export([run/0]).
run() ->
ServerPid = echo_server:start(),
io:format("Server started with PID: ~p~n", [ServerPid]),
Request = "Hello Erlang!",
ServerPid ! {self(), Request},
io:format("Client sent: ~p to server ~p~n", [Request, ServerPid]),
receive
{_ServerPid, echoed, Response} ->
io:format("Client received reply: ~p~n", [Response]);
_ ->
io:format("Client received unexpected message.~n")
after 5000 ->
io:format("Client timed out waiting for reply.~n")
end.Running the Echo System
To run it, compile both modules and call echo_client:run() in the shell — a full request-response cycle across two processes.
The Asynchronous Pattern
The Asynchronous ("fire and forget") pattern: the client sends a message and moves on immediately while the receiver works in the background. Great for logging or jobs.
Code: Asynchronous Logger Process
This logger process just receives messages and prints them, sending no reply — the server side of the asynchronous pattern.
-module(async_logger).
-export([start/0, loop/0]).
start() ->
spawn(async_logger, loop, []).
loop() ->
receive
{log, Message} ->
io:format("LOG: ~p~n", [Message]),
loop();
_ ->
io:format("Logger received unknown message.~n"),
loop()
end.Code: Asynchronous Logger Client
The matching client spawns the logger, sends a message, and continues immediately without waiting — non-blocking by design.
-module(logger_client).
-export([run/0]).
run() ->
LoggerPid = async_logger:start(),
io:format("Logger started with PID: ~p~n", [LoggerPid]),
LoggerPid ! {log, "User X logged in."},
io:format("Client sent first log. Continuing...~n"),
timer:sleep(10), % Give logger a moment to process
LoggerPid ! {log, "User Y viewed profile."},
io:format("Client sent second log. Task complete.~n").Concurrency Pattern Check
You're building a system where a user uploads a large video. Your main process needs to immediately tell the user "Video uploaded, processing in background." while a separate process transcodes the video. What pattern best describes the interaction between the main process and the video transcoder?
Recap: Basic Concurrency Patterns
Recap: Request-Response waits for a reply (interactive work), while Asynchronous fires and forgets (background tasks). Your core concurrency toolkit.
Frequently asked questions
Is the “Basic Concurrency Patterns” lesson free?
Yes — the full text of “Basic Concurrency Patterns” is free to read here on the web, and the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Basic Concurrency Patterns”?
Implement simple concurrent applications using process spawning, message reception, and explore basic patterns like client-server interactions. You practise Erlang OTP: Distributed & Fault-Tolerant Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
No prior experience is required. Erlang OTP: Distributed & Fault-Tolerant Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic Concurrency Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson?
Yes. Every Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Erlang & VM
- Erlang Processes & Messaging
- Basic Concurrency Patterns
- Pattern Matching & Guards