跳到主要内容

35 篇博文 含有标签「deep-dive」

查看所有标签

Deep Dive into Java Core Multi-Threading Concepts

· 阅读需 5 分钟

Java multithreading is a fundamental part of the language that allows concurrent execution of multiple parts of a program to achieve greater efficiency and responsiveness. This article explores the latest Java version's core multithreading concepts, the Java Memory Model (JMM), thread lifecycle, key classes, and real-life examples.

1. Java Thread Model​

Java's thread model is based on the OS thread model, providing abstraction to manage and create threads directly. Java threads allow programs to perform multiple tasks concurrently, improving resource utilization. Thread priorities help in determining the order of thread execution, though scheduling ultimately depends on the underlying OS.

2. Java Memory Model (JMM)​

Java Thread Memory Model Diagram​

+-------------------+ +-------------------+
| | | |
| Main Memory |<----->| Thread 1 |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Thread 2 |<----->| Thread 3 |
| | | |
+-------------------+ +-------------------+

The Java Memory Model (JMM) defines how threads interact through memory and what values can be read or written by threads. The JMM provides:

  1. Visibility: Changes made by one thread are visible to other threads.
  2. Atomicity: Certain actions are indivisible or atomic.
  3. Ordering: The sequence in which instructions are executed.

JMM Key Concepts:​

  • Happens-Before Relationship: Ensures that memory writes by one specific action are visible to another specific action.
  • Volatile Keyword: Variables declared as volatile ensure visibility of changes across threads.
  • Synchronized Blocks: Using synchronized enforces an order of access to variables between threads.

3. Thread Lifecycle and State Machine​

Java threads go through several states:

StateDescription
NewThe thread is created but not yet started.
RunnableThe thread is eligible to run but may be waiting for a CPU cycle.
BlockedThe thread is waiting to acquire a lock to enter a synchronized block.
WaitingThe thread is waiting indefinitely for another thread to perform an action.
Timed WaitingThe thread is waiting for another thread for a specified period.
TerminatedThe thread has completed execution.

Thread State Machine Diagram​

NEW -> Runnable -> BLOCKED -> WAITING <-> TIMED_WAITING -> TERMINATED
+-------------------+ +-------------------+
| | | |
| NEW |------>| RUNNABLE |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| BLOCKED |<----->| WAITING |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| TIMED_WAITING |<----->| TERMINATED |
| | | |
+-------------------+ +-------------------+

4. Key Threading Classes in Java​

4.1 Thread Class​

The Thread class provides methods to create, start, and manage threads. You can define a custom thread by extending the Thread class and overriding its run() method.

Example:

public class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running");
}
}

MyThread thread = new MyThread();
thread.start();

4.2 Runnable Interface​

The Runnable interface is a functional interface representing a task to be executed on a thread. It separates the task from the thread itself, allowing reusability and flexibility.

Example:

public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable is running");
}
}

Thread thread = new Thread(new MyRunnable());
thread.start();

4.3 Callable and Future​

Callable allows tasks to return results or throw checked exceptions, while Future represents the result of an asynchronous computation.

Example:

Callable<Integer> task = () -> {
Thread.sleep(1000);
return 123;
};

ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(task);

Integer result = future.get(); // blocks until the result is available
executor.shutdown();

4.4 Executors Framework​

The Executors framework provides a higher-level API for thread management, thread pooling, and scheduling, abstracting the creation and management of threads.

Example:

ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executor.submit(() -> {
System.out.println("Task running");
});
}
executor.shutdown();

5. Synchronization and Locks​

Java provides several ways to control access to shared resources, including synchronized methods and blocks, the ReentrantLock class, and the ReadWriteLock.

Synchronized​

The synchronized keyword ensures that only one thread can access a method or block at a time.

public synchronized void increment() {
count++;
}

ReentrantLock​

ReentrantLock provides explicit lock mechanisms, giving more control over thread coordination.

Lock lock = new ReentrantLock();
lock.lock();
try {
// critical section
} finally {
lock.unlock();
}

6. Real-Life Production Example​

Consider a production scenario where we have multiple tasks needing parallel execution but require control over access to shared resources. Here’s a multithreaded solution using a ScheduledExecutorService in a banking transaction processing system.

import java.util.concurrent.*;

public class TransactionProcessor {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);

public void startProcessing() {
Runnable task = () -> {
// Simulate fetching and processing a transaction
System.out.println("Processing transaction: " + Thread.currentThread().getName());
try {
Thread.sleep(2000); // Simulating a transaction processing delay
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};

// Schedule at fixed rate
scheduler.scheduleAtFixedRate(task, 0, 5, TimeUnit.SECONDS);
}

public static void main(String[] args) {
TransactionProcessor processor = new TransactionProcessor();
processor.startProcessing();
}
}

Explanation​

  • We use ScheduledExecutorService for periodic transaction processing.
  • The thread pool’s scheduleAtFixedRate method ensures that the task runs every 5 seconds, enabling controlled execution with set intervals.
  • This design is beneficial in systems where tasks need continuous or scheduled processing but must prevent overloading resources.

This article provides an overview of multithreading concepts, memory models, core classes, and real-world examples. Java’s concurrency mechanisms offer flexibility and control, making it essential to use these constructs carefully to manage resources efficiently and avoid pitfalls such as deadlocks and race conditions.

Deep Dive into Java Core Multi-Threading

· 阅读需 6 分钟

This article explores Java’s thread pool concept, detailing each component and state involved. We’ll cover tasks, threads, workers, thread pools, and the state transitions that occur during task execution. Detailed diagrams and annotated code examples demonstrate how each part interacts, making this a practical reference for developing multithreaded Java applications.


1. Introduction​

Java’s ThreadPoolExecutor provides a robust framework for managing a pool of worker threads that can efficiently process a series of asynchronous tasks. It’s designed to handle tasks without creating new threads for every request, conserving resources and reducing overhead. This article breaks down each term and component involved in a Java thread pool to provide a clear, practical understanding.


2. Java Thread Pool: Overview and Components​

A thread pool is a collection of threads that are managed to execute tasks concurrently. Rather than creating a new thread for every task, a pool of threads is reused, allowing efficient execution without constant thread creation and destruction.

Here’s a textual diagram illustrating a typical Java thread pool architecture:

+-------------------------------------------+
| Thread Pool |
|-------------------------------------------|
| |
| Task Queue --> [Task1] [Task2] [TaskN] |
| |
| +-------------------------------+ |
| | Worker Thread | |
| |-------------------------------| |
| | |--Thread-1--|--Running--| | |
| | |--Thread-2--|--Runnable-| | |
| | |--Thread-N--|--Waiting--| | |
| +-------------------------------+ |
+-------------------------------------------+

3. Core Terms Explained​

Task​

  • Definition: A Runnable or Callable object representing a unit of work.
  • Purpose: The task contains logic that needs to be executed by a thread in the pool.

Thread​

  • Definition: An individual worker capable of executing a task.
  • States: Threads can be in various states, including NEW, RUNNABLE, WAITING, TIMED_WAITING, BLOCKED, and TERMINATED.

Worker​

  • Definition: An entity within the thread pool that wraps a thread, providing task management.
  • Role: Handles the execution of tasks by submitting them to a thread in the pool.

Thread Pool​

  • Definition: A collection of threads managed by an executor that oversees thread lifecycle and task allocation.
  • Purpose: To improve performance by reusing threads for multiple tasks, avoiding the cost of creating and destroying threads frequently.

4. Thread Lifecycle in a Thread Pool​

A thread in a pool goes through multiple states throughout its lifecycle. Let’s illustrate these states and transitions with a state diagram.

+-----------+
| NEW | -- Thread created
+-----------+
|
v
+-----------+
| RUNNABLE | -- Thread ready to execute
+-----------+
|
v
+-----------+
| RUNNING | -- Thread actively executing a task
+-----------+
|
v
+-----------+
| BLOCKED | -- Thread blocked, waiting for a resource
+-----------+
|
v
+-----------+
| WAITING | -- Thread waiting indefinitely for a condition
+-----------+
|
v
+-----------+
| TERMINATED| -- Thread completed execution or terminated
+-----------+

5. Thread State Transitions​

Each thread’s state changes as it progresses through its lifecycle:

  1. NEW: The thread is created but has not yet started.
  2. RUNNABLE: The thread is ready to run but is waiting for CPU availability.
  3. RUNNING: The thread is actively running its assigned task.
  4. BLOCKED: The thread is waiting to acquire a lock to access a synchronized resource.
  5. WAITING/TIMED_WAITING: The thread is waiting indefinitely or for a specified time, typically for task completion or resource availability.
  6. TERMINATED: The thread has completed its task and will either be recycled for a new task or discarded if no longer needed.

6. Sample Code and Explanation​

Below is a sample code demonstrating a thread pool with comments explaining each part of the process.

import java.util.concurrent.*;

public class ThreadPoolExample {
public static void main(String[] args) {
// Create a thread pool with a fixed number of threads
ExecutorService threadPool = Executors.newFixedThreadPool(3);

// Submit multiple tasks to the thread pool
for (int i = 1; i <= 5; i++) {
threadPool.submit(new Task(i));
}

// Shutdown the pool after tasks are completed
threadPool.shutdown();
}
}

// Task class implementing Runnable, representing a unit of work for the pool
class Task implements Runnable {
private final int taskId;

public Task(int taskId) {
this.taskId = taskId;
}

@Override
public void run() {
System.out.println("Executing Task " + taskId + " with " + Thread.currentThread().getName());
try {
Thread.sleep(2000); // Simulate task duration
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Task " + taskId + " completed.");
}
}

In this example:

