🐝
Mess around software system design
  • README
  • ArchitectureTradeOffAnalysis
    • Estimation
    • Middleware
    • Network
    • Server
    • Storage
  • Conversion cheat sheet
  • Scenarios
    • TinyURL
      • Estimation
      • Flowchart
      • Shortening mechanisms
      • Rest API
      • Performance
      • Storage
      • Follow-up
    • TaskScheduler
      • JDK delay queue
      • Timer based
      • RabbitMQ based
      • Kafka-based fixed delay time
      • Redis-based customized delay time
      • MySQL-based customized delay time
      • Timer TimingWheel
      • Industrial Scheduler
      • Workflow Engine
      • Airflow Arch
    • GoogleDrive
      • Estimation
      • Flowchart
      • Storage
      • Follow-up
    • Youtube
      • Estimation
      • Flowchart
      • Performance
      • Storage
      • Follow-up
      • Netflix
    • Uber
      • Estimation
      • Rest api
      • Flowchart
      • KNN algorithms
      • Geohash-based KNN mechanism
      • Redis implementation
      • Storage
    • Twitter
      • Estimation
      • Flowchart
      • Storage
      • Scalability
      • Follow-up
    • Instant messenger
      • Architecture overview
      • Presence
      • Unread count
      • Notifications
      • Read receipt
      • Large group chat
      • Storage-Offline 1:1 Chat
      • Storage-Offline group chat
      • Storage-Message roaming
      • NonFunc-Realtime
      • NonFunc-Reliability
      • NonFunc-Ordering
      • NonFunc-Security
      • Livecast-LinkedIn
    • Distributed Lock
      • Single machine
      • AP model based
      • CP model based
      • Chubby-TODO
    • Payment system
      • Resilience
      • Consistency
      • Flash sale
    • Key value store
      • Master-slave KV
      • Peer-to-peer KV
      • Distributed cache
  • Time series scenarios
    • Observability
      • TimeSeries data
      • Distributed traces
      • Logs
      • Metrics
      • NonFunc requirments
  • Search engine
    • Typeahead
    • Search engine
    • Distributed crawler
      • Estimation
      • Flowchart
      • Efficiency
      • Robustness
      • Performance
      • Storage
      • Standalone implementation
      • Python Scrapy framework
    • Stream search
  • Big data
    • GFS/HDFS
      • Data flow
      • High availability
      • Consistency
    • Map reduce
    • Big table/Hbase
    • Haystack
    • TopK
    • Stateful stream
    • Lambda architecture
    • storm架构
    • Beam架构
    • Comparing stream frameworks
    • Instagram-[TODO]
  • MicroSvcs
    • Service Registry
      • Flowchart
      • Data model
      • High availability
      • Comparison
      • Implementation
    • Service governance
      • Load balancing
      • Circuit breaker
      • Bulkhead
      • Downgrade
      • Timeout
      • API gateway
      • RateLimiter
        • Config
        • Algorithm comparison
        • Sliding window
        • Industrial impl
    • MicroSvcs_ConfigCenter-[TODO]
    • MicroSvcs_Security
      • Authentication
      • Authorization
      • Privacy
  • Cache
    • Typical topics
      • Expiration algorithm
      • Access patterns
      • Cache penetration
      • Big key
      • Hot key
      • Distributed lock
      • Data consistency
      • High availability
    • Cache_Redis
      • Data structure
      • ACID
      • Performance
      • Availability
      • Cluster
      • Applications
    • Cache_Memcached
  • Message queue
    • Overview
    • Kafka
      • Ordering
      • At least once
      • Message backlog
      • Consumer idempotency
      • High performance
      • Internal leader election
    • MySQL-based msg queue
    • Other msg queues
      • ActiveMQ-TODO
      • RabbitMQ-TODO
      • RocketMQ-TODO
      • Comparison between MQ
  • Traditional DB
    • Index data structure
    • Index categories
    • Lock
    • MVCC
    • Redo & Undo logs
    • Binlog
    • Schema design
    • DB optimization
    • Distributed transactions
    • High availability
    • Scalability
    • DB migration
    • Partition
    • Sharding
      • Sharding strategies
      • Sharding ID generator overview
        • Auto-increment key
        • UUID
        • Snowflake
        • Implement example
      • Cross-shard pagination queries
      • Non-shard key queries
      • Capacity planning
  • Non-Traditional DB
    • NoSQL overview
    • Rum guess
    • Data structure
    • MySQL based key value
    • KeyValueStore
    • ObjectStore
    • ElasticSearch
    • TableStore-[TODO]
    • Time series DB
    • DistributedAcidDatabase-[TODO]
  • Java basics
    • IO
    • Exception handling
  • Java concurrency
    • Overview
      • Synchronized
      • Reentrant lock
      • Concurrent collections
      • CAS
      • Others
    • Codes
      • ThreadLocal
      • ThreadPool
      • ThreadLifeCycle
      • SingletonPattern
      • Future
      • BlockingQueue
      • Counter
      • ConcurrentHashmap
      • DelayedQueue
  • Java JVM
    • Overview
    • Dynamic proxy
    • Class loading
    • Garbage collection
    • Visibility
  • Server
    • Nginx-[TODO]
  • Distributed system theories
    • Elementary school with CAP
    • Consistency
      • Eventual with Gossip
      • Strong with Raft
      • Tunable with Quorum
      • Fault tolerant with BFT-TODO
      • AutoMerge with CRDT
    • Time in distributed system
      • Logical time
      • Physical time
    • DDIA_Studying-[TODO]
  • Protocols
    • ApiDesign
      • REST
      • RPC
    • Websockets
    • Serialization
      • Thrift
      • Avro
    • HTTP
    • HTTPS
    • Netty-TODO
  • Statistical data structure
    • BloomFilter
    • HyperLoglog
    • CountMinSketch
  • DevOps
    • Container_Docker
    • Container_Kubernetes-[TODO]
  • Network components
    • CDN
    • DNS
    • Load balancer
    • Reverse proxy
    • 云中网络-TODO
  • Templates
    • interviewRecord
  • TODO
    • RecommendationSystem-[TODO]
    • SessionServer-[TODO]
    • Disk
    • Unix philosophy and Kafka
    • Bitcoin
    • Design pattern
      • StateMachine
      • Factory
    • Akka
    • GoogleDoc
      • CRDT
