Please enable JavaScript.
Coggle requires JavaScript to display documents.
Interview-map, Spring, Multi Thread, database, Architecture, HEAP/STACK…
-
Spring
-
-
How
dependency injection
-
- userDao cần dataSource (someConfigClas)
- dataSource change from postgres to mongodb
- Đây chỉ là design pattern
- design pattern used to implement IOC
- make class independent from its dependencies
- help to achieve loosely coupling
-
ApplicationContext
(Spring's dependencies injection container)
- manage dependencies for you
- load bean
- wire bean
create
- new AnnotationConfigApplicationContext()
- new ClassPathXmlApplicationContext()
componentScan
(find bean)
-
-
if a component has Autowired
=> this component need inject a bean
- Component (class level) Spring 2.5
- Bean (method level) Spring 3.0
bean
-
bean init
- dependencies inject
- setProperties
init() method
ready to use
- 1 more item...
-
-
-
What
Spring FW is just a dependency injection container offering a couple of convenience layers(database, security, aop...) added on top
-
Spring utilities
- classPath
- file
- https
- myhost
- s3://
-
-
-
Multi Thread
thread safe
-
ThreadLocal
achieve thread-safe
- each thread has its own threadLocal instance
- allow store data, access only by specific thread
-
declare
- ThreadLocal<Integrer> a = new ThreadLocal<>()
- = ThreadLocal.withInitial(()->1)
-
mỗi khi thread access thread local, mỗi thread sẽ có được bản copy của field => mục đích để mỗi luồng có trạng thái riêng
-
-
- prevent conflict data
- secure data consistency
volatile
- volatile save data in main memory not CPU memory
- CPU has its own memory
- sử dụng khi chỉ có 1 thread ghi, các thread còn lại đọc
- nếu multiple thread ghi -> synchronize
actomic
- ActomicInteger
- ActomicLong
- ActomicBoolean
- ActomicRefference
-
lock
- one thread access one time
-
quản lý thread
thread pool
thread pool executor
- khi new một thread -> tốn resource creating
- khi một thread hoạt động xong, nó sẽ không chết mà ở vào trạng thái chờ để hoạt động tiếp
-
if all thread is busy, new task request will be keep in blockingQueue and waiting for any thread completed task to pick it
Concept
ThreadPool
-
group of thread that are waiting for the job, can be reusing many times
- save resource in multiple thread
instead of creating new thread -> thread pool keep a number of idle thread and they are all available
after completed a task, thread wont be died, it will be keep in thread pool
-
-
ThreadPoolExecutor
- preconfig threadpool insntace
- interface ServiceExecutor extend executor
- manage thread and schedule task
- config corepool-maxpool-capacityQueue
-
-
interface Executor
- execute()
- submit Runable for execution
ExecutorService
- controll task
- schedule task
- using Future<>
ThreadPoolExecutor
- advance class
- config corepool...
Executors
- helper Class
- preconfigured threadpool instance
-
Multi thread
-
đồng bộ
-
-
why
- to avoid conflict data
- secure data consistence
Đồng bộ hóa là việc sắp xếp thứ tự các luồng truy xuất vào cùng đối tượng sao cho đối tượng không bị xung đột dữ liệu
-
Synchronization is the ordering of threads accessing the same object so that the object does not have data conflicts
giải quyết vấn đề này 1 cách cụ thể, -> xem qua thread safe bên trái
issue
- Thread chia sẻ (data, files, code...) cùng nhau
- Nhưng mỗi thread lại chạy trên phần cứng riêng (có counter, register... riêng)
- Vấn đề xảy ra khi nhiều thread cùng thèm khát xử lý 1 object-data chung nào đó
- Việc dùng chung này là điểm hấp dẫn của đa luồng nhưng cũng gây ra một số phiền toái
deadlock
multi thread waiting for resource, acquired by another thread
data racing
2 or more thread access and update same resource in the same time,
-
-
Thread
Init
-
-
Java 8 Approach
CompletableFuture<T>
-
support
-
hỗ trợ viết logic
-
call back
- thenApply()
- thenAccept()
- thenRun()
-
-
-
-
nối luồng
thread.join()
t1.join vào luồng hiện tại
- => luồng hiện tại phải đợi t1 chạy xong mới được chạy tiếp
-
doc
-
1 máy chạy nhiều app
- Nếu máy CPU 1 core => cpu switch qua lại giữa các app
1 Tiến trình có nhiều luồng (game đế chế)
- tải map
- nhân vật chuyển động
- nghe nhạc
- chat
-
database
JPA mapping
-
mapping
-
-
fetchType
LAZY
=> toMany
OneToMany, ManyToMany
-
EAGER
=> ToOne
ManyToOne, OneToOne
-
-
-
-
Locking
- DB mechanism to prevent conflict data from multiple transtraction
- Solve multiple transaction issue
- optimistic
- pesimistic
mechanism
pesimistic
- default locking
- SELECT FOR UPDATE statements
- allow concurrency control
- when a transaction lock a row, another trans want to update that row, They have to wait till that row available
- locking a row via method setAttribute()
- an toàn nhất
nhược:
- đợi quá lâu
- có thể xảy ra deadlock
- locking entities on database level
- hold the lock, no transaction can update on locked data
optimistic locking
- detecting change on entities by checking version attribute
- cho phép nhiều transaction có thể hoàn thành mà không ảnh hưởng đến nhau
- wait for row updated (not throw exception)
- while using it, each transaction read data hold in version
- when update, it check data in version again
- If value changed -> OptimisticLockExxception is thrown
có 2 transaction đồng thời đến row
- kiểm tra version
- trans 1 updated -> edit version
- trans2 updated -> thấy khác version -> quăng lỗi
locking type
-
exclusive lock
write lock
- tại 1 thời điểm, chỉ có 1 transaction được ghi
- không thể share lock trên exclusive lock
update lock
mixing giữa 2 cái trên
- khi update -> exclusive lock
- read thì share lock
-
Isolation level
-
-
Repeatable read
same as read committed
- do not update when a transaction read this row
-
-
-
Second layer cache
help eliminate database access cost
concept
Hibernate
- first layer cache
- session scoped
-
-
- second-level cache is Session-Factory scoped
-
get data:
- check from 1st layer cache
- check from 2nd layer cache (ware house)
- fetch from db
database transaction
process to ensure ACID of database
- keep data consistency
- allow correct recovery from failure
- provide isolation
- commit / roll back
consistency
- conform to existing constraints
-
isolation
- not affect other transaction
- database transaction must complete task independently from other
- multiple transaction - concurrent transaction
-> dirty read (read_uncommitted)
Atomicity - all or nothing
- be completed or no effect
durability
- get written to persistent storage
-
-
- avoid using transaction across micro services
- if needed: TWO PHASE COMMIT
- load data -> ram
- commit change in ram
- commit -> disk
Architecture
microservice
-
what
concept
-
- together things change for same reason
- different things change for different reason
-
synchronous
- communication happen in real time
-
-
-
asynchronous
- communication happen independent of time
HEAP/STACK AREA
STACK ST
Stack memory tạm thời chứa local variable của method được gọi, và sẽ xóa đi khi method hoàn thành
-
-
-
-
HEAP AREA
quản lý theo concept
- young generation
- old generation
-
-
-
-
-
-
-
Asynchronous programing
-
-
issue:
- server overload, reject additional request
- server die
-
message broker
-
structure
- producer/publisher (sender)
- message broker (router)
- consumer/subscriber (receiver)
-
Kafka
structure
topic
concept of kafka
- after event stream enter Kafka, It is persisted as topic
- topic: topicName + message
- topic is saved in log file
- send data from one to one or multiple service
- producer push msg into Kafka topic
- consumer pull msg from Kafka topic
partition
- kafka topic are divided into several partitions
- each partition is a single log file
-
- Called Brokers
- Each Brokers holds a subset of record
offset
- record in partition is assigned a sequential identifier called the offset
- unique record within partition
message in Kafka
-
Kafka - data pipeline
- msg is stored in Kafka 7 days (default)
- after remove, offset not reset and keep going increase
- data after saved in partition is immutable
-
concept
-
-
-
Producer
save
- producer send msg -> broker (round robin)
- broker -> partition
-
-
-
-
consumer
read msg from topic, identify by topic name
-
characteristic
high scalable
distributed system
- easy scale up
- zero downtime
-
-
-
intro
kafka - event streaming platform
- more than a message broker
founded by LinkedIn
- develop via JVM (Java - Scala)
- => become open source via Apache License
Multithread
OS fundamental
context switching
- OS chạy 1 hoặc nhiều process cùng lúc
- process có thể có 1 hoặc nhiều thread cùng lúc, tất cả thread cạnh tranh nhau để chạy trên CPU
Context switch: là quá trình mà cpu…
- stop thread 1
- schedule thread 1 out
- schedule thread 2 in
- start thread 2...
- context switch is not cheap
- when switch to diff thread (store data for on thread, restore data for another thread)
- => Nếu có quá nhiều thread => quá trình quản lý thread còn tốn nhiều chi phí hơn
- Thread tiêu thụ ít resource hon process
- context switching giữa các thread trong cùng process thì tiết kiệm chi phí hơn CS giữa các thread khác process
-
WHEN
- mulithread in a single app
- new single app in diff process
Multithread Architecture
- task share a lot of dât
- thread are much fasster to create and destroy
- switching between thread of same process is faster
-
responsiveness
-
parallel
- mulitple task executing parallel using a single core
- multiple core we can truly run task completely in parallel
Thread
khi OS chạy một application
- load instance application từ disk/hard drive lên memory
- instance này được gọi là progress(context)
- mỗi Process completely isolate với process khác đang được run trên system
-
motivation
- responsiveness achieved by concurrency
- performance achieved by parallelism
-
OOP
-
-
Abstraction
purpose
data hiding
- display information needed
- hide unnecessary information
-
-
-
-
achieve
-
interface
achieve security
hide certain details, only show important details
-
-
-
Encapsulation
hiding, keeping data safe
achieved by using modifier, getter setter
-
WHAT
-
OOP approach designing a program using class and object rather than logics, functions
WHY
-
improve code readability, reusability
Java 8
Tóm tắt
default method
những api có sẵn, không cần override lại method mới trong interface
- static method interface - ultility method
- default method interface
-
-
-
-
-
Multithreading, parallel, Async
-
-
-
Java
Doc
WHY
user-friendly: easy to learn, to use, to write, to debug...
-
-
-
-
-
-
JVM
mainly structure
Class Loader
-
linking
verification, preparation
-
allocates memory for class variable, and default values
-
JVM memory
method area
class level information
class name, parent class name, method, variable...
-
stack area
every thread, JVM create one run-time stack corresponding
-
-
-
-
-
lambda expression
functional programming
-
lâp trình hướng đối tượng
hướng đến:
- tính kết hợp (combine)
- tối ưu tính tái sử dụng (reusebility)
-
-
-
key concept
first class object
interface implementation instance
-
-
-
-
-
Java Stream API
-
structure
non-terminal operations
- transform or filter element
- return Stream<T>
-
-
-
terminal operations
return value
boolean
- anyOf()
- allMatch()
- noneMatch()
-
-
-
-
-
-
-
-
core
-
hashCode & equal
Object define equal(), hasCode()
override when
- compare object by value instead of address
-
-
HashMap dataStructure <K,V>
-
-
-
Spring core module
Transaction
-
-
-
- series of actions that must all complete successfully
- achieved ACID of DB
- keep data consistency
- allow correct recovery from failure
- provide isolation
- commit / roll back
consistency
- conform to existing constraints
- data is in a consistent state when a transactions start till its end
-
isolation
- Each transaction is dependent of other transaction
- not affect other transaction
- database transaction must complete task independently from other
- multiple transaction - concurrent transaction
-> dirty read (read_uncommitted)
Atomicity - all or nothing
- be completed or no effect
- If any part of transaction fails, the entire will operation rolls back
-
durability
- All transaction results are permanently preserved
-
-
Spring security
-
-
HOW
step
-
-
-
UsernameAndPasswordToken
from Token, Header, FormField, Cookie...
concept
servlet filter
request -> a couple of of filter chain -> Dispatcher Servlet -> Controllers
-
-
-
-
-
Thread pool
limit of thread
-
thread are expensive
thread có runtime stack, memory, regisster... của riêng nó
Solution
Threadpool
-
-
benefit
no need to manually create, start, join the threads
-
-
OS
đơn nhân
tại 1 thời điểm, xử lý một dòng lệnh
(Giải thuật round robin)
-
Service communication
intro
-
batch executing system => msg queue
-
-
-
Database
concept
set of data, and how it organize
-
ACID
-
-
-
D-Durability: When committed, it remains so
-
Đa nhiệm: multitasking
-> tối ưu hóa sức mạnh của phần cứng
=>
- multithreading
- multi processing
-
-