  • ThreadPoolExecutor: Manages a pool of three threads.
  • Task: Each task simulates a job that runs for two seconds, allowing us to observe thread reuse.
  • Thread: Each task is assigned a thread by the thread pool, which either executes it immediately or queues it until a thread becomes available.

7. Handling TERMINATED Threads​

What Happens When a Thread Reaches TERMINATED?​

Once a thread in the pool completes its task and reaches the TERMINATED state, it does not automatically return to RUNNABLE. In a fixed pool, the terminated thread is replaced by a new one, ensuring the pool maintains its core size. In cached pools, the thread may be recycled if the task load increases, maintaining efficiency.

Reuse in Fixed Pools​

In fixed-size pools, a thread that terminates due to completion is usually retained within the pool as a worker for future tasks.

State Machine Transition​

The Java thread pool manages thread lifecycles by detecting TERMINATED threads and maintaining the required pool size by creating new threads as needed. This automatic management ensures the pool maintains availability and task-handling capacity.


8. Conclusion​

Java’s thread pool provides an efficient way to handle concurrent tasks by reusing threads and managing their lifecycles. Key concepts like tasks, threads, and workers work together to ensure a scalable, resource-efficient application structure. By understanding thread pool states, configurations, and transition mechanisms, developers can design more resilient and optimized concurrent applications.

Deep Dive into Spring Scheduler

· 阅读需 9 分钟

Introduction​

Spring Scheduler provides a simple way to schedule tasks within a Spring application. It offers flexibility in managing time-based events and task execution, allowing developers to configure tasks in a variety of ways, including sequential, parallel, and conditional execution.


Understanding Spring Scheduler Basics​

Spring Scheduler uses annotations to simplify the scheduling of methods within beans. The primary annotation used is @Scheduled, which can define various timing configurations, such as fixed rate, fixed delay, and cron expressions.

Enabling Scheduling​

Before defining tasks, enable scheduling in your Spring application by adding the @EnableScheduling annotation.

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SchedulingConfig {
// Any other scheduling-related configuration if needed
}

Simple Scheduling with @Scheduled​

The @Scheduled annotation can be applied to any method to enable scheduling. Below is a simple example:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SimpleScheduler {

@Scheduled(fixedRate = 5000)
public void printMessage() {
System.out.println("This message prints every 5 seconds.");
}
}

In this example, printMessage() runs every 5 seconds.


Scheduling Scenarios​

Spring Scheduler supports various scenarios for task scheduling, which can be useful for specific use cases in a production environment.

Sequential Job Execution​

In sequential execution, jobs run in a specific order, with each job waiting for the previous one to complete before starting.

Example: Sequential Scheduling Using Fixed Delay

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SequentialScheduler {

@Scheduled(fixedDelay = 10000)
public void job1() {
System.out.println("Job 1 started.");
// simulate a delay
try { Thread.sleep(2000); } catch (InterruptedException ignored) {}
System.out.println("Job 1 completed.");
}

@Scheduled(fixedDelay = 10000)
public void job2() {
System.out.println("Job 2 started after Job 1.");
// simulate a delay
try { Thread.sleep(3000); } catch (InterruptedException ignored) {}
System.out.println("Job 2 completed.");
}
}

With fixedDelay, Job 2 only begins after Job 1 completes.

Parallel Job Execution​

To execute jobs in parallel, we configure a task executor that runs multiple tasks concurrently.

Example: Parallel Scheduling Using a Custom Task Executor

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ParallelSchedulingConfig {

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2); // Number of concurrent tasks allowed
executor.setMaxPoolSize(5);
executor.initialize();
return executor;
}
}

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ParallelScheduler {

@Async
@Scheduled(fixedRate = 10000)
public void jobA() {
System.out.println("Parallel Job A started.");
}

@Async
@Scheduled(fixedRate = 10000)
public void jobB() {
System.out.println("Parallel Job B started concurrently with Job A.");
}
}

With @Async, jobA and jobB run concurrently, allowing for parallel execution.

Job Waiting Until Previous Job Completes​

To configure jobs to wait for previous jobs to finish, use fixedDelay with asynchronous methods to ensure that each task waits until its predecessor is completed.

Diagram: Job Waiting until Previous Job Completes

Job 1 ---> Wait ---> Job 2 ---> Wait ---> Job 3
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class WaitingScheduler {

@Scheduled(fixedDelay = 12000)
public void job() {
System.out.println("Job started and waits until the previous instance completes.");
// Simulate processing time
try { Thread.sleep(5000); } catch (InterruptedException ignored) {}
System.out.println("Job completed.");
}
}

In this scenario, each new execution waits until the previous one finishes.


Cron Expressions and Fixed Rates​

Spring Scheduler allows defining precise timing patterns using fixed rate, fixed delay, and cron expressions.

Fixed Rate vs Fixed Delay​

  • Fixed Rate: Executes a task at a specified interval, regardless of task completion time.
  • Fixed Delay: Executes a task with a delay between the end of the previous execution and the start of the next.
@Scheduled(fixedRate = 60000) // Executes every 60 seconds
public void fixedRateTask() {
System.out.println("Fixed Rate Task");
}

@Scheduled(fixedDelay = 60000) // Executes with a 60-second delay after completion
public void fixedDelayTask() {
System.out.println("Fixed Delay Task");
}

Using Cron Expressions​

Cron expressions allow precise scheduling based on time. Format: second minute hour day month weekday.

Example: Scheduling a Task for 5:00 AM Every Day

@Scheduled(cron = "0 0 5 * * ?")
public void dailyTask() {
System.out.println("This task runs at 5:00 AM every day.");
}

Conditional Scheduling with Annotations​

Spring allows scheduling tasks based on conditions. Combining @ConditionalOnProperty with @Scheduled, we can configure tasks to run only when certain conditions are met.

Conditional Scheduling Example​

This example schedules a job only if a specific property is set to true.

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")
public class ConditionalScheduler {

@Scheduled(fixedRate = 10000)
public void conditionalTask() {
System.out.println("This task runs only if 'scheduler.enabled=true'.");
}
}

To enable or disable this task, add scheduler.enabled=true or false in the configuration.


All-In-One Sample​

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableScheduling
public class ProductionSchedulerConfig {

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(5);
executor.initialize();
return executor;
}
}

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true", matchIfMissing = true)
public class ProductionScheduler {

@Async
@Scheduled(cron = "0 0 12 * * ?") // Executes at noon daily
public void dailySummaryJob() {


System.out.println("Executing daily summary job...");
}

@Async
@Scheduled(fixedDelay = 15000) // Executes every 15 seconds after the previous execution ends
public void periodicCleanupJob() {
System.out.println("Running periodic cleanup job...");
}
}

ConditionalOnProperty with Scheduled Job in Depth​

The @ConditionalOnProperty annotation in Spring is typically used to conditionally enable or disable beans based on specific configuration properties. In this case:

@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")

This will activate the annotated bean only if the scheduler.enabled property is set to true in your configuration (like in application.properties or environment variables).

Scenario: Running a Scheduled Job in Only One Pod in a Kubernetes Cluster​

When deploying a Spring application in a Kubernetes cluster across multiple containers (pods), ensuring that a scheduled job only runs in one specific pod can be tricky because Spring's @Scheduled jobs will run in every instance where the job bean is active. Here’s how to solve this problem by using the @ConditionalOnProperty annotation in combination with Kubernetes configurations.

Solution Outline​

  1. Set a Leader Pod: Configure only one of the pods to have scheduler.enabled=true using Kubernetes annotations and environment variables, making it the leader pod responsible for running the scheduled job.
  2. Configure Pods with Unique Environment Variables: Use Kubernetes configMaps or Secrets to assign the scheduler.enabled property as true only in the leader pod’s environment. Other pods should not have this property set to true, so they won’t run the job.

Steps to Implement​

  1. Create a ConfigMap or Secret for the Scheduler Property:

    • Define a ConfigMap that holds the scheduler.enabled setting for the lead pod.
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: scheduler-config
    data:
    scheduler.enabled: "true"
  2. Use Pod Annotations to Select the Leader Pod:

    • When deploying multiple replicas, use a label selector or custom script to ensure only one pod is given the scheduler.enabled=true setting.
  3. Configure the Deployment to Pass the scheduler.enabled Environment Variable:

    • In your Kubernetes Deployment file, you can use envFrom to load the environment variables from the ConfigMap, but with a selector so only one pod uses this ConfigMap.
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: scheduler-job
    spec:
    replicas: 3 # Running multiple instances
    selector:
    matchLabels:
    app: scheduler-app
    template:
    metadata:
    labels:
    app: scheduler-app
    spec:
    containers:
    - name: scheduler-container
    image: your-image
    envFrom:
    - configMapRef:
    name: scheduler-config
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
  4. Conditional Logic in Spring Boot:

    • With @ConditionalOnProperty, the @Scheduled method will only activate if the scheduler.enabled property is true. So, only the pod with the scheduler.enabled=true environment variable will run the job.
    @Component
    @ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")
    public class ScheduledJob {

    @Scheduled(fixedRate = 5000)
    public void performTask() {
    System.out.println("Scheduled job is running on the leader pod!");
    }
    }

Additional Consideration: Use Leader Election​

For robust setups, you could implement leader election to dynamically select the leader pod. This can be achieved using tools like Spring Cloud Kubernetes Leader Election or implementing a custom leader election mechanism based on a shared lock in an external data store, such as Redis or ZooKeeper.