Powered by GitBook
On this page
  • Scenario
  • Initial design
  • Storage
  • Query service DB
  • Trie
  • Data collections service
  • Scale
  • How to reduce response time
  • What if the trie too large for one machine
  • How to reduce the size of log file
  • Real world

Was this helpful?

  1. Search engine

Typeahead

PreviousNonFunc requirmentsNextSearch engine

Last updated 1 year ago

Was this helpful?

Scenario

  • Google suggestion

    • Prefix -> top n hot key words

    • DAU: 500M

    • Search: 6_6_500M = 18b (Every one search for 6 words, each word has 6 characters)

    • QPS = 18b / 86400 ~ 200k

    • Peak QPS = QPS * 2 ~ 400k

  • Twitter typeahead

Initial design

  • Query service

    • Each time a user types a character, the entire prefix is sent to query service.

  • Data collection service

Storage

Query service DB

Word count table

  • How to query on the db

  • Query SQL: Select * from hit_stats where keyword like ${key}% order by hitCount DESC Limit 10

    • Like operation is expensive. It is a range query.

    • where keyword like 'abc%' is equivalent to where keyword >= 'abc' AND keyword < 'abd'

keyword
hitCount

Amazon

20b

Apple

15b

Adidas

7b

Airbnb

3b

Prefix table

  • Convert a keyword table to a prefix table, put into memory

prefix
keywords

a

"amazon","apple"

am

"amazon","amc"

ad

"adidas","adobe"

don

"don't have", "donald trump"

Trie

  • Trie ( in memory ) + Serialized Trie ( on disk ).

    • Trie is must faster than DB because

      • All in-memory vs DB cache miss

  • Store word count at node, but it's slow

    • e.g. TopK. Always need to traverse the entire trie. Exponential complexity.

  • Instead, we can store the top n hot key words and their frequencies at each node, search becomes O(len).

prefix
keywords

a

"amazon","apple"

am

"amazon","amc"

ad

"adidas","adobe"

don

"don't have", "donald trump"

  • How do we add a new record {abd: 3b} to the trie

    • Insert the record into all nodes along its path in the trie.

    • If a node along the path is already full, then need to loop through all records inside the node and compared with the node to be inserted.

Data collections service

  • How frequently do you aggregate data

    • Real-time not impractical. Read QPS 200K + Write QPS 200K. Will slow down query service.

    • Once per week. Each week data collection service will fetch all the data within the most recent one week and aggregate them.

  • How does data collection service update query service? Offline update and works online.

    • All in-memory trie must have already been serialized. Read QPS already really high. Do not write to in-memory trie directly.

    • Use another machine. Data collection service updates query service.

Scale

How to reduce response time

  • Cache result

    • Front-end browser cache the results

  • Pre-fetch

    • Fetch the latest 1000 results

What if the trie too large for one machine

  • Use consistent hashing to decide which machine a particular string belongs to.

    • A record can exist only in one machine. Sharding according to char will not distribute the resource evenly. Instead, calculate consistent hashing code

    • a, am, ama, amax stored in different machines.

How to reduce the size of log file

  • Probablistic logging.

    • Too slow to calculate and too large amount of data to store.

    • Log with 1/10,000 probability

      • Say over the past two weeks "amazon" was searched 1 billion times, with 1/1000 probability we will only log 1 million times.

      • For a term that's searched 1000 times, we might end up logging only once or even zero times.

Real world

Correct twitter spelling
Typeahead
Scenario
Initial design
Storage
Query service DB
Word count table
Prefix table
Trie
Data collections service
Scale
How to reduce response time
What if the trie too large for one machine
How to reduce the size of log file
Real world