Performing retries 🧑🏻‍💻, Update/Delete Cascade ⛁, struct, interface & concurrency in go

By Prajwal Haniya

Techletter #49 | Novemeber 25, 2023

How to perform retries?

Performing retries is one of the important functionalities while performing a transaction, making an API call to a third-party application, etc. Imagine a scenario where you need to perform retries multiple times until you get a result or reach the maximum amount of retries. So, how can you implement using a simple while loop in JavaScript?

const maxRetires = 3;
let retries = 0;

const perfromTransaction = async () => {
	while (retries < maxRetries) {
		try {
				const result = await API_CALL();
				if (result.success) {
					return { success: true, message: 'Transaction performed successfully' }
				}
				rerurn { success: false, message: 'CHECK FOR WHY TRANSACTION FAILED' }
		} catch (error) {
			// if error occured while performing transaction then retry
			retries++;
		}
	}
	return { success: false, message: 'Failed to perfrom transaction even after retires' }
}

What do you mean by delete/update cascade?

On Delete Cascade:

The deletion of parent record will result in the removal of all child records entries, ensuring that there are no child records left after the parent is deleted.

On Update Cascade:

In the event of a change in the parent record id, the child records' parent_id will be updated. This is an uncommon occurrence and is only utilized in certain cases marked as UPDATE CASCADE .

Suppose you have a 4-digit parent ID that needs to be expanded to 10 digits. To accomplish this, using ON update cascade would enable you to modify the PRIMARY KEY value. Any tables that have foreign key reference s associated with this value will also be updated automatically.

Ref: https://copyprogramming.com/howto/difference-between-on-delete-cascade-on-update-cascade-in-mysql

Understanding Struct & interface in Go

Ref:

  1. https://www.golang-book.com/books/intro/9

  2. https://medium.com/@tim.chenbw/struct-and-interface-in-go-31d696bf3a17

package main

import "fmt"

type Shape interface {
	Area() float64
	Perimeter() float64
}

type Circle struct {
	Radius float64
}

func (c Circle) Area() float64 {
	return 3.14 * c.Radius * c.Radius
}

func (c Circle) Perimeter() float64 {
	return 2 * 3.14 * c.Radius
}

func printShapeInfo(s Shape) {
	fmt.Printf("Area: %f, Perimeter: %f\\n", s.Area(), s.Perimeter())
}

func main() {
	circle := Circle{Radius: 5}
	printShapeInfo(circle)
}

What is concurrency in go?

Unlike sequential programming, concurrent programming can make use of multiple CPU cores. This allows us to increase the work done by our program, by speeding up the program execution.

Concurrent programming is about writing instructions so that multiple tasks and processes can execute and interact at the same time.

Given the lightweight nature of goroutines, the premise of the language is that we should focus mainly on writing correct concurrent programs, letting Go’s runtime and hardware mechanics deal with parallelism. The principle is that if you need something to be done concurrently, create a goroutine to do it.

Ref: https://medium.com/codex/modern-concurrency-with-go-46e7c77afe25