By setting scheduler.enabled=true conditionally based on which pod is the elected leader, you can ensure the scheduled job runs in only one container at a time, with the ability to switch seamlessly if the leader pod goes down.

Conclusion​

Spring Scheduler provides flexibility and control over task scheduling. By using @Scheduled with different configurations, we can create complex scheduling workflows that address specific business needs.

Deep Dive Redis Integration With Spring Boot Application

· 阅读需 12 分钟

Table of Contents​


Introduction​

In distributed systems, caching is a critical mechanism to improve performance by reducing the load on backend services and databases. This article delves into the design and implementation of cache mechanisms for distributed systems, covering high-level architecture, detailed designs, and sample code for various scenarios.


High-Level Architecture Design​

Diagram​

graph TD
A[Client] --> B[Load Balancer]
B --> C[Service Layer]
C --> D[Cache Layer]
C --> E[Data Storage Layer]
D --> E
F[Monitoring and Metrics] --> C
F --> D
F --> E

Explanation​

  1. Client: The client sends requests to the distributed system.
  2. Load Balancer: Distributes incoming requests across multiple instances of the service layer.
  3. Service Layer: Handles business logic and interacts with the cache and data storage layers.
  4. Cache Layer: Stores frequently accessed data to reduce the load on the data storage layer.
  5. Data Storage Layer: The primary data store (e.g., database) where data is persisted.
  6. Monitoring and Metrics: Collects and monitors performance metrics for the entire system.

Detail Designs​

Cache Layer​

The cache layer is responsible for storing frequently accessed data to reduce the load on the data storage layer. It can be implemented using distributed caching solutions like Redis or Hazelcast.

Redis Setup and Integration​

Redis is an in-memory data structure store that can be used as a distributed cache. It supports various data structures like strings, hashes, lists, sets, and sorted sets.

Step 1: Install Redis​

You can install Redis on your local machine or use a cloud-based Redis service like AWS ElastiCache or Redis Labs.

For local installation on Linux:

sudo apt-get update
sudo apt-get install redis-server

For local installation on macOS:

brew install redis
Step 2: Start Redis Server​

Start the Redis server:

redis-server
Step 3: Verify Redis Installation​

Verify that Redis is running:

redis-cli ping

If Redis is running correctly, you should see PONG as the response.

Redis Configuration​

Redis configuration can be managed through the redis.conf file. Common configurations include setting the maximum memory limit, configuring persistence, and setting up replication.

Example redis.conf snippet:

maxmemory 256mb
maxmemory-policy allkeys-lru

Redis Integration with Spring Boot​

To integrate Redis with a Spring Boot application, you need to add the necessary dependencies and configure the Redis connection.

Step 1: Add Dependencies​

Add the following dependencies to your pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Step 2: Configure Redis Connection​

Configure the Redis connection in your application.yml:

spring:
redis:
host: localhost
port: 6379
cache:
type: redis
Step 3: Enable Caching​

Enable caching in your Spring Boot application by adding the @EnableCaching annotation to your main application class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Step 4: Use Redis Cache​

Use the @Cacheable annotation to cache method results in Redis:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class CacheService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from data storage layer
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Best Practices for Redis in Kubernetes​

When deploying Redis in Kubernetes, it's important to follow best practices to ensure high availability, scalability, and security.

Step 1: Deploy Redis in Kubernetes​

You can deploy Redis in Kubernetes using a StatefulSet for stateful applications or a Deployment for stateless applications. Here's an example of deploying Redis using a StatefulSet:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
volumeMounts:
- name: redis-data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
Step 2: Create a Headless Service​

Create a headless service to manage the Redis StatefulSet:

apiVersion: v1
kind: Service
metadata:
name: redis
spec:
clusterIP: None
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
Step 3: Configure Redis Access in Spring Boot​

When deploying your Spring Boot application in Kubernetes, you should use the service name to access Redis. Update your application.yml to use the service name:

spring:
redis:
host: redis
port: 6379
cache:
type: redis
Step 4: Enable Redis Persistence​

To enable Redis persistence, configure the redis.conf file to use either RDB (Redis Database Backup) or AOF (Append-Only File) persistence:

save 900 1
save 300 10
save 60 10000
appendonly yes
Step 5: Secure Redis​

Secure Redis by setting a password and enabling TLS:

requirepass yourpassword
tls-port 6379
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt
Step 6: Monitor Redis​

Use Prometheus and Grafana to monitor Redis performance and health. Deploy a Redis exporter to expose Redis metrics:

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-exporter
spec:
replicas: 1
selector:
matchLabels:
app: redis-exporter
template:
metadata:
labels:
app: redis-exporter
spec:
containers:
- name: redis-exporter
image: oliver006/redis_exporter:latest
args:
- --redis.addr=redis:6379
- --redis.password=yourpassword
ports:
- containerPort: 9121

Create a service to expose the Redis exporter:

apiVersion: v1
kind: Service
metadata:
name: redis-exporter
spec:
selector:
app: redis-exporter
ports:
- port: 9121
targetPort: 9121

Data Storage Layer​

The data storage layer is the primary data store where data is persisted. It can be a relational database, NoSQL database, or any other storage solution.

Diagram​

graph TD
A[Service Layer] --> B[Data Storage Layer]
B --> C[Relational Database]
B --> D[NoSQL Database]

Sample Code​

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {

public User findById(Long userId) {
// Database query logic
return new User();
}

public void save(User user) {
// Database save logic
}
}

Service Layer​

The service layer handles business logic and interacts with the cache and data storage layers. It is responsible for managing the flow of data between the client, cache, and data storage.

Diagram​

graph TD
A[Client] --> B[Service Layer]
B --> C[Cache Layer]
B --> D[Data Storage Layer]

Sample Code​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private CacheService cacheService;

@Autowired
private UserRepository userRepository;

public User getUserById(Long userId) {
User user = cacheService.getUserById(userId);
if (user == null) {
user = userRepository.findById(userId);
cacheService.saveUser(user);
}
return user;
}

public void saveUser(User user) {
userRepository.save(user);
cacheService.saveUser(user);
}
}

Load Balancer​

The load balancer distributes incoming requests across multiple instances of the service layer to ensure high availability and scalability.

Diagram​

graph TD
A[Client] --> B[Load Balancer]
B --> C[Service Instance 1]
B --> D[Service Instance 2]
B --> E[Service Instance 3]

Sample Code​

Load balancers are typically implemented using infrastructure components like NGINX, HAProxy, or cloud-provided load balancers (e.g., AWS ELB, Google Cloud Load Balancer).

Monitoring and Metrics​

Monitoring and metrics collection are essential for tracking the performance and health of the distributed system. Tools like Prometheus, Grafana, and ELK stack can be used for this purpose.

Diagram​

graph TD
A[Service Layer] --> B[Monitoring and Metrics]
B --> C[Prometheus]
B --> D[Grafana]
B --> E[ELK Stack]

Sample Code​

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;

public class MetricsCollector {

private static final Counter cacheHits = Counter.build()
.name("cache_hits_total")
.help("Total number of cache hits.")
.register();

private static final Counter cacheMisses = Counter.build()
.name("cache_misses_total")
.help("Total number of cache misses.")
.register();

private static final Gauge cacheSize = Gauge.build()
.name("cache_size")
.help("Current size of the cache.")
.register();

public static void recordCacheHit() {
cacheHits.inc();
}

public static void recordCacheMiss() {
cacheMisses.inc();
}

public static void setCacheSize(long size) {
cacheSize.set(size);
}
}

Complex Scenario 1: Flow Control and Backpressure Handling​

Problem Statement​

In high-traffic scenarios, the cache might become a bottleneck if not properly managed. Flow control and backpressure handling are essential to prevent the cache from being overwhelmed.

Solution Architecture​

  1. Rate Limiting: Implement rate limiting to control the number of requests hitting the cache.
  2. Circuit Breaker: Use a circuit breaker to handle failures gracefully and prevent cascading failures.
  3. Backpressure Handling: Implement backpressure mechanisms to manage the flow of data between services.

Flow Chart​

graph TD
A[Client Request] --> B{Rate Limiter}
B -->|Allowed| C[Cache Lookup]
B -->|Denied| D[Fallback to Database]
C --> E{Cache Hit?}
E -->|Yes| F[Return Cached Data]
E -->|No| G[Fetch from Database]
G --> H[Update Cache]
H --> F
D --> G

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Complex Scenario 2: Cache Invalidation and Data Synchronization​

Problem Statement​

Ensuring that the cache remains consistent with the underlying data store is challenging, especially in distributed systems.

Solution Architecture​

  1. Event-Driven Invalidation: Use event-driven mechanisms to invalidate the cache when data changes.
  2. Distributed Locking: Use distributed locking to ensure that only one service updates the cache at a time.
  3. TTL-Based Invalidation: Use Time-to-Live (TTL) to automatically invalidate cache entries after a certain period.

Sequence Diagram​

sequenceDiagram
participant Client
participant Service
participant Cache
participant Database

Client->>Service: Update User
Service->>Database: Update User in Database
Database-->>Service: User Updated
Service->>Cache: Invalidate User Cache
Cache-->>Service: Cache Invalidated
Service-->>Client: User Updated

Sample Code​

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@CacheEvict(value = "userCache", key = "#userId")
public void updateUser(Long userId, User user) {
// Update user in database
updateUserInDatabase(userId, user);
}

