Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eej_rate_limiter/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM arm64v8/openjdk:17-ea-16-jdk
FROM openjdk:17-jdk

ARG DEPENDENCY=build/dependency

Expand Down
2 changes: 1 addition & 1 deletion eej_rate_limiter/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.8'

services:
redis:
image: arm64v8/redis:7.4-bookworm # ARM64-compatible Redis
image: redis:7.4-bookworm
container_name: redis
ports:
- "6379:6379"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.enjoy.ds.ratelimiter.core;

import com.enjoy.ds.ratelimiter.core.model.APIRule;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.enjoy.ds.ratelimiter.core.model.APIRule;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public class User {
private String password; // Store hashed passwords!
private String role; // e.g., "ROLE_USER", "ROLE_ADMIN"

public static User copy(User original) {
public User(UUID uuid, String admin, String s, String admin1) {
}

public static User copy(User original) {
return new User(original.id, original.username, original.password, original.role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.List;
import java.util.Objects;

import lombok.NonNull;
import org.springframework.security.core.parameters.P;

public class SearchingUtil {
public static int searchMaximumElementLowerThan(
Expand All @@ -20,15 +18,15 @@ public static int searchMaximumElementLowerThan(
while (left < right) {
int mid = left + (right - left) / 2;

if(timestamps.get(mid) >= timestamp){
if (timestamps.get(mid) >= timestamp) {
right = mid;
} else if(timestamps.get(mid) < timestamp) {
} else if (timestamps.get(mid) < timestamp) {
res = mid;
left = mid + 1;
}
}

if(res != -1 && Objects.equals(timestamps.get(res), timestamp)){
if (res != -1 && Objects.equals(timestamps.get(res), timestamp)) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static org.mockito.Mockito.when;

import com.enjoy.ds.ratelimiter.cache.RedisService;
import com.enjoy.ds.ratelimiter.core.model.APIRule;
import com.enjoy.ds.ratelimiter.core.RateLimiterRuleService;
import com.enjoy.ds.ratelimiter.core.model.APIRule;
import com.enjoy.ds.ratelimiter.model.Post;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand Down
42 changes: 42 additions & 0 deletions key_value/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
Empty file added key_value/New Text Document.txt
Empty file.
40 changes: 40 additions & 0 deletions key_value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Distributed Key-Value Store Project
This project implements a distributed key-value store inspired by Chapters 5 (Sharding) and 6 (Data Partitioning and Replication) of System Design Interview: An Insider's Guide by Alex Xu. It is designed as a hands-on learning tool for system design concepts, particularly for junior-level interview preparation. The system runs three nodes on ports 8080, 8081, and 8082, using sharding to distribute data and timestamp-based versioning for conflict resolution.
Overview
The application is built with Java 17, Spring Boot, and Gradle, featuring a web-based interface to interact with the distributed store. Users can add and delete key-value pairs, with the data dynamically displayed across the nodes. The project emphasizes scalability, availability, and eventual consistency, aligning with distributed system design principles.
Current Implementation
Key Features

Sharding: Data is partitioned across three nodes using a simple hash-based sharding function (hash(key) % 3).
Versioning: Timestamps ensure last-write-wins conflict resolution for concurrent updates.
Distributed Architecture: Nodes communicate via HTTP to forward requests to the appropriate shard.

File Purposes

build.gradle.kts: Configures the Gradle build with Java 17 and Spring Boot dependencies. Ensures the project compiles and runs with the defined version (0.0.1-SNAPSHOT).
KeyValueStoreApplication.java: The main entry point that launches three nodes in separate threads on ports 8080, 8081, and 8082 (will do the dynamic node set-up later). Includes port availability checks and error handling for robust startup.
KeyValueEntry.java: Defines the data model for key-value pairs, including a timestamp for versioning.
NodeStorage.java: Manages in-memory storage for each node, providing methods to put, get, delete, and list key-value entries.
NodeConfig.java: Holds configuration details for each node (ID, port, and list of all nodes) for distributed communication.
KeyValueService.java: Implements the business logic, computing shard IDs, handling PUT, GET, and DELETE operations, and forwarding requests to the correct node.
KeyValueController.java: Exposes REST endpoints (/api/kv/put, /api/kv/get, /api/kv/delete, /api/kv/list) to interact with the service, with CORS support and detailed error handling.
index.html:

Build and Run:
Run gradle build to compile.
Access the interface at http://localhost:8080/.


Usage:
Add a key-value pair (e.g., key: user123, value: Alice) via the Computer section.
Delete a key and observe updates in the node tables and log.
Check the action log for results or errors.



Future Directions

Replication: Implement data replication across nodes for fault tolerance (Chapter 6), ensuring data availability if a node fails.
Consistent Hashing: Replace the current modulo-based sharding with consistent hashing to reduce rehashing costs during node additions/removals (Chapter 5).
ance learning and interview readiness.
![img.png](img.png)
34 changes: 34 additions & 0 deletions key_value/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id("org.springframework.boot") version "3.2.5"
id("io.spring.dependency-management") version "1.1.4"
java
id("org.jetbrains.kotlin.jvm") version "1.9.22" apply false // Optional, for Kotlin compatibility
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.withType<Test> {
useJUnitPlatform()
}

tasks.bootJar {
archiveFileName.set("distributed-key-value-store.jar")
}
Binary file added key_value/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions key_value/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Mon Apr 28 21:55:28 ICT 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading