Dependency Mock Server & Black Friday Preparation

Last year while preparing for Black Friday our team had an interesting challenge:

Some of our downstream services1 had proper setup to provide environments/APIs to load test.

This meant will have to configure our systems based on hopeful estimates based on last minute estimates and until then, we would could only sit idle. This wasn't appealing for obvious reasons of doing a hackjob and having a firefighting mode during Black Friday/Cyber Week.

Proposed Solution: Dependency Mock Server

The solution I came up with and which we used was to have a Mock Server that served endpoints similar to our dependencies and return dynamic responses based on request payload.

Server with Three Dependencies

Read more…

OT: Mapping thoughts, Memex & Zettelkasten.

Off Topic are posts where I talk about the human side of things.

Vannevar Bush, As We May Think

Have you read Vannevar Bush's article As We May Think? In that article he discusses an interesting concept:

"A System becoming an extension of human knowledge and memory which may be consulted with exceeding speed and flexibility."

Now this might not seem groundbreaking to us for we have Computers, Internet and Search Engines (Google, DDG, etc.). However it's important to know that Hypertext, which is the backbone of internet, was created thanks to the inspiration taken from Memex, which is the name of the system Bush proposed.

Read more…

Introducing Off Topic

I miss the days of yore,
where blogs had focus broad.
A mix of hobbies or talents varied,
a window into a person's psyche.

A journal of sorts where they write,
of things that spark their interests and delights.
Then came the internet of modern times,
that asked of everything, to be commercialized.

I protest, I detest,
this utilitarian web.
`Off Topic` is where,
I break this thread.

Visual guide to polling in Functional Programming (Scala)

In this post, let's look at how to poll a system using a Stream.

To make it easier/interesting, I will explain it using a visual approach.

Let's use the following problem statement.

  • We have API for a queue1 that can be queried using pollFn function.
  • We need to process data returned by queue using process function.
  • There may or may not be data at the time of querying.

    Read more…

FP for Sceptics: Option Type in Practice

In previous post we defined FP & error handling

Functional Programming (FP) is based around mathematical concepts like Type Theory - We define our system in terms of ADTs, data flow & functions.

FP promotes using types for error handling

  • Option
  • Either
  • Monad
  • etc.

Previous post also explained Option type and how it works.

ADTs in Practice took a practical system1 and designed ADTs for it.

In this post, we will reuse the same system but try to figure out where Option type makes most sense to use.

Option Type: Where to use it?

Read more…

FP for Sceptics: Introduction to Option Type

Functional Programming (FP) is based around mathematical concepts like Type Theory - We define our system in terms of ADTs, data flow & functions1.

We first implement "Happy Path" and then implement handlers for "Unhappy Path" (error handling). In ADTs in Practice we used "Exceptions" (IO.raiseError) for error handling.

However FP promotes using types for error handling, such as:

  • Option
  • Either
  • Monad
  • etc.

In this post we will start by looking at the simplest of these:

Option type denotes presence (Some(value)) or absence (None) of a value.

Option has Some(value) & None. What are two ways of using it?

Read more…

FP for Sceptics: ADTs in Practice

In previous post we defined ADTs as

Algebraic Data Types (or ADTs) model the flow of a program (or a system) in terms of data & functions that describe the complete behaviour and states the data can go through.

In this post, we will work through defining ADTs for an API service.

How to design ADT for a request-response flow?

API service will return User's Information by:

  • Extracting user id & password from the request
  • Checks them against an authorization service
  • Retrieves User's Information from database
  • Returns User Information in response

Read more…

FP for Sceptics: Introduction to ADTs (Algebraic Data Types)

Algebraic Data Types (or ADTs) model the flow of a program (or a system) in terms of data & functions that describe the complete behaviour and states the data can go through.

Let's take an example to grok the concept better.

Example: Area of a rectangle

Calculate the area of a rectangle from a list of positive integers

Here's a possible algorithm:

  • Take a list of integers (positive & negative)
  • Filter for positive integers ie., remove negative integers
  • Create pairs from the filtered list
  • Apply equation for area of rectangle

Read more…

Elegance of Monadic Composition

Functional programming has many interesting concepts but it can be hard to find practical applications for it in everyday work. In this post, I will explain how Monadic Composition can be used write elegant and easy to understand code.

Consider an API Donatron that accepts donations. The API's algorithm is as follows:

  1. Accepts donations as list of strings
  2. Should have a few valid integer donations
    • Else goto 6. Response: No Valid Ints
  3. Should have a few donations of value 10 or above
    • Else goto 6. Response: No Acceptable Donations Found
  4. Valid donations to external service should succeed
    • Else RuntimeException
  5. Log all accepted submissions
  6. Log Response
  7. Return Response

End goal is to be able to execute Donatron.donate function and get correct response.

Read more…

Elements of Coding Style

Rules of Software Engineering

There are three important rules in Software Engineering.

  • Rule 1: Easy to understand. Anyone should be able to pick up the code and start working on it.
  • Rule 2: Easy to debug. If the code breaks, it should be easy to quickly investigate & identify where is the issue.
  • Rule 3: Shipping is the most important feature. No matter how correct or elegant your code is, if you can't ship working code on time then why bother?

Having said that, Rule 3 should rarely be in opposition to Rule 1 & 2. If you have to veto Rule 1 & 2 to ship your feature, then something's horribly wrong in your code & process. Complexity in code is a given and it is an engineer's responsibility to figure out how best to contain the damage and stop it from affecting the rest of the system.

Less elegant yet easy to understand & easier to debug should be the minimum expectation from all code shipped.

Read more…