private void updateUserInDatabase(Long userId, User user) {
// Database update logic
}
}

Complex Scenario 3: Cache Stampede Prevention​

Problem Statement​

A cache stampede occurs when multiple requests for the same data hit the cache simultaneously, leading to a spike in database load.

Solution Architecture​

  1. Probabilistic Early Expiration: Use probabilistic early expiration to spread out cache expiration times.
  2. Locking Mechanism: Implement a locking mechanism to ensure that only one request populates the cache at a time.
  3. Background Refresh: Use background refresh to keep the cache up-to-date without impacting the user experience.

Flow Chart​

graph TD
A[Client Request] --> B{Cache Hit?}
B -->|Yes| C[Return Cached Data]
B -->|No| D{Lock Acquired?}
D -->|Yes| E[Fetch from Database]
D -->|No| F[Wait for Lock]
E --> G[Update Cache]
G --> C
F --> B

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class UserService {

private final Lock lock = new ReentrantLock();

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
try {
lock.lock();
// Double-check to ensure the cache is still empty
User cachedUser = fetchUserFromCache(userId);
if (cachedUser != null) {
return cachedUser;
}
// Fetch user from database
User user = fetchUserFromDatabase(userId);
// Update cache
updateCache(userId, user);
return user;
} finally {
lock.unlock();
}
}

private User fetchUserFromCache(Long userId) {
// Cache lookup logic
return null;
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}

private void updateCache(Long userId, User user) {
// Cache update logic
}
}

Conclusion​

Caching is a powerful mechanism for improving the performance and scalability of distributed systems. By understanding the high-level architecture and detailed designs, you can effectively implement cache mechanisms that handle various complex scenarios like flow control, cache invalidation, and cache stampede prevention. Use the provided sample code and diagrams to implement efficient caching in your distributed system.

Deep Dive Test Automation of Spring Boot Application

· 阅读需 8 分钟

Testing Spring Boot applications is crucial for validating functionality across the persistence, service, and controller layers, especially when deployed in containerized environments. This guide covers everything from configuring each layer’s tests to handling parallel endpoint testing and leveraging Docker for containerized environments.



1. Overview​

Testing a Spring Boot application ensures each layer functions as expected and that the system performs cohesively. Tests should cover:

  • Persistence Layer: Verifying data access and manipulation.
  • Service Layer: Ensuring business logic functions correctly.
  • Controller Layer: Testing API endpoints and interactions.

This article includes testing strategies for both development and containerized environments, plus configurations for parallel endpoint testing.


2. Testing the Persistence Layer​

The persistence layer involves data storage and access. To test without connecting to production databases, use embedded databases for unit testing and Testcontainers for more complex integration tests.

Embedded Database for Testing​

Embedded databases (like H2) are ideal for simple, fast tests:

  1. Add H2 dependency in pom.xml:

    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
    </dependency>
  2. Application configuration for testing (src/test/resources/application-test.yml):

    spring:
    datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:
    jpa:
    hibernate:
    ddl-auto: create-drop
    show-sql: true
  3. Persistence Layer Test:

    @SpringBootTest
    @Transactional
    public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSaveUser() {
    User user = new User("John Doe", "john.doe@example.com");
    User savedUser = userRepository.save(user);
    assertNotNull(savedUser.getId());
    }
    }

Integration Testing with Testcontainers​

For more realistic integration tests with databases (e.g., PostgreSQL), use Testcontainers:

  1. Add Testcontainers dependency:

    <dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>postgresql</artifactId>
    <version>1.16.2</version>
    <scope>test</scope>
    </dependency>
  2. Set up Testcontainers for PostgreSQL:

    @SpringBootTest
    public class UserRepositoryTest {

    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
    .withDatabaseName("testdb")
    .withUsername("user")
    .withPassword("password");

    @DynamicPropertySource
    static void configureProperties(DynamicPropertyRegistry registry) {
    postgres.start();
    registry.add("spring.datasource.url", postgres::getJdbcUrl);
    registry.add("spring.datasource.username", postgres::getUsername);
    registry.add("spring.datasource.password", postgres::getPassword);
    }

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindUserById() {
    // Test logic
    }
    }

3. Testing the Service Layer​

The service layer holds business logic. Use mocks for isolated unit tests and real dependencies for integration tests.

Unit Tests with Mocks​

  1. Define a test with @MockBean:
    @ExtendWith(MockitoExtension.class)
    public class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    public void testCreateUser() {
    User user = new User("Alice", "alice@example.com");
    when(userRepository.save(any(User.class))).thenReturn(user);

    User savedUser = userService.createUser(user);
    assertEquals("Alice", savedUser.getName());
    }
    }

Integration Testing with Dependencies​

For full service layer integration:

@SpringBootTest
@Transactional
public class UserServiceIntegrationTest {

@Autowired
private UserService userService;

@Test
public void testFindAllUsers() {
List<User> users = userService.findAllUsers();
assertNotNull(users);
}
}

4. Testing the Controller Layer​

Controller tests validate API endpoints. MockMvc is used for isolated testing, while containerized setups allow testing real endpoints.

MockMvc for Isolated Controller Testing​

MockMvc allows API testing without starting a full web server.

@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
public void testGetUser() throws Exception {
when(userService.getUserById(anyLong())).thenReturn(new User(1L, "Alice"));

mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"));
}
}

Testing Real Endpoints in a Container​

For real endpoint testing, use RestTemplate with a server running on a random port:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerIntegrationTest {

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate restTemplate;

@Test
public void testGetUser() {
ResponseEntity<User> response = restTemplate.getForEntity("http://localhost:" + port + "/users/1", User.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}

Parallel Testing for API Endpoints​

When testing multiple endpoints simultaneously, use RANDOM_PORT to prevent port conflicts.


5. End-to-End Testing in Containerized Environments​

Using Docker Compose, spin up services to perform full integration tests in a controlled environment.

Using Docker Compose for Integration Testing​

  1. Docker Compose File:

    version: '3'
    services:
    app:
    image: my-spring-app:latest
    ports:
    - "8080:8080"
    environment:
    - SPRING_PROFILES_ACTIVE=test
    postgres:
    image: postgres:13
    environment:
    POSTGRES_USER: user
    POSTGRES_PASSWORD: password
    POSTGRES_DB: testdb
  2. Run and Tear Down Tests:

    docker-compose up -d
    # Run integration tests
    docker-compose down

6. Sample Code and Configuration Summary​

This section provides practical code snippets and configurations to help automate testing in Spring Boot applications. The examples cover the persistence layer, service layer, controller layer, and containerized testing.

Maven Dependencies​

Make sure to include the necessary dependencies in your pom.xml for testing:

<dependencies>
<!-- Spring Boot Starter for Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- H2 Database for In-Memory Testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!-- Testcontainers for Integration Testing -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Sample Application Properties​

Define properties for the test environment in src/test/resources/application-test.yml:

spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true

Persistence Layer Test Example​

Example of testing a repository with an embedded H2 database:

@SpringBootTest
@Transactional
public class UserRepositoryTest {

@Autowired
private UserRepository userRepository;

@Test
public void testSaveUser() {
User user = new User("John Doe", "john.doe@example.com");
User savedUser = userRepository.save(user);
assertNotNull(savedUser.getId());
}
}

Service Layer Test Example​

Example of unit testing the service layer with Mockito:

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

@Test
public void testCreateUser() {
User user = new User("Alice", "alice@example.com");
when(userRepository.save(any(User.class))).thenReturn(user);

User savedUser = userService.createUser(user);
assertEquals("Alice", savedUser.getName());
}
}

Controller Layer Test Example​

Example of testing a controller using MockMvc:

@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
public void testGetUser() throws Exception {
when(userService.getUserById(anyLong())).thenReturn(new User(1L, "Alice"));

mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"));
}
}

Integration Testing with Testcontainers​

Example of a test that uses Testcontainers to spin up a PostgreSQL instance for integration tests:

@SpringBootTest
public class UserRepositoryIntegrationTest {

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("testdb")
.withUsername("user")
.withPassword("password");

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
postgres.start();
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Autowired
private UserRepository userRepository;

@Test
public void testFindUserById() {
User user = new User("Alice", "alice@example.com");
userRepository.save(user);

Optional<User> foundUser = userRepository.findById(user.getId());
assertTrue(foundUser.isPresent());
assertEquals("Alice", foundUser.get().getName());
}
}

Docker Compose Configuration​

An example of a docker-compose.yml file for running your application and PostgreSQL for testing:

version: '3.8'
services:
app:
image: my-spring-app:latest
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: test
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/testdb
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password

postgres:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: testdb

Running Tests in Docker​

Run your integration tests with Docker Compose:

docker-compose up -d
# Run tests
mvn test
docker-compose down

This guide offers configurations and code samples to help set up automated testing for Spring Boot applications in both development and containerized environments, suitable for integration into CI/CD pipelines.

Deep Dive into Caching for Distributed Systems

· 阅读需 8 分钟

Effective caching can significantly improve the performance and scalability of distributed systems by reducing the load on databases and backend services. This document covers high-level architecture, detailed design, and sample code for implementing caching in distributed systems.

1. Overview and High-Level Architecture​

Caching in distributed systems improves data retrieval speeds, reduces network latency, and offloads backend databases. A typical caching architecture includes clients, a caching layer, and a backend data source. The caching layer can reside in-memory on the application server or be a distributed cache like Redis, Memcached, or Hazelcast.

High-Level Architecture Diagram​

+----------+ +--------------+ +--------------------+
| Clients | <----> | Cache Layer | <----> | Backend Data Store |
+----------+ +--------------+ +--------------------+

Components:

  • Clients: The applications or services making requests for data.
  • Cache Layer: Responsible for storing frequently accessed data, reducing read times.
  • Backend Data Source: The main data storage, typically a database or another service.

2. Cache Design Patterns​

Caching strategies are essential in defining how data is managed between the cache and the main data store.

2.1 Read-Through Cache​

In a read-through cache, requests for data are directed to the cache. If the data is not in the cache, it retrieves it from the database, caches it, and then serves the data.

Diagram:

+--------------+ +----------+ +--------------+
Request | Application | -----> | Cache | -----> | Database |
+--------------+ +----------+ +--------------+
| |
+-----------------------+
(Data cached after first retrieval)

Code Example:

public class ReadThroughCache {
private Cache cache;
private Database database;

public Data getData(String key) {
Data data = cache.get(key);
if (data == null) {
data = database.get(key);
cache.put(key, data);
}
return data;
}
}

2.2 Write-Through Cache​

In this pattern, data is written to the cache and database simultaneously, ensuring cache consistency with the data source.

Diagram:

+--------------+ +----------+ +--------------+
Update | Application | -----> | Cache | -----> | Database |
+--------------+ +----------+ +--------------+
(Writes to cache and database together)

Code Example:

public class WriteThroughCache {
private Cache cache;
private Database database;

public void putData(String key, Data data) {
cache.put(key, data);
database.put(key, data);
}
}

2.3 Write-Behind Cache​

The write-behind cache writes data asynchronously to the backend, which enhances write performance but requires a queue to manage pending updates.

Diagram:

+--------------+ +----------+ +--------------+
Update | Application | -----> | Cache | -----> | Queue |
+--------------+ +----------+ +--------------+
(Data asynchronously written to Database)

Code Example:

import java.util.concurrent.*;

public class WriteBehindCacheService {
private final Cache cache;
private final Database database;
private final ExecutorService executorService;

public WriteBehindCacheService(Cache cache, Database database) {
this.cache = cache;
this.database = database;
this.executorService = Executors.newFixedThreadPool(2);
}

public void putData(String key, Data data) {
// Step 1: Write to cache
cache.put(key, data);

// Step 2: Asynchronously update the database
executorService.submit(() -> database.put(key, data));
}

public Data getData(String key) {
return cache.get(key); // Read from cache
}

public void shutdown() {
executorService.shutdown();
}
}

In this example, when putData is called, the data is immediately written to the cache, and an asynchronous task is created to update the database. This method improves application performance by deferring the database write.

2.4 Cache-Aside Pattern​

With the cache-aside pattern, the application retrieves data directly from the cache. On a cache miss, it loads data from the main store, updates the cache, and returns the result.

Diagram:

+--------------+ +----------+ +--------------+
| Application | ----> | Cache | ----> | Database |
+--------------+ +----------+ +--------------+
| |
(Data fetched and cached when not found in cache)

Code Example:

public class CacheAsideService {
private final Cache cache;
private final Database database;

public CacheAsideService(Cache cache, Database database) {
this.cache = cache;
this.database = database;
}

public Data getData(String key) {
// Step 1: Check cache first
Data data = cache.get(key);

if (data == null) {
// Step 2: Load from database on cache miss
data = database.get(key);

if (data != null) {
// Step 3: Store data in cache for future requests
cache.put(key, data);
}
}
return data;
}

public void putData(String key, Data data) {
// Step 1: Write to database
database.put(key, data);

// Step 2: Invalidate cache or update cache
cache.put(key, data);
}
}

In this example:

  • Read: getData() first tries to read from the cache. If there’s a cache miss, it fetches from the database, adds it to the cache, and returns the data.
  • Write: putData() writes to the database first, then updates or invalidates the cache.

2.5 Differences between Read-Through and Cache-Aside Patterns​

  1. Read-Through Pattern:

    • The cache sits directly in front of the database, intercepting reads.
    • If data is requested, the cache checks if it's present. If it’s a cache miss, it automatically fetches from the database, caches it, and returns the result.
    • This pattern works well for applications where you want to decouple the application logic from the cache management.
    • Common in systems where caching is managed as a service, like using a caching proxy or middleware.
  2. Cache-Aside Pattern:

    • The application controls cache management explicitly.
    • On a read operation, the application first checks the cache. If the data is not present (cache miss), the application fetches it from the database, then stores it in the cache for future use.
    • This pattern gives the application full control over what data is cached, which can optimize cache usage for applications with varying data access patterns.
    • Common in scenarios where cache storage is limited or there’s a need to frequently update specific items based on application logic.
FeatureRead-Through PatternCache-Aside Pattern
Cache ManagementManaged by the cache itselfManaged by the application
Caching BehaviorAutomatically loads from DB on cache missApplication decides when to cache
Best Use CasesFor systems requiring a cache as a serviceFor applications needing custom control over caching
ComplexityLower, as caching logic is handled internallyHigher, as caching must be managed in the application

3. Cache Topologies​

3.1 Single Node Cache​

Single-node caches store data locally on one machine and are simple to manage but lack scalability and resilience.

3.2 Distributed Cache​

A distributed cache spreads data across multiple nodes for scalability, enabling data to be accessed from various locations in a distributed system.

Diagram:

+----------+ +-------------+ +-------------+ +-------------+
| Client 1 |<--->| Cache Node 1|<--->| Cache Node 2|<--->| Cache Node 3|
+----------+ +-------------+ +-------------+ +-------------+

3.3 Hierarchical Cache​

Hierarchical caches use a layered approach to caching, with data stored at different levels depending on usage frequency.

Diagram:

+-------------+
| L1 Cache |
+-------------+
|
+-------------+
| L2 Cache |
+-------------+
|
+-------------+
| Backend DB |
+-------------+

4. Detailed Design and Components​

4.1 Cache Consistency​

Consistency in caching ensures that the data in the cache matches the data in the database. Techniques like cache synchronization and write-through strategies help maintain this consistency.

4.2 Cache Invalidation Strategies​

Cache invalidation ensures that stale data is removed or updated within the cache. Common strategies include:

  • Time-based Expiration: Setting a TTL (Time to Live) for cache entries.
  • Event-based Invalidation: Using events to invalidate cache entries based on specific triggers.

4.3 Eviction Policies​

Eviction policies determine how cache space is managed. Common policies include:

  • Least Recently Used (LRU): Removes data that hasn’t been accessed recently.
  • Least Frequently Used (LFU): Removes data that is accessed least frequently.

5. Implementation​

5.1 Cache Layer Implementation​

Using Redis as an example of a distributed cache layer:

import redis.clients.jedis.Jedis;

public class RedisCacheService {
private Jedis jedis = new Jedis("localhost");

public void put(String key, String value) {
jedis.set(key, value);
}

public String get(String key) {
return jedis.get(key);
}

public void delete(String key) {
jedis.del(key);
}
}

5.2 Cache Invalidation Mechanisms​

Implementing cache invalidation using a TTL and a cache purge mechanism:

import java.util.concurrent.TimeUnit;

public class CacheService {
private Cache cache = new Cache();

public void put(String key, Data value, long ttl, TimeUnit unit) {
cache.put(key, value);
cache.expire(key, ttl, unit);
}
}

6. Monitoring and Metrics​

Monitoring the cache layer provides insights into performance. Using tools like Prometheus and Grafana with metrics for cache hit/miss rates, latency, and eviction rates is essential.

# Example Prometheus configuration for Redis
scrape_configs:
- job_name: 'redis'
static_configs:
- targets: ['localhost:6379']

7. Conclusion​

Distributed caching is a vital component of scalable systems. By understanding different patterns, topologies, and consistency mechanisms, developers can effectively improve system performance and reliability.

Deep Dive into Caffeine Cache and Integration with Spring Boot

· 阅读需 13 分钟

Introduction​

Caffeine is a high-performance, near-optimal caching library for Java, widely used in Spring Boot applications to improve performance by reducing the load on databases and other backend services. This article delves into the details of Caffeine cache, its integration with Spring Boot, and how it works under the hood. We'll also provide real-life production use cases and sample code to illustrate its usage.


Caffeine Cache Overview​

Caffeine is designed to be efficient and easy to use, providing advanced features like eviction policies, time-based expiration, and statistics recording. It uses a combination of data structures and algorithms to manage cache entries efficiently.

Key Features​

  • High Performance: Optimized for fast read and write operations.
  • Eviction Policies: Supports LRU, FIFO, and LFU eviction policies.
  • Time-Based Expiration: Entries can be expired after a specified period of inactivity or since they were last written.
  • Statistics Recording: Tracks cache hits, misses, and evictions for monitoring and tuning.

Configuring Caffeine Cache in Spring Boot​

Adding Dependencies​

First, ensure you have the necessary dependencies in your pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>

Enabling Caching​

Enable caching in your Spring Boot application by adding the @EnableCaching annotation to your main application class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Configuring Cache Manager​

Create a configuration class to set up Caffeine cache:

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("userCache", "productCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}
}

YAML Configuration​

You can also configure Caffeine cache using YAML:

spring:
cache:
type: caffeine
cache-names:
- userCache
- productCache
caffeine:
spec: maximumSize=500,expireAfterAccess=600s

Explanation of Caffeine Cache Configuration​

  • CaffeineCacheManager: Manages multiple caches, each identified by a name.
  • Cache Names: "userCache" and "productCache" are used to identify different caches.
  • maximumSize(500): Sets the maximum number of entries the cache can hold.
  • expireAfterAccess(600, TimeUnit.SECONDS): Specifies that entries should be expired after 600 seconds of inactivity.
  • recordStats(): Enables statistics recording for monitoring cache performance.

How Caffeine Cache Works​

Cache Population​

When a cache miss occurs, Caffeine fetches the data from the underlying data source (e.g., database) and stores it in the cache.

Cache Eviction​

Caffeine uses various eviction policies to manage the cache size:

  • Least Recently Used (LRU): Evicts the least recently used entries first.
  • First In, First Out (FIFO): Evicts the oldest entries first.
  • Least Frequently Used (LFU): Evicts the least frequently used entries first.

Expiration​

Caffeine supports time-based expiration:

  • Expire After Access: Entries are expired after a specified period of inactivity.
  • Expire After Write: Entries are expired after a specified period since they were last written.

Statistics​

Caffeine can record statistics such as cache hits, misses, and evictions, which can be useful for monitoring and tuning the cache.

Statistics Recording​

Caffeine provides a recordStats() method that enables the collection of cache statistics. This method returns a Caffeine instance that will record statistics for the cache.

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}

Tracking Cache Hits, Misses, and Evictions​

Once statistics recording is enabled, you can access the statistics using the stats() method on the cache. This method returns a CacheStats object that contains various metrics:

  • hitCount(): The number of successful cache hits.
  • missCount(): The number of cache misses.
  • evictionCount(): The number of cache evictions.
  • loadSuccessCount(): The number of times a cache miss was successfully loaded.
  • loadFailureCount(): The number of times a cache miss failed to load.

Here's an example of how to access and log cache statistics:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;

public class CacheStatisticsExample {

public static void main(String[] args) {
Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

// Perform cache operations
cache.put("key1", "value1");
cache.getIfPresent("key1");
cache.getIfPresent("key2");

// Access cache statistics
CacheStats stats = cache.stats();
System.out.println("Cache Hits: " + stats.hitCount());
System.out.println("Cache Misses: " + stats.missCount());
System.out.println("Cache Evictions: " + stats.evictionCount());
}
}

Caffeine Cache Internals​

Cache Interface​

The Cache interface defines the core operations for interacting with the cache:

public interface Cache<K, V> {
V getIfPresent(K key);
V get(K key, Function<? super K, ? extends V> mappingFunction);
void put(K key, V value);
void invalidate(K key);
void invalidateAll();
long estimatedSize();
CacheStats stats();
}

Caffeine Class​

The Caffeine class is the builder for creating cache instances:

public final class Caffeine<K, V> {
public static <K, V> Caffeine<K, V>newBuilder() {
return new Caffeine<>();
}

public Caffeine<K, V> maximumSize(long maximumSize) {
// Implementation
return this;
}

public Caffeine<K, V> expireAfterAccess(long duration, TimeUnit unit) {
// Implementation
return this;
}

public Cache<K, V> build() {
// Implementation
return new BoundedLocalCache<>(this);
}
}

BoundedLocalCache Class​

The BoundedLocalCache class is the core implementation of the cache, handling operations like get, put, and eviction:

final class BoundedLocalCache<K, V> extends LocalCache<K, V> {
BoundedLocalCache(Caffeine<K, V> builder) {
// Initialization
}

@Override
public V getIfPresent(Object key) {
// Implementation
}

@Override
public void put(K key, V value) {
// Implementation
}

@Override
public void invalidate(Object key) {
// Implementation
}

@Override
public long estimatedSize() {
// Implementation
}

@Override
public CacheStats stats() {
// Implementation
}
}

Detailed Logic in Caffeine Cache​

Cache Lookup​

Caffeine uses a ConcurrentHashMap to store cache entries. When a lookup is performed, Caffeine checks if the key exists in the map:

public V getIfPresent(Object key) {
Node<K, V> node = data.get(key);
if (node == null) {
return null;
}
// Additional logic for expiration and eviction checks
return node.getValue();
}

Cache Miss and Method Execution​

If the key is not found in the cache, the method is executed, and the result is stored in the cache:

public V get(K key, Function<? super K, ? extends V> mappingFunction) {
V value = getIfPresent(key);
if (value != null) {
return value;
}
value = mappingFunction.apply(key);
put(key, value);
return value;
}

Cache Update​

When the result is fetched from the database, it is stored in the cache:

public void put(K key, V value) {
Node<K, V> node = new Node<>(key, value);
data.put(key, node);
// Additional logic for eviction and expiration
}

Real-Life Production Use Cases​

1. User Profile Caching​

In a social media application, user profiles are frequently accessed. Caching user profiles can significantly reduce the load on the database and improve response times.

2. Product Catalog Caching​

In an e-commerce application, product catalogs are often large and frequently accessed. Caching product data can improve performance and reduce database load.

3. API Response Caching​

In microservices, caching API responses can reduce the number of calls to downstream services, improving overall system performance.

4. Session Data Caching​

In applications with user sessions, caching session data can reduce the load on session storage systems and improve scalability.


Sample Code​

Caffeine Cache Configuration​

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("userCache", "productCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}
}

User Service with Caching​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User(userId, "John Doe");
}
}

Flow Diagram​

Here's a textual diagram to illustrate the flow:

graph TD
A[Client Request] --> B{Cache Lookup}
B -->|Cache Hit| C[Return Cached Data]
B -->|Cache Miss| D[Execute Method]
D --> E[Fetch from Database]
E --> F[Update Cache]
F --> C

Explanation of the Flow Diagram​

  1. Client Request: The client makes a request to fetch a user by ID.
  2. Cache Lookup: The cache manager looks up the userId in the "userCache".
  3. Cache Hit: If the user is found in the cache, the cached data is returned immediately.
  4. Cache Miss: If the user is not found in the cache, the method getUserById(Long userId) is executed.
  5. Execute Method: The method fetches the user from the database.
  6. Fetch from Database: The user is retrieved from the database.
  7. Update Cache: The fetched user is stored in the cache.
  8. Return Cached Data: The user data is returned to the client.

Statistics Logging, Tracking and Mornitoring​

Caffeine's statistics can be tracked using logging tools and even integrated with GUI tools for better visualization and monitoring.

1. Logging Cache Statistics​

Log cache statistics using a logging framework like SLF4J or Log4j. This allows you to periodically log the cache metrics to a log file or console.

Example: Logging Cache Statistics with SLF4J​

First, add the necessary dependencies in pom.xml:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>

Next, configure cache to log statistics periodically:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class CacheStatisticsLogger {

private static final Logger logger = LoggerFactory.getLogger(CacheStatisticsLogger.class);

public static void main(String[] args) {
Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

// Perform cache operations
cache.put("key1", "value1");
cache.getIfPresent("key1");
cache.getIfPresent("key2");

// Log cache statistics
logCacheStats(cache);
}

private static void logCacheStats(Cache<?, ?> cache) {
CacheStats stats = cache.stats();
logger.info("Cache Hits: {}", stats.hitCount());
logger.info("Cache Misses: {}", stats.missCount());
logger.info("Cache Evictions: {}", stats.evictionCount());
logger.info("Load Success Count: {}", stats.loadSuccessCount());
logger.info("Load Failure Count: {}", stats.loadFailureCount());
}
}

2. Integrating with GUI Tools​

Integrate Caffeine's statistics with GUI tools like Grafana or Prometheus for better visualization and monitoring. Here's an example of how to integrate with Prometheus and Grafana.

Step 1: Add Prometheus and Grafana Dependencies​

Add the necessary dependencies to pom.xml:

<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.15.0</version>
</dependency>

Step 2: Configure Prometheus Metrics Collector​

Create a configuration class to collect cache statistics using Prometheus:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Configuration
public class PrometheusConfig {

private final Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

private final Counter cacheHits = Counter.build()
.name("cache_hits_total")
.help("Total number of cache hits.")
.register();

private final Counter cacheMisses = Counter.build()
.name("cache_misses_total")
.help("Total number of cache misses.")
.register();

private final Counter cacheEvictions = Counter.build()
.name("cache_evictions_total")
.help("Total number of cache evictions.")
.register();

private final Gauge cacheSize = Gauge.build()
.name("cache_size")
.help("Current size of the cache.")
.register();

@PostConstruct
public void init() {
DefaultExports.initialize();
}

@Bean
public ServletRegistrationBean<MetricsServlet> metricsServlet() {
CollectorRegistry collectorRegistry = new CollectorRegistry();
collectorRegistry.register(cacheHits);
collectorRegistry.register(cacheMisses);
collectorRegistry.register(cacheEvictions);
collectorRegistry.register(cacheSize);
return new ServletRegistrationBean<>(new MetricsServlet(collectorRegistry), "/metrics");
}

@Bean
public Cache<String, String> cache() {
return cache;
}

@Bean
public Runnable cacheStatsCollector() {
return () -> {
CacheStats stats = cache.stats();
cacheHits.inc(stats.hitCount());
cacheMisses.inc(stats.missCount());
cacheEvictions.inc(stats.evictionCount());
cacheSize.set(cache.estimatedSize());
};
}
}

Step 3: Configure Prometheus and Grafana​

  1. Prometheus: Configure Prometheus to scrape the metrics endpoint (/metrics) of the Spring Boot application.

  2. Grafana: Set up Grafana to visualize the metrics collected by Prometheus. Dashboards can be created to display cache hits, misses, evictions, and other relevant metrics.

Example: Prometheus Configuration​

Create a prometheus.yml file:

global:
scrape_interval: 15s

scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:8080']

Example: Grafana Dashboard​

  1. Add Prometheus as a Data Source in Grafana.
  2. Create a Dashboard and add panels to visualize the cache metrics:
    • Cache Hits: cache_hits_total
    • Cache Misses: cache_misses_total
    • Cache Evictions: cache_evictions_total
    • Cache Size: cache_size

Summary​

By logging cache statistics using a logging framework like SLF4J or integrating with monitoring tools like Prometheus and Grafana, we can effectively track and visualize the performance of the Caffeine cache. This allows us to monitor cache hits, misses, evictions, and other metrics, helping us to tune and optimize the cache for better performance.

Conclusion​

Caffeine cache is a powerful tool for improving the performance and scalability of Spring Boot applications. By understanding how Caffeine works, including its eviction policies and expiration

Deep Dive into Finding Numbers in Strings with Python

· 阅读需 6 分钟

Introduction​

Handling strings that contain a mix of numbers and text is a common task in everyday programming. For example, you might need to extract numbers from complex text or identify whether certain strings contain numbers. In Python, there are various ways to tackle this problem, depending on your specific needs. This article will detail how to find numbers in strings using Python, providing concrete example code to help developers quickly master these techniques.


Why Find Numbers in Strings?​

  • Data Processing and Cleaning: Extract useful numeric information from web crawlers or log files, such as order numbers, timestamps, amounts, etc.
  • Input Validation: Detect whether user input strings contain numbers to ensure the validity of the input content.
  • Text Analysis: Extract numbers from text data for further data analysis or statistics.

Using String's isnumeric() Method​

Python's string type (str) provides several methods to determine character types, one of which is isnumeric(). The isnumeric() method checks whether a string contains only numeric characters. If all characters in the string are numeric, the method returns True; otherwise, it returns False.

string = "12345"
if string.isnumeric():
print(f"'{string}' contains only numbers")
else:
print(f"'{string}' contains more than just numbers")

In this example, isnumeric() checks if the string "12345" consists entirely of numeric characters. Although this method can determine if a string is entirely numeric, it cannot extract numbers from mixed strings.


Using Regular Expressions to Find Numbers​

Regular expressions (regex) are a powerful tool for text processing, ideal for finding and extracting numbers from strings. Python's re module provides comprehensive regex support, making it easy to match numbers in strings.

Example: Using Regular Expressions​

import re

# Define a string containing numbers
text = "Order number is 12345, amount is 678.90 dollar"

# Use regex to find all numbers
numbers = re.findall(r'\d+', text)

print(f"Numbers found in the string: {numbers}")

In this example, re.findall() uses the regex \d+, which matches one or more consecutive digits. re.findall() returns a list containing all matched numbers in the string. The output is: ['12345', '678'].


Finding Numbers with Decimal Points​

If you need to find numbers with decimal points in a string, you can use the following regex:

import re

text = "Order number is 12345, amount is 678.90 dollar"

# Find integers and decimals
numbers = re.findall(r'\d+\.?\d*', text)

print(f"Numbers found in the string: {numbers}")

In this regex, \d+ matches one or more digits, \. matches the decimal point, and \d* matches zero or more digits following the decimal point. The result is: ['12345', '678.90'].


Using List Comprehension to Extract Numbers​

For simple scenarios, you can use Python's list comprehension to quickly extract numbers from a string. By checking each character in the string to see if it is a digit, you can extract them.

Example: Using List Comprehension​

text = "Order number 12345, amount 678.90 dollar"

# Extract digit characters
digits = [char for char in text if char.isdigit()]

print(f"Extracted digit characters: {digits}")

In this example, isdigit() is used to check each character in the string, resulting in a list of all digit characters: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']. If you need to combine the extracted digits into a complete numeric string, you can use the join() method:

# Combine extracted digit characters into a complete numeric string
number_str = ''.join(digits)
print(f"Complete numeric string: {number_str}")

The output is: "1234567890".


Filtering Numbers with filter() Function​

Python's built-in filter() function can be used to filter out non-numeric characters from a string, thereby extracting numeric characters. The filter() function works similarly to list comprehension but is more functional in style.

Example: Using filter() Function​

text = "Order number 12345, amount 678.90 dollar"

# Use the filter function to extract numbers
numbers = ''.join(filter(str.isdigit, text))

print(f"Extracted numbers: {numbers}")

In this example, the filter() function checks each character in the string, keeping only those that satisfy isdigit(), and then joins the extracted numeric characters into a complete string.


Splitting Strings to Extract Numbers​

In some scenarios, you can split the string first and then process each segment to extract numbers. The split() method can split a string into multiple parts based on a specified delimiter, suitable for handling structured text.

Example: Using split() Method​

import re

text = "Order number: 12345, amount: 678.90"

# Split the string by spaces or punctuation
parts = re.split(r'[,:: ]+', text)

# Extract numeric parts
numbers = [part for part in parts if part.replace('.', '', 1).isdigit()]

print(f"Extracted numbers: {numbers}")

In this example, re.split() is used to split the string, and then list comprehension extracts all numeric parts. replace('.', '', 1) ensures that decimal points are handled correctly.


Using ast.literal_eval to Parse Numbers​

In some applications, you may need to extract numeric values from complex strings and parse them into numeric types (integers or floats). Python's ast.literal_eval() method can safely parse a string into a Python data type.

Example: Using ast.literal_eval​

import ast
import re

text = "Amount: 678.90"

# Extract the numeric part of the string
number_str = re.search(r'\d+\.?\d*', text).group()

# Parse the string into a number
number = ast.literal_eval(number_str)

print(f"Parsed number: {number}")

In this example, the regex extracts the numeric part, and ast.literal_eval() converts it into the actual numeric type (float). This method is useful for extracting and converting numbers from text.


Conclusion​

Finding and extracting numbers from strings is a common task in Python programming, whether for data cleaning, user input validation, or text analysis. This article has introduced various methods to find numbers in Python, including using string methods, regular expressions, list comprehension, the filter() function, and ast.literal_eval(). Each method has its strengths and can be chosen based on different application scenarios. With these tools, developers can quickly and efficiently extract and process numeric data from strings, enhancing code flexibility and processing capabilities.

Deep Dive into Keycloak

· 阅读需 5 分钟

Introduction​

Keycloak is an open-source Identity and Access Management (IAM) solution that provides authentication, authorization, and user management for modern applications and services. It is designed to be easy to integrate with existing systems and supports a wide range of protocols and standards, including OAuth 2.0, OpenID Connect, and SAML. This article will delve into the architecture of Keycloak, exploring its components, features, and practical applications.

Keycloak Architecture​

2.1 Overview​

Keycloak is built on a modular architecture that allows for flexibility and extensibility. It consists of several key components that work together to provide a comprehensive IAM solution. The architecture can be divided into the following main areas:

  • Authentication: Handles user authentication and session management.
  • Authorization: Provides fine-grained access control and permissions management.
  • User Federation: Integrates with external user stores.
  • Identity Brokering: Allows users to authenticate using external identity providers.
  • Admin Console: Provides a web-based interface for managing Keycloak.

2.2 Key Components​

+-------------------+ +-------------------+ +-------------------+
| Client Application| | Keycloak Server | | External Identity |
| (e.g., Web App) | | (Auth Server) | | Provider (e.g., |
| | | | | Google, Facebook)|
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Authentication Request| |
|-------------------------->| |
| | (2) Redirect to Auth Server|
| |-------------------------->|
| | |
| | (3) User Authentication |
| |<--------------------------|
| | |
| | (4) Authorization Grant |
| |<--------------------------|
| | |
| | (5) Access Token Request |
| |-------------------------->|
| | |
| | (6) Access Token Response|
| |<--------------------------|
| | |
| (7) Access Resource | |
|<--------------------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| Resource Server | | Keycloak Server | | External Identity |
| (e.g., API) | | (Auth Server) | | Provider (e.g., |
| | | | | Google, Facebook)|
+-------------------+ +-------------------+ +-------------------+

2.3 Authentication Flows​

Keycloak supports various authentication flows, including:

  • Standard Flow: The default flow where the client redirects the user to the Keycloak server for authentication.
  • Implicit Flow: Suitable for client-side applications where the access token is returned directly to the client.
  • Direct Access Grants: Allows clients to obtain an access token using the resource owner's credentials.
  • Client Credentials: Used for machine-to-machine communication.

2.4 Authorization Services​

Keycloak provides fine-grained authorization services, including:

  • Role-Based Access Control (RBAC): Assigns roles to users and defines permissions based on those roles.
  • Policy-Based Access Control (PBAC): Uses policies to define complex access control rules.
  • Permission Management: Allows administrators to manage permissions for resources and users.

2.5 User Federation​

Keycloak supports user federation, allowing integration with external user stores such as LDAP and Active Directory. This enables organizations to manage users in their existing user stores while leveraging Keycloak for authentication and authorization.

2.6 Identity Brokering​

Keycloak supports identity brokering, allowing users to authenticate using external identity providers such as Google, Facebook, and GitHub. This enables organizations to provide a seamless authentication experience for users who prefer to use their existing accounts.

2.7 Admin Console​

Keycloak provides a web-based admin console that allows administrators to manage users, roles, clients, and other Keycloak entities. The admin console is a powerful tool for configuring and managing Keycloak.

Practical Applications​

3.1 Single Sign-On (SSO)​

Keycloak enables Single Sign-On (SSO), allowing users to authenticate once and access multiple applications without re-entering their credentials. This is particularly useful in enterprise environments where users need to access multiple internal applications.

