Eval and Evalsha

Overview EVAL and EVALSHA are Redis commands used to execute Lua scripts within the Redis server. They differ primarily in how they handle script loading and execution: EVAL: This command takes the Lua script as an argument. Redis parses and hashes the script every time EVAL is called. This adds overhead, especially for frequently executed scripts. EVALSHA: This command takes the SHA1 hash of the Lua script as an argument. Redis retrieves the script from its internal cache using this hash....

October 2, 2024

Go Memory Allocation

How does Go handel memory allocation Memory Allocator Overview The Go memory allocator uses a hierarchical structure: Heap: The main memory area where dynamically allocated objects reside. Spans: Large blocks of memory (usually 8KB) used to allocate objects. Objects: Individual allocated pieces of memory. graph TD Heap --> Span1[Span 1] Heap --> Span2[Span 2] Heap --> Span3[Span 3] Span1 --> Obj1[Object 1] Span1 --> Obj2[Object 2] Span2 --> Obj3[Object 3] Span2 --> Obj4[Object 4] Span3 --> Obj5[Object 5] Size Classes Go uses size classes to group objects of similar sizes....

October 2, 2024

Redlock Algorithm

Redlock Algorithm: Distributed Lock Management Introduction The Redlock algorithm is a distributed lock algorithm designed to provide a reliable locking mechanism in distributed systems. It was proposed by the Redis community as a way to implement distributed locks using multiple independent Redis instances. Purpose The main purpose of the Redlock algorithm is to ensure that: Mutual exclusion is guaranteed Deadlock free operation is possible Fault tolerance is achieved up to a certain degree Algorithm Overview The Redlock algorithm uses multiple Redis instances (typically 5) to achieve consensus on lock acquisition and release....

October 2, 2024

Go's GMP Model

What is the GPM model in Go? GMP Model The GMP model is the cornerstone of Go’s runtime scheduler. G (Goroutine): Represents a goroutine, which is a lightweight thread of execution. Contains the stack, the instruction pointer, and other information important for scheduling. Many Gs can exist at the same time. P (Processor): Represents a logical processor, which can be thought of as a context for scheduling. Acts as a local scheduler, managing a queue of runnable goroutines....

October 1, 2024

Golang Interview

How does Go handle dependencies? Go uses a module system for dependency management. The go.mod file specifies the module’s dependencies and their versions. The go get command is used to download and install dependencies. What is the difference between go run and go build ? Feature go run go build Purpose Compile and run in one step Compile to a permanent executable Output Temporary executable (deleted) Permanent executable on disk Use Case Quick testing of small programs Building applications for deployment Performance Slower due to temporary compilation Faster execution of compiled binary Debugging Limited debugging capabilities Supports debugging and profiling Configuration Options None Various options for customization Use go run for quick tests and development....

September 14, 2024

System Design

How to Solve Cache Penetration? Cache penetration occurs when requests for non-existent data bypass the cache and hit the database repeatedly. Solutions include: Caching Empty Results: Store empty results for non-existent keys with a short expiration time. Bloom Filters: Use a Bloom filter to check if a key exists before querying the database. Rate Limiting: Limit requests for certain keys to prevent overwhelming the database with requests for non-existent data. How to Solve Cache Avalanche?...

September 13, 2024

Go Pprof

Overview of Pprof pprof is a tool that comes with Go’s standard library and is used for collecting and viewing profiling data. It can collect different types of profiles including: CPU Profile: Measures where the program spends most of its time. Memory Profile: Measures the amount of memory allocated and retained. Block Profile: Measures where the program spends time waiting for synchronization primitives. Mutex Profile: Measures contention on mutexes. Setting Up pprof To use pprof, you need to import the net/http/pprof package and set up HTTP server to serve the profiling data....

September 12, 2024

Redis, Pika, and Codis

Comprehensive Technical Analysis: Redis, Pika, and Codis Introduction In the realm of distributed data storage and caching systems, Redis, Pika, and Codis represent three distinct approaches to solving scalability, persistence, and performance challenges. This comprehensive analysis delves deep into the architectures, features, and use cases of these systems, providing detailed code examples and visual representations to facilitate a thorough understanding. Redis Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that has become a cornerstone in modern application architectures....

September 9, 2024

Git

GIT CHEAT SHEET CREATE Clone an existing repository: $ git clone ssh://user@domain.com/repo.git Create a new local repository: $ git init LOCAL CHANGES Changed files in your working directory: $ git status Changes to tracked files: $ git diff Add all current changes to the next commit: $ git add . Add some changes in <file> to the next commit: $ git add -p <file> Commit all local changes in tracked files: $ git commit -a Commit previously staged changes: $ git commit Change the last commit (do not amend published commits!...

September 8, 2024

Go Garbage Collection

How does garbage collection work in Go? Garbage Collection Go uses a concurrent, tri-color mark-and-sweep garbage collector with write barriers. Garbage Collection Phases Mark Setup: Preparation for marking phase. Marking: Identifying live objects. Mark Termination: Completion of marking phase. Sweep: Reclaiming memory from dead objects. graph LR A[Mark Setup] --> B[Marking] B --> C[Mark Termination] C --> D[Sweep] D --> A Tri-Color Algorithm Objects are divided into three sets: White: Potentially garbage objects....

September 7, 2024