design patterns – Highly Available System | Transfer State Between Two Instances

If we had a highly available service running and that service required an update, I typically see that the service goes down for sometime, and then the new service comes up.

For discussion’s sake, let’s say this service tracks Virtual Machines on a host and takes actions with the virtualization layer (KVM/Hyper-V) based on some work queue, and also responds to user requests based on the work needed to be done.

I’m wondering if you could design some sort umbrella service that can act as a temporary interface to your service as the old version begins shutting down, and the new service begins to come up, so from the your service’s user’s perspective, an update is nearly seamless. What are some ideas on how to transfer state from Old Version -> Interface -> New Version?

design patterns – Prove that Feature Flags turned off Items in Production Environment

We want to implement Feature Deploy Flags, so Development Application Environment will have a new Product feature Toggled on. And Release and Production Environment Webpage, it is turned off.

Its toggled through appsettings.json file.

What Are Feature Flags?

Our clients, do not like Feature Flag, and want us to Different Source Control Git Branches. The argument is, “We Cannot Ensure, the Features are turned off or toggled off in the Production environment. How do you know there is not new code leakage?”

Well we took screenshots of our APIs not working in Swagger/Postman, additionally showed “Page is not Found in Webpage”, when browsing to new feature webpage.

What else can we do to ensure, Deploy Flag Feature are turned off? How would someone prove this?

It will be more confusing for developers to create New Source Control Branches for every toggle, etc.

argument patterns – passing splice instructions

Consider some passing splice instructions using start,stop,step pattern