3.2 API Security​

Keycloak is widely used to secure APIs by providing access tokens that grant limited access to resources. This is essential for building secure microservices architectures.

3.3 User Management​

Keycloak provides comprehensive user management features, including user registration, profile management, and password management. It also supports user federation, allowing integration with external user stores.

3.4 Custom Extensions​

Keycloak is highly extensible, allowing developers to create custom extensions to meet specific requirements. This includes custom authentication flows, custom user federation providers, and custom identity providers.

Conclusion​

Keycloak is a powerful and flexible Identity and Access Management solution that provides comprehensive authentication, authorization, and user management capabilities. Its modular architecture and support for various protocols and standards make it a popular choice for securing modern applications and services.

Understanding the architecture and features of Keycloak is essential for developers and administrators who want to leverage its capabilities to build secure and scalable applications. By leveraging Keycloak, organizations can provide a seamless and secure authentication and authorization experience for their users.

References​


This article provides a comprehensive overview of Keycloak, covering its architecture, components, and practical applications. By understanding Keycloak, developers and administrators can build secure and scalable applications that meet the demands of today's digital landscape.

Deep Dive into MapStruct: A Java Bean Mapping Framework

· 阅读需 6 分钟
  1. Introduction
  2. Why Use MapStruct?
  3. How to Use MapStruct in Spring Boot
  4. Benefits and Advantages
  5. Textual Diagram
  6. Sample Code
  7. Generated Mapper Code
  8. Excluding Certain Attributes
  9. Conclusion

Introduction​

MapStruct is a Java annotation processor for the generation of type-safe and performant bean mapping code. It simplifies the mapping between different Java bean types by automatically generating the necessary mapping code at compile time. This article will explore why and how to use MapStruct in a Spring Boot application, its benefits and advantages, and provide detailed sample code.


Why Use MapStruct?​

Manual Mapping​

Manual mapping between Java beans can be tedious and error-prone. It often involves writing repetitive and boilerplate code, which can lead to maintenance issues.

Reflection-Based Mapping​

Frameworks like Dozer use reflection to map beans, which can be slow and difficult to debug. Reflection-based mapping also lacks type safety and can lead to runtime errors.

MapStruct​

MapStruct generates mapping code at compile time, ensuring type safety and performance. It reduces boilerplate code and makes the mapping process more maintainable and easier to debug.


How to Use MapStruct in Spring Boot​

Step 1: Add Dependencies​

Add the necessary dependencies to your pom.xml file. Note that we are using the latest version of MapStruct.

<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- MapStruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
</dependency>

<!-- MapStruct Processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.3.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

Step 2: Define Mapping Interface​

Create a mapping interface using the @Mapper annotation.

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

@Mapping(source = "emailAddress", target = "email")
@Mapping(source = "birthDate", target = "birthDate", dateFormat = "yyyyMMdd")
@Mapping(target = "password", ignore = true) // Exclude password attribute
UserDTO userToUserDTO(User user);

@Mapping(source = "email", target = "emailAddress")
@Mapping(target = "birthDate", expression = "java(LocalDate.parse(userDTO.getBirthDate(), DateTimeFormatter.ofPattern(\"yyyyMMdd\")))")
@Mapping(target = "password", ignore = true) // Exclude password attribute
User userDTOToUser(UserDTO userDTO);
}

Step 3: Use the Mapper​

Inject the mapper into your service or controller and use it to map between beans.

import org.springframework.stereotype.Service;

@Service
public class UserService {
private final UserMapper userMapper = UserMapper.INSTANCE;

public UserDTO getUserDTO(User user) {
return userMapper.userToUserDTO(user);
}

public User getUser(UserDTO userDTO) {
return userMapper.userDTOToUser(userDTO);
}
}

Benefits and Advantages​

Type Safety​

MapStruct generates type-safe mapping code at compile time, reducing the risk of runtime errors.

Performance​

Since the mapping code is generated at compile time, it avoids the performance overhead associated with reflection-based mapping.

Maintainability​

MapStruct reduces boilerplate code, making the mapping process more maintainable and easier to understand.

Customization​

MapStruct allows for easy customization of mappings through annotations and configuration options.


Textual Diagram​

graph TD
A[User Entity] -->|MapStruct| B[User DTO]
B -->|MapStruct| C[User Entity]
D[User Service] -->|Uses| A
D -->|Uses| B
E[User Controller] -->|Uses| D

Explanation​

  1. User Entity: Represents the domain object.
  2. User DTO: Represents the data transfer object.
  3. MapStruct: Generates mapping code between User Entity and User DTO.
  4. User Service: Uses the MapStruct-generated mapper to convert between User Entity and User DTO.
  5. User Controller: Uses the User Service to handle requests and responses.

Sample Code​

User Entity​

import java.time.LocalDate;

public class User {
private Long id;
private String name;
private String emailAddress;
private LocalDate birthDate;
private String password; // Password attribute to be excluded

// Getters and Setters
}

User DTO​

public class UserDTO {
private Long id;
private String name;
private String email;
private String birthDate;
private String password; // Password attribute to be excluded

// Getters and Setters
}

User Mapper​

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

@Mapping(source = "emailAddress", target = "email")
@Mapping(source = "birthDate", target = "birthDate", dateFormat = "yyyyMMdd")
@Mapping(target = "password", ignore = true) // Exclude password attribute
UserDTO userToUserDTO(User user);

@Mapping(source = "email", target = "emailAddress")
@Mapping(target = "birthDate", expression = "java(LocalDate.parse(userDTO.getBirthDate(), DateTimeFormatter.ofPattern(\"yyyyMMdd\")))")
@Mapping(target = "password", ignore = true) // Exclude password attribute
User userDTOToUser(UserDTO userDTO);
}

User Service​

import org.springframework.stereotype.Service;

@Service
public class UserService {
private final UserMapper userMapper = UserMapper.INSTANCE;

public UserDTO getUserDTO(User user) {
return userMapper.userToUserDTO(user);
}

public User getUser(UserDTO userDTO) {
return userMapper.userDTOToUser(userDTO);
}
}

User Controller​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;

@GetMapping("/{id}")
public UserDTO getUserDTO(@PathVariable Long id) {
User user = new User(); // Assume this is fetched from a repository
return userService.getUserDTO(user);
}

@PostMapping
public User createUser(@RequestBody UserDTO userDTO) {
return userService.getUser(userDTO);
}
}

Generated Mapper Code​

After compiling the project, MapStruct will generate the following mapper implementation:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class UserMapperImpl implements UserMapper {

@Override
public UserDTO userToUserDTO(User user) {
if (user == null) {
return null;
}

UserDTO userDTO = new UserDTO();
userDTO.setId(user.getId());
userDTO.setName(user.getName());
userDTO.setEmail(user.getEmailAddress());
userDTO.setBirthDate(user.getBirthDate().format(DateTimeFormatter.ofPattern("yyyyMMdd")));

return userDTO;
}

@Override
public User userDTOToUser(UserDTO userDTO) {
if (userDTO == null) {
return null;
}

User user = new User();
user.setId(userDTO.getId());
user.setName(userDTO.getName());
user.setEmailAddress(userDTO.getEmail());
user.setBirthDate(LocalDate.parse(userDTO.getBirthDate(), DateTimeFormatter.ofPattern("yyyyMMdd")));

return user;
}
}

Excluding Certain Attributes​

Exclude Attribute Example​

In the UserMapper interface, we use the @Mapping annotation with ignore = true to exclude the password attribute from the mapping.

@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

@Mapping(source = "emailAddress", target = "email")
@Mapping(source = "birthDate", target = "birthDate", dateFormat = "yyyyMMdd")
@Mapping(target = "password", ignore = true) // Exclude password attribute
UserDTO userToUserDTO(User user);

@Mapping(source = "email", target = "emailAddress")
@Mapping(target = "birthDate", expression = "java(LocalDate.parse(userDTO.getBirthDate(), DateTimeFormatter.ofPattern(\"yyyyMMdd\")))")
@Mapping(target = "password", ignore = true) // Exclude password attribute
User userDTOToUser(UserDTO userDTO);
}

Generated Mapper Code with Exclusion​

The generated mapper code will exclude the password attribute from the mapping.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class UserMapperImpl implements UserMapper {

@Override
public UserDTO userToUserDTO(User user) {
if (user == null) {
return null;
}

UserDTO userDTO = new UserDTO();
userDTO.setId(user.getId());
userDTO.setName(user.getName());
userDTO.setEmail(user.getEmailAddress());
userDTO.setBirthDate(user.getBirthDate().format(DateTimeFormatter.ofPattern("yyyyMMdd")));

return userDTO;
}

@Override
public User userDTOToUser(UserDTO userDTO) {
if (userDTO == null) {
return null;
}

User user = new User();
user.setId(userDTO.getId());
user.setName(userDTO.getName());
user.setEmailAddress(userDTO.getEmail());
user.setBirthDate(LocalDate.parse(userDTO.getBirthDate(), DateTimeFormatter.ofPattern("yyyyMMdd")));

return user;
}
}

Conclusion​

MapStruct is a powerful and efficient Java bean mapping framework that simplifies the process of mapping between different Java bean types. By generating type-safe and performant mapping code at compile time, MapStruct reduces boilerplate code, improves maintainability, and enhances the overall quality of your Spring Boot applications. Whether you are working on a small project or a large enterprise application, MapStruct is a valuable tool that can significantly streamline your development process.