Go is very powerful language for software development with its simplicity, concurrency, first-class functions and tools.

In Go, usually, a package is responsible only for one thing. Applications grows with several packages with their own responsibilities. When the number of packages in the project and responsibilities of the project increase, it is possible to mess up on communication with other packages. (In here, the communication refers to call functions on different packages/modules on the flow with/without waiting result. Like incrementing counter metric when an order received/full-filled)

One of the known ways of decoupling on communication is the event driven software…


Elixir EventBus is a library that allows different modules to communicate with each other without knowing about each other. A module/function can create an Event struct, and deliver to the EventBus without knowing which modules will consume.

Modules can also listen to events on the EventBus, without knowing who sent the events. Thus, modules can communicate without depending on each other. Moreover, it is very easy to substitute a consumer module. As long as the new module understands the Event struct that are being sent and received, the other modules will never know.

Get started with decoupled modules in 4 steps

Decoupled modules help us to write clear…


In microservice architecture, it is possible to get lost with logs, errors, monitors, etc... Having a common logging place is good practice to make the service logs query-able sequentially. In the microservice world each service is responsible for its own exceptions and logs.

Consider pipelined microservices which make calls to other microservices inside/outside the universe. If one call in the chain fails, we can expect that previous calls in the chain might raise errors as well, but how you can trace which request caused this error in the chain?

Demonstration

The quick solution is; if the request does not have a…


Event-driven architecture pattern is a distributed asynchronous architecture pattern to create highly scalable reactive applications. The pattern suits for every level application stack from small to complex ones. The main idea is delivering and processing events asynchronously.

There are a couple of ways to implement event-driven pattern to your stack. One of them is using “Broker Topology”. In broker topology, you send events to a central broker and all subscribers of these broker receive and process events asynchronously.

Event Bus with multiple subscribers(green arrows) and notifiers(red arrows)

In this story, I shared four implementations of event bus and with their goods and drawbacks. In all implementations, all event generators…


In chatbot world, while your audience keeps growing, your processing time should stay the same. The processing time should not increase in direct proportion or exponentially with the number of users.

Event-driven architectures consist of events and event handlers. Event handlers are the subscribers for the events occurred in internal/external systems. In details, one event may have more than one event handler. And also, each event handler may create one or more events after processing the event data. Serverless platforms give the opportunity to implement highly scalable, event-driven architectures without pain.

A Serverless Event Driven System Architecture for Chatbots

Chatbot Credentials

Chatbot credentials (app id, secret, page tokens, etc…) can…


Chatbot backend is not different than a regular backend, but there are some cheats to keep it efficient and responsive over the time. Basically, chatbots work with webhooks.

Let’s start with a simple question — “Do we need to process it immediately?” Yes/No!

High Level System Architecture of a Backend for a Chatbot (C: Client, W: Worker)

Webhook Security

In almost all bot platforms, every request comes with a signature, or token, in the ‘HTTP header’, and/or ‘query string’. Verify this token or signature and return 401(unauthorized), if the request does not pass your security requirements.

Use always SSL/TLS to secure transactions between servers.

Webhook Handling

For good and responsive web backend, fast data processing is a very…


Polymorphism is the provision of a single interface to entities of different types. Basically, it allows different data types respond to same function. So the same function shapes for different data types to accomplish the same behaviour. Elixir language has ‘protocols’ to implement polymorphism with a clean way.

Andrew Johnson / Getty Images

Lets implement a basic protocol that convert Kelvin and Fahrenheit temperatures to Celsius.

defmodule Kelvin do
defstruct name: "Kelvin", symbol: "K", degree: 0
end
defmodule Fahrenheit do
defstruct name: "Fahrenheit", symbol: "°F", degree: 0
end
defmodule Celsius do
defstruct name: "Celsius", symbol: "°C", degree: 0
end
defprotocol Temperature do @doc """ Convert…


Coding seems cool without error checks, does not it? When if/else checks effect the next execution behaviours, then the code becomes a mess. Luckily, there is a pattern called ‘Railway Oriented Programming’ for error handling. It simply helps you to focus on the happy path with SOLID principles like single responsibility and open-close while errors follow the failure pipe. Elixir language has ‘pattern matching on function level’ and ‘pipelining’ to apply ‘Railway Oriented Programming’ pattern.

Image Credit— params |> validate() |> update_db() |> send_email()

With Elixir’s ‘pattern matching’ and ‘reusable function names’, you can define the same function with different parameters with same and different arities. Let’s see what…


With powerful macros, Elixir language allows you to code magic without pain. Sometimes, you might need to run a snippet of code before and after execution of a block of code on several function calls. For instance, you need to measure the time elapsed in code blocks, then Elixir macros will run for help.

defmodule TimeFrame do
defmacro execute(name, units \\ :micro_seconds, do: yield) do
quote do
start = System.monotonic_time(unquote(units))
result = unquote(yield)
time_spent = System.monotonic_time(unquote(units)) - start
IO.puts("Executed #{unquote(name)} in #{time_spent} #{unquote(units)}")
result
end
end
end

Let’s run a piece of code and print automatically how much time was…


Elixir/Erlang concurrency model is based on the actor pattern which runs on top of BEAM(Erlang Virtual Machine) instead of running directly on Operating System. The actor pattern’s actor is an “Elixir Process” in Elixir programming language.

Anatomy of an actor (Elixir/Erlang Process)

An Elixir/Erlang Process

  • runs on top of BEAM (Erlang Virtual Machine)
  • has an address (pid)
  • is a completely isolated process
  • does not share memory
  • communicates with other processes by messages
  • has a private state (can be modified only private calculation functions / msg listeners)
  • might have private message listeners(calculation functions) to process incoming messages from inside and outside
  • has its own garbage collector

Elixir/Erlang Process’s Mailbox

Mustafa Turan

Go, Elixir, Ruby, Software Architecture, Microservices

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store