r`data = Range@10;
r`f[data_, start_: 1, stop_: - 1, every_: 1] :=
  data[[start ;; stop ;; every]] 
  1. Is there a performance penalty if the splice is effectively a non-splice i.e. [[1;;-1;;1]] or [[1;;;;1]] or [[;;Length@data;;]] etc compared to say returning data? The reason I ask is if so, must I specifically skip splicing in such casses?

  2. Consider the case when multiple splice instrcutions have to be passed: start1,stop1,step1, start2,stop2,step2…. How to structure this compactly in func arguments? e.g. is there something better than r`f[data_, splice1_:{-1,1,1},...]

What Design Patterns for encouraging component reuse?

Looking for design patterns?

Design patterns are useful tools. But you should not choose them from a catalogue to solve your problems. Starting with the choice of a tool will make you a subject of the law of the instrument:

if you have a hammer in the hand, every problem will start to look like a nail.

You need to first design the reusable component. Maybe at a moment of your reengineering you’ll recognize a nail and take the hammer, and then a screw and take the screwdriver. The tool should be chosen to solve very specific parts of your design.

With design patterns, it’s the same: Try to rework the identified components to isolate them in a library with a well identified API that can be used in several projects. When doing so on your own, you’ll find some coupled elements glued together. Try to understand the reason and what you should do to go further. In some cases you may recognize a close match with some design patterns you know.

Or in need for some general design principles?

Believe it or not, isolating your identified features in self-contained components is a first step towards reuse. In this regard, some more general principles aim at facilitating decoupling of conponents, in particular:

  • Your component should be open for extension (to account for special needs) but closed for modification (because if you modify the component, for a specific application, it’s no longer reusable as such).
  • Your component should not depend on application-specific code. If there is some application specific dependency, you should invert this dependency, for example with dependency injection.

There are a couple of other such principles. Together they are called SOLID.

software – What are the Design Patterns that encourage component reuse?

Hi I am an Website and an App developer,
One of the major issues that I am facing is that certain components / features have to be redeveloped over and over again with a new project. For instance login, signup, user account settings etc with each new UI.

Is there a recommended method or a best practice (Design Pattern) where I could develop my code in a ‘Plug and Play’ method such that I don’t have to redevelop these components over an over again.

Modular Design Patterns in C

I have created a modular design pattern which provide a single interface that can be used create instances with swapable back-end components, however I’m not entirely satisfied with it.

My practical implementation involves creating a generic interface to certain kinds of device drivers. The hope is that I can create an interface which exposes a layer meant as an adapter (for initializing drivers which non-interface conforming implementations) and to bring up framework infrastructure.

To bring focus to the design pattern itself I am showing a simplified example.

The source code can be found here.

say I have some application with a main.c like so:

#include <stdio.h>
#include <stdlib.h>
#include "boatModuleIF.h"

int main(int argc, char *argv()) {

    char *type = argv(1);
    int typeNum = atoi(type);

    moduleIF_t *IF = init_moduleIF(typeNum);

    if (IF) {
    else {
        printf("Failed to init module");

    return 0;

I use boatModuleIFDefs as a header which is common to all boatModuleComponents (i.e. boatModule, boatModuleIF, cnc, beneteau)

It exposes the following to those components:


typedef struct moduleIF_CTX *moduleIF_CTX_t;

typedef struct {
    void (*printCfg)(moduleIF_CTX_t ctx);

    moduleIF_CTX_t ctx;
} moduleIF_t;

__attribute__((weak)) extern moduleIF_t *init_moduleIF_CNC();
__attribute__((weak)) extern moduleIF_t *init_moduleIF_Beneteau();

#endif // __MODULEIFDEFS_H_

Note that the init function for the CNC and Beneteau module interfaces are declared as weak symbols. This means that this generic code can expose a symbol which may or may not be defined. This is used as a proxy to determine whether our application has been compiled with the cnc.c “driver”.

with boatModuleIF’s header and source implemented as:


#include "boatModuleIFDefs.h"

enum {
    TYPE_CNC = 0,

moduleIF_t *init_moduleIF(int type);

#endif // __TESTMODULEIF_H_


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "boatModuleIFDefs.h"
#include "boatModuleIF.h"

moduleIF_t *init_moduleIF(int type) {
    switch (type) {
        case TYPE_CNC:
            if (init_moduleIF_CNC) {
                return init_moduleIF_CNC();
            else {
                printf("failed cnc");
                return 0;
        case TYPE_BENETEAU:
            if (init_moduleIF_Beneteau) {
                return init_moduleIF_Beneteau();
            else {
                printf("failed ben");
                return 0;
            return 0;

boatModule also has a defs file which looks like:

#ifndef __TESTMTRDEFS_H_
#define __TESTMTRDEFS_H_

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
    uint8_t  size;
    uint8_t  speed;
} boatCfg_t;

__attribute__((weak)) extern boatCfg_t dfltBoatCfg;

#endif // __TESTMTRDEFS_H_

Note that the dfltBoatCfg is defined as a weak symbol similarily to our init functions. This is useful if we have multiple kinds of configurations that may not be applicaple to all sailboats.

boatModule’s header and source implemented as:


#ifndef __TESTMODULE_H_
#define __TESTMODULE_H_
#include "boatModuleIFDefs.h"

moduleIF_t *init_moduleIF();

#endif // __TESTMODULE_H_


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "boatModuleIFDefs.h"
#include "boatModule.h"
#include "boatModuleDefs.h"

struct moduleIF_CTX {
    uint8_t size;
    uint8_t speed;

static void printCfg(moduleIF_CTX_t ctx) {
    printf("speed %d size %d", ctx->speed, ctx->size);

moduleIF_t *init_moduleIF() {
    struct moduleIF_CTX * const CTX = calloc(1, sizeof(struct moduleIF_CTX));

    if (&dfltBoatCfg) {
        CTX->size = dfltBoatCfg.size;
        CTX->speed = dfltBoatCfg.speed;

    moduleIF_t * const IF = (moduleIF_t *) calloc(1, sizeof(moduleIF_t));
    IF->ctx = CTX;
    IF->printCfg = printCfg;
    return IF;

Then we have two source files for two different boatModule implementations (which here are a stand-in for drivers)


#include "boatModuleDefs.h"

boatCfg_t dfltBoatCfg = {
    .size = 10,
    .speed= 10,


#include "boatModuleDefs.h"

boatCfg_t dfltBoatCfg = {
    .size = 5,
    .speed= 5,

The interesting part (imho) is really in the makefile however.


all: joinedModules

joinedModules: boatModule_Cnc.o boatModule_Beneteau.o boatModuleIF.o boatModuleDefs.h
    gcc boatModuleIF.o boatModule_Cnc.o boatModule_Beneteau.o main.c -o getBoatCfg

    gcc -c boatModuleIF.c

boatModule_Beneteau.o: boatModule.o beneteau.o boatModuleDefs.h
    ld -r beneteau.o boatModule.o -o boatModule_Beneteau.o
    objcopy --redefine-sym printCfg=printCfg_Beneteau boatModule_Beneteau.o
    objcopy --redefine-sym init_moduleIF=init_moduleIF_Beneteau boatModule_Beneteau.o
    objcopy --redefine-sym dfltBoatCfg=dfltBoatCfg_Beneteau boatModule_Beneteau.o

boatModule_Cnc.o: boatModule.o cnc.o boatModuleDefs.h
    ld -r cnc.o boatModule.o -o boatModule_Cnc.o
    objcopy --redefine-sym printCfg=printCfg_CNC boatModule_Cnc.o
    objcopy --redefine-sym init_moduleIF=init_moduleIF_CNC boatModule_Cnc.o
    objcopy --redefine-sym dfltBoatCfg=dfltBoatCfg_CNC boatModule_Cnc.o

boatModule.o: beneteau.o cnc.o boatModuleDefs.h
    gcc -c -fPIE boatModule.c

cnc.o: boatModuleDefs.h
    gcc -c -fPIE cnc.c

beneteau.o: boatModuleDefs.h
    gcc -c -fPIE beneteau.c

Essentially what I am doing is merging both the module object file and a backend component together to create a new merged object file. I can then redefine the global symbols used by both to prevent symbol collisions in such a way that maintains the “simplicity” of the generic module interface files.

This has some benefits, notably:

  • During initialization of the interface I don’t need some messy switch case which would have to check whether each type boat has each kind of boat config.
  • If I want to add new configuration’s there’s little that needs to be added (except for one addtional if statement in the moduleif init and the definition in the boat/driver that actually cares about that config).
  • If I want to add a new boat then all I need is to add a new init weak symbol and some tweaks to the makefile (really just adding the source name, the rest could be done automatically with make rules)

The issue is this doesn’t quiet feel right and I’m wondering if this is some kind of code smell?
If so is there some better/modified approach I could take would allow me to create this kind of modular design pattern that is future proofed against maintanence hell in the event that we should have to support many types of boats with many different configurations?

Any and all feedback is much appreciated.

design patterns – Reusable / temporary instances of an object / item

I am working on a design where I have containers that hold different types of objects. These objects are used within each other / dependent.
We are displaying them as a tree in a table format with columns that have attributes attached to the objects.

If objects in a container are set as public, those objects can be used /referenced in another container. Those objects are being displayed with a referenced icon in the object dedicated attribute column.

It is not clear enough for the users that the objects that are references, aren’t actually sitting in the container, but rather are just things used from somewhere else.

I need to display them in the container that references them because of complicated dependencies, usage between the objects, merging the dependencies between objects, etc so I am thinking to improve as follows:

  • reduce opacity / show a slightly different style for the referenced objects, and
  • offer a toggle that allows user to hide things that are from other containers.

I am not sure this is enough and I was wondering if any of you encountered something similar?
Do you have a better solution / pattern that would work for such a use case?

Note: users can filter by attributes, referenced or not, from the table header, but somehow that is not enough.

design patterns – How to organize functions that need to do multiple things?

How to organize functions that need to do multiple things? For example logout function in client applications. Logout function needs to do:

  • clear cookies,
  • clear local storage,
  • clear user state,
  • clear user theme style,
  • clear user specific feature,
  • some other things…

Last logout function that I wrote has 25 lines of code.

I think that there are better patterns to solve this problem, but I don’t now which…

design patterns – Versioning of data handling pipeline elements

I have developed a custom-made Python package, which provides 2 classes to play with: Stage and Step. They operate based on a Chain of Responsibility design pattern, and behave in a way where:

  • Both Stage and Step possess handle(request) method
  • Stage has a list of Stages or Steps underneath it
  • Step is the endpoint, which contains logic to be executed on the incoming request

In case of Stage, executing its handle(request) method will make it iterate over children elements in its list, and execute their handle(request) method while passing request argument in-between them (updated with each element it passes through).

In case of a Step, I have an abstract method logic(request), which needs to be overwritten by the user when inheriting from this class, in order to introduce some logic it needs to apply to incoming request argument.

Pipeline logic

Now, I would like to introduce some sort of version control mechanism inside of this pipeline, so that pipeline can identify itself with some unique marker, unique to the set of version and list of steps included in it. I need it, so that if let’s say I introduce some new steps into the pipeline in the future, I would like to be able to e.g. redo so older requests, which have been done with an older version of the pipeline.

I have been wondering about what sort of mechanisms I could leverage to get this done, but so far my only idea was to tag each step & stage with version attribute, like self._version = “1.0”, plus have the running script actually count number of steps+stages inside of the pipeline from Stage 0’s perspective, and somehow combine it with versions sum. While simple, this would require me to remember to up-rev version attribute on each step I would rework etc., and would be prone for an error.

I was wondering, if there are any alternative methods of doing something like this?

Another idea I came upon, was for a mechanism which would physically read class files each stage/step are instantiated from, and compile some sort of numeric representation of all characters in the file, which could be summed up and represented at the Stage 0 piece as a version of the pipeline. Dunno, if that is worth consideration though, since it is a blunt-approach to the problem.

design patterns – Forms in natural language

Natural language forms are much suited in short queries. Like i would like to book a ticket to “Choose Destination” on “Choose Date”. These are much intuitive and direct. You can check out Luke’s post on mad-libs form to learn more. But when it comes to long forms, it can increase the load on the user.

Standard forms are lot easier when it comes forms that has to be filled frequently. For an enterprise product like the one you are designing, label and form field will do more good than sentence with a form field. I would suggest you break the form into 3 simple steps with titles & progress tracker. Say for example : Adults, Children, Review. In each step you can have multiple fields based on your requirement.