Sobes.tech
Senior

How to store voice messages in messengers like WhatsApp? Why is it better to store images in S3 rather than in Postgres? What are the options for storing images in Postgres?

sobes.tech AI

Answer from AI

Storing voice messages in a messenger:

Voice messages are binary audio files that are usually stored as separate files in object storage (e.g., Amazon S3, Google Cloud Storage) with links in a database. This approach allows:

  • Efficient scaling of large file storage.
  • Fast content delivery via CDN.
  • Reduced load on the database.

Why it's better to store images in S3 rather than in Postgres:

  • Performance: Databases are optimized for structured data, not large binary objects (BLOB). Storing large files in a database slows down operations and increases backup sizes.
  • Scalability: S3 and similar services easily scale to handle huge volumes of data.
  • Cost: Storing large files in object storage is usually cheaper.
  • Delivery: S3 integrates with CDN, speeding up loading times.

Options for storing images in Postgres:

  1. BYTEA: storing binary data directly in the table. Suitable for small files but increases database size and reduces performance.
  2. Large Objects (LOB): a special mechanism in Postgres for storing large binary data with streaming read/write capabilities.
  3. Storing links: only the path or URL to the file is stored in the database, while the files themselves are stored outside the database (recommended approach).

Overall, for a messenger, it is better to store voice messages and images in object storage, and store metadata and links in the database.